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