19368eea42a1afb01dd44110582f997115b50e742François Gaffie/* 2b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * Copyright (c) 2011-2014, Intel Corporation 3b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * All rights reserved. 4b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 5b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * Redistribution and use in source and binary forms, with or without modification, 6b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * are permitted provided that the following conditions are met: 7b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 8b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 1. Redistributions of source code must retain the above copyright notice, this 9b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * list of conditions and the following disclaimer. 10b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 11b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 2. Redistributions in binary form must reproduce the above copyright notice, 12b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * this list of conditions and the following disclaimer in the documentation and/or 13b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * other materials provided with the distribution. 14b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 15b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 3. Neither the name of the copyright holder nor the names of its contributors 16b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * may be used to endorse or promote products derived from this software without 17b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * specific prior written permission. 18b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 19b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2968a912857707864bbaaff9808717813105072a6ePatrick Benavoli */ 3068a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "RequestMessage.h" 3168a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include <assert.h> 3268a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include <algorithm> 3368a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include <ctype.h> 3468a912857707864bbaaff9808717813105072a6ePatrick Benavoli 3568a912857707864bbaaff9808717813105072a6ePatrick Benavoli#define base CMessage 3668a912857707864bbaaff9808717813105072a6ePatrick Benavoli 37d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalveusing std::string; 38d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalve 399368eea42a1afb01dd44110582f997115b50e742François Gaffieconst char *const CRequestMessage::gacDelimiters = " \t\n\v\f\r"; 40cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron 419368eea42a1afb01dd44110582f997115b50e742François GaffieCRequestMessage::CRequestMessage(const string &strCommand) 429368eea42a1afb01dd44110582f997115b50e742François Gaffie : base(MsgType::ECommandRequest), _strCommand(strCommand) 4368a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 4468a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 4568a912857707864bbaaff9808717813105072a6ePatrick Benavoli 4668a912857707864bbaaff9808717813105072a6ePatrick BenavoliCRequestMessage::CRequestMessage() 4768a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 4868a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 4968a912857707864bbaaff9808717813105072a6ePatrick Benavoli 5068a912857707864bbaaff9808717813105072a6ePatrick Benavoli// Command Name 519368eea42a1afb01dd44110582f997115b50e742François Gaffievoid CRequestMessage::setCommand(const string &strCommand) 5268a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 5368a912857707864bbaaff9808717813105072a6ePatrick Benavoli _strCommand = trim(strCommand); 5468a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 5568a912857707864bbaaff9808717813105072a6ePatrick Benavoli 569368eea42a1afb01dd44110582f997115b50e742François Gaffieconst string &CRequestMessage::getCommand() const 5768a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 5868a912857707864bbaaff9808717813105072a6ePatrick Benavoli return _strCommand; 5968a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 6068a912857707864bbaaff9808717813105072a6ePatrick Benavoli 6168a912857707864bbaaff9808717813105072a6ePatrick Benavoli// Arguments 629368eea42a1afb01dd44110582f997115b50e742François Gaffievoid CRequestMessage::addArgument(const string &strArgument) 6368a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 6468a912857707864bbaaff9808717813105072a6ePatrick Benavoli _argumentVector.push_back(trim(strArgument)); 6568a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 6668a912857707864bbaaff9808717813105072a6ePatrick Benavoli 679368eea42a1afb01dd44110582f997115b50e742François Gaffiesize_t CRequestMessage::getArgumentCount() const 6868a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 6968a912857707864bbaaff9808717813105072a6ePatrick Benavoli return _argumentVector.size(); 7068a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 7168a912857707864bbaaff9808717813105072a6ePatrick Benavoli 729368eea42a1afb01dd44110582f997115b50e742François Gaffieconst std::vector<string> &CRequestMessage::getArguments() const 7368a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 749368eea42a1afb01dd44110582f997115b50e742François Gaffie return _argumentVector; 759368eea42a1afb01dd44110582f997115b50e742François Gaffie} 769368eea42a1afb01dd44110582f997115b50e742François Gaffie 779368eea42a1afb01dd44110582f997115b50e742François Gaffieconst string &CRequestMessage::getArgument(size_t argument) const 789368eea42a1afb01dd44110582f997115b50e742François Gaffie{ 799368eea42a1afb01dd44110582f997115b50e742François Gaffie assert(argument < _argumentVector.size()); 8068a912857707864bbaaff9808717813105072a6ePatrick Benavoli 819368eea42a1afb01dd44110582f997115b50e742François Gaffie return _argumentVector[argument]; 8268a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 8368a912857707864bbaaff9808717813105072a6ePatrick Benavoli 849368eea42a1afb01dd44110582f997115b50e742François Gaffieconst string CRequestMessage::packArguments(size_t uiStartArgument, size_t uiNbArguments) const 856ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli{ 866ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli string strPackedArguments; 876ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 886ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli assert(uiStartArgument + uiNbArguments <= _argumentVector.size()); 896ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 906ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli // Pack arguments, separating them with a space 916ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli bool bFirst = true; 926ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 939368eea42a1afb01dd44110582f997115b50e742François Gaffie for (size_t argument = uiStartArgument; argument < uiStartArgument + uiNbArguments; 949368eea42a1afb01dd44110582f997115b50e742François Gaffie argument++) { 956ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 966ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli if (!bFirst) { 976ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 986ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli strPackedArguments += " "; 996ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli } else { 1006ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 1016ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli bFirst = false; 1026ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli } 1036ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 1049368eea42a1afb01dd44110582f997115b50e742François Gaffie strPackedArguments += _argumentVector[argument]; 1056ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli } 1066ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 1076ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli return strPackedArguments; 1086ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli} 1096ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli 11068a912857707864bbaaff9808717813105072a6ePatrick Benavoli// Fill data to send 11168a912857707864bbaaff9808717813105072a6ePatrick Benavolivoid CRequestMessage::fillDataToSend() 11268a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 11368a912857707864bbaaff9808717813105072a6ePatrick Benavoli // Send command 11468a912857707864bbaaff9808717813105072a6ePatrick Benavoli writeString(getCommand()); 11568a912857707864bbaaff9808717813105072a6ePatrick Benavoli 11668a912857707864bbaaff9808717813105072a6ePatrick Benavoli // Arguments 1179368eea42a1afb01dd44110582f997115b50e742François Gaffie for (size_t argument = 0; argument < getArgumentCount(); argument++) { 11868a912857707864bbaaff9808717813105072a6ePatrick Benavoli 1199368eea42a1afb01dd44110582f997115b50e742François Gaffie writeString(getArgument(argument)); 12068a912857707864bbaaff9808717813105072a6ePatrick Benavoli } 12168a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 12268a912857707864bbaaff9808717813105072a6ePatrick Benavoli 12368a912857707864bbaaff9808717813105072a6ePatrick Benavoli// Collect received data 12468a912857707864bbaaff9808717813105072a6ePatrick Benavolivoid CRequestMessage::collectReceivedData() 12568a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 12668a912857707864bbaaff9808717813105072a6ePatrick Benavoli // Receive command 12768a912857707864bbaaff9808717813105072a6ePatrick Benavoli string strCommand; 12868a912857707864bbaaff9808717813105072a6ePatrick Benavoli 12968a912857707864bbaaff9808717813105072a6ePatrick Benavoli readString(strCommand); 13068a912857707864bbaaff9808717813105072a6ePatrick Benavoli 13168a912857707864bbaaff9808717813105072a6ePatrick Benavoli setCommand(strCommand); 13268a912857707864bbaaff9808717813105072a6ePatrick Benavoli 13368a912857707864bbaaff9808717813105072a6ePatrick Benavoli // Arguments 13468a912857707864bbaaff9808717813105072a6ePatrick Benavoli while (getRemainingDataSize()) { 13568a912857707864bbaaff9808717813105072a6ePatrick Benavoli 13668a912857707864bbaaff9808717813105072a6ePatrick Benavoli string strArgument; 13768a912857707864bbaaff9808717813105072a6ePatrick Benavoli 13868a912857707864bbaaff9808717813105072a6ePatrick Benavoli readString(strArgument); 13968a912857707864bbaaff9808717813105072a6ePatrick Benavoli 14068a912857707864bbaaff9808717813105072a6ePatrick Benavoli addArgument(strArgument); 14168a912857707864bbaaff9808717813105072a6ePatrick Benavoli } 14268a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 14368a912857707864bbaaff9808717813105072a6ePatrick Benavoli 14468a912857707864bbaaff9808717813105072a6ePatrick Benavoli// Size 145911844b16c6b627f421bc3368de427de3ca9f60cPatrick Benavolisize_t CRequestMessage::getDataSize() const 14668a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 14768a912857707864bbaaff9808717813105072a6ePatrick Benavoli // Command 148911844b16c6b627f421bc3368de427de3ca9f60cPatrick Benavoli size_t uiSize = getStringSize(getCommand()); 14968a912857707864bbaaff9808717813105072a6ePatrick Benavoli 15068a912857707864bbaaff9808717813105072a6ePatrick Benavoli // Arguments 1519368eea42a1afb01dd44110582f997115b50e742François Gaffie for (size_t uiArgument = 0; uiArgument < getArgumentCount(); uiArgument++) { 15268a912857707864bbaaff9808717813105072a6ePatrick Benavoli 15368a912857707864bbaaff9808717813105072a6ePatrick Benavoli uiSize += getStringSize(getArgument(uiArgument)); 15468a912857707864bbaaff9808717813105072a6ePatrick Benavoli } 15568a912857707864bbaaff9808717813105072a6ePatrick Benavoli return uiSize; 15668a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 15768a912857707864bbaaff9808717813105072a6ePatrick Benavoli 15868a912857707864bbaaff9808717813105072a6ePatrick Benavoli// Trim input string 1599368eea42a1afb01dd44110582f997115b50e742François Gaffiestring CRequestMessage::trim(const string &strToTrim) 16068a912857707864bbaaff9808717813105072a6ePatrick Benavoli{ 16168a912857707864bbaaff9808717813105072a6ePatrick Benavoli // Trim string 16268a912857707864bbaaff9808717813105072a6ePatrick Benavoli string strTrimmed = strToTrim; 16368a912857707864bbaaff9808717813105072a6ePatrick Benavoli 1649368eea42a1afb01dd44110582f997115b50e742François Gaffie strTrimmed.erase(strTrimmed.find_last_not_of(gacDelimiters) + 1); 165cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron 166cec86c1904a45a7027218adb3d34c8764bdfcc38Georges-Henri Baron strTrimmed.erase(0, strTrimmed.find_first_not_of(gacDelimiters)); 16768a912857707864bbaaff9808717813105072a6ePatrick Benavoli 16868a912857707864bbaaff9808717813105072a6ePatrick Benavoli return strTrimmed; 16968a912857707864bbaaff9808717813105072a6ePatrick Benavoli} 170