Fawkes API Fawkes Development Version
example_plugin_netping.cpp
1
2/***************************************************************************
3 * example_plugin_netping.cpp - Fawkes example plugin network ping
4 *
5 * Created: Tue May 08 18:14:34 2007
6 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
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 <netcomm/fawkes/client_handler.h>
25#include <utils/system/argparser.h>
26#include <utils/system/signal.h>
27
28#include <cstdio>
29#include <cstdlib>
30
31using namespace fawkes;
32
33/** Example Plugin network ping tool
34 * Small class that waits for a reply of the example plugin after a short
35 * network message was sent.
36 */
38{
39public:
40 /** Constructor. */
42 {
43 quit = false;
44 }
45
46 /** The handler got deregistered.
47 * @param id the id of the calling client
48 */
49 virtual void
50 deregistered(unsigned int id) noexcept
51 {
52 printf("Got deregistered\n");
53 quit = true;
54 }
55
56 /** Inbound mesage received.
57 * @param m message
58 * @param id the id of the calling thread
59 */
60 virtual void
61 inbound_received(FawkesNetworkMessage *m, unsigned int id) noexcept
62 {
63 if (m->payload_size() == sizeof(unsigned int)) {
64 unsigned int *u = (unsigned int *)m->payload();
65 printf("Received message of type %hu with payload u=%u\n", m->msgid(), *u);
66 } else {
67 printf("Received message of invalid size, ignoring\n");
68 }
69 quit = true;
70 }
71
72 virtual void
73 connection_died(unsigned int id) noexcept
74 {
75 printf("Connection died.\n");
76 quit = true;
77 }
78
79 virtual void
80 connection_established(unsigned int id) noexcept
81 {
82 printf("Connection established\n");
83 }
84
85 /** Set to true if answer has been received or handler was deregistered.
86 * False at object creation.
87 */
88 bool quit;
89};
90
91/** Config tool main.
92 * @param argc argument count
93 * @param argv arguments
94 */
95int
96main(int argc, char **argv)
97{
98 ArgumentParser argp(argc, argv, "Hn:i:");
99
100 FawkesNetworkClient *c = new FawkesNetworkClient("localhost", 1910);
101 c->connect();
102
104 c->register_handler(&r, FAWKES_CID_EXAMPLE_PLUGIN);
105
106 const char * tmp;
107 unsigned int *u = (unsigned int *)malloc(sizeof(unsigned int));
108 ;
109 unsigned int id = 1;
110 if ((tmp = argp.arg("n")) != NULL) {
111 int i = atoi(tmp);
112 if (i > 0) {
113 *u = i;
114 }
115 }
116
117 if ((tmp = argp.arg("i")) != NULL) {
118 int i = atoi(tmp);
119 if (i > 0) {
120 id = i;
121 }
122 }
123
125 new FawkesNetworkMessage(FAWKES_CID_EXAMPLE_PLUGIN, id, u, sizeof(unsigned int));
126 c->enqueue(msg);
127
128 while (!r.quit) {
129 c->wait(FAWKES_CID_EXAMPLE_PLUGIN);
130 }
131
132 c->deregister_handler(FAWKES_CID_EXAMPLE_PLUGIN);
133 c->disconnect();
134 delete c;
135
136 return 0;
137}
Example Plugin network ping tool Small class that waits for a reply of the example plugin after a sho...
bool quit
Set to true if answer has been received or handler was deregistered.
virtual void connection_established(unsigned int id) noexcept
Client has established a connection.
virtual void deregistered(unsigned int id) noexcept
The handler got deregistered.
virtual void connection_died(unsigned int id) noexcept
Client connection died.
virtual void inbound_received(FawkesNetworkMessage *m, unsigned int id) noexcept
Inbound mesage received.
Parse command line arguments.
Definition: argparser.h:64
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:177
Message handler for FawkesNetworkClient.
Simple Fawkes network client.
Definition: client.h:52
void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id)
Register handler.
Definition: client.cpp:658
void wait(unsigned int component_id, unsigned int timeout_sec=15)
Wait for messages for component ID.
Definition: client.cpp:785
void connect()
Connect to remote.
Definition: client.cpp:424
void disconnect()
Disconnect socket.
Definition: client.cpp:539
void deregister_handler(unsigned int component_id)
Deregister handler.
Definition: client.cpp:676
void enqueue(FawkesNetworkMessage *message)
Enqueue message to send.
Definition: client.cpp:596
Representation of a message that is sent over the network.
Definition: message.h:77
Fawkes library namespace.