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 <plugins/examples/basics/net_thread.h>
24 #include <netcomm/fawkes/component_ids.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),
41  FawkesNetworkHandler(FAWKES_CID_EXAMPLE_PLUGIN)
42 {
43 }
44 
45 
46 /** Destructor. */
48 {
49 }
50 
51 
52 /** Initialize thread.
53  * This method is called just after all aspects have been initialized but before
54  * the thread is run. Here we add this thread as a handler to the Fawkes network
55  * hub. This cannot happen in the constructor as fnethandler has not been
56  * initialized at that time.
57  * @see Thread::init()
58  * @see Aspects
59  */
60 void
62 {
63  fnethub->add_handler( this );
64 }
65 
66 
67 void
69 {
70  logger->log_info("ExampleNetworkThread", "Removing this thread from list of Fawkes network hub handlers");
71  fnethub->remove_handler( this );
72 }
73 
74 
75 /** Thread loop.
76  * Nothing to do here since nobody will every wake us up (we do not have the
77  * BlockedTimingAspect nor does any other thread wake us up). This is ok because
78  * everything is done in the network handler call.
79  *
80  * Note that in general incoming messages should be parsed and appropriate
81  * actions enqueued. Then in the next loop iteration you process these
82  * incoming messages. This is the best way to avoid strange behavior and low
83  * latencies in network message handling.
84  *
85  * As an example for this see the FawkesConfigManager.
86  *
87  * @see FawkesConfigManager
88  */
89 void
91 {
92 }
93 
94 
95 /** Handle network message.
96  * The message is put into the inbound queue and processed in processAfterLoop().
97  * @param msg message
98  */
99 void
101 {
102  if ( msg->payload_size() == sizeof(unsigned int) ) {
103  unsigned int *u = (unsigned int *)msg->payload();
104  logger->log_info("ExamplePlugin", "Message of type %u with payload u=%u received, sending reply", msg->msgid(), *u);
105  unsigned int *ru = (unsigned int *)malloc(sizeof(unsigned int));
106  *ru = *u;
107  fnethub->send(msg->clid(), FAWKES_CID_EXAMPLE_PLUGIN, msg->msgid(),
108  ru, sizeof(unsigned int));
109  // ru is now owned by the generated message and will be automatically freed
110  } else {
111  logger->log_error("ExamplePlugin", "Message of invalid size received");
112  }
113 }
114 
115 
116 /** Client connected.
117  * Ignored.
118  * @param clid client ID
119  */
120 void
122 {
123  logger->log_info("ExamplePlugin", "Client %u connected", clid);
124 }
125 
126 
127 /** Client disconnected.
128  * If the client was a subscriber it is removed.
129  * @param clid client ID
130  */
131 void
133 {
134  logger->log_info("ExamplePlugin", "Client %u disconnected", clid);
135 }
virtual void handle_network_message(fawkes::FawkesNetworkMessage *msg)
Handle network message.
Definition: net_thread.cpp:100
void * payload() const
Get payload buffer.
Definition: message.cpp:321
ExampleNetworkThread(const char *name)
Constructor.
Definition: net_thread.cpp:39
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
FawkesNetworkHub * fnethub
This is the Fawkes network hub member used to access the Fawkes network protocol. ...
virtual void client_disconnected(unsigned int clid)
Client disconnected.
Definition: net_thread.cpp:132
Fawkes library namespace.
unsigned int clid() const
Get client ID.
Definition: message.cpp:281
virtual void init()
Initialize thread.
Definition: net_thread.cpp:61
virtual void add_handler(FawkesNetworkHandler *handler)=0
Add a message handler.
Representation of a message that is sent over the network.
Definition: message.h:75
Thread class encapsulation of pthreads.
Definition: thread.h:42
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
virtual ~ExampleNetworkThread()
Destructor.
Definition: net_thread.cpp:47
virtual void finalize()
Finalize the thread.
Definition: net_thread.cpp:68
virtual void send(FawkesNetworkMessage *msg)=0
Method to send a message to a specific client.
virtual void loop()
Thread loop.
Definition: net_thread.cpp:90
Network handler abstract base class.
Definition: handler.h:31
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
unsigned short int msgid() const
Get message type ID.
Definition: message.cpp:301
virtual void client_connected(unsigned int clid)
Client connected.
Definition: net_thread.cpp:121
virtual void remove_handler(FawkesNetworkHandler *handler)=0
Remove a message handler.
size_t payload_size() const
Get payload size.
Definition: message.cpp:311