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 <netcomm/fawkes/client.h>
24 #include <blackboard/remote.h>
25 
26 #include <utils/system/argparser.h>
27 
28 #include <interfaces/SwitchInterface.h>
29 
30 #include <string>
31 #include <cstdlib>
32 #include <cstdio>
33 
34 using namespace fawkes;
35 
36 /** Print usage.
37  * @param program_name program name
38  */
39 void
40 print_usage(const char *program_name)
41 {
42  printf("Usage: %s [-e interface_id|-d interface_id] [-r host[:port]]\n"
43  " -e Send enable msg to the switch interface specified by interface_id\n"
44  " -d Send disable msg to the switch interface specified by interface_id\n"
45  " -r host[:port] Remote host (and optionally port) to connect to\n\n",
46  program_name);
47 }
48 
49 int
50 main(int argc, char **argv)
51 {
52  ArgumentParser argp(argc, argv, "he:d:r:");
53 
54  if ( argp.has_arg("h") ) {
55  print_usage(argp.program_name());
56  exit(0);
57  }
58 
59  std::string host = "localhost";
60  unsigned short int port = 1910;
61  if ( argp.has_arg("r") ) {
62  argp.parse_hostport("r", host, port);
63  }
64 
65  FawkesNetworkClient *c = new FawkesNetworkClient(host.c_str(), port);
66  try {
67  c->connect();
68  } catch( Exception &e ) {
69  printf("Could not connect to host: %s\n", host.c_str());
70  exit(1);
71  }
72 
73  try {
74  BlackBoard *bb = new RemoteBlackBoard(c);
75  //SwitchInterface *sw_if = bb->open_for_reading<SwitchInterface>("Start");
76  if (argp.has_arg("e")) {
77  const char *switch_name = argp.arg("e");
78  SwitchInterface *sw_if = bb->open_for_reading<SwitchInterface>(switch_name);
79  //send enable msg
81  sw_if->msgq_enqueue(em);
82  bb->close(sw_if);
83  }else if (argp.has_arg("d")) {
84  const char *switch_name = argp.arg("d");
85  SwitchInterface *sw_if = bb->open_for_reading<SwitchInterface>(switch_name);
86  //send disable msg
88  sw_if->msgq_enqueue(dm);
89  bb->close(sw_if);
90  }
91 
92  delete bb;
93 
94  } catch (Exception &e) {
95  printf("Error connecting to BlackBoard: %s\n", e.what());
96  c->disconnect();
97  }
98 
99  c->disconnect();
100  delete c;
101 
102  return 0;
103 }
Simple Fawkes network client.
Definition: client.h:52
Fawkes library namespace.
void disconnect()
Disconnect socket.
Definition: client.cpp:529
void connect()
Connect to remote.
Definition: client.cpp:417
Parse command line arguments.
Definition: argparser.h:66
virtual const char * what() const
Get primary string.
Definition: exception.cpp:661
SwitchInterface Fawkes BlackBoard Interface.
Base class for exceptions in Fawkes.
Definition: exception.h:36
DisableSwitchMessage Fawkes BlackBoard Interface Message.
unsigned int msgq_enqueue(Message *message)
Enqueue message at end of queue.
Definition: interface.cpp:903
EnableSwitchMessage Fawkes BlackBoard Interface Message.
Remote BlackBoard.
Definition: remote.h:48
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
The BlackBoard abstract class.
Definition: blackboard.h:48
virtual void close(Interface *interface)=0
Close interface.