46 using std::ostringstream;
47 using std::istringstream;
55 #include "PPTConnection.h"
56 #include "PPTProtocol.h"
59 #include "BESInternalError.h"
61 PPTConnection::~PPTConnection()
105 if (!buffer.empty()) {
106 sendChunk(buffer, extensions);
109 map<string, string> no_extensions;
110 sendChunk(
"", no_extensions);
113 sendChunk(
"", extensions);
121 map<string, string> extensions;
122 extensions[
"status"] = PPTProtocol::PPT_EXIT_NOW;
123 send(
"", extensions);
127 sendChunk(
"", extensions);
138 void PPTConnection::sendChunk(
const string &buffer, map<string, string> &extensions)
141 if (extensions.size()) {
144 strm << hex << setw(7) << setfill(
'0') << buffer.length() <<
"d";
145 if (!buffer.empty()) {
148 string toSend = strm.str();
159 if (extensions.size()) {
161 map<string, string>::const_iterator i = extensions.begin();
162 map<string, string>::const_iterator ie = extensions.end();
163 for (; i != ie; i++) {
165 string value = (*i).second;
166 if (!value.empty()) {
167 estrm <<
"=" << value;
171 string xstr = estrm.str();
172 strm << hex << setw(7) << setfill(
'0') << xstr.length() <<
"x" << xstr;
173 string toSend = strm.str();
186 BESDEBUG(
"ppt",
"PPTConnection::send - sending " << buffer << endl);
188 _mySock->send(buffer, 0, buffer.length());
206 return _mySock->receive(buffer, buffer_size);
209 int PPTConnection::readChunkHeader(
char *buffer,
int buffer_size)
211 char *temp_buffer = buffer;
212 int totalBytesRead = 0;
215 int bytesRead =
readBuffer(temp_buffer, buffer_size);
216 BESDEBUG(
"ppt",
"PPTConnection::readChunkHeader - read " << bytesRead <<
" bytes" << endl );
221 if (bytesRead == 0) {
224 if (bytesRead < buffer_size) {
225 buffer_size = buffer_size - bytesRead;
226 temp_buffer = temp_buffer + bytesRead;
227 totalBytesRead += bytesRead;
230 totalBytesRead += bytesRead;
234 buffer[totalBytesRead] =
'\0';
235 return totalBytesRead;
253 bool PPTConnection::receive(map<string, string> &extensions, ostream *strm)
255 ostream *use_strm = _out;
256 if (strm) use_strm = strm;
260 BESDEBUG(
"ppt",
"PPTConnection::receive: buffer size = " << _inBuff_len << endl );
262 _inBuff_len = _mySock->getRecvBufferSize() + 1;
263 _inBuff =
new char[_inBuff_len + 1];
269 int bytesRead = readChunkHeader(_inBuff, 8);
270 BESDEBUG(
"ppt",
"Reading header, read " << bytesRead <<
" bytes" << endl );
275 lenbuffer[0] = _inBuff[0];
276 lenbuffer[1] = _inBuff[1];
277 lenbuffer[2] = _inBuff[2];
278 lenbuffer[3] = _inBuff[3];
279 lenbuffer[4] = _inBuff[4];
280 lenbuffer[5] = _inBuff[5];
281 lenbuffer[6] = _inBuff[6];
283 istringstream lenstrm(lenbuffer);
284 unsigned long inlen = 0;
285 lenstrm >> hex >> setw(7) >> inlen;
286 BESDEBUG(
"ppt",
"Reading header, chunk length = " << inlen << endl );
287 BESDEBUG(
"ppt",
"Reading header, chunk type = " << _inBuff[7] << endl );
289 if (_inBuff[7] ==
'x') {
291 receive(xstrm, inlen);
294 else if (_inBuff[7] ==
'd') {
300 receive(*use_strm, inlen);
303 string err = (string)
"type of data is " + _inBuff[7] +
", should be x for extensions or d for data";
319 void PPTConnection::receive(ostream &strm,
const int len)
321 BESDEBUG(
"ppt",
"PPTConnect::receive - len = " << len << endl );
324 string err =
"buffer has not been initialized";
329 if( len > _inBuff_len )
331 to_read = _inBuff_len;
333 BESDEBUG(
"ppt",
"PPTConnect::receive - to_read = " << to_read << endl );
336 int bytesRead =
readBuffer( _inBuff, to_read );
339 string err =
"Failed to read data from socket";
342 BESDEBUG(
"ppt",
"PPTConnect::receive - bytesRead = "
343 << bytesRead << endl );
346 _inBuff[bytesRead] =
'\0';
347 strm.write( _inBuff, bytesRead );
352 if( bytesRead < len )
354 BESDEBUG(
"ppt",
"PPTConnect::receive - remaining = "
355 << (len - bytesRead) << endl );
356 receive( strm, len - bytesRead );
377 unsigned int index = 0;
380 string::size_type semi = xstr.find(
';', index);
381 if (semi == string::npos) {
382 string err =
"malformed extensions " + xstr.substr(index, xstr.length() - index) +
", missing semicolon";
385 string::size_type eq = xstr.find(
'=', index);
386 if (eq == string::npos || eq > semi) {
388 var = xstr.substr(index, semi - index);
389 extensions[var] =
"";
391 else if (eq == semi - 1) {
392 string err =
"malformed extensions " + xstr.substr(index, xstr.length() - index)
393 +
", missing value after =";
397 var = xstr.substr(index, eq - index);
398 val = xstr.substr(eq + 1, semi - eq - 1);
399 extensions[var] = val;
402 if (index >= xstr.length()) {
420 struct pollfd arr[1];
421 arr[0].fd = getSocket()->getSocketDescriptor();
422 arr[0].events = POLLIN;
425 struct pollfd p = {};
426 p.fd = getSocket()->getSocketDescriptor();
428 struct pollfd arr[1];
433 for (
int j = 0; j < _timeout; j++) {
434 if (poll(arr, 1, 1000) < 0) {
436 if (errno == EINTR || errno == EAGAIN)
continue;
438 throw BESInternalError(
string(
"poll error") +
" " + strerror(errno), __FILE__, __LINE__);
441 if (arr[0].revents == POLLIN) {
445 cout <<
" " << j << flush;
453 unsigned int PPTConnection::getRecvChunkSize()
455 return _mySock->getRecvBufferSize() - PPT_CHUNK_HEADER_SPACE;
458 unsigned int PPTConnection::getSendChunkSize()
460 return _mySock->getSendBufferSize() - PPT_CHUNK_HEADER_SPACE;
471 strm << BESIndent::LMarg <<
"PPTConnection::dump - (" << (
void *)
this <<
")" << endl;
474 BESIndent::UnIndent();