Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * net_thread.cpp - Fawkes Example Plugin Network Thread 00004 * 00005 * Generated: Tue May 08 17:49:56 2006-2007 00006 * Copyright 2006-2008 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 <plugins/examples/basics/net_thread.h> 00024 #include <netcomm/fawkes/component_ids.h> 00025 00026 #include <cstdlib> 00027 #include <unistd.h> 00028 00029 using namespace fawkes; 00030 00031 /** @class ExampleNetworkThread net_thread.h <plugins/examples/basics/net_thread.h> 00032 * Network thread of example plugin. 00033 * @author Tim Niemueller 00034 */ 00035 00036 /** Constructor. 00037 * @param name thread name 00038 */ 00039 ExampleNetworkThread::ExampleNetworkThread(const char *name) 00040 : Thread(name, Thread::OPMODE_WAITFORWAKEUP), 00041 FawkesNetworkHandler(FAWKES_CID_EXAMPLE_PLUGIN) 00042 { 00043 } 00044 00045 00046 /** Destructor. */ 00047 ExampleNetworkThread::~ExampleNetworkThread() 00048 { 00049 } 00050 00051 00052 /** Initialize thread. 00053 * This method is called just after all aspects have been initialized but before 00054 * the thread is run. Here we add this thread as a handler to the Fawkes network 00055 * hub. This cannot happen in the constructor as fnethandler has not been 00056 * initialized at that time. 00057 * @see Thread::init() 00058 * @see Aspects 00059 */ 00060 void 00061 ExampleNetworkThread::init() 00062 { 00063 fnethub->add_handler( this ); 00064 } 00065 00066 00067 void 00068 ExampleNetworkThread::finalize() 00069 { 00070 logger->log_info("ExampleNetworkThread", "Removing this thread from list of Fawkes network hub handlers"); 00071 fnethub->remove_handler( this ); 00072 } 00073 00074 00075 /** Thread loop. 00076 * Nothing to do here since nobody will every wake us up (we do not have the 00077 * BlockedTimingAspect nor does any other thread wake us up). This is ok because 00078 * everything is done in the network handler call. 00079 * 00080 * Note that in general incoming messages should be parsed and appropriate 00081 * actions enqueued. Then in the next loop iteration you process these 00082 * incoming messages. This is the best way to avoid strange behavior and low 00083 * latencies in network message handling. 00084 * 00085 * As an example for this see the FawkesConfigManager. 00086 * 00087 * @see FawkesConfigManager 00088 */ 00089 void 00090 ExampleNetworkThread::loop() 00091 { 00092 } 00093 00094 00095 /** Handle network message. 00096 * The message is put into the inbound queue and processed in processAfterLoop(). 00097 * @param msg message 00098 */ 00099 void 00100 ExampleNetworkThread::handle_network_message(FawkesNetworkMessage *msg) 00101 { 00102 if ( msg->payload_size() == sizeof(unsigned int) ) { 00103 unsigned int *u = (unsigned int *)msg->payload(); 00104 logger->log_info("ExamplePlugin", "Message of type %u with payload u=%u received, sending reply", msg->msgid(), *u); 00105 unsigned int *ru = (unsigned int *)malloc(sizeof(unsigned int)); 00106 *ru = *u; 00107 fnethub->send(msg->clid(), FAWKES_CID_EXAMPLE_PLUGIN, msg->msgid(), 00108 ru, sizeof(unsigned int)); 00109 // ru is now owned by the generated message and will be automatically freed 00110 } else { 00111 logger->log_error("ExamplePlugin", "Message of invalid size received"); 00112 } 00113 } 00114 00115 00116 /** Client connected. 00117 * Ignored. 00118 * @param clid client ID 00119 */ 00120 void 00121 ExampleNetworkThread::client_connected(unsigned int clid) 00122 { 00123 logger->log_info("ExamplePlugin", "Client %u connected", clid); 00124 } 00125 00126 00127 /** Client disconnected. 00128 * If the client was a subscriber it is removed. 00129 * @param clid client ID 00130 */ 00131 void 00132 ExampleNetworkThread::client_disconnected(unsigned int clid) 00133 { 00134 logger->log_info("ExamplePlugin", "Client %u disconnected", clid); 00135 }