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