/***********************************************************************/ /* Course: Computer Science 402a */ /* Assignment: Assignment 1 */ /* Instructor: Mark Giesbrecht */ /* Submitted by: Dan Fraser */ /* Student Number: 001219229 */ /* Due Date: Wednesday, February 16 */ /***********************************************************************/ /* cal_server.cc - Implementation for the user server functions */ /***********************************************************************/ #include "cal_server.h" void *server(void *data) { StreamSocket server; char buffer[1024]; int my_port = server.bind(); register_me(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[50]; 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(int my_port) { string message; char temp_int[50]; char buffer[1024]; char *temp; string result; StreamSocket nameserver; nameserver.connect(NAMESERVER,ns_port); sprintf(temp_int,"%d",my_port); message = "register " + my_name + " " + temp_int; cout << "sending registration: " << message << endl; nameserver.send(message.c_str(),message.length()); nameserver.receive(&buffer,sizeof(buffer)); cout << "registration completed, returned: " << buffer << endl; temp = strtok(buffer," "); result = temp; if (result != "OK") { perror("unable to register with nameserver"); exit(1); } temp = strtok(NULL," "); my_uid = atoi(temp); cout << "aha! my uid seems to be " << my_uid << endl; nameserver.disconnect(); }