int main() { struct sockaddr_in fsin; msg_t snd; msg_t rcv; unsigned short port = 13; int sock; unsigned int alen; int running[NUM_CLIENTS + 1]; int num_complete = 0; int client_count = 0; int i; int prod_not_done = 0; // Declare and initialize order size int order_size; srandom(time(NULL)); order_size = random() % 40001 + 10000; sock = serverUDPsock(port); printf("Initial Order Size: %d\n", order_size); printf("Server waiting...\n"); while (1) { alen = sizeof(fsin); if (recvfrom(sock, &rcv, sizeof(msg_t), 0, (SA *) &fsin, &alen) > 0) { switch (rcv.msg_code) { case 0 : printf("Received from Client: %d Message Code: 0 (Client Finished)\n", rcv.client_id); running[rcv.client_id] = 0; break; case 1 : client_count++; running[client_count] = 1; printf("Received Message Code: 1 initializing client: %d\n", client_count); // Initialize client's parameters client[client_count].id = client_count; client[client_count].cap = random() % 491 + 10; client[client_count].dur = random() % 401 + 100; client[client_count].num_iter = 0; client[client_count].total_dur = 0; // Compose and send a message to the client snd.param.cap = client[client_count].cap; snd.param.dur = client[client_count].dur; snd.param.id = client_count; printf("Sending to Client: %d Message Code: 1 (Initialize Client)\n", client_count); sendto(sock, (msg_t *) &snd, sizeof(msg_t), 0, (SA *) &fsin, alen); break; case 2 : printf("Received from Client: %d Message Code: 2 (Make Request)\n", rcv.client_id); if (order_size >= client[rcv.client_id].cap) { snd.msg_code = 1; snd.num_make = client[rcv.client_id].cap; printf("Sending to Client: %d Make Request granted for %d items\n", rcv.client_id, snd.num_make); order_size -= snd.num_make; sendto(sock, (msg_t *) &snd, sizeof(msg_t)+1, 0, (SA *) &fsin, alen); } else if (order_size < client[rcv.client_id].cap && order_size > 0) { snd.msg_code = 2; snd.num_make = order_size; printf("Sending to Client: %d Make Request granted for %d items\n", rcv.client_id, snd.num_make); order_size -= snd.num_make; sendto(sock, (msg_t *)&snd, sizeof(msg_t), 0, (SA *) &fsin, alen); } else { snd.num_make = 0; snd.msg_code = 0; printf("Sending to Client: %d Message Code: 0 Stop Request\n", rcv.client_id); sendto(sock, (msg_t *) &snd, sizeof(msg_t), 0, (SA *) &fsin, alen); } break; case 3 : printf("Received from Client: %d Message Code: 3 (Completed Iteration)\n", rcv.client_id); client[rcv.client_id].num_iter++; client[rcv.client_id].num_made += rcv.num_make; client[rcv.client_id].total_dur += client[rcv.client_id].dur; break; } printf("Order size: %d\n", order_size); prod_not_done = 0; for (i = 1; i < NUM_CLIENTS + 1; i++) { if (running[i] == 1) { prod_not_done = 1; } } if (!prod_not_done){ break; } } } //print information for (i = 1; i < NUM_CLIENTS + 1; i++) { printf("Client %d has completed %d total items made in %d time in %d iterations.\n", client[i].id, client[i].num_made, client[i].total_dur, client[i].num_iter); } order_size = 0; printf("Clearing order data\n"); printf("Current state is: MANUFACTURING\n"); exit(1); }
/* * NAME: main * * This method starts the server */ int main(int argc, char *argv[]) { struct sockaddr_in fsin; /* the from address of a client */ unsigned short port = 13 ; /* service name or port number */ char buf[MAXBUFLEN]; /* "input" buffer; any size > 0 */ msgBuf from_msg; /* message received */ msgBuf to_msg; /* message sent */ int sock; /* server socket */ time_t now; /* current time */ char timeStr[MAXBUFLEN]; /* time string */ unsigned int alen; /* from-address length */ int ii, order_size, parts_remaining, duration, lines_active, total_produced, done; //switch for the server switch (argc) { case 1: break; case 2: port = atoi( argv[1] ); printf("PORT %d\n", port); break; default: snprintf(buf, MAXBUFLEN , "usage: %s [port]\n" , argv[0] ); err_quit( buf ) ; } srandom(time(NULL)); sock = serverUDPsock(port); //Loop for the server while (1) { lines_active = 0; total_produced = 0; order_size = (random() % 1001) + 1000; parts_remaining = order_size; done = 0; printf("Order size: %d\n", order_size); //during all output while (!done) { alen = sizeof(fsin); if ( recvfrom( sock, (void *)&from_msg, sizeof(from_msg), 0, (SA *) &fsin, &alen ) < 0 ) err_sys( "recvfrom" ); /* New factory line */ if (from_msg.info.factory_ID == 1234 && lines_active < 5) { lines_active++; printf("LINES ACTIVE: %d\n", lines_active); to_msg.info.factory_ID = lines_active; to_msg.info.capacity = (random() % 41) + 10; to_msg.info.duration = (random() % 5) + 1; to_msg.info.produce = to_msg.info.capacity; sendto( sock , (void *) &to_msg, sizeof(to_msg), 0, (SA *) &fsin, alen ); printf ("Line %d started: Capacity: %d Duration: %d Produce: %d\n", lines_active, to_msg.info.capacity, to_msg.info.duration, to_msg.info.produce); } else { to_msg.info.factory_ID = from_msg.info.factory_ID; to_msg.info.capacity = from_msg.info.capacity; to_msg.info.duration = from_msg.info.duration; if (parts_remaining < 1) // if done { done = 1; } else if ((parts_remaining - from_msg.info.capacity) >= 0) // if this isn't last order { to_msg.info.produce = from_msg.info.capacity; parts_remaining -= from_msg.info.produce; total_produced += from_msg.info.capacity; sendto( sock , (void *) &to_msg, sizeof(to_msg), 0, (SA *) &fsin, alen ); } else //if this is last order { to_msg.info.produce = parts_remaining; total_produced += parts_remaining; parts_remaining = 0; sendto( sock , (void *) &to_msg, sizeof(to_msg), 0, (SA *) &fsin, alen ); } } //once program has finished all output if (done) { while (lines_active > 0) { to_msg.info.factory_ID = from_msg.info.factory_ID; to_msg.info.capacity = from_msg.info.capacity; to_msg.info.duration = from_msg.info.duration; to_msg.info.produce = 0; sendto( sock , (void *) &to_msg, sizeof(to_msg), 0, (SA *) &fsin, alen ); if ( recvfrom( sock, (void *)&from_msg, sizeof(from_msg), 0, (SA *) &fsin, &alen ) < 0 ) err_sys( "recvfrom" ); lines_active--; } } printf ("Parts Remaining: %d Total Produced: %d\n", parts_remaining, total_produced); } printf ("Total items produced: %d\n", total_produced); } }