/***********************************************************************/ /* Course: Computer Science 402a */ /* Assignment: Assignment 3 */ /* Instructor: Mark Giesbrecht */ /* Submitted by: Dan Fraser */ /* Student Number: 001219229 */ /* Due Date: Wednesday, February 16 */ /***********************************************************************/ /* cal_server.cc - Implementation for the user server functions */ /***********************************************************************/ // interface details in cal_server.h #define REQUEST_SIZE 1024 #define TEMPINT_SIZE 50 #include "cal_server.h" void *server(void *data) { StreamSocket server; char buffer[REQUEST_SIZE]; int my_port = server.bind(); register_me(server.localHost(), my_port); server.listen(5); while (1) { int rsock = server.accept(); server.receive(rsock, &buffer, sizeof(buffer)); handle_message(server,rsock,buffer); } pthread_exit(NULL); return NULL; } void handle_message(StreamSocket server, int rsock, char *buffer) { string Command, reply; char *command; char *my_buffer = strdup(buffer); command = strtok(my_buffer," "); Command = command; if (Command == "meeting") { reply = meeting(buffer); server.send(rsock, reply.c_str(), reply.length()); } if (Command == "confirm") { reply = confirm(buffer); server.send(rsock, reply.c_str(), reply.length()); } if (Command == "cancel") { reply = cancel(buffer); server.send(rsock, reply.c_str(), reply.length()); } if (Command == "cancel_temp") { reply = cancel_temp(buffer); server.send(rsock, reply.c_str(), reply.length()); } free(my_buffer); } string meeting(char *buffer) { char temp_int[TEMPINT_SIZE]; string reply; vector start_times; Meeting *temp_meeting; int length, o_uid, meeting_id; char *start_time; o_uid = atoi(strtok(NULL," ")); length = atoi(strtok(NULL," ")); temp_meeting = new Meeting(o_uid, length); temp_meeting->addAttendee(o_uid); temp_meeting->makeTentative(); cout << "got length: " << length << endl; int rc = pthread_mutex_lock(&busy_mutex); if (rc) { perror("pthread_mutex lock"); pthread_exit(NULL); } while ((start_time = strtok(NULL," ")) != NULL) { if (busy.check(atoi(start_time),length)) { busy.allocate(temp_meeting, atoi(start_time), length); start_times.insert(start_times.end(),atoi(start_time)); // cout << "got valid start time: " << start_time << endl; } else { // cout << "got conflicting start time: " << start_time << endl; } } rc = pthread_mutex_unlock(&busy_mutex); if (rc) { perror("pthread_mutex unlock"); pthread_exit(NULL); } reply = "OK"; for (vector::const_iterator i = start_times.begin(); i != start_times.end(); i++) { sprintf(temp_int," %d",*i); reply += temp_int; } return reply; } string cancel(char *buffer) { string reply; int start_time; Meeting *temp_meeting; strtok(buffer," "); // eat the command start_time = atoi(strtok(NULL, " ")); int rc = pthread_mutex_lock(&busy_mutex); if (rc) { perror("pthread_mutex lock"); pthread_exit(NULL); } temp_meeting = busy.getMeeting(start_time); if (temp_meeting == NULL) { perror("found a null meeting... that's bad"); reply = "ERROR"; return reply; } if (!busy.deallocate(temp_meeting)) { perror("unable to deallocate temp meetings"); reply = "ERROR"; return reply; } rc = pthread_mutex_unlock(&busy_mutex); if (rc) { perror("pthread_mutex unlock"); pthread_exit(NULL); } cout << "Server message: MEETING DELETED! Check calendar!" << endl; reply = "OK"; return reply; } string cancel_temp(char *buffer) { string reply; int start_time; char *temp_string; Meeting *temp_meeting; int rc = pthread_mutex_lock(&busy_mutex); if (rc) { perror("pthread_mutex lock"); pthread_exit(NULL); } strtok(buffer," "); // eat the command while((temp_string = strtok(NULL, " ")) != NULL) { start_time = atoi(temp_string); temp_meeting = busy.getMeeting(start_time); if (temp_meeting == NULL) { perror("found a null meeting... that's bad"); reply = "ERROR"; return reply; } if (temp_meeting->isTentative()) { if (!busy.deallocate(temp_meeting)) { perror("unable to deallocate temp meetings"); reply = "ERROR"; return reply; } else { cout << "deleting meeting" << endl; } } else { } } rc = pthread_mutex_unlock(&busy_mutex); if (rc) { perror("pthread_mutex unlock"); pthread_exit(NULL); } reply = "OK"; return reply; } string confirm(char *buffer) { string reply; int start_time; Meeting *temp_meeting; strtok(buffer," "); // eat the command start_time = atoi(strtok(NULL, " ")); int rc = pthread_mutex_lock(&busy_mutex); if (rc) { perror("pthread_mutex lock"); pthread_exit(NULL); } temp_meeting = busy.getMeeting(start_time); if (temp_meeting == NULL) { perror("found a null meeting... that's bad"); reply = "ERROR"; return reply; } if (!busy.deallocate(temp_meeting)) { perror("unable to deallocate temp meetings"); reply = "ERROR"; return reply; } temp_meeting->makeConfirmed(); temp_meeting->setStart() = start_time; busy.allocate(temp_meeting,start_time,temp_meeting->getLength()); rc = pthread_mutex_unlock(&busy_mutex); if (rc) { perror("pthread_mutex unlock"); pthread_exit(NULL); } cout << "Server message: NEW MEETING ADDED! Check calendar!" << endl; reply = "OK"; return reply; } void register_me(string my_ip, int my_port) { // char request[REQUEST_SIZE]; char *request = (char*) malloc(sizeof(char) * REQUEST_SIZE); int *result; rpc_hostInfo hi; CLIENT *clnt; clnt = clnt_create(nameserver_host.c_str(), NAMESERVER, DATE_VERS, "tcp"); if (clnt <= 0) { perror("clnt_create"); exit(1); } printf("registering with nameserver\n"); hi.name = my_name.c_str(); hi.host = my_ip.c_str(); hi.port = my_port; hi.uid = 0; // auto-assign a uid hi.gossip = 0; result = register_1(&hi, clnt); printf("back from request with: %d\n", *result); if (*result < 0) { perror("unable to register with nameserver"); exit(1); } my_uid = *result; cout << "aha! my uid seems to be " << my_uid << endl; clnt_destroy(clnt); }