|
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/messages_impl/tia_control_message_parser_1_0.h" 00035 #include "tia-private/newtia/messages/tia_control_message_tags_1_0.h" 00036 #include "tia-private/newtia/tia_exceptions.h" 00037 00038 #include <sstream> 00039 #include <boost/algorithm/string.hpp> 00040 00041 using std::string; 00042 using std::pair; 00043 00044 namespace tia 00045 { 00046 00047 unsigned const TiAControlMessageParser10::MAX_LINE_LENGTH_ = 1000; 00048 00049 //----------------------------------------------------------------------------- 00050 TiAControlMessage TiAControlMessageParser10::parseMessage (InputStream& stream) const 00051 { 00052 string version = readVersion (stream); 00053 pair<string, string> command_and_parameter = readCommandAndParameter (stream); 00054 string content = readContent (stream); 00055 return TiAControlMessage (version, command_and_parameter.first, 00056 command_and_parameter.second, content); 00057 } 00058 00059 //----------------------------------------------------------------------------- 00060 string TiAControlMessageParser10::readVersion (InputStream& stream) const 00061 { 00062 string id_and_version = boost::algorithm::trim_copy(stream.readUntil(TiAControlMessageTags10::NEW_LINE_CHAR) ); 00063 00064 if (id_and_version != TiAControlMessageTags10::ID_AND_VERSION) 00065 throw TiAException (string ("TiAControlMessageParser10::readVersion Wrong Message ID and Version. Required \"") + TiAControlMessageTags10::ID_AND_VERSION + "\" but was \"" + id_and_version + "\"."); 00066 00067 return TiAControlMessageTags10::VERSION; 00068 } 00069 00070 //----------------------------------------------------------------------------- 00071 pair<string, string> TiAControlMessageParser10::readCommandAndParameter (InputStream& stream) const 00072 { 00073 string line = boost::algorithm::trim_copy(stream.readUntil(TiAControlMessageTags10::NEW_LINE_CHAR)); 00074 return getPair (line); 00075 } 00076 00077 //----------------------------------------------------------------------------- 00078 string TiAControlMessageParser10::readContent (InputStream& stream) const 00079 { 00080 string line = boost::algorithm::trim_copy(stream.readUntil(TiAControlMessageTags10::NEW_LINE_CHAR) ); 00081 if (line.size () == 0) 00082 return ""; 00083 00084 pair<string, string> content_length_pair = getPair (line); 00085 if (content_length_pair.first != TiAControlMessageTags10::CONTENT_LENGTH) 00086 throw TiAException (string ("TiAControlMessageParser10::readContent: Invalid field. Expected \"") + TiAControlMessageTags10::CONTENT_LENGTH + "\" but was \"" + content_length_pair.first + "\""); 00087 00088 std::istringstream isstr (content_length_pair.second); 00089 size_t length = 0; 00090 isstr >> length; 00091 if (isstr.fail ()) 00092 throw TiAException (string ("TiAControlMessageParser10::readContent: Error while converting \"") + content_length_pair.second + "\" into a number."); 00093 00094 string empty_line = stream.readUntil(TiAControlMessageTags10::NEW_LINE_CHAR); 00095 if (empty_line.size ()) 00096 throw TiAException (string ("TiAControlMessageParser10::readContent: Expecting an empty line before content starts.")); 00097 00098 string content = stream.readString (length); 00099 if (content.size () != length) 00100 throw TiAException (string ("TiAControlMessageParser10::readContent: Missing bytes!")); 00101 00102 return content; 00103 } 00104 00105 00106 //----------------------------------------------------------------------------- 00107 pair<string, string> TiAControlMessageParser10::getPair (std::string const& str) const 00108 { 00109 size_t delimiter_index = 0; 00110 while ((str[delimiter_index] != TiAControlMessageTags10::COMMAND_DELIMITER) && 00111 delimiter_index < str.size ()) 00112 delimiter_index++; 00113 00114 string tag = str.substr (0, delimiter_index); 00115 string value; 00116 if (delimiter_index < str.size () - 1) 00117 value = boost::algorithm::trim_copy(str.substr (delimiter_index + 1, str.size () + 1 - delimiter_index)); 00118 00119 return pair<string, string> (tag, value); 00120 } 00121 00122 00123 }