Fawkes API  Fawkes Development Version
main.cpp
1 
2 /***************************************************************************
3  * main.cpp - Fawkes switch toggler tool main
4  *
5  * Created: Thu Dez 10 15:34:18 2015
6  * Copyright 2015 Gesche Gierse
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <blackboard/remote.h>
24 #include <interfaces/SwitchInterface.h>
25 #include <netcomm/fawkes/client.h>
26 #include <utils/system/argparser.h>
27 
28 #include <cstdio>
29 #include <cstdlib>
30 #include <string>
31 
32 using namespace fawkes;
33 
34 /** Print usage.
35  * @param program_name program name
36  */
37 void
38 print_usage(const char *program_name)
39 {
40  printf("Usage: %s [-e interface_id|-d interface_id] [-r host[:port]]\n"
41  " -e Send enable msg to the switch interface specified by interface_id\n"
42  " -d Send disable msg to the switch interface specified by interface_id\n"
43  " -r host[:port] Remote host (and optionally port) to connect to\n\n",
44  program_name);
45 }
46 
47 int
48 main(int argc, char **argv)
49 {
50  ArgumentParser argp(argc, argv, "he:d:r:");
51 
52  if (argp.has_arg("h")) {
53  print_usage(argp.program_name());
54  exit(0);
55  }
56 
57  std::string host = "localhost";
58  unsigned short int port = 1910;
59  if (argp.has_arg("r")) {
60  argp.parse_hostport("r", host, port);
61  }
62 
63  FawkesNetworkClient *c = new FawkesNetworkClient(host.c_str(), port);
64  try {
65  c->connect();
66  } catch (Exception &e) {
67  printf("Could not connect to host: %s\n", host.c_str());
68  exit(1);
69  }
70 
71  try {
72  BlackBoard *bb = new RemoteBlackBoard(c);
73  //SwitchInterface *sw_if = bb->open_for_reading<SwitchInterface>("Start");
74  if (argp.has_arg("e")) {
75  const char * switch_name = argp.arg("e");
76  SwitchInterface *sw_if = bb->open_for_reading<SwitchInterface>(switch_name);
77  //send enable msg
79  sw_if->msgq_enqueue(em);
80  bb->close(sw_if);
81  } else if (argp.has_arg("d")) {
82  const char * switch_name = argp.arg("d");
83  SwitchInterface *sw_if = bb->open_for_reading<SwitchInterface>(switch_name);
84  //send disable msg
86  sw_if->msgq_enqueue(dm);
87  bb->close(sw_if);
88  }
89 
90  delete bb;
91 
92  } catch (Exception &e) {
93  printf("Error connecting to BlackBoard: %s\n", e.what());
94  c->disconnect();
95  }
96 
97  c->disconnect();
98  delete c;
99 
100  return 0;
101 }
Parse command line arguments.
Definition: argparser.h:64
The BlackBoard abstract class.
Definition: blackboard.h:46
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual void close(Interface *interface)=0
Close interface.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual const char * what() const noexcept
Get primary string.
Definition: exception.cpp:639
Simple Fawkes network client.
Definition: client.h:52
void connect()
Connect to remote.
Definition: client.cpp:424
void disconnect()
Disconnect socket.
Definition: client.cpp:539
unsigned int msgq_enqueue(Message *message, bool proxy=false)
Enqueue message at end of queue.
Definition: interface.cpp:915
Remote BlackBoard.
Definition: remote.h:50
DisableSwitchMessage Fawkes BlackBoard Interface Message.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
SwitchInterface Fawkes BlackBoard Interface.
Fawkes library namespace.