1 /* 2 * Copyright (c) 2011-2014, Intel Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation and/or 13 * other materials provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors 16 * may be used to endorse or promote products derived from this software without 17 * specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #include "RequestMessage.h" 31 #include "Utility.h" 32 #include <assert.h> 33 #include <algorithm> 34 #include <ctype.h> 35 36 #define base CMessage 37 38 using std::string; 39 40 const char *const CRequestMessage::gacDelimiters = " \t\n\v\f\r"; 41 42 CRequestMessage::CRequestMessage(const string &strCommand) 43 : base(MsgType::ECommandRequest), _strCommand(strCommand) 44 { 45 } 46 47 CRequestMessage::CRequestMessage() 48 { 49 } 50 51 // Command Name 52 void CRequestMessage::setCommand(const string &strCommand) 53 { 54 _strCommand = trim(strCommand); 55 } 56 57 const string &CRequestMessage::getCommand() const 58 { 59 return _strCommand; 60 } 61 62 // Arguments 63 void CRequestMessage::addArgument(const string &strArgument) 64 { 65 _argumentVector.push_back(trim(strArgument)); 66 } 67 68 size_t CRequestMessage::getArgumentCount() const 69 { 70 return _argumentVector.size(); 71 } 72 73 const std::vector<string> &CRequestMessage::getArguments() const 74 { 75 return _argumentVector; 76 } 77 78 const string &CRequestMessage::getArgument(size_t argument) const 79 { 80 assert(argument < _argumentVector.size()); 81 82 return _argumentVector[argument]; 83 } 84 85 string CRequestMessage::packArguments(size_t uiStartArgument, size_t uiNbArguments) const 86 { 87 assert(uiStartArgument + uiNbArguments <= _argumentVector.size()); 88 89 auto start = begin(_argumentVector) + uiStartArgument; 90 return utility::asString(std::vector<std::string>(start, start + uiNbArguments), " "); 91 } 92 93 // Fill data to send 94 void CRequestMessage::fillDataToSend() 95 { 96 // Send command 97 writeString(getCommand()); 98 99 // Arguments 100 for (size_t argument = 0; argument < getArgumentCount(); argument++) { 101 102 writeString(getArgument(argument)); 103 } 104 } 105 106 // Collect received data 107 void CRequestMessage::collectReceivedData() 108 { 109 // Receive command 110 string strCommand; 111 112 readString(strCommand); 113 114 setCommand(strCommand); 115 116 // Arguments 117 while (getRemainingDataSize()) { 118 119 string strArgument; 120 121 readString(strArgument); 122 123 addArgument(strArgument); 124 } 125 } 126 127 // Size 128 size_t CRequestMessage::getDataSize() const 129 { 130 // Command 131 size_t uiSize = getStringSize(getCommand()); 132 133 // Arguments 134 for (size_t uiArgument = 0; uiArgument < getArgumentCount(); uiArgument++) { 135 136 uiSize += getStringSize(getArgument(uiArgument)); 137 } 138 return uiSize; 139 } 140 141 // Trim input string 142 string CRequestMessage::trim(const string &strToTrim) 143 { 144 // Trim string 145 string strTrimmed = strToTrim; 146 147 strTrimmed.erase(strTrimmed.find_last_not_of(gacDelimiters) + 1); 148 149 strTrimmed.erase(0, strTrimmed.find_first_not_of(gacDelimiters)); 150 151 return strTrimmed; 152 } 153