[5284] | 1 |
|
---|
[7464] | 2 | #include "ariba/config.h"
|
---|
[5284] | 3 | #include "transport_peer.hpp"
|
---|
| 4 | #include "transport.hpp"
|
---|
[10653] | 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>
|
---|
[5284] | 9 |
|
---|
[10653] | 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 |
|
---|
[5284] | 16 | // namespace ariba::transport
|
---|
| 17 | namespace ariba {
|
---|
| 18 | namespace transport {
|
---|
| 19 |
|
---|
| 20 | using namespace ariba::addressing;
|
---|
[10653] | 21 | using boost::asio::ip::tcp;
|
---|
[5284] | 22 |
|
---|
[10653] | 23 | #ifdef HAVE_LIBBLUETOOTH
|
---|
| 24 | using boost::asio::bluetooth::rfcomm;
|
---|
| 25 | #endif
|
---|
| 26 |
|
---|
| 27 | use_logging_cpp(transport_peer);
|
---|
| 28 |
|
---|
[5284] | 29 | transport_peer::transport_peer( endpoint_set& local_set ) : local(local_set) {
|
---|
[10653] | 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 |
|
---|
[7041] | 50 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[10653] | 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 | }
|
---|
[7041] | 62 | #endif
|
---|
[5284] | 63 | }
|
---|
| 64 |
|
---|
[10653] | 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 |
|
---|
[7041] | 82 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[10653] | 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 | }
|
---|
[7041] | 99 | #endif
|
---|
[10653] | 100 |
|
---|
| 101 | transport_peer::~transport_peer() {
|
---|
[5284] | 102 | }
|
---|
| 103 |
|
---|
| 104 | void transport_peer::start() {
|
---|
[10653] | 105 | foreach(TcpIpPtr tcp, tcps) {
|
---|
| 106 | tcp->start();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[7041] | 109 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[10653] | 110 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
| 111 | x->start();
|
---|
| 112 | }
|
---|
[7041] | 113 | #endif
|
---|
[5284] | 114 | }
|
---|
| 115 |
|
---|
| 116 | void transport_peer::stop() {
|
---|
[10653] | 117 | foreach(TcpIpPtr tcp, tcps) {
|
---|
| 118 | tcp->stop();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[7041] | 121 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[10653] | 122 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
| 123 | x->stop();
|
---|
| 124 | }
|
---|
[7041] | 125 | #endif
|
---|
[5284] | 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
[10653] | 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 |
|
---|
[7041] | 138 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[10653] | 139 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
| 140 | x->send(endpoints, message, priority);
|
---|
| 141 | }
|
---|
[7041] | 142 | #endif
|
---|
[5284] | 143 | }
|
---|
| 144 |
|
---|
[5614] | 145 | void transport_peer::terminate( const address_v* remote ) {
|
---|
[10653] | 146 | if (remote->instanceof<tcpip_endpoint>())// TODO direkt auf der richtigen verbindung
|
---|
| 147 | {
|
---|
| 148 | foreach(TcpIpPtr tcp, tcps) {
|
---|
| 149 | tcp->terminate(remote);
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
[7041] | 152 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[10653] | 153 | if (remote->instanceof<rfcomm_endpoint>()) {
|
---|
| 154 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
| 155 | x->terminate(remote);
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
[7041] | 158 | #endif
|
---|
[5284] | 159 | }
|
---|
| 160 |
|
---|
| 161 | void transport_peer::register_listener( transport_listener* listener ) {
|
---|
[10653] | 162 | foreach(TcpIpPtr tcp, tcps) {
|
---|
| 163 | tcp->register_listener(listener);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[7041] | 166 | #ifdef HAVE_LIBBLUETOOTH
|
---|
[10653] | 167 | foreach(rfcomm_transport::sptr x, rfcomms) {
|
---|
| 168 | x->register_listener(listener);
|
---|
| 169 | }
|
---|
[7041] | 170 | #endif
|
---|
[5284] | 171 | }
|
---|
| 172 |
|
---|
| 173 | }} // namespace ariba::transport
|
---|