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 "RemoteProcessorProtocol.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
CRequestMessage(const string & strCommand)42 CRequestMessage::CRequestMessage(const string& strCommand) : base(ECommandRequest), _strCommand(strCommand)
43 {
44 }
45
CRequestMessage()46 CRequestMessage::CRequestMessage()
47 {
48 }
49
50 // Command Name
setCommand(const string & strCommand)51 void CRequestMessage::setCommand(const string& strCommand)
52 {
53 _strCommand = trim(strCommand);
54 }
55
getCommand() const56 const string& CRequestMessage::getCommand() const
57 {
58 return _strCommand;
59 }
60
61 // Arguments
addArgument(const string & strArgument)62 void CRequestMessage::addArgument(const string& strArgument)
63 {
64 _argumentVector.push_back(trim(strArgument));
65 }
66
getArgumentCount() const67 uint32_t CRequestMessage::getArgumentCount() const
68 {
69 return _argumentVector.size();
70 }
71
getArgument(uint32_t uiArgument) const72 const string& CRequestMessage::getArgument(uint32_t uiArgument) const
73 {
74 assert(uiArgument < _argumentVector.size());
75
76 return _argumentVector[uiArgument];
77 }
78
packArguments(uint32_t uiStartArgument,uint32_t uiNbArguments) const79 const string CRequestMessage::packArguments(uint32_t uiStartArgument, uint32_t uiNbArguments) const
80 {
81 string strPackedArguments;
82
83 assert(uiStartArgument + uiNbArguments <= _argumentVector.size());
84
85 // Pack arguments, separating them with a space
86 uint32_t uiArgument;
87
88 bool bFirst = true;
89
90 for (uiArgument = uiStartArgument; uiArgument < uiStartArgument + uiNbArguments; uiArgument++) {
91
92 if (!bFirst) {
93
94 strPackedArguments += " ";
95 } else {
96
97 bFirst = false;
98 }
99
100 strPackedArguments += _argumentVector[uiArgument];
101 }
102
103 return strPackedArguments;
104 }
105
106 // Fill data to send
fillDataToSend()107 void CRequestMessage::fillDataToSend()
108 {
109 // Send command
110 writeString(getCommand());
111
112 // Arguments
113 uint32_t uiArgument;
114
115 for (uiArgument = 0; uiArgument < getArgumentCount(); uiArgument++) {
116
117 writeString(getArgument(uiArgument));
118 }
119 }
120
121 // Collect received data
collectReceivedData()122 void CRequestMessage::collectReceivedData()
123 {
124 // Receive command
125 string strCommand;
126
127 readString(strCommand);
128
129 setCommand(strCommand);
130
131 // Arguments
132 while (getRemainingDataSize()) {
133
134 string strArgument;
135
136 readString(strArgument);
137
138 addArgument(strArgument);
139 }
140 }
141
142 // Size
getDataSize() const143 size_t CRequestMessage::getDataSize() const
144 {
145 // Command
146 size_t uiSize = getStringSize(getCommand());
147
148 // Arguments
149 uint32_t uiArgument;
150
151 for (uiArgument = 0; uiArgument < getArgumentCount(); uiArgument++) {
152
153 uiSize += getStringSize(getArgument(uiArgument));
154 }
155 return uiSize;
156 }
157
158 // Trim input string
trim(const string & strToTrim)159 string CRequestMessage::trim(const string& strToTrim)
160 {
161 // Trim string
162 string strTrimmed = strToTrim;
163
164 strTrimmed.erase(strTrimmed.find_last_not_of(gacDelimiters) + 1 );
165
166 strTrimmed.erase(0, strTrimmed.find_first_not_of(gacDelimiters));
167
168 return strTrimmed;
169 }
170