IBR-DTNSuite  0.10
ApiP2PExtensionHandler.cpp
Go to the documentation of this file.
1 /*
2  * ApiP2PExtensionHandler.cpp
3  *
4  * Created on: 25.02.2013
5  * Author: morgenro
6  */
7 
9 #include "core/BundleCore.h"
10 #include <ibrdtn/utils/Utils.h>
12 
13 namespace dtn
14 {
15  namespace api
16  {
18  : ProtocolHandler(client, stream), _proto(proto)
19  {
20  }
21 
23  {
24  }
25 
27  {
28  std::string buffer = "";
29  _stream << ClientHandler::API_STATUS_OK << " SWITCHED TO P2P_EXTENSION" << std::endl;
30 
31  // run as long the stream is ok
32  while (_stream.good())
33  {
34  getline(_stream, buffer);
35 
36  if (buffer.length() == 0) continue;
37 
38  // search for '\r\n' and remove the '\r'
39  std::string::reverse_iterator iter = buffer.rbegin();
40  if ( (*iter) == '\r' ) buffer = buffer.substr(0, buffer.length() - 1);
41 
42  std::vector<std::string> cmd = dtn::utils::Utils::tokenize(" ", buffer);
43  if (cmd.empty()) continue;
44 
45  if (cmd[0] == "exit")
46  {
47  // return to previous level
48  break;
49  }
50  else
51  {
52  // forward to standard command set
53  processCommand(cmd);
54  }
55  }
56  }
57 
59  {
61  }
62 
64  {
66  }
67 
69  {
70  }
71 
73  {
74  return _proto;
75  }
76 
78  {
79  ibrcommon::MutexLock l(_write_lock);
80  _stream << CMD_CONNECT << " CONNECT " << uri.value << std::endl;
81  }
82 
84  {
85  ibrcommon::MutexLock l(_write_lock);
86  _stream << CMD_DISCONNECT << " DISCONNECT " << uri.value << std::endl;
87  }
88 
89  void ApiP2PExtensionHandler::processCommand(const std::vector<std::string> &cmd)
90  {
91  try {
92  if (cmd[0] == "connected") {
93  const dtn::data::EID eid(cmd[1]);
95 
96  if (cmd[2] == "tcp") {
98  }
99  else if (cmd[2] == "udp") {
101  }
102 
103  const dtn::core::Node::URI uri(dtn::core::Node::NODE_CONNECTED, proto, cmd[3], 0, 30);
104  fireConnected(eid, uri);
105 
106  ibrcommon::MutexLock l(_write_lock);
107  _stream << ClientHandler::API_STATUS_OK << " NODE CONNECTED" << std::endl;
108  }
109  else if (cmd[0] == "disconnected") {
110  const dtn::data::EID eid(cmd[1]);
112 
113  if (cmd[2] == "tcp") {
115  }
116  else if (cmd[2] == "udp") {
118  }
119 
120  const dtn::core::Node::URI uri(dtn::core::Node::NODE_CONNECTED, proto, cmd[3], 0, 10);
121  fireDisconnected(eid, uri);
122 
123  ibrcommon::MutexLock l(_write_lock);
124  _stream << ClientHandler::API_STATUS_OK << " NODE DISCONNECTED" << std::endl;
125  }
126  else if (cmd[0] == "discovered") {
127  const dtn::data::EID eid(cmd[1]);
128  const dtn::core::Node::URI uri(dtn::core::Node::NODE_P2P_DIALUP, this->getProtocol(), cmd[2], 120, 10);
129  fireDiscovered(eid, uri);
130 
131  ibrcommon::MutexLock l(_write_lock);
132  _stream << ClientHandler::API_STATUS_OK << " NODE DISCOVERED" << std::endl;
133  }
134  else if (cmd[0] == "interface") {
135  if (cmd[1] == "up") {
136  const ibrcommon::vinterface iface(cmd[2]);
137  fireInterfaceUp(iface);
138 
139  ibrcommon::MutexLock l(_write_lock);
140  _stream << ClientHandler::API_STATUS_OK << " INTERFACE UP" << std::endl;
141  }
142  else if (cmd[1] == "down") {
143  const ibrcommon::vinterface iface(cmd[2]);
144  fireInterfaceDown(iface);
145 
146  ibrcommon::MutexLock l(_write_lock);
147  _stream << ClientHandler::API_STATUS_OK << " INTERFACE DOWN" << std::endl;
148  }
149  else {
150  ibrcommon::MutexLock l(_write_lock);
151  _stream << ClientHandler::API_STATUS_BAD_REQUEST << " UNKNOWN INTERFACE STATE" << std::endl;
152  }
153  }
154  else {
155  ibrcommon::MutexLock l(_write_lock);
156  _stream << ClientHandler::API_STATUS_BAD_REQUEST << " UNKNOWN ACTION" << std::endl;
157  }
158  } catch (const std::exception&) {
159  ibrcommon::MutexLock l(_write_lock);
160  _stream << ClientHandler::API_STATUS_BAD_REQUEST << " ERROR" << std::endl;
161  }
162  }
163  } /* namespace api */
164 } /* namespace dtn */