Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
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
*/
39
ExampleNetworkThread::ExampleNetworkThread
(
const
char
*name)
40
:
Thread
(name,
Thread
::OPMODE_WAITFORWAKEUP),
41
FawkesNetworkHandler
(FAWKES_CID_EXAMPLE_PLUGIN)
42
{
43
}
44
45
46
/** Destructor. */
47
ExampleNetworkThread::~ExampleNetworkThread
()
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
61
ExampleNetworkThread::init
()
62
{
63
fnethub
->
add_handler
(
this
);
64
}
65
66
67
void
68
ExampleNetworkThread::finalize
()
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
90
ExampleNetworkThread::loop
()
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
100
ExampleNetworkThread::handle_network_message
(
FawkesNetworkMessage
*msg)
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
121
ExampleNetworkThread::client_connected
(
unsigned
int
clid)
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
132
ExampleNetworkThread::client_disconnected
(
unsigned
int
clid)
133
{
134
logger
->
log_info
(
"ExamplePlugin"
,
"Client %u disconnected"
, clid);
135
}
src
plugins
examples
basics
net_thread.cpp
Generated by
1.8.1.1