00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <netcomm/fawkes/client.h>
00024 #include <netcomm/fawkes/client_handler.h>
00025 #include <utils/system/argparser.h>
00026 #include <utils/system/signal.h>
00027
00028 #include <cstdio>
00029 #include <cstdlib>
00030
00031 using namespace fawkes;
00032
00033
00034
00035
00036
00037 class ExamplePluginClientNetworkReceiver : public FawkesNetworkClientHandler
00038 {
00039 public:
00040
00041 ExamplePluginClientNetworkReceiver()
00042 {
00043 quit = false;
00044 }
00045
00046
00047
00048
00049 virtual void deregistered(unsigned int id) throw()
00050 {
00051 printf("Got deregistered\n");
00052 quit = true;
00053 }
00054
00055
00056
00057
00058
00059 virtual void inbound_received(FawkesNetworkMessage *m,
00060 unsigned int id) throw()
00061 {
00062 if ( m->payload_size() == sizeof(unsigned int) ) {
00063 unsigned int *u = (unsigned int *)m->payload();
00064 printf("Received message of type %hu with payload u=%u\n", m->msgid(), *u);
00065 } else {
00066 printf("Received message of invalid size, ignoring\n");
00067 }
00068 quit = true;
00069 }
00070
00071 virtual void connection_died(unsigned int id) throw()
00072 {
00073 printf("Connection died.\n");
00074 quit = true;
00075 }
00076
00077
00078 virtual void connection_established(unsigned int id) throw()
00079 {
00080 printf("Connection established\n");
00081 }
00082
00083
00084
00085
00086
00087 bool quit;
00088 };
00089
00090
00091
00092
00093
00094 int
00095 main(int argc, char **argv)
00096 {
00097 ArgumentParser argp(argc, argv, "Hn:i:");
00098
00099 FawkesNetworkClient *c = new FawkesNetworkClient("localhost", 1910);
00100 c->connect();
00101
00102 ExamplePluginClientNetworkReceiver r;
00103 c->register_handler(&r, FAWKES_CID_EXAMPLE_PLUGIN);
00104
00105 const char *tmp;
00106 unsigned int *u = (unsigned int *)malloc(sizeof(unsigned int));;
00107 unsigned int id = 1;
00108 if ( (tmp = argp.arg("n")) != NULL ) {
00109 int i = atoi(tmp);
00110 if ( i > 0 ) {
00111 *u = i;
00112 }
00113 }
00114
00115 if ( (tmp = argp.arg("i")) != NULL ) {
00116 int i = atoi(tmp);
00117 if ( i > 0 ) {
00118 id = i;
00119 }
00120 }
00121
00122
00123 FawkesNetworkMessage *msg = new FawkesNetworkMessage(FAWKES_CID_EXAMPLE_PLUGIN, id, u, sizeof(unsigned int));
00124 c->enqueue(msg);
00125
00126 while ( ! r.quit ) {
00127 c->wait(FAWKES_CID_EXAMPLE_PLUGIN);
00128 }
00129
00130 c->deregister_handler(FAWKES_CID_EXAMPLE_PLUGIN);
00131 c->disconnect();
00132 delete c;
00133
00134 return 0;
00135 }
00136