IBR-DTNSuite
0.10
Main Page
Namespaces
Classes
Files
File List
File Members
EventConnection.cpp
Go to the documentation of this file.
1
/*
2
* EventConnection.cpp
3
*
4
* Copyright (C) 2011 IBR, TU Braunschweig
5
*
6
* Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7
*
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
11
*
12
* http://www.apache.org/licenses/LICENSE-2.0
13
*
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
19
*
20
*/
21
22
#include "
EventConnection.h
"
23
24
#include "
core/EventDispatcher.h
"
25
#include "
core/NodeEvent.h
"
26
#include "
core/GlobalEvent.h
"
27
#include "
core/CustodyEvent.h
"
28
#include "
routing/QueueBundleEvent.h
"
29
#include "
net/BundleReceivedEvent.h
"
30
#include "
net/TransferAbortedEvent.h
"
31
#include "
net/TransferCompletedEvent.h
"
32
#include "
net/ConnectionEvent.h
"
33
34
#include <
ibrdtn/utils/Utils.h
>
35
36
namespace
dtn
37
{
38
namespace
api
39
{
40
EventConnection::EventConnection
(
ClientHandler
&client,
ibrcommon::socketstream
&stream)
41
:
ProtocolHandler
(client, stream),
_running
(true)
42
{
43
}
44
45
EventConnection::~EventConnection
()
46
{
47
}
48
49
void
EventConnection::raiseEvent
(
const
dtn::core::Event
*evt)
throw
()
50
{
51
ibrcommon::MutexLock
l(_mutex);
52
if
(!
_running
)
return
;
53
54
try
{
55
const
dtn::core::NodeEvent
&node =
dynamic_cast<
const
dtn::core::NodeEvent
&
>
(*evt);
56
57
// start with the event tag
58
_stream <<
"Event: "
<< node.
getName
() << std::endl;
59
_stream <<
"Action: "
;
60
61
switch
(node.
getAction
())
62
{
63
case
dtn::core::NODE_AVAILABLE
:
64
_stream <<
"available"
;
65
break
;
66
case
dtn::core::NODE_UNAVAILABLE
:
67
_stream <<
"unavailable"
;
68
break
;
69
case
dtn::core::NODE_DATA_ADDED
:
70
_stream <<
"data_added"
;
71
break
;
72
case
dtn::core::NODE_DATA_REMOVED
:
73
_stream <<
"data_removed"
;
74
break
;
75
default
:
76
break
;
77
}
78
79
_stream << std::endl;
80
81
// write the node eid
82
_stream <<
"EID: "
<< node.
getNode
().
getEID
().
getString
() << std::endl;
83
84
// close the event
85
_stream << std::endl;
86
}
catch
(
const
std::bad_cast&) { };
87
88
try
{
89
const
dtn::core::GlobalEvent
&global =
dynamic_cast<
const
dtn::core::GlobalEvent
&
>
(*evt);
90
91
// start with the event tag
92
_stream <<
"Event: "
<< global.
getName
() << std::endl;
93
_stream <<
"Action: "
;
94
95
switch
(global.
getAction
())
96
{
97
case
dtn::core::GlobalEvent::GLOBAL_BUSY
:
98
_stream <<
"busy"
;
99
break
;
100
case
dtn::core::GlobalEvent::GLOBAL_IDLE
:
101
_stream <<
"idle"
;
102
break
;
103
case
dtn::core::GlobalEvent::GLOBAL_POWERSAVE
:
104
_stream <<
"powersave"
;
105
break
;
106
case
dtn::core::GlobalEvent::GLOBAL_RELOAD
:
107
_stream <<
"reload"
;
108
break
;
109
case
dtn::core::GlobalEvent::GLOBAL_SHUTDOWN
:
110
_stream <<
"shutdown"
;
111
break
;
112
case
dtn::core::GlobalEvent::GLOBAL_SUSPEND
:
113
_stream <<
"suspend"
;
114
break
;
115
case
dtn::core::GlobalEvent::GLOBAL_INTERNET_AVAILABLE
:
116
_stream <<
"internet available"
;
117
break
;
118
case
dtn::core::GlobalEvent::GLOBAL_INTERNET_UNAVAILABLE
:
119
_stream <<
"internet unavailable"
;
120
break
;
121
default
:
122
break
;
123
}
124
_stream << std::endl;
125
126
// close the event
127
_stream << std::endl;
128
}
catch
(
const
std::bad_cast&) { };
129
130
try
{
131
const
dtn::net::BundleReceivedEvent
&received =
dynamic_cast<
const
dtn::net::BundleReceivedEvent
&
>
(*evt);
132
133
// start with the event tag
134
_stream <<
"Event: "
<< received.
getName
() << std::endl;
135
_stream <<
"Peer: "
<< received.
peer
.
getString
() << std::endl;
136
_stream <<
"Local: "
<< (received.
fromlocal
?
"true"
:
"false"
) << std::endl;
137
138
// write the bundle data
139
_stream <<
"Source: "
<< received.
bundle
.
source
.
getString
() << std::endl;
140
_stream <<
"Timestamp: "
<< received.
bundle
.
timestamp
.
toString
() << std::endl;
141
_stream <<
"Sequencenumber: "
<< received.
bundle
.
sequencenumber
.
toString
() << std::endl;
142
_stream <<
"Lifetime: "
<< received.
bundle
.
lifetime
.
toString
() << std::endl;
143
_stream <<
"Procflags: "
<< received.
bundle
.
procflags
.
toString
() << std::endl;
144
145
// write the destination eid
146
_stream <<
"Destination: "
<< received.
bundle
.
destination
.
getString
() << std::endl;
147
148
if
(received.
bundle
.
get
(
dtn::data::PrimaryBlock::FRAGMENT
))
149
{
150
// write fragmentation values
151
_stream <<
"Appdatalength: "
<< received.
bundle
.
appdatalength
.
toString
() << std::endl;
152
_stream <<
"Fragmentoffset: "
<< received.
bundle
.
fragmentoffset
.
toString
() << std::endl;
153
}
154
155
// close the event
156
_stream << std::endl;
157
}
catch
(
const
std::bad_cast&) { };
158
159
try
{
160
const
dtn::core::CustodyEvent
&custody =
dynamic_cast<
const
dtn::core::CustodyEvent
&
>
(*evt);
161
162
_stream <<
"Event: "
<< custody.
getName
() << std::endl;
163
_stream <<
"Action: "
;
164
165
switch
(custody.
getAction
())
166
{
167
case
dtn::core::CUSTODY_ACCEPT
:
168
_stream <<
"accept"
;
169
break
;
170
case
dtn::core::CUSTODY_REJECT
:
171
_stream <<
"reject"
;
172
break
;
173
default
:
174
break
;
175
}
176
_stream << std::endl;
177
178
// write the bundle data
179
_stream <<
"Source: "
<< custody.
getBundle
().
source
.
getString
() << std::endl;
180
_stream <<
"Timestamp: "
<< custody.
getBundle
().
timestamp
.
toString
() << std::endl;
181
_stream <<
"Sequencenumber: "
<< custody.
getBundle
().
sequencenumber
.
toString
() << std::endl;
182
_stream <<
"Lifetime: "
<< custody.
getBundle
().
lifetime
.
toString
() << std::endl;
183
_stream <<
"Procflags: "
<< custody.
getBundle
().
procflags
.toString() << std::endl;
184
185
// write the destination eid
186
_stream <<
"Destination: "
<< custody.
getBundle
().
destination
.
getString
() << std::endl;
187
188
if
(custody.
getBundle
().
fragment
)
189
{
190
// write fragmentation values
191
_stream <<
"Appdatalength: "
<< custody.
getBundle
().
appdatalength
.
toString
() << std::endl;
192
_stream <<
"Fragmentoffset: "
<< custody.
getBundle
().
offset
.
toString
() << std::endl;
193
}
194
195
// close the event
196
_stream << std::endl;
197
}
catch
(
const
std::bad_cast&) { };
198
199
try
{
200
const
dtn::net::TransferAbortedEvent
&aborted =
dynamic_cast<
const
dtn::net::TransferAbortedEvent
&
>
(*evt);
201
202
// start with the event tag
203
_stream <<
"Event: "
<< aborted.
getName
() << std::endl;
204
_stream <<
"Peer: "
<< aborted.
getPeer
().
getString
() << std::endl;
205
206
// write the bundle data
207
_stream <<
"Source: "
<< aborted.
getBundleID
().
source
.
getString
() << std::endl;
208
_stream <<
"Timestamp: "
<< aborted.
getBundleID
().
timestamp
.
toString
() << std::endl;
209
_stream <<
"Sequencenumber: "
<< aborted.
getBundleID
().
sequencenumber
.
toString
() << std::endl;
210
211
if
(aborted.
getBundleID
().
fragment
)
212
{
213
// write fragmentation values
214
_stream <<
"Fragmentoffset: "
<< aborted.
getBundleID
().
offset
.
toString
() << std::endl;
215
}
216
217
// close the event
218
_stream << std::endl;
219
220
}
catch
(
const
std::bad_cast&) { };
221
222
try
{
223
const
dtn::net::TransferCompletedEvent
&completed =
dynamic_cast<
const
dtn::net::TransferCompletedEvent
&
>
(*evt);
224
225
// start with the event tag
226
_stream <<
"Event: "
<< completed.
getName
() << std::endl;
227
_stream <<
"Peer: "
<< completed.
getPeer
().
getString
() << std::endl;
228
229
// write the bundle data
230
_stream <<
"Source: "
<< completed.
getBundle
().
source
.
getString
() << std::endl;
231
_stream <<
"Timestamp: "
<< completed.
getBundle
().
timestamp
.
toString
() << std::endl;
232
_stream <<
"Sequencenumber: "
<< completed.
getBundle
().
sequencenumber
.
toString
() << std::endl;
233
_stream <<
"Lifetime: "
<< completed.
getBundle
().
lifetime
.
toString
() << std::endl;
234
_stream <<
"Procflags: "
<< completed.
getBundle
().
procflags
.toString() << std::endl;
235
236
// write the destination eid
237
_stream <<
"Destination: "
<< completed.
getBundle
().
destination
.
getString
() << std::endl;
238
239
if
(completed.
getBundle
().
fragment
)
240
{
241
// write fragmentation values
242
_stream <<
"Appdatalength: "
<< completed.
getBundle
().
appdatalength
.
toString
() << std::endl;
243
_stream <<
"Fragmentoffset: "
<< completed.
getBundle
().
offset
.
toString
() << std::endl;
244
}
245
246
// close the event
247
_stream << std::endl;
248
249
}
catch
(
const
std::bad_cast&) { };
250
251
try
{
252
const
dtn::net::ConnectionEvent
&connection =
dynamic_cast<
const
dtn::net::ConnectionEvent
&
>
(*evt);
253
254
// start with the event tag
255
_stream <<
"Event: "
<< connection.
getName
() << std::endl;
256
_stream <<
"Action: "
;
257
258
switch
(connection.
state
)
259
{
260
case
dtn::net::ConnectionEvent::CONNECTION_UP
:
261
_stream <<
"up"
;
262
break
;
263
case
dtn::net::ConnectionEvent::CONNECTION_DOWN
:
264
_stream <<
"down"
;
265
break
;
266
case
dtn::net::ConnectionEvent::CONNECTION_SETUP
:
267
_stream <<
"setup"
;
268
break
;
269
case
dtn::net::ConnectionEvent::CONNECTION_TIMEOUT
:
270
_stream <<
"timeout"
;
271
break
;
272
default
:
273
break
;
274
}
275
_stream << std::endl;
276
277
// write the peer eid
278
_stream <<
"Peer: "
<< connection.
peer
.
getString
() << std::endl;
279
280
// close the event
281
_stream << std::endl;
282
}
catch
(
const
std::bad_cast&) { };
283
284
try
{
285
const
dtn::routing::QueueBundleEvent
&queued =
dynamic_cast<
const
dtn::routing::QueueBundleEvent
&
>
(*evt);
286
287
// start with the event tag
288
_stream <<
"Event: "
<< queued.
getName
() << std::endl;
289
290
// write the bundle data
291
_stream <<
"Source: "
<< queued.
bundle
.
source
.
getString
() << std::endl;
292
_stream <<
"Timestamp: "
<< queued.
bundle
.
timestamp
.
toString
() << std::endl;
293
_stream <<
"Sequencenumber: "
<< queued.
bundle
.
sequencenumber
.
toString
() << std::endl;
294
_stream <<
"Lifetime: "
<< queued.
bundle
.
lifetime
.
toString
() << std::endl;
295
_stream <<
"Procflags: "
<< queued.
bundle
.
procflags
.toString() << std::endl;
296
297
// write the destination eid
298
_stream <<
"Destination: "
<< queued.
bundle
.
destination
.
getString
() << std::endl;
299
300
if
(queued.
bundle
.
fragment
)
301
{
302
// write fragmentation values
303
_stream <<
"Appdatalength: "
<< queued.
bundle
.
appdatalength
.
toString
() << std::endl;
304
_stream <<
"Fragmentoffset: "
<< queued.
bundle
.
offset
.
toString
() << std::endl;
305
}
306
307
// close the event
308
_stream << std::endl;
309
}
catch
(
const
std::bad_cast&) { };
310
}
311
312
void
EventConnection::run
()
313
{
314
std::string buffer;
315
316
// announce protocol change
317
_stream
<<
ClientHandler::API_STATUS_OK
<<
" SWITCHED TO EVENT"
<< std::endl;
318
319
// run as long the stream is ok
320
while
(
_stream
.good())
321
{
322
getline(
_stream
, buffer);
323
324
// search for '\r\n' and remove the '\r'
325
std::string::reverse_iterator iter = buffer.rbegin();
326
if
( (*iter) ==
'\r'
) buffer = buffer.substr(0, buffer.length() - 1);
327
328
std::vector<std::string> cmd =
dtn::utils::Utils::tokenize
(
" "
, buffer);
329
if
(cmd.empty())
continue
;
330
331
// return to previous level
332
if
(cmd[0] ==
"exit"
)
break
;
333
}
334
335
ibrcommon::MutexLock
l(_mutex);
336
_running =
false
;
337
}
338
339
void
EventConnection::setup
()
340
{
341
// bind to several events
342
dtn::core::EventDispatcher<dtn::core::NodeEvent>::add
(
this
);
343
dtn::core::EventDispatcher<dtn::core::GlobalEvent>::add
(
this
);
344
dtn::core::EventDispatcher<dtn::core::CustodyEvent>::add
(
this
);
345
dtn::core::EventDispatcher<dtn::net::BundleReceivedEvent>::add
(
this
);
346
dtn::core::EventDispatcher<dtn::net::TransferAbortedEvent>::add
(
this
);
347
dtn::core::EventDispatcher<dtn::net::TransferCompletedEvent>::add
(
this
);
348
dtn::core::EventDispatcher<dtn::net::ConnectionEvent>::add
(
this
);
349
dtn::core::EventDispatcher<dtn::routing::QueueBundleEvent>::add
(
this
);
350
}
351
352
void
EventConnection::finally
()
353
{
354
// unbind to events
355
dtn::core::EventDispatcher<dtn::core::NodeEvent>::remove
(
this
);
356
dtn::core::EventDispatcher<dtn::core::GlobalEvent>::remove
(
this
);
357
dtn::core::EventDispatcher<dtn::core::CustodyEvent>::remove
(
this
);
358
dtn::core::EventDispatcher<dtn::net::BundleReceivedEvent>::remove
(
this
);
359
dtn::core::EventDispatcher<dtn::net::TransferAbortedEvent>::remove
(
this
);
360
dtn::core::EventDispatcher<dtn::net::TransferCompletedEvent>::remove
(
this
);
361
dtn::core::EventDispatcher<dtn::net::ConnectionEvent>::remove
(
this
);
362
dtn::core::EventDispatcher<dtn::routing::QueueBundleEvent>::remove
(
this
);
363
}
364
365
void
EventConnection::__cancellation
() throw ()
366
{
367
}
368
}
/* namespace api */
369
}
/* namespace dtn */
daemon
src
api
EventConnection.cpp
Generated on Mon Jul 22 2013 15:15:59 for IBR-DTNSuite by
1.8.3.1