From d498d97da7b281cb2c9e1b70120287280361ee79 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 5 Sep 2013 16:38:48 -0700 Subject: [PATCH] maintain a double ended queue of created assignments --- assignment-server/src/main.cpp | 47 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/assignment-server/src/main.cpp b/assignment-server/src/main.cpp index 671ca889df..54d1ce05e6 100644 --- a/assignment-server/src/main.cpp +++ b/assignment-server/src/main.cpp @@ -21,7 +21,7 @@ const int MAX_PACKET_SIZE_BYTES = 1400; int main(int argc, const char* argv[]) { - std::queue assignmentQueue; + std::deque assignmentQueue; sockaddr_in senderSocket; unsigned char senderData[MAX_PACKET_SIZE_BYTES] = {}; @@ -38,43 +38,48 @@ int main(int argc, const char* argv[]) { // construct the requested assignment from the packet data Assignment requestAssignment(senderData, receivedBytes); - // grab the first assignment in the queue, if it exists + qDebug() << "Received request for assignment:" << requestAssignment; + + // make sure there are assignments in the queue at all if (assignmentQueue.size() > 0) { - Assignment firstAssignment = assignmentQueue.front(); - bool eitherHasPool = (firstAssignment.getPool() || requestAssignment.getPool()); - bool bothHavePool = (firstAssignment.getPool() && requestAssignment.getPool()); - - // make sure there is a pool match for the created and requested assignment - // or that neither has a designated pool - if ((eitherHasPool && bothHavePool && strcmp(firstAssignment.getPool(), requestAssignment.getPool()) == 0) - || !eitherHasPool) { - assignmentQueue.pop(); + // enumerate assignments until we find one to give this client (if possible) + for (std::deque::iterator assignment = assignmentQueue.begin(); + assignment != assignmentQueue.end(); + assignment++) { - int numAssignmentBytes = firstAssignment.packToBuffer(assignmentPacket + numSendHeaderBytes); + bool eitherHasPool = (assignment->getPool() || requestAssignment.getPool()); + bool bothHavePool = (assignment->getPool() && requestAssignment.getPool()); - // send the assignment - serverSocket.send((sockaddr*) &senderSocket, assignmentPacket, numSendHeaderBytes + numAssignmentBytes); + // make sure there is a pool match for the created and requested assignment + // or that neither has a designated pool + if ((eitherHasPool && bothHavePool + && strcmp(assignment->getPool(), requestAssignment.getPool()) == 0) + || !eitherHasPool) { + assignmentQueue.erase(assignment); + + int numAssignmentBytes = assignment->packToBuffer(assignmentPacket + numSendHeaderBytes); + + // send the assignment + serverSocket.send((sockaddr*) &senderSocket, + assignmentPacket, + numSendHeaderBytes + numAssignmentBytes); + } } } } else if (senderData[0] == PACKET_TYPE_CREATE_ASSIGNMENT && packetVersionMatch(senderData)) { // construct the create assignment from the packet data Assignment createdAssignment(senderData, receivedBytes); - qDebug() << "Received an assignment:" << createdAssignment; + qDebug() << "Received a created assignment:" << createdAssignment; // assignment server is on a public server // assume that the address we now have for the sender is the public address/port // and store that with the assignment so it can be given to the requestor later createdAssignment.setDomainSocket((sockaddr*) &senderSocket); - // until we have a GUID setup just keep the latest assignment - if (assignmentQueue.size() > 0) { - assignmentQueue.pop(); - } - // add this assignment to the queue - assignmentQueue.push(createdAssignment); + assignmentQueue.push_back(createdAssignment); } } }