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
29using 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 */
57void
59{
60 fnethub->add_handler(this);
61}
62
63void
65{
66 logger->log_info("ExampleNetworkThread",
67 "Removing this thread from list of Fawkes network hub handlers");
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 */
85void
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 */
94void
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 */
116void
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 */
126void
128{
129 logger->log_info("ExamplePlugin", "Client %u disconnected", clid);
130}
virtual void loop()
Thread loop.
Definition: net_thread.cpp:86
virtual void finalize()
Finalize the thread.
Definition: net_thread.cpp:64
virtual ~ExampleNetworkThread()
Destructor.
Definition: net_thread.cpp:45
virtual void handle_network_message(fawkes::FawkesNetworkMessage *msg)
Handle network message.
Definition: net_thread.cpp:95
virtual void init()
Initialize thread.
Definition: net_thread.cpp:58
ExampleNetworkThread(const char *name)
Constructor.
Definition: net_thread.cpp:39
virtual void client_disconnected(unsigned int clid)
Client disconnected.
Definition: net_thread.cpp:127
virtual void client_connected(unsigned int clid)
Client connected.
Definition: net_thread.cpp:117
FawkesNetworkHub * fnethub
This is the Fawkes network hub member used to access the Fawkes network protocol.
Network handler abstract base class.
Definition: handler.h:32
virtual void send(FawkesNetworkMessage *msg)=0
Method to send a message to a specific client.
virtual void remove_handler(FawkesNetworkHandler *handler)=0
Remove a message handler.
virtual void add_handler(FawkesNetworkHandler *handler)=0
Add a message handler.
Representation of a message that is sent over the network.
Definition: message.h:77
unsigned short int msgid() const
Get message type ID.
Definition: message.cpp:294
unsigned int clid() const
Get client ID.
Definition: message.cpp:276
void * payload() const
Get payload buffer.
Definition: message.cpp:312
size_t payload_size() const
Get payload size.
Definition: message.cpp:303
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.