|
TOBI Interface A
0.1
|
00001 /* 00002 This file is part of the TOBI Interface A (TiA) library. 00003 00004 Commercial Usage 00005 Licensees holding valid Graz University of Technology Commercial 00006 licenses may use this file in accordance with the Graz University 00007 of Technology Commercial License Agreement provided with the 00008 Software or, alternatively, in accordance with the terms contained in 00009 a written agreement between you and Graz University of Technology. 00010 00011 -------------------------------------------------- 00012 00013 GNU Lesser General Public License Usage 00014 Alternatively, this file may be used under the terms of the GNU Lesser 00015 General Public License version 3.0 as published by the Free Software 00016 Foundation and appearing in the file lgpl.txt included in the 00017 packaging of this file. Please review the following information to 00018 ensure the GNU General Public License version 3.0 requirements will be 00019 met: http://www.gnu.org/copyleft/lgpl.html. 00020 00021 In case of GNU Lesser General Public License Usage ,the TiA library 00022 is distributed in the hope that it will be useful, 00023 but WITHOUT ANY WARRANTY; without even the implied warranty of 00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00025 GNU General Public License for more details. 00026 00027 You should have received a copy of the GNU Lesser General Public License 00028 along with the TiA library. If not, see <http://www.gnu.org/licenses/>. 00029 00030 Copyright 2010 Graz University of Technology 00031 Contact: TiA@tobi-project.org 00032 */ 00033 00034 #include "tia-private/newtia/server_impl/tia_server_state_server_impl.h" 00035 #include "tia-private/newtia/messages_impl/tia_control_message_builder_1_0.h" 00036 00037 #include <iostream> 00038 00039 namespace tia 00040 { 00041 00042 //----------------------------------------------------------------------------- 00043 std::map<ServerState, std::string> TiAServerStateServerImpl::state_map_; 00044 00045 //----------------------------------------------------------------------------- 00046 void TiAServerStateServerImpl::init () 00047 { 00048 state_map_[SERVER_STATE_RUNNING] = TiAControlMessageTags10::SERVER_STATE_RUNNING; 00049 state_map_[SERVER_STATE_SHUTDOWN] = TiAControlMessageTags10::SERVER_STATE_SHUTDOWN; 00050 } 00051 00052 00053 //----------------------------------------------------------------------------- 00054 TiAServerStateServerImpl::TiAServerStateServerImpl( 00055 boost::shared_ptr<TCPServerSocket> server_socket, unsigned port) 00056 : server_socket_ (server_socket), 00057 message_builder_ (new TiAControlMessageBuilder10), 00058 port_ (port), 00059 current_state_ (SERVER_STATE_RUNNING) 00060 { 00061 init (); 00062 server_socket_->startListening (port_, this); 00063 } 00064 00065 //----------------------------------------------------------------------------- 00066 TiAServerStateServerImpl::TiAServerStateServerImpl (boost::shared_ptr<TCPServerSocket> server_socket) 00067 : server_socket_ (server_socket), 00068 message_builder_ (new TiAControlMessageBuilder10), 00069 port_ (0), 00070 current_state_ (SERVER_STATE_RUNNING) 00071 { 00072 init (); 00073 port_ = server_socket_->startListening (this); 00074 } 00075 00076 //----------------------------------------------------------------------------- 00077 TiAServerStateServerImpl::~TiAServerStateServerImpl () 00078 { 00079 server_socket_->stopListening (); 00080 } 00081 00082 //----------------------------------------------------------------------------- 00083 unsigned TiAServerStateServerImpl::getPort () const 00084 { 00085 return port_; 00086 } 00087 00088 //----------------------------------------------------------------------------- 00089 void TiAServerStateServerImpl::emitState (ServerState server_state) 00090 { 00091 current_state_ = server_state; 00092 00093 std::list<boost::shared_ptr<Socket> >::iterator client_iter = server_to_client_sockets_.begin(); 00094 while (client_iter != server_to_client_sockets_.end ()) 00095 { 00096 try 00097 { 00098 emitStateViaSocket (*client_iter, current_state_); 00099 ++client_iter; 00100 } 00101 catch (TiALostConnection& /*exc*/) 00102 { 00103 client_iter = server_to_client_sockets_.erase (client_iter); 00104 } 00105 } 00106 } 00107 00108 //----------------------------------------------------------------------------- 00109 void TiAServerStateServerImpl::newConnection (boost::shared_ptr<Socket> socket) 00110 { 00111 try 00112 { 00113 // std::cout << __FUNCTION__ << " new client connected to server state server" << std::endl; 00114 emitStateViaSocket (socket, current_state_); 00115 server_to_client_sockets_.push_back (socket); 00116 } 00117 catch (TiALostConnection& /*exc*/) 00118 { 00119 // do nothing 00120 } 00121 } 00122 00123 //----------------------------------------------------------------------------- 00124 void TiAServerStateServerImpl::emitStateViaSocket (boost::shared_ptr<Socket> socket, ServerState server_state) 00125 { 00126 socket->sendString (message_builder_->buildTiAMessage (TiAControlMessage ("1.0", state_map_[server_state], "", ""))); 00127 } 00128 00129 00130 }