/***********************************************************************/ /* Course: Computer Science 402a */ /* Assignment: Assignment 2 */ /* Instructor: Mark Giesbrecht */ /* Submitted by: Dan Fraser */ /* Student Number: 001219229 */ /* Due Date: Wednesday, February 16 */ /***********************************************************************/ /* nameserver.cc -- Implementation of the nameserver. */ /***********************************************************************/ #define RETURN_SIZE 1024 #define TEMPINT_SIZE 50 #include "nameserver.h" #include "StreamSocket.h" #include "HostInfo.h" #include #include #include #include vector Registry; map Names; /************************************************************************ Function Name: register Input Parameters: data - input string with name, ip, port separated by spaces Output Parameters: returns result string and UID Description: registers user, adds to local database, returns a UID. ************************************************************************/ char **register_1_svc(char **data, struct svc_req *req) { static char *result = (char *) malloc(sizeof(char) * RETURN_SIZE); string Name; char *ip; char *name; printf("starting registration\n"); name = strdup(strtok(*data," ")); Name = name; ip = strdup(strtok(NULL," ")); int port = atoi(strtok(NULL," ")); printf("name: %s, ip: %s, port: %d\n",name, ip, port); HostInfo hostinfo(name, ip, port); int UID = hostinfo.getUID(); Registry.insert(Registry.end(), hostinfo); Names[Name] = UID; sprintf(result,"OK %d",UID); cout << "register: "<< result << endl; return &result; } /************************************************************************ Function Name: unregister Input Parameters: data - string containing uid to delete Output Parameters: result string Description: removes user from local databases ************************************************************************/ char **unregister_1_svc(char **data, struct svc_req *req) { static char *result = (char *) malloc(sizeof(char) * RETURN_SIZE); char *uid = strdup(strtok(*data," ")); 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; strcpy(result,"ERROR"); return (char**)&result; } else { Names.erase(p->getRealname()); Registry.erase(p); cout << UID << " erased." << endl; strcpy(result,"OK"); return (char**)&result; } } /************************************************************************ Function Name: find Input Parameters: data - string with uid of user to find Output Parameters: result string with hostname and portnumber. Description: looks up user in database, returns hostname and port. ************************************************************************/ char **find_1_svc(char **data, struct svc_req *req) { static char *result = (char *) malloc(sizeof(char) * RETURN_SIZE); char *uid = strdup(strtok(*data," ")); int UID = atoi(uid); vector::iterator p = find(Registry.begin(), Registry.end(),UID); if ( p == Registry.end() ) { cout << "can't find " << UID << endl; strcpy(result,"ERROR"); return (char**) &result; } else { sprintf(result,"OK %s %d",p->getHostname().c_str(), p->getPort()); return (char**) &result; } } /************************************************************************ Function Name: findname Input Parameters: data - string containing name to find Output Parameters: result string with uid Description: returns result string with uid of found user. ************************************************************************/ char **findname_1_svc(char **data, struct svc_req *req) { static char *result = (char *) malloc(sizeof(char) * RETURN_SIZE); char *name = strdup(strtok(*data," ")); 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; strcpy(result,"ERROR"); return (char**)&result; } else { sprintf(result,"OK %d",UID); return (char**)&result; } }