IBR-DTNSuite  0.12
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"
32 #include "net/ConnectionEvent.h"
33 
34 #include <ibrdtn/utils/Utils.h>
35 
36 namespace dtn
37 {
38  namespace api
39  {
41  : ProtocolHandler(client, stream), _running(true)
42  {
43  }
44 
46  {
47  }
48 
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  {
64  _stream << "available";
65  break;
67  _stream << "unavailable";
68  break;
70  _stream << "data_added";
71  break;
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  {
98  _stream << "busy";
99  break;
101  _stream << "idle";
102  break;
104  _stream << "resume";
105  break;
107  _stream << "reload";
108  break;
110  _stream << "shutdown";
111  break;
113  _stream << "suspend";
114  break;
116  _stream << "internet available";
117  break;
119  _stream << "internet unavailable";
120  break;
122  _stream << "start discovery";
123  break;
125  _stream << "stop discovery";
126  break;
127  default:
128  break;
129  }
130  _stream << std::endl;
131 
132  // close the event
133  _stream << std::endl;
134  } catch (const std::bad_cast&) { };
135 
136  try {
137  const dtn::net::BundleReceivedEvent &received = dynamic_cast<const dtn::net::BundleReceivedEvent&>(*evt);
138 
139  // start with the event tag
140  _stream << "Event: " << received.getName() << std::endl;
141  _stream << "Peer: " << received.peer.getString() << std::endl;
142  _stream << "Local: " << (received.fromlocal ? "true" : "false") << std::endl;
143 
144  // write the bundle data
145  _stream << "Source: " << received.bundle.source.getString() << std::endl;
146  _stream << "Timestamp: " << received.bundle.timestamp.toString() << std::endl;
147  _stream << "Sequencenumber: " << received.bundle.sequencenumber.toString() << std::endl;
148  _stream << "Lifetime: " << received.bundle.lifetime.toString() << std::endl;
149  _stream << "Procflags: " << received.bundle.procflags.toString() << std::endl;
150 
151  // write the destination eid
152  _stream << "Destination: " << received.bundle.destination.getString() << std::endl;
153 
155  {
156  // write fragmentation values
157  _stream << "Appdatalength: " << received.bundle.appdatalength.toString() << std::endl;
158  _stream << "Fragmentoffset: " << received.bundle.fragmentoffset.toString() << std::endl;
159  }
160 
161  // close the event
162  _stream << std::endl;
163  } catch (const std::bad_cast&) { };
164 
165  try {
166  const dtn::core::CustodyEvent &custody = dynamic_cast<const dtn::core::CustodyEvent&>(*evt);
167 
168  _stream << "Event: " << custody.getName() << std::endl;
169  _stream << "Action: ";
170 
171  switch (custody.getAction())
172  {
174  _stream << "accept";
175  break;
177  _stream << "reject";
178  break;
179  default:
180  break;
181  }
182  _stream << std::endl;
183 
184  // write the bundle data
185  _stream << "Source: " << custody.getBundle().source.getString() << std::endl;
186  _stream << "Timestamp: " << custody.getBundle().timestamp.toString() << std::endl;
187  _stream << "Sequencenumber: " << custody.getBundle().sequencenumber.toString() << std::endl;
188  _stream << "Lifetime: " << custody.getBundle().lifetime.toString() << std::endl;
189  _stream << "Procflags: " << custody.getBundle().procflags.toString() << std::endl;
190 
191  // write the destination eid
192  _stream << "Destination: " << custody.getBundle().destination.getString() << std::endl;
193 
194  if (custody.getBundle().isFragment())
195  {
196  // write fragmentation values
197  _stream << "Appdatalength: " << custody.getBundle().appdatalength.toString() << std::endl;
198  _stream << "Fragmentoffset: " << custody.getBundle().fragmentoffset.toString() << std::endl;
199  }
200 
201  // close the event
202  _stream << std::endl;
203  } catch (const std::bad_cast&) { };
204 
205  try {
206  const dtn::net::TransferAbortedEvent &aborted = dynamic_cast<const dtn::net::TransferAbortedEvent&>(*evt);
207 
208  // start with the event tag
209  _stream << "Event: " << aborted.getName() << std::endl;
210  _stream << "Peer: " << aborted.getPeer().getString() << std::endl;
211 
212  // write the bundle data
213  _stream << "Source: " << aborted.getBundleID().source.getString() << std::endl;
214  _stream << "Timestamp: " << aborted.getBundleID().timestamp.toString() << std::endl;
215  _stream << "Sequencenumber: " << aborted.getBundleID().sequencenumber.toString() << std::endl;
216 
217  if (aborted.getBundleID().isFragment())
218  {
219  // write fragmentation values
220  _stream << "Fragmentoffset: " << aborted.getBundleID().fragmentoffset.toString() << std::endl;
221  _stream << "Fragmentpayload: " << aborted.getBundleID().getPayloadLength() << std::endl;
222  }
223 
224  // close the event
225  _stream << std::endl;
226 
227  } catch (const std::bad_cast&) { };
228 
229  try {
230  const dtn::net::TransferCompletedEvent &completed = dynamic_cast<const dtn::net::TransferCompletedEvent&>(*evt);
231 
232  // start with the event tag
233  _stream << "Event: " << completed.getName() << std::endl;
234  _stream << "Peer: " << completed.getPeer().getString() << std::endl;
235 
236  // write the bundle data
237  _stream << "Source: " << completed.getBundle().source.getString() << std::endl;
238  _stream << "Timestamp: " << completed.getBundle().timestamp.toString() << std::endl;
239  _stream << "Sequencenumber: " << completed.getBundle().sequencenumber.toString() << std::endl;
240  _stream << "Lifetime: " << completed.getBundle().lifetime.toString() << std::endl;
241  _stream << "Procflags: " << completed.getBundle().procflags.toString() << std::endl;
242 
243  // write the destination eid
244  _stream << "Destination: " << completed.getBundle().destination.getString() << std::endl;
245 
246  if (completed.getBundle().isFragment())
247  {
248  // write fragmentation values
249  _stream << "Appdatalength: " << completed.getBundle().appdatalength.toString() << std::endl;
250  _stream << "Fragmentoffset: " << completed.getBundle().fragmentoffset.toString() << std::endl;
251  }
252 
253  // close the event
254  _stream << std::endl;
255 
256  } catch (const std::bad_cast&) { };
257 
258  try {
259  const dtn::net::ConnectionEvent &connection = dynamic_cast<const dtn::net::ConnectionEvent&>(*evt);
260 
261  // start with the event tag
262  _stream << "Event: " << connection.getName() << std::endl;
263  _stream << "Action: ";
264 
265  switch (connection.getState())
266  {
268  _stream << "up";
269  break;
271  _stream << "down";
272  break;
274  _stream << "setup";
275  break;
277  _stream << "timeout";
278  break;
279  default:
280  break;
281  }
282  _stream << std::endl;
283 
284  // write the peer eid
285  _stream << "Peer: " << connection.getNode().getEID().getString() << std::endl;
286 
287  // close the event
288  _stream << std::endl;
289  } catch (const std::bad_cast&) { };
290 
291  try {
292  const dtn::routing::QueueBundleEvent &queued = dynamic_cast<const dtn::routing::QueueBundleEvent&>(*evt);
293 
294  // start with the event tag
295  _stream << "Event: " << queued.getName() << std::endl;
296 
297  // write the bundle data
298  _stream << "Source: " << queued.bundle.source.getString() << std::endl;
299  _stream << "Timestamp: " << queued.bundle.timestamp.toString() << std::endl;
300  _stream << "Sequencenumber: " << queued.bundle.sequencenumber.toString() << std::endl;
301  _stream << "Lifetime: " << queued.bundle.lifetime.toString() << std::endl;
302  _stream << "Procflags: " << queued.bundle.procflags.toString() << std::endl;
303 
304  // write the destination eid
305  _stream << "Destination: " << queued.bundle.destination.getString() << std::endl;
306 
307  if (queued.bundle.isFragment())
308  {
309  // write fragmentation values
310  _stream << "Appdatalength: " << queued.bundle.appdatalength.toString() << std::endl;
311  _stream << "Fragmentoffset: " << queued.bundle.fragmentoffset.toString() << std::endl;
312  }
313 
314  // close the event
315  _stream << std::endl;
316  } catch (const std::bad_cast&) { };
317  }
318 
320  {
321  std::string buffer;
322 
323  // announce protocol change
324  _stream << ClientHandler::API_STATUS_OK << " SWITCHED TO EVENT" << std::endl;
325 
326  // run as long the stream is ok
327  while (_stream.good())
328  {
329  getline(_stream, buffer);
330 
331  // search for '\r\n' and remove the '\r'
332  std::string::reverse_iterator iter = buffer.rbegin();
333  if ( (*iter) == '\r' ) buffer = buffer.substr(0, buffer.length() - 1);
334 
335  std::vector<std::string> cmd = dtn::utils::Utils::tokenize(" ", buffer);
336  if (cmd.empty()) continue;
337 
338  // return to previous level
339  if (cmd[0] == "exit") break;
340  }
341 
342  ibrcommon::MutexLock l(_mutex);
343  _running = false;
344  }
345 
347  {
348  // bind to several events
357  }
358 
360  {
361  // unbind to events
370  }
371 
373  {
374  }
375  } /* namespace api */
376 } /* namespace dtn */