/* * mainClient.cpp * * Gouverneur Th. - Cuisinier Gi. * * Main functions and call for the client */ #include #include #include #include #include #include #include using namespace std; void * thread_udp (void *); #ifdef TRU64 extern "C" { #endif void Handler (int ); int Arme (int, void (*)(int)); #ifdef TRU64 } #endif Sock_Client *client; int main(void) { pthread_t thUdp; int pid; pid = getpid(); std::string login, pass; cout << "Welcome in Inpres-Hollyday C++ Client." << endl; cout << endl << "Login: "; cin >> login; cout << "Password: "; cin >> pass; pthread_create(&thUdp, NULL, thread_udp, (void*) &pid); try { client = new Sock_Client(ADDRESS_REMOT, PORT_CHAMBRE); client->addCommand(Command("LOGIN", (void(*)(void*))ClientProtocol::cmdLogin, 1)); /* args: OK|NOK */ client->addCommand(Command("BROOM", (void(*)(void*))ClientProtocol::cmdBRoom, 5)); /* args: type de chambre, date arrivee, nombre de nuit, nom client */ client->addCommand(Command("CROOM", (void(*)(void*))ClientProtocol::cmdCRoom, 2)); /* args: num chambre, nom client */ client->addCommand(Command("LROOMS", (void(*)(void*))ClientProtocol::cmdLRooms, 0)); /* args: -- */ client->addCommand(Command("SUSP", (void(*)(void*))ClientProtocol::sendSusp, 0)); /* args: -- */ client->start(); Arme(SIGINT, Handler); // handle ctrl+c and clean propertly /* try to get authentificated */ Message m; m.numRequest = client->getMsgSent() + 1; m.ident = "Hotel California"; m.token = "LOGIN"; // login first m.args.Push(login); m.args.Push(pass); *client << m.serialize(); // send it through ! ClientProtocol::waiting = true; while(1) { if (ClientProtocol::auth && !ClientProtocol::waiting) { /* print menu and wait for user-input, * this will block all socket operation * and read will be delayed until next * user-action, in this protocol implementation, * we don't care of this, cause all request are * client-based. */ cout << endl << endl << "Menu: " << endl << "-----" << endl << endl; cout << "1. Book a room." << endl; cout << "2. Cancel a reservation." << endl; cout << "3. List booked room." << endl; cout << "4. Exit." << endl; cout << "\tYour choice: "; int c; cin >> c; cout << endl << endl; if (ClientProtocol::execOption(c, client)) // we should wait for an answer... ClientProtocol::waiting = true; } client->evolve(); } } catch (ClientProtocol::exErrAuth e) { cout << "Error, authentification incorrect, shutdown..." << endl; if (client) client->stop(); exit(-1); } catch (Exception e) { cout << e << endl; } } #ifdef TRU64 extern "C" { #endif int Arme (int s, void (*f)(int)) { struct sigaction a; a.sa_flags = 0; a.sa_handler = f; return sigaction(s, &a, NULL); } void Handler (int s) { if (client) client->stop(); cout << endl << "Received CTRL+C or SHUTDOWN message from server, exiting now..." << endl; exit(0); } #ifdef TRU64 } #endif void * thread_udp (void *p) { Sock_Udp *s = new Sock_Udp(ADDRESS_BIND, PORT_NOTIFY); s->start(); while (1) { string line; *s >> line; if (line == string("SHUTDOWN\n")) { int *qi = (int*)p; kill(*qi, SIGINT); return NULL; } } return NULL; }