Ignore:
Timestamp:
Jul 25, 2012, 11:41:36 AM (12 years ago)
Author:
Michael Tänzer
Message:

Merge the ASIO branch back into trunk

Location:
source/ariba/utility/transport
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • source/ariba/utility/transport

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • source/ariba/utility/transport/transport_peer.cpp

    r7834 r10653  
    33#include "transport_peer.hpp"
    44#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
    515
    616// namespace ariba::transport
     
    919
    1020using namespace ariba::addressing;
     21using boost::asio::ip::tcp;
     22
     23#ifdef HAVE_LIBBLUETOOTH
     24using boost::asio::bluetooth::rfcomm;
     25#endif
     26
     27use_logging_cpp(transport_peer);
    1128
    1229transport_peer::transport_peer( endpoint_set& local_set ) : local(local_set) {
    13         // setup tcp transports
    14         tcp = NULL;
    15         //cout << "#tcpip_transports = " << local.tcp.size() << endl;
    16         if (local.tcp.size()==1) {
    17                 tcp = new tcpip(local.tcp.begin()->value());
    18                 //cout << "Started tcpip_transport on port "  << local.tcp.begin()->value() << endl;
    19         }
    20 
     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   
    2150        #ifdef HAVE_LIBBLUETOOTH
    22         // setup rfcomm transports
    23         rfc = NULL;
    24         //cout << "#rfcomm_transports = " << local.rfcomm.size() << endl;
    25         if ( local.rfcomm.size() == 1 ) {
    26                 rfc = new rfcomm( local.rfcomm.begin()->value() );
    27                 //cout << "Started rfcomm_transport on port "  << local.rfcomm.begin()->value() << endl;
    28         }
     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    }
    2962        #endif
    3063}
    3164
     65void 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
     83void 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
    32101transport_peer::~transport_peer() {
    33         if (tcp !=NULL ) delete tcp;
    34 #ifdef HAVE_LIBBLUETOOTH
    35         if (rfc !=NULL ) delete rfc;
    36 #endif
    37102}
    38103
    39104void transport_peer::start() {
    40         if (tcp!=NULL) tcp->start();
     105    foreach(TcpIpPtr tcp, tcps) {
     106        tcp->start();
     107    }
     108   
    41109#ifdef HAVE_LIBBLUETOOTH
    42         if (rfc!=NULL) rfc->start();
     110    foreach(rfcomm_transport::sptr x, rfcomms) {
     111        x->start();
     112    }
    43113#endif
    44114}
    45115
    46116void transport_peer::stop() {
    47         if (tcp!=NULL) tcp->stop();
     117    foreach(TcpIpPtr tcp, tcps) {
     118        tcp->stop();
     119    }
     120   
    48121#ifdef HAVE_LIBBLUETOOTH
    49         if (rfc!=NULL) rfc->stop();
     122        foreach(rfcomm_transport::sptr x, rfcomms) {
     123                x->stop();
     124        }
    50125#endif
    51126}
    52127
    53 void transport_peer::send( const address_v* remote, const uint8_t* data, size_t size ) {
    54         if (remote->instanceof<tcpip_endpoint>() && tcp!=NULL) {
    55                 tcp->send(remote,data,size);
    56         } else
     128
     129void 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   
    57138#ifdef HAVE_LIBBLUETOOTH
    58         if (remote->instanceof<rfcomm_endpoint>() && rfc!=NULL) {
    59                 rfc->send(remote,data,size);
    60         } else
    61 #endif
    62                 cerr << "Could not send message to " << remote->to_string() << endl;
    63 }
    64 
    65 void transport_peer::send( const endpoint_set& endpoints, const uint8_t* data, size_t size ) {
    66         if (tcp!=NULL) tcp->send(endpoints,data,size);
    67 #ifdef HAVE_LIBBLUETOOTH
    68         if (rfc!=NULL) rfc->send(endpoints,data,size);
     139    foreach(rfcomm_transport::sptr x, rfcomms) {
     140                x->send(endpoints, message, priority);
     141        }
    69142#endif
    70143}
    71144
    72145void transport_peer::terminate( const address_v* remote ) {
    73         if (remote->instanceof<tcpip_endpoint>() && tcp!=NULL)
    74                 tcp->terminate(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        }
    75152#ifdef HAVE_LIBBLUETOOTH
    76         if (remote->instanceof<rfcomm_endpoint>() && rfc!=NULL)
    77                 rfc->terminate(remote);
     153        if (remote->instanceof<rfcomm_endpoint>()) {
     154                foreach(rfcomm_transport::sptr x, rfcomms) {
     155                        x->terminate(remote);
     156                }
     157        }
    78158#endif
    79159}
    80160
    81161void transport_peer::register_listener( transport_listener* listener ) {
    82         if (tcp!=NULL) tcp->register_listener(listener);
     162    foreach(TcpIpPtr tcp, tcps) {
     163        tcp->register_listener(listener);
     164    }
     165   
    83166#ifdef HAVE_LIBBLUETOOTH
    84         if (rfc!=NULL) rfc->register_listener(listener);
     167    foreach(rfcomm_transport::sptr x, rfcomms) {
     168        x->register_listener(listener);
     169    }
    85170#endif
    86171}
Note: See TracChangeset for help on using the changeset viewer.