From 855bcb3753a2fc5e736129fdafc2cc632129f484 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 15 Jul 2013 11:47:54 -0700 Subject: [PATCH] revamp assignment server to accept requests from webpage --- assignment-server/src/main.cpp | 43 +++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/assignment-server/src/main.cpp b/assignment-server/src/main.cpp index a67db7b6c5..3d8b1846a7 100644 --- a/assignment-server/src/main.cpp +++ b/assignment-server/src/main.cpp @@ -8,19 +8,25 @@ #include #include +#include #include #include #include - const int ASSIGNMENT_SERVER_LISTEN_PORT = 7007; const int MAX_PACKET_SIZE_BYTES = 1400; +struct Assignment { + char birdIndex; +}; + int main(int argc, const char* argv[]) { + std::queue assignmentQueue; + sockaddr_in senderSocket; - char senderData[MAX_PACKET_SIZE_BYTES] = {}; + unsigned char senderData[MAX_PACKET_SIZE_BYTES] = {}; ssize_t receivedBytes = 0; UDPSocket serverSocket(ASSIGNMENT_SERVER_LISTEN_PORT); @@ -29,23 +35,32 @@ int main(int argc, const char* argv[]) { unsigned char assignmentPacket[numHeaderBytes + sizeof(char)]; populateTypeAndVersion(assignmentPacket, PACKET_TYPE_SEND_ASSIGNMENT); - int birdIndex = 1; - while (true) { if (serverSocket.receive((sockaddr*) &senderSocket, &senderData, &receivedBytes)) { - if (senderData[0] == PACKET_TYPE_REQUEST_ASSIGNMENT) { - // for now just send back a dummy assignment packet - - // give this assignee the next bird index - assignmentPacket[numHeaderBytes] = birdIndex++; - // wrap back to 0 if the bird index is 6 now - if (birdIndex == 6) { - birdIndex = 1; + int numHeaderBytes = numBytesForPacketHeader(senderData); + + if (senderData[0] == PACKET_TYPE_REQUEST_ASSIGNMENT) { + // grab the FI assignment in the queue, if it exists + if (assignmentQueue.size() > 0) { + Assignment firstAssignment = assignmentQueue.front(); + assignmentQueue.pop(); + + printf("Pulled an assignment with index %d\n", firstAssignment.birdIndex); + + assignmentPacket[numHeaderBytes] = firstAssignment.birdIndex; + + // send the assignment + serverSocket.send((sockaddr*) &senderSocket, assignmentPacket, sizeof(assignmentPacket)); } + } else if (senderData[0] == PACKET_TYPE_SEND_ASSIGNMENT) { + Assignment newAssignment; + newAssignment.birdIndex = *(senderData + numHeaderBytes); - // send the assignment - serverSocket.send((sockaddr*) &senderSocket, assignmentPacket, sizeof(assignmentPacket)); + printf("Created an assignment in the queue with %d\n", newAssignment.birdIndex); + + // add this assignment to the queue + assignmentQueue.push(newAssignment); } } }