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 
31 using 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 {
39  public:
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 deregistered(unsigned int id) throw()
50  {
51  printf("Got deregistered\n");
52  quit = true;
53  }
54 
55  /** Inbound mesage received.
56  * @param m message
57  * @param id the id of the calling thread
58  */
60  unsigned int id) throw()
61  {
62  if ( m->payload_size() == sizeof(unsigned int) ) {
63  unsigned int *u = (unsigned int *)m->payload();
64  printf("Received message of type %hu with payload u=%u\n", m->msgid(), *u);
65  } else {
66  printf("Received message of invalid size, ignoring\n");
67  }
68  quit = true;
69  }
70 
71  virtual void connection_died(unsigned int id) throw()
72  {
73  printf("Connection died.\n");
74  quit = true;
75  }
76 
77 
78  virtual void connection_established(unsigned int id) throw()
79  {
80  printf("Connection established\n");
81  }
82 
83 
84  /** Set to true if answer has been received or handler was deregistered.
85  * False at object creation.
86  */
87  bool quit;
88 };
89 
90 /** Config tool main.
91  * @param argc argument count
92  * @param argv arguments
93  */
94 int
95 main(int argc, char **argv)
96 {
97  ArgumentParser argp(argc, argv, "Hn:i:");
98 
99  FawkesNetworkClient *c = new FawkesNetworkClient("localhost", 1910);
100  c->connect();
101 
103  c->register_handler(&r, FAWKES_CID_EXAMPLE_PLUGIN);
104 
105  const char *tmp;
106  unsigned int *u = (unsigned int *)malloc(sizeof(unsigned int));;
107  unsigned int id = 1;
108  if ( (tmp = argp.arg("n")) != NULL ) {
109  int i = atoi(tmp);
110  if ( i > 0 ) {
111  *u = i;
112  }
113  }
114 
115  if ( (tmp = argp.arg("i")) != NULL ) {
116  int i = atoi(tmp);
117  if ( i > 0 ) {
118  id = i;
119  }
120  }
121 
122 
123  FawkesNetworkMessage *msg = new FawkesNetworkMessage(FAWKES_CID_EXAMPLE_PLUGIN, id, u, sizeof(unsigned int));
124  c->enqueue(msg);
125 
126  while ( ! r.quit ) {
127  c->wait(FAWKES_CID_EXAMPLE_PLUGIN);
128  }
129 
130  c->deregister_handler(FAWKES_CID_EXAMPLE_PLUGIN);
131  c->disconnect();
132  delete c;
133 
134  return 0;
135 }
136 
Message handler for FawkesNetworkClient.
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:182
Simple Fawkes network client.
Definition: client.h:52
Fawkes library namespace.
void disconnect()
Disconnect socket.
Definition: client.cpp:529
void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id)
Register handler.
Definition: client.cpp:646
void enqueue(FawkesNetworkMessage *message)
Enqueue message to send.
Definition: client.cpp:587
void wait(unsigned int component_id, unsigned int timeout_sec=15)
Wait for messages for component ID.
Definition: client.cpp:780
Representation of a message that is sent over the network.
Definition: message.h:75
void connect()
Connect to remote.
Definition: client.cpp:417
Parse command line arguments.
Definition: argparser.h:66
virtual void connection_established(unsigned int id)
Client has established a connection.
bool quit
Set to true if answer has been received or handler was deregistered.
Example Plugin network ping tool Small class that waits for a reply of the example plugin after a sho...
virtual void deregistered(unsigned int id)
The handler got deregistered.
virtual void connection_died(unsigned int id)
Client connection died.
virtual void inbound_received(FawkesNetworkMessage *m, unsigned int id)
Inbound mesage received.
void deregister_handler(unsigned int component_id)
Deregister handler.
Definition: client.cpp:665