Merge pull request #947 from birarda/master

add API to attach payloads to assignments
This commit is contained in:
ZappoMan 2013-09-17 13:39:48 -07:00
commit 6344ae8fd3
2 changed files with 51 additions and 13 deletions

View file

@ -22,7 +22,9 @@ Assignment::Assignment(Assignment::Command command, Assignment::Type type, Assig
_location(location),
_attachedPublicSocket(NULL),
_attachedLocalSocket(NULL),
_numberOfInstances(1)
_numberOfInstances(1),
_payload(NULL),
_numPayloadBytes(0)
{
// set the create time on this assignment
gettimeofday(&_time, NULL);
@ -37,7 +39,9 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
_location(GlobalLocation),
_attachedPublicSocket(NULL),
_attachedLocalSocket(NULL),
_numberOfInstances(1)
_numberOfInstances(1),
_payload(NULL),
_numPayloadBytes(0)
{
// set the create time on this assignment
gettimeofday(&_time, NULL);
@ -63,32 +67,55 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
memcpy(&_type, dataBuffer + numBytesRead, sizeof(Assignment::Type));
numBytesRead += sizeof(Assignment::Type);
if (numBytes > numBytesRead) {
if (_command != Assignment::RequestCommand) {
sockaddr* newSocket = NULL;
if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) {
// IPv4 address
newSocket = (sockaddr*) new sockaddr_in;
unpackSocket(dataBuffer + numBytesRead, newSocket);
numBytesRead += unpackSocket(dataBuffer + numBytesRead, newSocket);
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) {
delete _attachedLocalSocket;
_attachedLocalSocket = newSocket;
} else {
delete _attachedPublicSocket;
_attachedPublicSocket = newSocket;
}
}
if (numBytes > numBytesRead) {
_numPayloadBytes = numBytes - numBytesRead;
memcpy(_payload, dataBuffer + numBytesRead, numBytes - numBytesRead);
}
}
Assignment::~Assignment() {
delete _attachedPublicSocket;
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 {
@ -141,6 +168,11 @@ int Assignment::packToBuffer(unsigned char* buffer) {
numPackedBytes += packSocket(buffer + numPackedBytes, socketToPack);
}
if (_numPayloadBytes) {
memcpy(buffer + numPackedBytes, _payload, _numPayloadBytes);
numPackedBytes += _numPayloadBytes;
}
return numPackedBytes;
}

View file

@ -56,6 +56,10 @@ public:
Assignment::Location getLocation() const { return _location; }
const timeval& getTime() const { return _time; }
uchar* getPayload() { return _payload; }
int getNumPayloadBytes() const { return _numPayloadBytes; }
void setPayload(uchar *payload, int numBytes);
int getNumberOfInstances() const { return _numberOfInstances; }
void setNumberOfInstances(int numberOfInstances) { _numberOfInstances = numberOfInstances; }
void decrementNumberOfInstances() { --_numberOfInstances; }
@ -83,6 +87,8 @@ private:
sockaddr* _attachedLocalSocket; /// pointer to a local socket that relates to assignment, depends on direction
timeval _time; /// time the assignment was created (set in constructor)
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);