Fawkes API  Fawkes Development Version
net_thread.cpp
1 
2 /***************************************************************************
3  * net_thread.cpp - Fawkes Example Plugin Network Thread
4  *
5  * Generated: Tue May 08 17:49:56 2006-2007
6  * Copyright 2006-2008 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/component_ids.h>
24 #include <plugins/examples/basics/net_thread.h>
25 
26 #include <cstdlib>
27 #include <unistd.h>
28 
29 using namespace fawkes;
30 
31 /** @class ExampleNetworkThread net_thread.h <plugins/examples/basics/net_thread.h>
32  * Network thread of example plugin.
33  * @author Tim Niemueller
34  */
35 
36 /** Constructor.
37  * @param name thread name
38  */
40 : Thread(name, Thread::OPMODE_WAITFORWAKEUP), FawkesNetworkHandler(FAWKES_CID_EXAMPLE_PLUGIN)
41 {
42 }
43 
44 /** Destructor. */
46 {
47 }
48 
49 /** Initialize thread.
50  * This method is called just after all aspects have been initialized but before
51  * the thread is run. Here we add this thread as a handler to the Fawkes network
52  * hub. This cannot happen in the constructor as fnethandler has not been
53  * initialized at that time.
54  * @see Thread::init()
55  * @see Aspects
56  */
57 void
59 {
60  fnethub->add_handler(this);
61 }
62 
63 void
65 {
66  logger->log_info("ExampleNetworkThread",
67  "Removing this thread from list of Fawkes network hub handlers");
68  fnethub->remove_handler(this);
69 }
70 
71 /** Thread loop.
72  * Nothing to do here since nobody will every wake us up (we do not have the
73  * BlockedTimingAspect nor does any other thread wake us up). This is ok because
74  * everything is done in the network handler call.
75  *
76  * Note that in general incoming messages should be parsed and appropriate
77  * actions enqueued. Then in the next loop iteration you process these
78  * incoming messages. This is the best way to avoid strange behavior and low
79  * latencies in network message handling.
80  *
81  * As an example for this see the FawkesConfigManager.
82  *
83  * @see FawkesConfigManager
84  */
85 void
87 {
88 }
89 
90 /** Handle network message.
91  * The message is put into the inbound queue and processed in processAfterLoop().
92  * @param msg message
93  */
94 void
96 {
97  if (msg->payload_size() == sizeof(unsigned int)) {
98  unsigned int *u = (unsigned int *)msg->payload();
99  logger->log_info("ExamplePlugin",
100  "Message of type %u with payload u=%u received, sending reply",
101  msg->msgid(),
102  *u);
103  unsigned int *ru = (unsigned int *)malloc(sizeof(unsigned int));
104  *ru = *u;
105  fnethub->send(msg->clid(), FAWKES_CID_EXAMPLE_PLUGIN, msg->msgid(), ru, sizeof(unsigned int));
106  // ru is now owned by the generated message and will be automatically freed
107  } else {
108  logger->log_error("ExamplePlugin", "Message of invalid size received");
109  }
110 }
111 
112 /** Client connected.
113  * Ignored.
114  * @param clid client ID
115  */
116 void
118 {
119  logger->log_info("ExamplePlugin", "Client %u connected", clid);
120 }
121 
122 /** Client disconnected.
123  * If the client was a subscriber it is removed.
124  * @param clid client ID
125  */
126 void
128 {
129  logger->log_info("ExamplePlugin", "Client %u disconnected", clid);
130 }
ExampleNetworkThread::handle_network_message
virtual void handle_network_message(fawkes::FawkesNetworkMessage *msg)
Handle network message.
Definition: net_thread.cpp:94
fawkes::FawkesNetworkHub::send
virtual void send(FawkesNetworkMessage *msg)=0
ExampleNetworkThread::client_disconnected
virtual void client_disconnected(unsigned int clid)
Client disconnected.
Definition: net_thread.cpp:126
fawkes::Logger::log_info
virtual void log_info(const char *component, const char *format,...)=0
fawkes::FawkesNetworkHub::add_handler
virtual void add_handler(FawkesNetworkHandler *handler)=0
fawkes::FawkesNetworkAspect::fnethub
FawkesNetworkHub * fnethub
Definition: fawkes_network.h:50
fawkes::FawkesNetworkHandler
Definition: handler.h:35
ExampleNetworkThread::client_connected
virtual void client_connected(unsigned int clid)
Client connected.
Definition: net_thread.cpp:116
ExampleNetworkThread::init
virtual void init()
Initialize thread.
Definition: net_thread.cpp:57
ExampleNetworkThread::~ExampleNetworkThread
virtual ~ExampleNetworkThread()
Destructor.
Definition: net_thread.cpp:44
fawkes::LoggingAspect::logger
Logger * logger
Definition: logging.h:50
fawkes::Logger::log_error
virtual void log_error(const char *component, const char *format,...)=0
fawkes
fawkes::FawkesNetworkMessage::payload_size
size_t payload_size() const
Get payload size.
Definition: message.cpp:307
fawkes::FawkesNetworkMessage::payload
void * payload() const
Get payload buffer.
Definition: message.cpp:316
fawkes::Thread
Definition: thread.h:44
fawkes::FawkesNetworkMessage
Definition: message.h:80
fawkes::FawkesNetworkHub::remove_handler
virtual void remove_handler(FawkesNetworkHandler *handler)=0
ExampleNetworkThread::finalize
virtual void finalize()
Finalize the thread.
Definition: net_thread.cpp:63
ExampleNetworkThread::loop
virtual void loop()
Thread loop.
Definition: net_thread.cpp:85
fawkes::FawkesNetworkMessage::clid
unsigned int clid() const
Get client ID.
Definition: message.cpp:280
fawkes::FawkesNetworkMessage::msgid
unsigned short int msgid() const
Get message type ID.
Definition: message.cpp:298
ExampleNetworkThread::ExampleNetworkThread
ExampleNetworkThread(const char *name)
Constructor.
Definition: net_thread.cpp:38