[12060] | 1 | #include "transport_peer.hpp"
|
---|
[5284] | 2 |
|
---|
[12060] | 3 | // ariba
|
---|
| 4 | #include "StreamTransport/StreamTransport.hpp"
|
---|
| 5 | #include "ariba/utility/addressing2/tcpip_endpoint.hpp"
|
---|
| 6 |
|
---|
| 7 | // boost
|
---|
[10653] | 8 | #include <boost/asio/error.hpp>
|
---|
| 9 | #include <boost/foreach.hpp>
|
---|
[5284] | 10 |
|
---|
| 11 | // namespace ariba::transport
|
---|
| 12 | namespace ariba {
|
---|
| 13 | namespace transport {
|
---|
| 14 |
|
---|
[12060] | 15 | using namespace addressing2;
|
---|
[10653] | 16 | using boost::asio::ip::tcp;
|
---|
[5284] | 17 |
|
---|
[10653] | 18 | #ifdef HAVE_LIBBLUETOOTH
|
---|
| 19 | using boost::asio::bluetooth::rfcomm;
|
---|
| 20 | #endif
|
---|
| 21 |
|
---|
| 22 | use_logging_cpp(transport_peer);
|
---|
| 23 |
|
---|
[12060] | 24 | transport_peer::transport_peer() :
|
---|
| 25 | local(new addressing2::endpoint_set())
|
---|
| 26 | {
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | EndpointSetPtr transport_peer::add_listenOn_endpoints(EndpointSetPtr endpoints)
|
---|
| 30 | {
|
---|
| 31 | // TCP Endpoints
|
---|
| 32 | BOOST_FOREACH( shared_ptr<tcpip_endpoint> endp, endpoints->get_tcpip_endpoints() )
|
---|
| 33 | {
|
---|
| 34 | // automatic port detection
|
---|
| 35 | bool port_detection = false;
|
---|
| 36 | uint16_t try_port = 41322;
|
---|
[10653] | 37 |
|
---|
[12060] | 38 | tcp::endpoint asio_endp = endp->to_asio();
|
---|
| 39 | if ( asio_endp.port() == 0 )
|
---|
| 40 | {
|
---|
| 41 | port_detection = true;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | // create new server socket
|
---|
| 46 | do
|
---|
| 47 | {
|
---|
| 48 | try
|
---|
| 49 | {
|
---|
| 50 | // automatic port detection
|
---|
| 51 | if ( port_detection )
|
---|
| 52 | {
|
---|
| 53 | asio_endp.port(try_port);
|
---|
| 54 | endp = tcpip_endpoint::create_TcpIP_Endpoint(asio_endp);
|
---|
| 55 | }
|
---|
[10653] | 56 |
|
---|
[12060] | 57 | TransportProtocolPtr tmp_ptr(new StreamTransport<tcp>(endp->to_asio()));
|
---|
| 58 | transport_streams.push_back(tmp_ptr);
|
---|
| 59 | logging_info("Listening on IP/TCP " << endp->to_string());
|
---|
| 60 |
|
---|
| 61 | local->add_endpoint(endp);
|
---|
| 62 | port_detection = false;
|
---|
[10653] | 63 | }
|
---|
| 64 |
|
---|
[12060] | 65 | catch (boost::system::system_error& e)
|
---|
| 66 | {
|
---|
| 67 | // address in use
|
---|
| 68 | if (e.code() == boost::asio::error::address_in_use)
|
---|
| 69 | {
|
---|
| 70 | // BRANCH: automatic port detection
|
---|
| 71 | if ( port_detection )
|
---|
| 72 | {
|
---|
| 73 | // give up ?
|
---|
| 74 | if ( try_port > 41422 )
|
---|
| 75 | {
|
---|
| 76 | logging_warn("[WARN] Unable to find free port. Giving up. :-( Last try was: "
|
---|
| 77 | << endp->to_string() << ". Endpoint will be ignored!");
|
---|
| 78 |
|
---|
| 79 | port_detection = false;
|
---|
| 80 | }
|
---|
| 81 | else
|
---|
| 82 | {
|
---|
| 83 | // try next port
|
---|
| 84 | try_port++;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | // BRANCH: explicit given port --> error
|
---|
| 88 | else
|
---|
| 89 | {
|
---|
| 90 | logging_warn("[WARN] Address already in use: "
|
---|
| 91 | << endp->to_string() << ". Endpoint will be ignored!");
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | // Rethrow
|
---|
| 96 | else
|
---|
| 97 | {
|
---|
| 98 | throw;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | } while ( port_detection );
|
---|
[10653] | 102 | }
|
---|
| 103 |
|
---|
[12060] | 104 | // TODO Bluetooth Endpoints
|
---|
[7041] | 105 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[12060] | 106 | // foreach(rfcomm_channel_address channel, local.rfcomm) {
|
---|
| 107 | // if (local.bluetooth.size() > 0) {
|
---|
| 108 | // foreach(mac_address mac, local.bluetooth) {
|
---|
| 109 | // rfcomm::endpoint endp(mac.bluetooth(), channel.value());
|
---|
| 110 | // create_service(endp);
|
---|
| 111 | // }
|
---|
| 112 | // } else {
|
---|
| 113 | // rfcomm::endpoint endp(channel.value());
|
---|
| 114 | // create_service(endp);
|
---|
| 115 | // }
|
---|
| 116 | // }
|
---|
[7041] | 117 | #endif
|
---|
[12060] | 118 |
|
---|
| 119 | return local;
|
---|
[5284] | 120 | }
|
---|
| 121 |
|
---|
[12060] | 122 | //void transport_peer::create_service(tcp::endpoint endp) {
|
---|
| 123 | // try {
|
---|
| 124 | // TransportProtocolPtr tmp_ptr(new StreamTransport<tcp>(endp));
|
---|
| 125 | // tcps.push_back(tmp_ptr);
|
---|
| 126 | // logging_info("Listening on IP/TCP " << endp);
|
---|
| 127 | //
|
---|
| 128 | // } catch (boost::system::system_error& e) {
|
---|
| 129 | // if (e.code() == boost::asio::error::address_in_use) {
|
---|
| 130 | // logging_warn("[WARN] Address already in use: "
|
---|
| 131 | // << endp << ". Endpoint will be ignored!");
|
---|
| 132 | // } else {
|
---|
| 133 | // // Rethrow
|
---|
| 134 | // throw;
|
---|
| 135 | // }
|
---|
| 136 | // }
|
---|
| 137 | //}
|
---|
[10653] | 138 |
|
---|
[7041] | 139 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[12060] | 140 | //void transport_peer::create_service(rfcomm::endpoint endp) {
|
---|
| 141 | // try {
|
---|
| 142 | // TransportProtocolPtr tmp_ptr(new StreamTransport<rfcomm>(endp));
|
---|
| 143 | // rfcomms.push_back(tmp_ptr);
|
---|
| 144 | // logging_info("Listening on bluetooth/RFCOMM " << endp);
|
---|
| 145 | //
|
---|
| 146 | // } catch (boost::system::system_error& e) {
|
---|
| 147 | // if (e.code() == boost::asio::error::address_in_use) {
|
---|
| 148 | // logging_warn("[WARN] Address already in use: "
|
---|
| 149 | // << endp << ". Endpoint will be ignored!");
|
---|
| 150 | // } else {
|
---|
| 151 | // // Rethrow
|
---|
| 152 | // throw;
|
---|
| 153 | // }
|
---|
| 154 | // }
|
---|
| 155 | //}
|
---|
[7041] | 156 | #endif
|
---|
[10653] | 157 |
|
---|
| 158 | transport_peer::~transport_peer() {
|
---|
[5284] | 159 | }
|
---|
| 160 |
|
---|
[12060] | 161 | void transport_peer::start()
|
---|
| 162 | {
|
---|
| 163 | BOOST_FOREACH(TransportProtocolPtr stream, transport_streams)
|
---|
| 164 | {
|
---|
| 165 | stream->start();
|
---|
[10653] | 166 | }
|
---|
[5284] | 167 | }
|
---|
| 168 |
|
---|
[12060] | 169 | void transport_peer::stop()
|
---|
| 170 | {
|
---|
| 171 | BOOST_FOREACH(TransportProtocolPtr stream, transport_streams)
|
---|
| 172 | {
|
---|
| 173 | stream->stop();
|
---|
[10653] | 174 | }
|
---|
[5284] | 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
[10653] | 178 | void transport_peer::send(
|
---|
[12060] | 179 | const const_EndpointSetPtr endpoints,
|
---|
[10653] | 180 | reboost::message_t message,
|
---|
| 181 | uint8_t priority)
|
---|
| 182 | {
|
---|
[12060] | 183 | BOOST_FOREACH(TransportProtocolPtr stream, transport_streams)
|
---|
| 184 | {
|
---|
| 185 | stream->send(endpoints, message, priority);
|
---|
[10653] | 186 | }
|
---|
[5284] | 187 | }
|
---|
| 188 |
|
---|
[12060] | 189 | // XXX DEPRECATED
|
---|
| 190 | //void transport_peer::terminate( const address_v* remote ) {
|
---|
| 191 | // if (remote->instanceof<tcpip_endpoint>())// TODO direkt auf der richtigen verbindung
|
---|
| 192 | // {
|
---|
| 193 | // foreach(TransportProtocolPtr tcp, tcps) {
|
---|
| 194 | // tcp->terminate(remote);
|
---|
| 195 | // }
|
---|
| 196 | // }
|
---|
| 197 | //#ifdef HAVE_LIBBLUETOOTH
|
---|
| 198 | // if (remote->instanceof<rfcomm_endpoint>()) {
|
---|
| 199 | // foreach(TransportProtocolPtr x, rfcomms) {
|
---|
| 200 | // x->terminate(remote);
|
---|
| 201 | // }
|
---|
| 202 | // }
|
---|
| 203 | //#endif
|
---|
| 204 | //}
|
---|
[5284] | 205 |
|
---|
[12060] | 206 | void transport_peer::register_listener( transport_listener* listener )
|
---|
| 207 | {
|
---|
| 208 | BOOST_FOREACH(TransportProtocolPtr stream, transport_streams)
|
---|
| 209 | {
|
---|
| 210 | stream->register_listener(listener);
|
---|
[10653] | 211 | }
|
---|
[5284] | 212 | }
|
---|
| 213 |
|
---|
| 214 | }} // namespace ariba::transport
|
---|