IBR-DTN  1.0.0
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>
11 #include <ibrcommon/net/vinterface.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  if (cmd.size() < 2) throw ibrcommon::Exception("not enough parameters");
94 
95  const dtn::data::EID eid(cmd[1]);
96  const dtn::core::Node::URI uri(dtn::core::Node::NODE_CONNECTED, this->getProtocol(), cmd[2], 120, -40);
97  fireConnected(eid, uri);
98 
99  ibrcommon::MutexLock l(_write_lock);
100  _stream << ClientHandler::API_STATUS_OK << " NODE CONNECTED" << std::endl;
101  }
102  else if (cmd[0] == "disconnected") {
103  if (cmd.size() < 2) throw ibrcommon::Exception("not enough parameters");
104 
105  const dtn::data::EID eid(cmd[1]);
106  const dtn::core::Node::URI uri(dtn::core::Node::NODE_CONNECTED, this->getProtocol(), cmd[2], 0, 0);
107  fireDisconnected(eid, uri);
108 
109  ibrcommon::MutexLock l(_write_lock);
110  _stream << ClientHandler::API_STATUS_OK << " NODE DISCONNECTED" << std::endl;
111  }
112  else if (cmd[0] == "discovered") {
113  if (cmd.size() < 2) throw ibrcommon::Exception("not enough parameters");
114 
115  const dtn::data::EID eid(cmd[1]);
116  const dtn::core::Node::URI uri(dtn::core::Node::NODE_P2P_DIALUP, this->getProtocol(), cmd[2], 120, -50);
117  fireDiscovered(eid, uri);
118 
119  ibrcommon::MutexLock l(_write_lock);
120  _stream << ClientHandler::API_STATUS_OK << " NODE DISCOVERED" << std::endl;
121  }
122  else if (cmd[0] == "interface") {
123  if (cmd.size() < 2) throw ibrcommon::Exception("not enough parameters");
124 
125  if (cmd[1] == "up") {
126  const ibrcommon::vinterface iface(cmd[2]);
127  fireInterfaceUp(iface);
128 
129  ibrcommon::MutexLock l(_write_lock);
130  _stream << ClientHandler::API_STATUS_OK << " INTERFACE UP" << std::endl;
131  }
132  else if (cmd[1] == "down") {
133  const ibrcommon::vinterface iface(cmd[2]);
134  fireInterfaceDown(iface);
135 
136  ibrcommon::MutexLock l(_write_lock);
137  _stream << ClientHandler::API_STATUS_OK << " INTERFACE DOWN" << std::endl;
138  }
139  else {
140  ibrcommon::MutexLock l(_write_lock);
141  _stream << ClientHandler::API_STATUS_BAD_REQUEST << " UNKNOWN INTERFACE STATE" << std::endl;
142  }
143  }
144  else {
145  ibrcommon::MutexLock l(_write_lock);
146  _stream << ClientHandler::API_STATUS_BAD_REQUEST << " UNKNOWN ACTION" << std::endl;
147  }
148  } catch (const std::exception&) {
149  ibrcommon::MutexLock l(_write_lock);
150  _stream << ClientHandler::API_STATUS_BAD_REQUEST << " ERROR" << std::endl;
151  }
152  }
153  } /* namespace api */
154 } /* namespace dtn */
void fireDisconnected(const dtn::data::EID &eid, const dtn::core::Node::URI &uri) const
ApiP2PExtensionHandler(ClientHandler &client, ibrcommon::socketstream &stream, dtn::core::Node::Protocol proto)
virtual dtn::core::Node::Protocol getProtocol() const
void remove(const dtn::core::Node &n)
dtn::net::ConnectionManager & getConnectionManager()
Definition: BundleCore.cpp:260
virtual void disconnect(const dtn::core::Node::URI &uri)
void fireInterfaceDown(const ibrcommon::vinterface &iface) const
void add(const dtn::core::Node &n)
std::string value
Definition: Node.h:89
virtual void connect(const dtn::core::Node::URI &uri)
void fireConnected(const dtn::data::EID &eid, const dtn::core::Node::URI &uri) const
void fireInterfaceUp(const ibrcommon::vinterface &iface) const
ibrcommon::socketstream & _stream
Definition: ClientHandler.h:52
void fireDiscovered(const dtn::data::EID &eid, const dtn::core::Node::URI &uri) const
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
static BundleCore & getInstance()
Definition: BundleCore.cpp:82