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
78
SwitchInterface::EnableSwitchMessage
*em =
new
SwitchInterface::EnableSwitchMessage
();
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
85
SwitchInterface::DisableSwitchMessage
*dm =
new
SwitchInterface::DisableSwitchMessage
();
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
}
fawkes::SwitchInterface
Definition:
SwitchInterface.h:39
fawkes::FawkesNetworkClient::connect
void connect()
Connect to remote.
Definition:
client.cpp:430
fawkes::BlackBoard
Definition:
blackboard.h:50
fawkes::RemoteBlackBoard
Definition:
remote.h:53
fawkes::SwitchInterface::DisableSwitchMessage
Definition:
SwitchInterface.h:147
fawkes::FawkesNetworkClient::disconnect
void disconnect()
Disconnect socket.
Definition:
client.cpp:545
fawkes::SwitchInterface::EnableSwitchMessage
Definition:
SwitchInterface.h:127
fawkes::BlackBoard::close
virtual void close(Interface *interface)=0
fawkes
fawkes::ArgumentParser
Definition:
argparser.h:69
fawkes::Exception::what
virtual const char * what() const
Get primary string.
Definition:
exception.cpp:639
fawkes::BlackBoard::open_for_reading
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
fawkes::Interface::msgq_enqueue
unsigned int msgq_enqueue(Message *message)
Enqueue message at end of queue.
Definition:
interface.cpp:884
fawkes::FawkesNetworkClient
Definition:
client.h:57
fawkes::Exception
Definition:
exception.h:41
src
tools
ffswitch
main.cpp
Generated by
1.8.17