1 |
|
---|
2 | #include "ariba/config.h"
|
---|
3 | #include "transport_peer.hpp"
|
---|
4 | #include "transport.hpp"
|
---|
5 | #include "ariba/utility/logging/Logging.h"
|
---|
6 | #include <boost/asio/ip/tcp.hpp>
|
---|
7 | #include <boost/asio/error.hpp>
|
---|
8 | #include <boost/foreach.hpp>
|
---|
9 |
|
---|
10 | #ifdef ECLIPSE_PARSER
|
---|
11 | #define foreach(a, b) for(a : b)
|
---|
12 | #else
|
---|
13 | #define foreach(a, b) BOOST_FOREACH(a, b)
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | // namespace ariba::transport
|
---|
17 | namespace ariba {
|
---|
18 | namespace transport {
|
---|
19 |
|
---|
20 | using namespace ariba::addressing;
|
---|
21 | using boost::asio::ip::tcp;
|
---|
22 |
|
---|
23 | #ifdef HAVE_LIBBLUETOOTH
|
---|
24 | using boost::asio::bluetooth::rfcomm;
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | use_logging_cpp(transport_peer);
|
---|
28 |
|
---|
29 | transport_peer::transport_peer( endpoint_set& local_set ) : local(local_set) {
|
---|
30 |
|
---|
31 | // setup tcp transports
|
---|
32 | foreach(tcp_port_address port, local.tcp) {
|
---|
33 |
|
---|
34 | if (local.ip.size() > 0) {
|
---|
35 | foreach(ip_address ip_addr, local.ip) {
|
---|
36 |
|
---|
37 | tcp::endpoint endp(ip_addr.asio(), port.asio());
|
---|
38 | create_service(endp);
|
---|
39 | }
|
---|
40 | } else {
|
---|
41 | tcp::endpoint endp_v6(tcp::v6(), port.asio());
|
---|
42 | tcp::endpoint endp_v4(tcp::v4(), port.asio());
|
---|
43 |
|
---|
44 | create_service(endp_v6);
|
---|
45 | create_service(endp_v4);
|
---|
46 | }
|
---|
47 |
|
---|
48 | }
|
---|
49 |
|
---|
50 | #ifdef HAVE_LIBBLUETOOTH
|
---|
51 | foreach(rfcomm_channel_address channel, local.rfcomm) {
|
---|
52 | if (local.bluetooth.size() > 0) {
|
---|
53 | foreach(mac_address mac, local.bluetooth) {
|
---|
54 | rfcomm::endpoint endp(mac.bluetooth(), channel.value());
|
---|
55 | create_service(endp);
|
---|
56 | }
|
---|
57 | } else {
|
---|
58 | rfcomm::endpoint endp(channel.value());
|
---|
59 | create_service(endp);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | #endif
|
---|
63 | }
|
---|
64 |
|
---|
65 | void transport_peer::create_service(tcp::endpoint endp) {
|
---|
66 | try {
|
---|
67 | TcpIpPtr tmp_ptr(new tcpip(endp));
|
---|
68 | tcps.push_back(tmp_ptr);
|
---|
69 | logging_info("Listening on IP/TCP " << endp);
|
---|
70 |
|
---|
71 | } catch (boost::system::system_error& e) {
|
---|
72 | if (e.code() == boost::asio::error::address_in_use) {
|
---|
73 | logging_warn("[WARN] Address already in use: "
|
---|
74 | << endp << ". Endpoint will be ignored!");
|
---|
75 | } else {
|
---|
76 | // Rethrow
|
---|
77 | throw;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | #ifdef HAVE_LIBBLUETOOTH
|
---|
83 | void transport_peer::create_service(rfcomm::endpoint endp) {
|
---|
84 | try {
|
---|
85 | rfcomm_transport::sptr tmp_ptr(new rfcomm_transport(endp));
|
---|
86 | rfcomms.push_back(tmp_ptr);
|
---|
87 | logging_info("Listening on bluetooth/RFCOMM " << endp);
|
---|
88 |
|
---|
89 | } catch (boost::system::system_error& e) {
|
---|
90 | if (e.code() == boost::asio::error::address_in_use) {
|
---|
91 | logging_warn("[WARN] Address already in use: "
|
---|
92 | << endp << ". Endpoint will be ignored!");
|
---|
93 | } else {
|
---|
94 | // Rethrow
|
---|
95 | throw;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | transport_peer::~transport_peer() {
|
---|
102 | }
|
---|
103 |
|
---|
104 | void transport_peer::start() {
|
---|
105 | foreach(TcpIpPtr tcp, tcps) {
|
---|
106 | tcp->start();
|
---|
107 | }
|
---|
108 |
|
---|
109 | #ifdef HAVE_LIBBLUETOOTH
|
---|
110 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
111 | x->start();
|
---|
112 | }
|
---|
113 | #endif
|
---|
114 | }
|
---|
115 |
|
---|
116 | void transport_peer::stop() {
|
---|
117 | foreach(TcpIpPtr tcp, tcps) {
|
---|
118 | tcp->stop();
|
---|
119 | }
|
---|
120 |
|
---|
121 | #ifdef HAVE_LIBBLUETOOTH
|
---|
122 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
123 | x->stop();
|
---|
124 | }
|
---|
125 | #endif
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | void transport_peer::send(
|
---|
130 | const endpoint_set& endpoints,
|
---|
131 | reboost::message_t message,
|
---|
132 | uint8_t priority)
|
---|
133 | {
|
---|
134 | foreach(TcpIpPtr tcp, tcps) {
|
---|
135 | tcp->send(endpoints, message, priority);
|
---|
136 | }
|
---|
137 |
|
---|
138 | #ifdef HAVE_LIBBLUETOOTH
|
---|
139 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
140 | x->send(endpoints, message, priority);
|
---|
141 | }
|
---|
142 | #endif
|
---|
143 | }
|
---|
144 |
|
---|
145 | void transport_peer::terminate( const address_v* remote ) {
|
---|
146 | if (remote->instanceof<tcpip_endpoint>())// TODO direkt auf der richtigen verbindung
|
---|
147 | {
|
---|
148 | foreach(TcpIpPtr tcp, tcps) {
|
---|
149 | tcp->terminate(remote);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | #ifdef HAVE_LIBBLUETOOTH
|
---|
153 | if (remote->instanceof<rfcomm_endpoint>()) {
|
---|
154 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
155 | x->terminate(remote);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | #endif
|
---|
159 | }
|
---|
160 |
|
---|
161 | void transport_peer::register_listener( transport_listener* listener ) {
|
---|
162 | foreach(TcpIpPtr tcp, tcps) {
|
---|
163 | tcp->register_listener(listener);
|
---|
164 | }
|
---|
165 |
|
---|
166 | #ifdef HAVE_LIBBLUETOOTH
|
---|
167 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
168 | x->register_listener(listener);
|
---|
169 | }
|
---|
170 | #endif
|
---|
171 | }
|
---|
172 |
|
---|
173 | }} // namespace ariba::transport
|
---|