IBR-DTN  1.0.0
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 #include "core/EventDispatcher.h"
24 
25 #include <ibrdtn/utils/Utils.h>
26 
27 namespace dtn
28 {
29  namespace api
30  {
31  EventConnection::EventConnection(ClientHandler &client, ibrcommon::socketstream &stream)
32  : ProtocolHandler(client, stream), _running(true)
33  {
34  }
35 
37  {
38  }
39 
41  {
42  ibrcommon::MutexLock l(_mutex);
43  if (!_running) return;
44 
45  // start with the event tag
46  _stream << "Event: " << node.getName() << std::endl;
47  _stream << "Action: ";
48 
49  switch (node.getAction())
50  {
52  _stream << "available";
53  break;
55  _stream << "unavailable";
56  break;
58  _stream << "data_added";
59  break;
61  _stream << "data_removed";
62  break;
63  default:
64  break;
65  }
66 
67  _stream << std::endl;
68 
69  // write the node eid
70  _stream << "EID: " << node.getNode().getEID().getString() << std::endl;
71 
72  // close the event
73  _stream << std::endl;
74  }
75 
77  {
78  ibrcommon::MutexLock l(_mutex);
79  if (!_running) return;
80 
81  // start with the event tag
82  _stream << "Event: " << global.getName() << std::endl;
83  _stream << "Action: ";
84 
85  switch (global.getAction())
86  {
88  _stream << "busy";
89  break;
91  _stream << "idle";
92  break;
94  _stream << "normal";
95  break;
97  _stream << "reload";
98  break;
100  _stream << "shutdown";
101  break;
103  _stream << "low-energy";
104  break;
106  _stream << "internet available";
107  break;
109  _stream << "internet unavailable";
110  break;
112  _stream << "start discovery";
113  break;
115  _stream << "stop discovery";
116  break;
117  default:
118  break;
119  }
120  _stream << std::endl;
121 
122  // close the event
123  _stream << std::endl;
124  }
125 
127  {
128  ibrcommon::MutexLock l(_mutex);
129  if (!_running) return;
130 
131  // start with the event tag
132  _stream << "Event: " << received.getName() << std::endl;
133  _stream << "Peer: " << received.peer.getString() << std::endl;
134  _stream << "Local: " << (received.fromlocal ? "true" : "false") << std::endl;
135 
136  // write the bundle data
137  _stream << "Source: " << received.bundle.source.getString() << std::endl;
138  _stream << "Timestamp: " << received.bundle.timestamp.toString() << std::endl;
139  _stream << "Sequencenumber: " << received.bundle.sequencenumber.toString() << std::endl;
140  _stream << "Lifetime: " << received.bundle.lifetime.toString() << std::endl;
141  _stream << "Procflags: " << received.bundle.procflags.toString() << std::endl;
142 
143  // write the destination eid
144  _stream << "Destination: " << received.bundle.destination.getString() << std::endl;
145 
146  if (received.bundle.get(dtn::data::PrimaryBlock::FRAGMENT))
147  {
148  // write fragmentation values
149  _stream << "Appdatalength: " << received.bundle.appdatalength.toString() << std::endl;
150  _stream << "Fragmentoffset: " << received.bundle.fragmentoffset.toString() << std::endl;
151  }
152 
153  // close the event
154  _stream << std::endl;
155  }
156 
158  {
159  ibrcommon::MutexLock l(_mutex);
160  if (!_running) return;
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().isFragment())
189  {
190  // write fragmentation values
191  _stream << "Appdatalength: " << custody.getBundle().appdatalength.toString() << std::endl;
192  _stream << "Fragmentoffset: " << custody.getBundle().fragmentoffset.toString() << std::endl;
193  }
194 
195  // close the event
196  _stream << std::endl;
197  }
198 
200  {
201  ibrcommon::MutexLock l(_mutex);
202  if (!_running) return;
203 
204  // start with the event tag
205  _stream << "Event: " << aborted.getName() << std::endl;
206  _stream << "Peer: " << aborted.getPeer().getString() << std::endl;
207 
208  // write the bundle data
209  _stream << "Source: " << aborted.getBundleID().source.getString() << std::endl;
210  _stream << "Timestamp: " << aborted.getBundleID().timestamp.toString() << std::endl;
211  _stream << "Sequencenumber: " << aborted.getBundleID().sequencenumber.toString() << std::endl;
212 
213  if (aborted.getBundleID().isFragment())
214  {
215  // write fragmentation values
216  _stream << "Fragmentoffset: " << aborted.getBundleID().fragmentoffset.toString() << std::endl;
217  _stream << "Fragmentpayload: " << aborted.getBundleID().getPayloadLength() << std::endl;
218  }
219 
220  // close the event
221  _stream << std::endl;
222  }
223 
225  {
226  ibrcommon::MutexLock l(_mutex);
227  if (!_running) return;
228 
229  // start with the event tag
230  _stream << "Event: " << completed.getName() << std::endl;
231  _stream << "Peer: " << completed.getPeer().getString() << std::endl;
232 
233  // write the bundle data
234  _stream << "Source: " << completed.getBundle().source.getString() << std::endl;
235  _stream << "Timestamp: " << completed.getBundle().timestamp.toString() << std::endl;
236  _stream << "Sequencenumber: " << completed.getBundle().sequencenumber.toString() << std::endl;
237  _stream << "Lifetime: " << completed.getBundle().lifetime.toString() << std::endl;
238  _stream << "Procflags: " << completed.getBundle().procflags.toString() << std::endl;
239 
240  // write the destination eid
241  _stream << "Destination: " << completed.getBundle().destination.getString() << std::endl;
242 
243  if (completed.getBundle().isFragment())
244  {
245  // write fragmentation values
246  _stream << "Appdatalength: " << completed.getBundle().appdatalength.toString() << std::endl;
247  _stream << "Fragmentoffset: " << completed.getBundle().fragmentoffset.toString() << std::endl;
248  }
249 
250  // close the event
251  _stream << std::endl;
252  }
253 
255  {
256  ibrcommon::MutexLock l(_mutex);
257  if (!_running) return;
258 
259  // start with the event tag
260  _stream << "Event: " << connection.getName() << std::endl;
261  _stream << "Action: ";
262 
263  switch (connection.getState())
264  {
266  _stream << "up";
267  break;
269  _stream << "down";
270  break;
272  _stream << "setup";
273  break;
275  _stream << "timeout";
276  break;
277  default:
278  break;
279  }
280  _stream << std::endl;
281 
282  // write the peer eid
283  _stream << "Peer: " << connection.getNode().getEID().getString() << std::endl;
284 
285  // close the event
286  _stream << std::endl;
287  }
288 
290  {
291  ibrcommon::MutexLock l(_mutex);
292  if (!_running) return;
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  }
317 
319  {
320  std::string buffer;
321 
322  // announce protocol change
323  _stream << ClientHandler::API_STATUS_OK << " SWITCHED TO EVENT" << std::endl;
324 
325  // run as long the stream is ok
326  while (_stream.good())
327  {
328  getline(_stream, buffer);
329 
330  // search for '\r\n' and remove the '\r'
331  std::string::reverse_iterator iter = buffer.rbegin();
332  if ( (*iter) == '\r' ) buffer = buffer.substr(0, buffer.length() - 1);
333 
334  std::vector<std::string> cmd = dtn::utils::Utils::tokenize(" ", buffer);
335  if (cmd.empty()) continue;
336 
337  // return to previous level
338  if (cmd[0] == "exit") break;
339  }
340 
341  ibrcommon::MutexLock l(_mutex);
342  _running = false;
343  }
344 
346  {
347  // bind to several events
356  }
357 
359  {
360  // unbind to events
369  }
370 
372  {
373  }
374  } /* namespace api */
375 } /* namespace dtn */
void raiseEvent(const dtn::core::NodeEvent &evt)
EventConnection(ClientHandler &client, ibrcommon::socketstream &stream)
static void add(EventReceiver< E > *receiver)
static void remove(const EventReceiver< E > *receiver)
bool _running
Definition: dtninbox.cpp:122
ibrcommon::socketstream & _stream
Definition: ClientHandler.h:52
static std::vector< std::string > tokenize(const std::string &token, const std::string &data, const std::string::size_type max=std::string::npos)
Definition: Utils.cpp:60