|
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/client/tia_new_client_impl.h" 00035 #include "tia/defines.h" 00036 00037 #include "tia-private/newtia/tia_meta_info_parse_and_build_functions.h" 00038 #include "tia-private/newtia/messages_impl/tia_control_message_builder_1_0.h" 00039 #include "tia-private/newtia/messages_impl/tia_control_message_parser_1_0.h" 00040 #include "tia-private/newtia/messages/standard_control_messages.h" 00041 #include "tia-private/newtia/messages/tia_control_message_tags_1_0.h" 00042 #include "tia-private/newtia/tia_meta_info_parse_and_build_functions.h" 00043 #include "tia-private/newtia/string_utils.h" 00044 #include "tia-private/newtia/network_impl/boost_tcp_socket_impl.h" 00045 #include "tia-private/newtia/network_impl/boost_udp_read_socket.h" 00046 00047 #include "tia-private/newtia/tia_datapacket_parser.h" 00048 #include "tia-private/datapacket/data_packet_3_impl.h" 00049 00050 using namespace std; 00051 00052 namespace tia 00053 { 00054 00055 unsigned const MAX_LINE_LENGTH = 50; 00056 00057 //----------------------------------------------------------------------------- 00058 TiANewClientImpl::TiANewClientImpl () 00059 : MESSAGE_VERSION_ (TiAControlMessageTags10::VERSION), 00060 receiving_ (false), 00061 buffer_size_ (BUFFER_SIZE), 00062 message_builder_ (new TiAControlMessageBuilder10), 00063 message_parser_ (new TiAControlMessageParser10), 00064 data_packet_parser(0) 00065 { 00066 packet_.reset( new DataPacket3Impl); 00067 } 00068 00069 //----------------------------------------------------------------------------- 00070 00071 TiANewClientImpl::~TiANewClientImpl() 00072 { 00073 00074 } 00075 00076 //----------------------------------------------------------------------------- 00077 00078 void TiANewClientImpl::connect (const std::string& address, short unsigned port) 00079 { 00080 server_ip_address_ = address; 00081 boost::asio::ip::tcp::endpoint server_address (boost::asio::ip::address_v4::from_string (address), port); 00082 socket_.reset (new BoostTCPSocketImpl (io_service_, server_address, buffer_size_)); 00083 sendMessage (CheckProtocolVersionTiAControlMessage (MESSAGE_VERSION_)); 00084 waitForOKResponse (); 00085 } 00086 00087 //----------------------------------------------------------------------------- 00088 bool TiANewClientImpl::connected () const 00089 { 00090 return socket_.get (); 00091 } 00092 00093 //----------------------------------------------------------------------------- 00094 void TiANewClientImpl::disconnect () 00095 { 00096 if (receiving_) 00097 stopReceiving (); 00098 if (data_socket_.get ()) 00099 data_socket_.reset (0); 00100 socket_.reset (0); 00101 data_packet_parser.reset(0); 00102 } 00103 00104 //----------------------------------------------------------------------------- 00105 void TiANewClientImpl::requestConfig () 00106 { 00107 sendMessage (GetMetaInfoTiAControlMessage (MESSAGE_VERSION_)); 00108 TiAControlMessage metainfo_message = waitForControlMessage (TiAControlMessageTags10::METAINFO); 00109 config_ = parseTiAMetaInfoFromXMLString (metainfo_message.getContent ()); 00110 } 00111 00112 //----------------------------------------------------------------------------- 00113 SSConfig TiANewClientImpl::config () const 00114 { 00115 return config_; 00116 } 00117 00118 //----------------------------------------------------------------------------- 00119 void TiANewClientImpl::startReceiving (bool use_udp_bc) 00120 { 00121 if (receiving_) 00122 return; 00123 00124 if (!data_socket_.get ()) 00125 { 00126 sendMessage (GetDataConnectionTiAControlMessage (MESSAGE_VERSION_, use_udp_bc)); 00127 TiAControlMessage dataconnection_message = waitForControlMessage (TiAControlMessageTags10::DATA_CONNECTION_PORT); 00128 unsigned port = toUnsigned (dataconnection_message.getParameters ()); 00129 if (use_udp_bc) 00130 { 00131 boost::asio::ip::udp::endpoint server_data_address (boost::asio::ip::address_v4::from_string (server_ip_address_), port); 00132 data_socket_.reset (new BoostUDPReadSocket (io_service_, server_data_address, buffer_size_)); 00133 } 00134 else 00135 { 00136 boost::asio::ip::tcp::endpoint server_data_address (boost::asio::ip::address_v4::from_string (server_ip_address_), port); 00137 data_socket_.reset (new BoostTCPSocketImpl (io_service_, server_data_address, buffer_size_)); 00138 } 00139 } 00140 00141 data_packet_parser.reset(new TiADataPacketParser(*data_socket_) ); 00142 00143 sendMessage (TiAControlMessage (MESSAGE_VERSION_, TiAControlMessageTags10::START_DATA_TRANSMISSION, "", "")); 00144 waitForOKResponse (); 00145 00146 receiving_ = true; 00147 } 00148 00149 //----------------------------------------------------------------------------- 00150 bool TiANewClientImpl::receiving() const 00151 { 00152 return receiving_; 00153 } 00154 00155 //----------------------------------------------------------------------------- 00156 void TiANewClientImpl::stopReceiving() 00157 { 00158 if (receiving_) 00159 { 00160 sendMessage (TiAControlMessage (MESSAGE_VERSION_, TiAControlMessageTags10::STOP_DATA_TRANSMISSION, "", "")); 00161 waitForOKResponse(); 00162 receiving_ = false; 00163 } 00164 } 00165 00166 00167 //----------------------------------------------------------------------------- 00168 00169 DataPacket* TiANewClientImpl::getEmptyDataPacket() 00170 { 00171 return(packet_.get()); 00172 } 00173 00174 //----------------------------------------------------------------------------- 00175 00176 void TiANewClientImpl::getDataPacket (DataPacket& packet) 00177 { 00178 if (!receiving_) 00179 return; 00180 00181 if (!data_socket_.get ()) 00182 return; 00183 00184 packet.reset(); 00185 data_packet_parser->parseDataPacket(packet); 00186 00187 if (!receiving_) 00188 data_socket_.reset (0); 00189 } 00190 00191 //----------------------------------------------------------------------------- 00192 void TiANewClientImpl::setBufferSize (size_t size) 00193 { 00194 buffer_size_ = size; 00195 } 00196 00197 //----------------------------------------------------------------------------- 00198 void TiANewClientImpl::sendMessage (TiAControlMessage const& message) 00199 { 00200 if (socket_.get ()) 00201 socket_->sendString (message_builder_->buildTiAMessage (message)); 00202 } 00203 00204 //----------------------------------------------------------------------------- 00205 void TiANewClientImpl::waitForOKResponse () 00206 { 00207 waitForControlMessage (TiAControlMessageTags10::OK); 00208 } 00209 00210 //----------------------------------------------------------------------------- 00211 void TiANewClientImpl::waitForErrorResponse () 00212 { 00213 waitForControlMessage (TiAControlMessageTags10::ERROR_STR); 00214 } 00215 00216 //----------------------------------------------------------------------------- 00217 TiAControlMessage TiANewClientImpl::waitForControlMessage (std::string const& command_name) 00218 { 00219 if (!socket_.get ()) 00220 throw TiAException ("TiANewClientImpl: Connection to server not initializsed."); 00221 00222 TiAControlMessage message = message_parser_->parseMessage (*socket_); 00223 00224 if (message.getVersion () != TiAControlMessageTags10::VERSION) 00225 throw std::runtime_error (string ("wrong server response: awaiting \"") + TiAControlMessageTags10::VERSION + "\" but was \"" + message.getVersion() + "\""); 00226 00227 if (message.getCommand () != command_name) 00228 throw std::runtime_error (string ("wrong server response: awaiting \"") + command_name + "\" but was \"" + message.getCommand () + "\""); 00229 00230 return message; 00231 } 00232 00233 00234 00235 00236 }