use char array of static length for pool

This commit is contained in:
Stephen Birarda 2013-10-22 13:35:26 -07:00
parent 85b5d8564f
commit 1eba133a6d
4 changed files with 79 additions and 57 deletions

View file

@ -35,7 +35,7 @@ pid_t* childForks = NULL;
sockaddr_in customAssignmentSocket = {};
int numForks = 0;
Assignment::Type overiddenAssignmentType = Assignment::AllTypes;
QString assignmentPool = QString();
const char* assignmentPool = NULL;
int argc = 0;
char** argv = NULL;
@ -222,8 +222,7 @@ int main(int argc, char* argv[]) {
}
const char ASSIGNMENT_POOL_OPTION[] = "--pool";
const char* assignmentPoolString = getCmdOption(argc, (const char**) argv, ASSIGNMENT_POOL_OPTION);
::assignmentPool = QString(assignmentPoolString);
::assignmentPool = getCmdOption(argc, (const char**) argv, ASSIGNMENT_POOL_OPTION);
const char* NUM_FORKS_PARAMETER = "-n";
const char* numForksString = getCmdOption(argc, (const char**)argv, NUM_FORKS_PARAMETER);

View file

@ -175,7 +175,7 @@ void DomainServer::civetwebUploadHandler(struct mg_connection *connection, const
// create an assignment for this saved script, for now make it local only
Assignment *scriptAssignment = new Assignment(Assignment::CreateCommand,
Assignment::AgentType,
QString(),
NULL,
Assignment::LocalLocation);
// check how many instances of this assignment the user wants by checking the ASSIGNMENT-INSTANCES header
@ -336,7 +336,9 @@ void DomainServer::prepopulateStaticAssignmentFile() {
qDebug() << "The pool for this voxel-assignment is" << assignmentPool << "\n";
}
Assignment voxelServerAssignment(Assignment::CreateCommand, Assignment::VoxelServerType, assignmentPool);
Assignment voxelServerAssignment(Assignment::CreateCommand,
Assignment::VoxelServerType,
(assignmentPool.isEmpty() ? NULL : assignmentPool.toLocal8Bit().constData()));
int payloadLength = config.length() + sizeof(char);
voxelServerAssignment.setPayload((uchar*)config.toLocal8Bit().constData(), payloadLength);
@ -399,9 +401,14 @@ Assignment* DomainServer::deployableAssignmentForRequest(Assignment& requestAssi
std::deque<Assignment*>::iterator assignment = _assignmentQueue.begin();
while (assignment != _assignmentQueue.end()) {
bool requestIsAllTypes = requestAssignment.getType() == Assignment::AllTypes;
bool assignmentTypesMatch = (*assignment)->getType() == requestAssignment.getType();
bool nietherHasPool = !(*assignment)->hasPool() && !requestAssignment.hasPool();
bool assignmentPoolsMatch = memcmp((*assignment)->getPool(),
requestAssignment.getPool(),
MAX_ASSIGNMENT_POOL_BYTES) == 0;
if (requestAssignment.getType() == Assignment::AllTypes ||
(*assignment)->getType() == requestAssignment.getType()) {
if ((requestIsAllTypes || assignmentTypesMatch) && (nietherHasPool || assignmentPoolsMatch)) {
Assignment* deployableAssignment = *assignment;

View file

@ -36,19 +36,17 @@ Assignment::Assignment() :
_uuid(),
_command(Assignment::RequestCommand),
_type(Assignment::AllTypes),
_pool(),
_location(Assignment::LocalLocation),
_numberOfInstances(1),
_payload(),
_numPayloadBytes(0)
{
setPool(NULL);
}
Assignment::Assignment(Assignment::Command command, Assignment::Type type, const QString& pool, Assignment::Location location) :
Assignment::Assignment(Assignment::Command command, Assignment::Type type, const char* pool, Assignment::Location location) :
_command(command),
_type(type),
_pool(pool),
_location(location),
_numberOfInstances(1),
_payload(),
@ -58,42 +56,8 @@ Assignment::Assignment(Assignment::Command command, Assignment::Type type, const
// this is a newly created assignment, generate a random UUID
_uuid = QUuid::createUuid();
}
}
Assignment::Assignment(const Assignment& otherAssignment) {
_uuid = otherAssignment._uuid;
_command = otherAssignment._command;
_type = otherAssignment._type;
_location = otherAssignment._location;
_pool = otherAssignment._pool;
_numberOfInstances = otherAssignment._numberOfInstances;
setPayload(otherAssignment._payload, otherAssignment._numPayloadBytes);
}
Assignment& Assignment::operator=(const Assignment& rhsAssignment) {
Assignment temp(rhsAssignment);
swap(temp);
return *this;
}
void Assignment::swap(Assignment& otherAssignment) {
using std::swap;
swap(_uuid, otherAssignment._uuid);
swap(_command, otherAssignment._command);
swap(_type, otherAssignment._type);
swap(_location, otherAssignment._location);
swap(_pool, otherAssignment._pool);
swap(_numberOfInstances, otherAssignment._numberOfInstances);
for (int i = 0; i < MAX_PAYLOAD_BYTES; i++) {
swap(_payload[i], otherAssignment._payload[i]);
}
swap(_numPayloadBytes, otherAssignment._numPayloadBytes);
setPool(pool);
}
Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
@ -101,7 +65,7 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
_numberOfInstances(1),
_payload(),
_numPayloadBytes(0)
{
{
int numBytesRead = 0;
if (dataBuffer[0] == PACKET_TYPE_REQUEST_ASSIGNMENT) {
@ -125,17 +89,58 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
if (dataBuffer[numBytesRead] != '\0') {
// read the pool from the data buffer
_pool = QString((char*) dataBuffer + numBytesRead);
setPool((const char*) dataBuffer + numBytesRead);
} else {
// skip past the null pool
// skip past the null pool and null out our pool
setPool(NULL);
numBytesRead++;
}
if (numBytes > numBytesRead) {
setPayload(dataBuffer + numBytesRead, numBytes - numBytesRead);
}
}
Assignment::Assignment(const Assignment& otherAssignment) {
_uuid = otherAssignment._uuid;
_command = otherAssignment._command;
_type = otherAssignment._type;
_location = otherAssignment._location;
setPool(otherAssignment._pool);
_numberOfInstances = otherAssignment._numberOfInstances;
setPayload(otherAssignment._payload, otherAssignment._numPayloadBytes);
}
Assignment& Assignment::operator=(const Assignment& rhsAssignment) {
Assignment temp(rhsAssignment);
swap(temp);
return *this;
}
void Assignment::swap(Assignment& otherAssignment) {
using std::swap;
swap(_uuid, otherAssignment._uuid);
swap(_command, otherAssignment._command);
swap(_type, otherAssignment._type);
swap(_location, otherAssignment._location);
for (int i = 0; i < sizeof(_pool); i++) {
swap(_pool[i], otherAssignment._pool[i]);
}
swap(_numberOfInstances, otherAssignment._numberOfInstances);
for (int i = 0; i < MAX_PAYLOAD_BYTES; i++) {
swap(_payload[i], otherAssignment._payload[i]);
}
swap(_numPayloadBytes, otherAssignment._numPayloadBytes);
}
void Assignment::setPayload(const uchar* payload, int numBytes) {
if (numBytes > MAX_PAYLOAD_BYTES) {
@ -152,6 +157,14 @@ void Assignment::setPayload(const uchar* payload, int numBytes) {
memcpy(_payload, payload, _numPayloadBytes);
}
void Assignment::setPool(const char* pool) {
memset(_pool, '\0', sizeof(_pool));
if (pool) {
strcpy(_pool, pool);
}
}
const char* Assignment::getTypeName() const {
switch (_type) {
case Assignment::AudioMixerType:
@ -179,10 +192,11 @@ int Assignment::packToBuffer(unsigned char* buffer) {
numPackedBytes += NUM_BYTES_RFC4122_UUID;
}
if (!_pool.isEmpty()) {
if (_pool) {
// pack the pool for this assignment, it exists
memcpy(buffer + numPackedBytes, _pool.toLocal8Bit().constData(), _pool.toLocal8Bit().size());
numPackedBytes += _pool.toLocal8Bit().size();
int numBytesNullTerminatedPool = strlen(_pool) + sizeof('\0');
memcpy(buffer + numPackedBytes, _pool, numBytesNullTerminatedPool);
numPackedBytes += numBytesNullTerminatedPool;
} else {
// otherwise pack the null character
buffer[numPackedBytes++] = '\0';

View file

@ -16,6 +16,7 @@
#include "NodeList.h"
const int MAX_PAYLOAD_BYTES = 1024;
const int MAX_ASSIGNMENT_POOL_BYTES = 64 + sizeof('\0');
/// Holds information used for request, creation, and deployment of assignments
class Assignment : public NodeData {
@ -46,7 +47,7 @@ public:
Assignment();
Assignment(Assignment::Command command,
Assignment::Type type,
const QString& pool = QString(),
const char* pool = NULL,
Assignment::Location location = Assignment::LocalLocation);
Assignment(const Assignment& otherAssignment);
Assignment& operator=(const Assignment &rhsAssignment);
@ -70,8 +71,9 @@ public:
int getNumPayloadBytes() const { return _numPayloadBytes; }
void setPayload(const uchar *payload, int numBytes);
void setPool(const QString& pool) { _pool = pool; }
const QString& getPool() const { return _pool; }
void setPool(const char* pool);
const char* getPool() const { return _pool; }
bool hasPool() const { return (bool) strlen(_pool); }
int getNumberOfInstances() const { return _numberOfInstances; }
void setNumberOfInstances(int numberOfInstances) { _numberOfInstances = numberOfInstances; }
@ -98,7 +100,7 @@ protected:
QUuid _uuid; /// the 16 byte UUID for this assignment
Assignment::Command _command; /// the command for this assignment (Create, Deploy, Request)
Assignment::Type _type; /// the type of the assignment, defines what the assignee will do
QString _pool; /// the destination pool for this assignment
char _pool[MAX_ASSIGNMENT_POOL_BYTES]; /// the destination pool for this assignment
Assignment::Location _location; /// the location of the assignment, allows a domain to preferentially use local ACs
int _numberOfInstances; /// the number of instances of this assignment
uchar _payload[MAX_PAYLOAD_BYTES]; /// an optional payload attached to this assignment, a maximum for 1024 bytes will be packed