|
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 00035 #include "tia-private/newtia/server_impl/control_connection_2.h" 00036 #include "tia-private/newtia/commands/check_protocol_version_control_command.h" 00037 #include "tia-private/newtia/commands/get_data_connection_control_command.h" 00038 #include "tia-private/newtia/commands/start_data_transmission_control_command.h" 00039 #include "tia-private/newtia/commands/stop_data_transmission_control_command.h" 00040 #include "tia-private/newtia/commands/get_metainfo_control_command.h" 00041 #include "tia-private/newtia/commands/get_serverstate_connection_command.h" 00042 00043 #include "tia-private/newtia/messages/tia_control_message_tags_1_0.h" 00044 #include "tia-private/newtia/messages_impl/tia_control_message_parser_1_0.h" 00045 #include "tia-private/newtia/messages_impl/tia_control_message_builder_1_0.h" 00046 #include "tia-private/newtia/messages/standard_control_messages.h" 00047 #include "tia-private/newtia/tia_exceptions.h" 00048 00049 using std::string; 00050 00051 #include <iostream> 00052 00053 namespace tia 00054 { 00055 unsigned ControlConnection2::next_free_id_ = 0; 00056 00057 //----------------------------------------------------------------------------- 00058 ControlConnection2::ControlConnection2 (boost::shared_ptr<Socket> socket, DataServer& data_server, 00059 TiAServerStateServer& server_state_server, 00060 HardwareInterface& hardware_interface) 00061 : running_ (true), socket_ (socket), 00062 data_server_ (data_server), 00063 server_state_server_ (server_state_server), 00064 command_context_(hardware_interface), 00065 parser_ (new TiAControlMessageParser10), 00066 builder_ (new TiAControlMessageBuilder10), 00067 id_ (next_free_id_++) 00068 { 00069 command_map_[TiAControlMessageTags10::CHECK_PROTOCOL_VERSION] = new CheckProtocolVersionControlCommand (); 00070 command_map_[TiAControlMessageTags10::GET_METAINFO] = new GetMetaInfoControlCommand (command_context_); 00071 command_map_[TiAControlMessageTags10::GET_DATA_CONNECTION] = new GetDataConnectionControlCommand (command_context_, data_server_); 00072 command_map_[TiAControlMessageTags10::START_DATA_TRANSMISSION] = new StartDataTransmissionControlCommand (command_context_, data_server_); 00073 command_map_[TiAControlMessageTags10::STOP_DATA_TRANSMISSION] = new StopDataTransmissionControlCommand (command_context_, data_server_); 00074 command_map_[TiAControlMessageTags10::GET_SERVER_STATE_CONNECTION] = new GetServerStateConnectionCommand (server_state_server_.getPort()); 00075 } 00076 00077 //----------------------------------------------------------------------------- 00078 ControlConnection2::~ControlConnection2 () 00079 { 00080 #ifdef DEBUG 00081 std::cout << "ControlConnection2::Destructor" << std::endl; 00082 #endif 00083 00084 for (CommandMap::iterator iter = command_map_.begin(); iter != command_map_.end(); 00085 ++iter) 00086 delete iter->second; 00087 delete thread_; 00088 } 00089 00090 //----------------------------------------------------------------------------- 00091 void ControlConnection2::asyncStart () 00092 { 00093 //#ifdef WIN32 00094 // SetPriorityClass(sig_server_ptr->native_handle(), HIGH_PRIORITY_CLASS); 00095 // SetThreadPriority(sig_server_ptr->native_handle(), THREAD_PRIORITY_HIGHEST ); 00096 //#endif 00097 thread_ = new boost::thread (boost::bind (&ControlConnection2::run, boost::ref(*this))); 00098 } 00099 00100 //----------------------------------------------------------------------------- 00101 void ControlConnection2::stop () 00102 { 00103 running_ = false; 00104 } 00105 00106 //----------------------------------------------------------------------------- 00107 bool ControlConnection2::isRunning() const 00108 { 00109 return running_; 00110 } 00111 00112 //----------------------------------------------------------------------------- 00113 void ControlConnection2::run () 00114 { 00115 running_ = true; 00116 while (running_) 00117 { 00118 try 00119 { 00120 socket_->waitForData (); 00121 00122 //std::cout << "ServerControlConnection::run " << std::endl; 00123 00124 TiAControlMessage message = parser_->parseMessage (*socket_); 00125 message.setRemoteEndpointIP (socket_->getRemoteEndPointAsString()); 00126 00127 //std::cout << "ServerControlConnection::run received message: " << message.getVersion() << "; command = \"" << message.getCommand() << "\""<< std::endl; 00128 00129 CommandMap::iterator command_iter = command_map_.find (message.getCommand ()); 00130 string response; 00131 if (command_iter != command_map_.end ()) 00132 response = builder_->buildTiAMessage (command_iter->second->execute (message)); 00133 else 00134 response = builder_->buildTiAMessage (ErrorControlMessage (message.getVersion())); 00135 00136 //std::cout << "ServerControlConnection::run will send: \"" << response << "\"" << std::endl; 00137 00138 socket_->sendString (response); 00139 } 00140 catch (TiALostConnection& exc) 00141 { 00142 //std::cout << "ServerControlConnection::run lost connection to client: \"" << exc.what() << "\"" << std::endl; 00143 running_ = false; 00144 } 00145 catch (TiAException& exc) 00146 { 00147 std::cout << "ServerControlConnection::run error occured: \"" << exc.what() << "\"" << std::endl; 00148 running_ = false; 00149 } 00150 } 00151 } 00152 00153 }