source: source/ariba/utility/transport/transport_peer.cpp@ 10752

Last change on this file since 10752 was 10700, checked in by Michael Tänzer, 12 years ago

Merge CMake branch into trunk

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