44#include "PPTConnection.h"
45#include "PPTProtocolNames.h"
48#include "BESInternalError.h"
54using std::ostringstream;
55using std::istringstream;
65#define prolog string("PPTConnection::").append(__func__).append("() - ")
67PPTConnection::~PPTConnection()
111 if (!buffer.empty()) {
112 sendChunk(buffer, extensions);
115 map<string, string> no_extensions;
116 sendChunk(
"", no_extensions);
119 sendChunk(
"", extensions);
127 map<string, string> extensions;
128 extensions[
"status"] = PPT_EXIT_NOW;
129 send(
"", extensions);
133 sendChunk(
"", extensions);
144void PPTConnection::sendChunk(
const string &buffer, map<string, string> &extensions)
147 if (extensions.size()) {
150 strm << hex << setw(7) << setfill(
'0') << buffer.length() <<
"d";
151 if (!buffer.empty()) {
154 string toSend = strm.str();
165 if (extensions.size()) {
167 map<string, string>::const_iterator i = extensions.begin();
168 map<string, string>::const_iterator ie = extensions.end();
169 for (; i != ie; i++) {
171 string value = (*i).second;
172 if (!value.empty()) {
173 estrm <<
"=" << value;
177 string xstr = estrm.str();
178 strm << hex << setw(7) << setfill(
'0') << xstr.length() <<
"x" << xstr;
179 string toSend = strm.str();
192 BESDEBUG(MODULE, prolog <<
"Sending " << buffer << endl);
193 _mySock->send(buffer, 0, buffer.length());
211 return _mySock->receive(buffer, buffer_size);
214int PPTConnection::readChunkHeader(
char *buffer,
int buffer_size)
216 char *temp_buffer = buffer;
217 int totalBytesRead = 0;
220 int bytesRead =
readBuffer(temp_buffer, buffer_size);
221 BESDEBUG( MODULE, prolog <<
"Read " << bytesRead <<
" bytes" << endl );
226 if (bytesRead == 0) {
229 if (bytesRead < buffer_size) {
230 buffer_size = buffer_size - bytesRead;
231 temp_buffer = temp_buffer + bytesRead;
232 totalBytesRead += bytesRead;
235 totalBytesRead += bytesRead;
239 buffer[totalBytesRead] =
'\0';
240 return totalBytesRead;
258bool PPTConnection::receive(map<string, string> &extensions, ostream *strm)
260 ostream *use_strm = _out;
261 if (strm) use_strm = strm;
265 BESDEBUG( MODULE, prolog <<
"buffer size = " << _inBuff_len << endl );
267 _inBuff_len = _mySock->getRecvBufferSize() + 1;
268 _inBuff =
new char[_inBuff_len + 1];
274 int bytesRead = readChunkHeader(_inBuff, 8);
275 BESDEBUG( MODULE, prolog <<
"Reading header, read " << bytesRead <<
" bytes" << endl );
277 throw BESInternalError(prolog +
"Failed to read chunk header", __FILE__, __LINE__);
280 lenbuffer[0] = _inBuff[0];
281 lenbuffer[1] = _inBuff[1];
282 lenbuffer[2] = _inBuff[2];
283 lenbuffer[3] = _inBuff[3];
284 lenbuffer[4] = _inBuff[4];
285 lenbuffer[5] = _inBuff[5];
286 lenbuffer[6] = _inBuff[6];
288 istringstream lenstrm(lenbuffer);
289 unsigned long inlen = 0;
290 lenstrm >> hex >> setw(7) >> inlen;
291 BESDEBUG( MODULE, prolog <<
"Reading header, chunk length = " << inlen << endl );
292 BESDEBUG( MODULE, prolog <<
"Reading header, chunk type = " << _inBuff[7] << endl );
294 if (_inBuff[7] ==
'x') {
296 receive(xstrm, inlen);
299 else if (_inBuff[7] ==
'd') {
305 receive(*use_strm, inlen);
308 string err = (string)
"type of data is " + _inBuff[7] +
", should be x for extensions or d for data";
324void PPTConnection::receive(ostream &strm,
const int len)
326 BESDEBUG( MODULE, prolog <<
"len = " << len << endl );
329 string err =
"buffer has not been initialized";
334 if( len > _inBuff_len )
336 to_read = _inBuff_len;
338 BESDEBUG( MODULE, prolog <<
"to_read = " << to_read << endl );
341 int bytesRead =
readBuffer( _inBuff, to_read );
344 string err =
"Failed to read data from socket";
347 BESDEBUG( MODULE, prolog <<
"bytesRead = " << bytesRead << endl );
350 _inBuff[bytesRead] =
'\0';
351 strm.write( _inBuff, bytesRead );
356 if( bytesRead < len )
358 BESDEBUG( MODULE, prolog <<
"remaining = " << (len - bytesRead) << endl );
359 receive( strm, len - bytesRead );
380 unsigned int index = 0;
383 string::size_type semi = xstr.find(
';', index);
384 if (semi == string::npos) {
385 string err =
"malformed extensions " + xstr.substr(index, xstr.length() - index) +
", missing semicolon";
388 string::size_type eq = xstr.find(
'=', index);
389 if (eq == string::npos || eq > semi) {
391 var = xstr.substr(index, semi - index);
392 extensions[var] =
"";
394 else if (eq == semi - 1) {
395 string err =
"malformed extensions " + xstr.substr(index, xstr.length() - index)
396 +
", missing value after =";
400 var = xstr.substr(index, eq - index);
401 val = xstr.substr(eq + 1, semi - eq - 1);
402 extensions[var] = val;
405 if (index >= xstr.length()) {
423 struct pollfd arr[1];
424 arr[0].fd = getSocket()->getSocketDescriptor();
425 arr[0].events = POLLIN;
428 struct pollfd p = {};
429 p.fd = getSocket()->getSocketDescriptor();
431 struct pollfd arr[1];
436 for (
int j = 0; j < _timeout; j++) {
437 if (poll(arr, 1, 1000) < 0) {
439 if (errno == EINTR || errno == EAGAIN)
continue;
441 throw BESInternalError(
string(
"poll error") +
" " + strerror(errno), __FILE__, __LINE__);
444 if (arr[0].revents == POLLIN) {
448 cout <<
" " << j << flush;
456unsigned int PPTConnection::getRecvChunkSize()
458 return _mySock->getRecvBufferSize() - PPT_CHUNK_HEADER_SPACE;
461unsigned int PPTConnection::getSendChunkSize()
463 return _mySock->getSendBufferSize() - PPT_CHUNK_HEADER_SPACE;
474 strm << BESIndent::LMarg <<
"PPTConnection::dump - (" << (
void *)
this <<
")" << endl;
477 BESIndent::UnIndent();
exception thrown if internal error encountered
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void sendExit()
Send the exit token as an extension.
virtual int readBuffer(char *inBuff, const unsigned int buff_size)
read a buffer of data from the socket
virtual void sendExtensions(std::map< std::string, std::string > &extensions)
send the specified extensions
virtual void send(const std::string &buffer)
sends the buffer to the socket
virtual void read_extensions(std::map< std::string, std::string > &extensions, const std::string &xstr)
the string passed are extensions, read them and store the name/value pairs into the passed map
virtual int readBufferNonBlocking(char *inBuff, const int buff_size)
read a buffer of data from the socket without blocking
virtual void dump(std::ostream &strm) const
dumps information about this object