add API to attach payloads to assignments

This commit is contained in:
Stephen Birarda 2013-09-17 13:24:25 -07:00
parent 0e6dae6403
commit 94f04a9806
2 changed files with 58 additions and 17 deletions

View file

@ -22,7 +22,9 @@ Assignment::Assignment(Assignment::Command command, Assignment::Type type, Assig
_location(location), _location(location),
_attachedPublicSocket(NULL), _attachedPublicSocket(NULL),
_attachedLocalSocket(NULL), _attachedLocalSocket(NULL),
_numberOfInstances(1) _numberOfInstances(1),
_payload(NULL),
_numPayloadBytes(0)
{ {
// set the create time on this assignment // set the create time on this assignment
gettimeofday(&_time, NULL); gettimeofday(&_time, NULL);
@ -37,7 +39,9 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
_location(GlobalLocation), _location(GlobalLocation),
_attachedPublicSocket(NULL), _attachedPublicSocket(NULL),
_attachedLocalSocket(NULL), _attachedLocalSocket(NULL),
_numberOfInstances(1) _numberOfInstances(1),
_payload(NULL),
_numPayloadBytes(0)
{ {
// set the create time on this assignment // set the create time on this assignment
gettimeofday(&_time, NULL); gettimeofday(&_time, NULL);
@ -65,23 +69,31 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
if (numBytes > numBytesRead) { if (numBytes > numBytesRead) {
sockaddr* newSocket = NULL; if (_command != Assignment::RequestCommand) {
sockaddr* newSocket = NULL;
if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) {
// IPv4 address if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) {
newSocket = (sockaddr*) new sockaddr_in; // IPv4 address
unpackSocket(dataBuffer + numBytesRead, newSocket); newSocket = (sockaddr*) new sockaddr_in;
} else { numBytesRead += unpackSocket(dataBuffer + numBytesRead, newSocket);
// IPv6 address, or bad designator
qDebug("Received a socket that cannot be unpacked!\n"); if (_command == Assignment::CreateCommand) {
delete _attachedLocalSocket;
_attachedLocalSocket = newSocket;
} else {
delete _attachedPublicSocket;
_attachedPublicSocket = newSocket;
}
} else {
// IPv6 address, or bad designator
qDebug("Received a socket that cannot be unpacked!\n");
}
} }
if (_command == Assignment::CreateCommand) { _numPayloadBytes = numBytes - numBytesRead;
delete _attachedLocalSocket;
_attachedLocalSocket = newSocket; if (_numPayloadBytes > 0) {
} else { memcpy(_payload, dataBuffer + numBytesRead, numBytes - numBytesRead);
delete _attachedPublicSocket;
_attachedPublicSocket = newSocket;
} }
} }
} }
@ -89,6 +101,25 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
Assignment::~Assignment() { Assignment::~Assignment() {
delete _attachedPublicSocket; delete _attachedPublicSocket;
delete _attachedLocalSocket; delete _attachedLocalSocket;
delete _payload;
_numPayloadBytes = 0;
}
const int MAX_PAYLOAD_BYTES = 1024;
void Assignment::setPayload(uchar* payload, int numBytes) {
_payload = payload;
if (numBytes > MAX_PAYLOAD_BYTES) {
qDebug("Set payload called with number of bytes greater than maximum (%d). Will only transfer %d bytes.\n",
MAX_PAYLOAD_BYTES,
MAX_PAYLOAD_BYTES);
_numPayloadBytes = 1024;
} else {
_numPayloadBytes = numBytes;
}
} }
QString Assignment::getUUIDStringWithoutCurlyBraces() const { QString Assignment::getUUIDStringWithoutCurlyBraces() const {
@ -141,6 +172,11 @@ int Assignment::packToBuffer(unsigned char* buffer) {
numPackedBytes += packSocket(buffer + numPackedBytes, socketToPack); numPackedBytes += packSocket(buffer + numPackedBytes, socketToPack);
} }
if (_numPayloadBytes) {
memcpy(buffer + numPackedBytes, _payload, _numPayloadBytes);
numPackedBytes += _numPayloadBytes;
}
return numPackedBytes; return numPackedBytes;
} }

View file

@ -56,6 +56,9 @@ public:
Assignment::Location getLocation() const { return _location; } Assignment::Location getLocation() const { return _location; }
const timeval& getTime() const { return _time; } const timeval& getTime() const { return _time; }
uchar* getPayload() { return _payload; }
void setPayload(uchar *payload, int numBytes);
int getNumberOfInstances() const { return _numberOfInstances; } int getNumberOfInstances() const { return _numberOfInstances; }
void setNumberOfInstances(int numberOfInstances) { _numberOfInstances = numberOfInstances; } void setNumberOfInstances(int numberOfInstances) { _numberOfInstances = numberOfInstances; }
void decrementNumberOfInstances() { --_numberOfInstances; } void decrementNumberOfInstances() { --_numberOfInstances; }
@ -83,6 +86,8 @@ private:
sockaddr* _attachedLocalSocket; /// pointer to a local socket that relates to assignment, depends on direction sockaddr* _attachedLocalSocket; /// pointer to a local socket that relates to assignment, depends on direction
timeval _time; /// time the assignment was created (set in constructor) timeval _time; /// time the assignment was created (set in constructor)
int _numberOfInstances; /// the number of instances of this assignment int _numberOfInstances; /// the number of instances of this assignment
uchar *_payload; /// an optional payload attached to this assignment, a maximum for 1024 bytes will be packed
int _numPayloadBytes; /// number of bytes in the payload, up to a maximum of 1024
}; };
QDebug operator<<(QDebug debug, const Assignment &assignment); QDebug operator<<(QDebug debug, const Assignment &assignment);