revamp assignment server to accept requests from webpage

This commit is contained in:
Stephen Birarda 2013-07-15 11:47:54 -07:00
parent a851f32d3e
commit 855bcb3753

View file

@ -8,19 +8,25 @@
#include <arpa/inet.h>
#include <fstream>
#include <queue>
#include <PacketHeaders.h>
#include <SharedUtil.h>
#include <UDPSocket.h>
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<Assignment> 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);
}
}
}