Kevin Rocard | 93250d1 | 2012-07-19 17:48:30 +0200 | [diff] [blame] | 1 | /* |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 2 | * INTEL CONFIDENTIAL |
| 3 | * Copyright © 2011 Intel |
| 4 | * Corporation All Rights Reserved. |
| 5 | * |
| 6 | * The source code contained or described herein and all documents related to |
| 7 | * the source code ("Material") are owned by Intel Corporation or its suppliers |
| 8 | * or licensors. Title to the Material remains with Intel Corporation or its |
| 9 | * suppliers and licensors. The Material contains trade secrets and proprietary |
| 10 | * and confidential information of Intel or its suppliers and licensors. The |
| 11 | * Material is protected by worldwide copyright and trade secret laws and |
| 12 | * treaty provisions. No part of the Material may be used, copied, reproduced, |
| 13 | * modified, published, uploaded, posted, transmitted, distributed, or |
| 14 | * disclosed in any way without Intel’s prior express written permission. |
| 15 | * |
| 16 | * No license under any patent, copyright, trade secret or other intellectual |
| 17 | * property right is granted to or conferred upon you by disclosure or delivery |
| 18 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 19 | * otherwise. Any license under such intellectual property rights must be |
| 20 | * express and approved by Intel in writing. |
| 21 | * |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 22 | * CREATED: 2011-06-01 |
| 23 | * UPDATED: 2011-07-27 |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 24 | */ |
| 25 | #include "RequestMessage.h" |
| 26 | #include "RemoteProcessorProtocol.h" |
| 27 | #include <assert.h> |
| 28 | #include <algorithm> |
| 29 | #include <ctype.h> |
| 30 | |
| 31 | #define base CMessage |
| 32 | |
| 33 | CRequestMessage::CRequestMessage(const string& strCommand) : base(ECommandRequest), _strCommand(strCommand) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | CRequestMessage::CRequestMessage() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | // Command Name |
| 42 | void CRequestMessage::setCommand(const string& strCommand) |
| 43 | { |
| 44 | _strCommand = trim(strCommand); |
| 45 | } |
| 46 | |
| 47 | const string& CRequestMessage::getCommand() const |
| 48 | { |
| 49 | return _strCommand; |
| 50 | } |
| 51 | |
| 52 | // Arguments |
| 53 | void CRequestMessage::addArgument(const string& strArgument) |
| 54 | { |
| 55 | _argumentVector.push_back(trim(strArgument)); |
| 56 | } |
| 57 | |
| 58 | uint32_t CRequestMessage::getArgumentCount() const |
| 59 | { |
| 60 | return _argumentVector.size(); |
| 61 | } |
| 62 | |
| 63 | const string& CRequestMessage::getArgument(uint32_t uiArgument) const |
| 64 | { |
| 65 | assert(uiArgument < _argumentVector.size()); |
| 66 | |
| 67 | return _argumentVector[uiArgument]; |
| 68 | } |
| 69 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 70 | const string CRequestMessage::packArguments(uint32_t uiStartArgument, uint32_t uiNbArguments) const |
| 71 | { |
| 72 | string strPackedArguments; |
| 73 | |
| 74 | assert(uiStartArgument + uiNbArguments <= _argumentVector.size()); |
| 75 | |
| 76 | // Pack arguments, separating them with a space |
| 77 | uint32_t uiArgument; |
| 78 | |
| 79 | bool bFirst = true; |
| 80 | |
| 81 | for (uiArgument = uiStartArgument; uiArgument < uiStartArgument + uiNbArguments; uiArgument++) { |
| 82 | |
| 83 | if (!bFirst) { |
| 84 | |
| 85 | strPackedArguments += " "; |
| 86 | } else { |
| 87 | |
| 88 | bFirst = false; |
| 89 | } |
| 90 | |
| 91 | strPackedArguments += _argumentVector[uiArgument]; |
| 92 | } |
| 93 | |
| 94 | return strPackedArguments; |
| 95 | } |
| 96 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 97 | // Fill data to send |
| 98 | void CRequestMessage::fillDataToSend() |
| 99 | { |
| 100 | // Send command |
| 101 | writeString(getCommand()); |
| 102 | |
| 103 | // Arguments |
| 104 | uint32_t uiArgument; |
| 105 | |
| 106 | for (uiArgument = 0; uiArgument < getArgumentCount(); uiArgument++) { |
| 107 | |
| 108 | writeString(getArgument(uiArgument)); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Collect received data |
| 113 | void CRequestMessage::collectReceivedData() |
| 114 | { |
| 115 | // Receive command |
| 116 | string strCommand; |
| 117 | |
| 118 | readString(strCommand); |
| 119 | |
| 120 | setCommand(strCommand); |
| 121 | |
| 122 | // Arguments |
| 123 | while (getRemainingDataSize()) { |
| 124 | |
| 125 | string strArgument; |
| 126 | |
| 127 | readString(strArgument); |
| 128 | |
| 129 | addArgument(strArgument); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Size |
| 134 | uint32_t CRequestMessage::getDataSize() const |
| 135 | { |
| 136 | // Command |
| 137 | uint32_t uiSize = getStringSize(getCommand()); |
| 138 | |
| 139 | // Arguments |
| 140 | uint32_t uiArgument; |
| 141 | |
| 142 | for (uiArgument = 0; uiArgument < getArgumentCount(); uiArgument++) { |
| 143 | |
| 144 | uiSize += getStringSize(getArgument(uiArgument)); |
| 145 | } |
| 146 | return uiSize; |
| 147 | } |
| 148 | |
| 149 | // Trim input string |
| 150 | string CRequestMessage::trim(const string& strToTrim) |
| 151 | { |
| 152 | // Trim string |
| 153 | string strTrimmed = strToTrim; |
| 154 | |
| 155 | strTrimmed.erase(remove_if(strTrimmed.begin(), strTrimmed.end(), ::isspace), strTrimmed.end()); |
| 156 | |
| 157 | return strTrimmed; |
| 158 | } |