Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * example_plugin_netping.cpp - Fawkes example plugin network ping 00004 * 00005 * Created: Tue May 08 18:14:34 2007 00006 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 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 /** Example Plugin network ping tool 00034 * Small class that waits for a reply of the example plugin after a short 00035 * network message was sent. 00036 */ 00037 class ExamplePluginClientNetworkReceiver : public FawkesNetworkClientHandler 00038 { 00039 public: 00040 /** Constructor. */ 00041 ExamplePluginClientNetworkReceiver() 00042 { 00043 quit = false; 00044 } 00045 00046 /** The handler got deregistered. 00047 * @param id the id of the calling client 00048 */ 00049 virtual void deregistered(unsigned int id) throw() 00050 { 00051 printf("Got deregistered\n"); 00052 quit = true; 00053 } 00054 00055 /** Inbound mesage received. 00056 * @param m message 00057 * @param id the id of the calling thread 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 /** Set to true if answer has been received or handler was deregistered. 00085 * False at object creation. 00086 */ 00087 bool quit; 00088 }; 00089 00090 /** Config tool main. 00091 * @param argc argument count 00092 * @param argv arguments 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