IBR-DTNSuite  0.10
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 << "powersave";
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;
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 
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  {
168  _stream << "accept";
169  break;
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  {
261  _stream << "up";
262  break;
264  _stream << "down";
265  break;
267  _stream << "setup";
268  break;
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 
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 
340  {
341  // bind to several events
350  }
351 
353  {
354  // unbind to events
363  }
364 
366  {
367  }
368  } /* namespace api */
369 } /* namespace dtn */