TOBI Interface A  0.1
/home/breidi/Dropbox/libtia/src/tia/newtia/server_impl/control_connection_server_2_impl.cpp
00001 #include "tia-private/newtia/server_impl/control_connection_server_2_impl.h"
00002 
00003 #include "tia-private/newtia/server_impl/control_connection_2.h"
00004 #include <iostream>
00005 
00006 namespace tia
00007 {
00008 
00009 static const int SECONDS_TO_RE_CHECK_CONNECTIONS = 1;
00010 
00011 //-----------------------------------------------------------------------------
00012 
00013 ControlConnectionServer2Impl::ControlConnectionServer2Impl(
00014     boost::shared_ptr<TCPServerSocket> server_socket, unsigned port,
00015     DataServer* data_server, TiAServerStateServer* server_state_server,
00016     HardwareInterface* hardware_interface)
00017  : server_socket_ (server_socket), server_socket_port_ (port),
00018    data_server_(data_server), server_state_server_(server_state_server),
00019    hardware_interface_(hardware_interface), check_connections_timer_(io_service_)
00020 {
00021   server_socket_->startListening (server_socket_port_, this);
00022 
00023   boost::system::error_code err;
00024   checkConnections (err);
00025   io_thread_  = new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
00026 }
00027 
00028 //-----------------------------------------------------------------------------
00029 
00030 ControlConnectionServer2Impl::~ControlConnectionServer2Impl ()
00031 {
00032   #ifdef DEBUG
00033     std::cout << "ControlConnectionServer2Impl::~ControlConnectionServer2Impl ()" << std::endl;
00034   #endif
00035 
00036   io_service_.stop();
00037   io_thread_->join();
00038   if(io_thread_)
00039     delete io_thread_;
00040 }
00041 
00042 //-----------------------------------------------------------------------------
00043 
00044 void ControlConnectionServer2Impl::newConnection (boost::shared_ptr<Socket> socket)
00045 {
00046   try
00047   {
00048     boost::unique_lock<boost::mutex> lock(mutex_);
00049 
00050     tia::ControlConnection2* control_connection =
00051         new tia::ControlConnection2 (socket, *data_server_, *server_state_server_,
00052                                      *hardware_interface_);
00053     unsigned id = control_connection->getId();
00054     connections_[id] = control_connection;
00055 
00056     std::cout << " Client " << id <<" @" << socket->getRemoteEndPointAsString();
00057     std::cout << " has connected. (local: "  << socket->getLocalEndPointAsString() << ")"<< std::endl;
00058     std::cout << " # Connected clients: " << connections_.size () << std::endl;
00059 
00060     control_connection->asyncStart ();
00061   }
00062   catch (TiALostConnection& /*exc*/)
00063   {
00064     // do nothing
00065   }
00066 }
00067 
00068 //-----------------------------------------------------------------------------
00069 
00070 void ControlConnectionServer2Impl::checkConnections (boost::system::error_code error)
00071 {
00072   if(error)
00073       return;
00074   boost::unique_lock<boost::mutex> lock(mutex_);
00075 
00076   #ifdef DEBUG
00077     static unsigned call_counter = 0;
00078     std::cout << " <- check connection "<< ++call_counter <<" -> " << std::endl;
00079   #endif
00080 
00081   unsigned int nr_removed = 0;
00082 
00083   for (std::map<unsigned, tia::ControlConnection2*>::iterator iter = connections_.begin();
00084        iter != connections_.end();   )
00085   {
00086     if (!(iter->second->isRunning()))
00087     {
00088       std::map<unsigned, tia::ControlConnection2*>::iterator iter_to_del = iter;
00089       std::cout << "  -- Removing connection to: " ;
00090       std::cout << iter->second->getRemoteEndPointAsString() << std::endl;
00091 
00092       nr_removed++;
00093       delete iter->second;
00094       ++iter;
00095       connections_.erase (iter_to_del);
00096     }
00097     else
00098       ++iter;
00099 
00100   }
00101 
00102   if(nr_removed)
00103     std::cout << " # Connected clients: " << connections_.size() << std::endl;
00104 
00105   check_connections_timer_.cancel ();
00106   check_connections_timer_.expires_from_now (boost::posix_time::seconds (SECONDS_TO_RE_CHECK_CONNECTIONS));
00107   check_connections_timer_.async_wait(boost::bind(&ControlConnectionServer2Impl::checkConnections, this, boost::asio::placeholders::error));
00108 }
00109 
00110 //-----------------------------------------------------------------------------
00111 
00112 }
 All Data Structures Files Functions Variables Typedefs Enumerations