IBR-DTNSuite
0.12
Main Page
Namespaces
Classes
Files
File List
File Members
IBR-DTNSuite
Namespaces
Classes
Files
File List
daemon
src
api
ApiP2PExtensionHandler.cpp
ApiP2PExtensionHandler.h
ApiServer.cpp
ApiServer.h
BinaryStreamClient.cpp
BinaryStreamClient.h
BundleStreamBuf.cpp
BundleStreamBuf.h
ClientHandler.cpp
ClientHandler.h
EventConnection.cpp
EventConnection.h
ExtendedApiHandler.cpp
ExtendedApiHandler.h
ManagementConnection.cpp
ManagementConnection.h
NativeSerializer.cpp
NativeSerializer.h
NativeSerializerCallback.cpp
NativeSerializerCallback.h
NativeSession.cpp
NativeSession.h
OrderedStreamHandler.cpp
OrderedStreamHandler.h
Registration.cpp
Registration.h
core
net
routing
security
storage
CapsuleWorker.cpp
CapsuleWorker.h
Component.cpp
Component.h
config.h
Configuration.cpp
Configuration.h
Debugger.cpp
Debugger.h
DevNull.cpp
DevNull.h
DTNTPWorker.cpp
DTNTPWorker.h
EchoWorker.cpp
EchoWorker.h
Main.cpp
NativeDaemon.cpp
NativeDaemon.h
NTService.cpp
ibrcommon
ibrdtn
tools
File Members
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
8
#include "
api/ApiP2PExtensionHandler.h
"
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
{
17
ApiP2PExtensionHandler::ApiP2PExtensionHandler
(
ClientHandler
&client,
ibrcommon::socketstream
&stream,
dtn::core::Node::Protocol
proto)
18
:
ProtocolHandler
(client, stream), _proto(proto)
19
{
20
}
21
22
ApiP2PExtensionHandler::~ApiP2PExtensionHandler
()
23
{
24
}
25
26
void
ApiP2PExtensionHandler::run
()
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
58
void
ApiP2PExtensionHandler::finally
()
59
{
60
dtn::core::BundleCore::getInstance
().
getConnectionManager
().
remove
(
this
);
61
}
62
63
void
ApiP2PExtensionHandler::setup
()
64
{
65
dtn::core::BundleCore::getInstance
().
getConnectionManager
().
add
(
this
);
66
}
67
68
void
ApiP2PExtensionHandler::__cancellation
() throw ()
69
{
70
}
71
72
dtn::core::Node::Protocol
ApiP2PExtensionHandler::getProtocol
()
const
73
{
74
return
_proto;
75
}
76
77
void
ApiP2PExtensionHandler::connect
(
const
dtn::core::Node::URI
&uri)
78
{
79
ibrcommon::MutexLock
l(_write_lock);
80
_stream
<<
CMD_CONNECT
<<
" CONNECT "
<< uri.
value
<< std::endl;
81
}
82
83
void
ApiP2PExtensionHandler::disconnect
(
const
dtn::core::Node::URI
&uri)
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]);
94
dtn::core::Node::Protocol
proto =
dtn::core::Node::CONN_TCPIP
;
95
96
if
(cmd[2] ==
"tcp"
) {
97
proto =
dtn::core::Node::CONN_TCPIP
;
98
}
99
else
if
(cmd[2] ==
"udp"
) {
100
proto =
dtn::core::Node::CONN_UDPIP
;
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]);
111
dtn::core::Node::Protocol
proto =
dtn::core::Node::CONN_TCPIP
;
112
113
if
(cmd[2] ==
"tcp"
) {
114
proto =
dtn::core::Node::CONN_TCPIP
;
115
}
116
else
if
(cmd[2] ==
"udp"
) {
117
proto =
dtn::core::Node::CONN_UDPIP
;
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 */
daemon
src
api
ApiP2PExtensionHandler.cpp
Generated on Thu Mar 27 2014 09:26:19 for IBR-DTNSuite by
1.8.4