/***********************************************************************/ /* Course: Computer Science 402a */ /* Assignment: Assignment 1 */ /* Instructor: Mark Giesbrecht */ /* Submitted by: Dan Fraser */ /* Student Number: 001219229 */ /* Due Date: Wednesday, February 16 */ /***********************************************************************/ /* nameserver.cc -- Implementation of the nameserver. */ /***********************************************************************/ #include "StreamSocket.h" #include "HostInfo.h" #include #include #include #include int main() { char buf[1024]; StreamSocket foo; int my_msg; int bleah; vector Registry; map Names; if((bleah = foo.bind()) == 0) { perror("whine, can't get my favourite port"); } else { cout << "got port "<< bleah<< endl; } foo.listen(5); while ((my_msg = foo.accept())) { cout <<"accepted connection from " << foo.remoteHost() << endl; while (foo.receive(my_msg, &buf, 1024) > 0) { char *hostname, *name, *command; string Name, Command; // yes, yes, I know, there's no error checking here. :) command = strdup(strtok(buf," ")); Command = command; free(command); free(hostname); printf("command: %s\n",Command.c_str()); if (Command == "register") { string result; char int_temp[50]; name = strdup(strtok(NULL," ")); int port = atoi(strtok(NULL," ")); Name = name; HostInfo hostinfo(name, foo.remoteHost(), port); int UID = hostinfo.getUID(); Registry.insert(Registry.end(), hostinfo); Names[Name] = UID; sprintf(int_temp,"%d",UID); result = "OK "; result += int_temp; cout << "register: "<< result << endl; foo.send(my_msg,result.c_str(),result.length()); } if (Command == "unregister") { char *uid = strdup(strtok(NULL," ")); int UID = atoi(uid); vector::iterator p = find(Registry.begin(), Registry.end(),UID); if ( p == Registry.end() ) { cout << "can't find " << UID <<" to unregister" << endl; foo.send(my_msg,"ERROR",sizeof("ERROR")); } else { Names.erase(p->getRealname()); Registry.erase(p); cout << UID << " erased." << endl; foo.send(my_msg,"OK",sizeof("OK")); } } if (Command == "find") { char *uid = strdup(strtok(NULL," ")); int UID = atoi(uid); vector::iterator p = find(Registry.begin(), Registry.end(),UID); if ( p == Registry.end() ) { cout << "can't find " << UID << endl; foo.send(my_msg,"ERROR",sizeof("ERROR")); } else { char int_temp[50]; sprintf(int_temp," %d",p->getPort()); string success = "OK " + p->getHostname() + int_temp; cout << success << endl; foo.send(my_msg,success.c_str(),success.length()); } } if (Command == "findname") { char *name = strdup(strtok(NULL," ")); string Name = name; cout << "trying to return uid for " << Name << "." << endl; int UID = Names[Name]; cout <<" got " << UID << " from map." << endl; if ( UID <= 0 ) { cout << "can't find " << UID << endl; foo.send(my_msg,"ERROR",sizeof("ERROR")); } else { char int_temp[50]; sprintf(int_temp,"%d",UID); string success = "OK "; success += int_temp; cout << success << endl; foo.send(my_msg,success.c_str(),success.length()); } } } foo.disconnect(my_msg); } }