--------------------------------------------------------- texnologia.cpp --------------------------------------------------------- #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include "mysql.h" //Epistrefei tin diefthinsi apo arrays pelatwn kai ypalilwn (kai texnikwn se sunexia) void *adressbook(int i); //declaration panw apo to include gt xrisimopoieitai i sunartisi mesa se mia methodo #include "texnologia.h" void emulateerror(pelaths *); //dinei se random xrono se random pelati ena random error void emulatecust(pelaths *); //kalei se olous tous pelates tin act method using namespace std; void *adb[2]; //krataei tis diefthinseis tou adressbook int main(int argc, char *argv[]) { pelaths customers[CUST_NO]; ypalilos ypal[EMPL_NO]; adb[0] = customers; //init ta global pointers pou kratan tis adb[1] = ypal; //diefthinseis ap ta arrays for (;;) { emulateerror(customers); //Ena error emulatecust(customers); //act oloi oi pelates } return EXIT_SUCCESS; } //epistrefei tin diefthinsi apo 0:pelates 1:ypalilous void *adressbook(int i){ return adb[i]; } void emulateerror(pelaths customers[CUST_NO]) { time_t t1,t2; int i, tmp; (void)time(&t1); //pernoume tin wra tmp = rand()%10; //se posa sec tha parei o pelaths (ews 10) for ((void)time(&t2); (int) t2-t1 < tmp; (void)time(&t2)); //perimenoume na ftasei o xronos //eftase i ora gia ena error tmp = rand()%(CUST_NO-1); //dialegoume random customer i = rand()%3+1; //dialegoume random error - excluding NOERROR customers[tmp].haveerror = (errortype)i; //topo8etoume to provlima ston pelati } void emulatecust(pelaths customers[CUST_NO]) { int i; for (i=0; i < CUST_NO; i++) customers[i].act(); } --------------------------------------------------------- --------------------------------------------------------- texnologia.h --------------------------------------------------------- #ifndef TEXNOLOGIA_H #define TEXNOLOGIA_H #define CUST_NO 10 #define EMPL_NO 1 extern void *adressbook(int); using namespace std; //Typos/kathgoria provlimatos enum errortype { NOERROR = 0, PSTN = 1, ISDN = 2, ADSL = 3, INTERNET = 4}; //Structure pou krataei ena record gia tin vasi dedomenwn typedef struct{ int id; string onoma; unsigned long long tel; errortype category; bool condition; }record; class ypalilos; //declaration class pelaths; //declaration class pelaths { public: //Constructors/Destructor pelaths():haveerror((errortype)0), reported(0), found(NULL){} pelaths(string on, unsigned long long til): haveerror((errortype)0), reported(0), onoma(on), phone_num(til),found(NULL){} ~pelaths(){} //public methodoi void act(); //Kanei ton pelati na drasei, elegxei an exei provlima string get_onoma(){return onoma;} //epistrefei to onoma tou pelati errortype get_error(){return haveerror;} //epistrefei ton typo toy provlimatos //public idiotites unsigned long long phone_num; //arithmos tilefonou pelath errortype haveerror; //typos problimatos (0=NOERROR) private://private methodoi bool findhelp(); //psaxnei gia diathesimo upalilo kai ton kalei void talk(); //kanei tin sinomilia //private idiotites int reported; //an exei dilothei i vlavi exei to ID string onoma; //onoma pelath ypalilos *found; //pointer ston upalilo pou vrethike gia eksipiretisi }; class ypalilos { /* ypothetoume oti leitourgei enas kai mono ypalilos, tou opoiou to interface tha fenetai * tha fenetai stin othoni. Gia skopous realismou, i efthini tis ilopoiisis twn diadikasiwn * afinetai ston xristi tou programmatos. Autos einai ypefthinos na kanei tis diadikasies, * aftos einai ipefthinos na pei An epidiorthothike i vlavi i oxi (aneksartita apo to an * ontws epidiorthothike). O pelaths tha vasizete stis apantiseis tou upalilou gia to * an i vlavi tou epidiorthothike i graftike stin vasi. * O upalilos tha exei diathesimes energies MONO otan ton "paroun tilefono". */ public: //Constructor/Destructor ypalilos(); ~ypalilos(){} int answer(pelaths*,int); //H "main" tou ypaliliou bool getcond(){return busy;} //Katastasi upalilou (busy i oxi) private://Private methodoi (diaxirisi vasis, ginete mono ap ton upalilo) void db_add(){cout << "add db" <answer(this, reported); //"Klisi" ston upalilo //Meta to peras tis klisis... if (tmp) //An eixe "thetiki" apantisi, tote analoga me tin erwtisi ananeonoume { if (reported) //an to provlima itan dilomeno, tote to "nai" simene oti epidiorthothike { haveerror = (errortype)0; reported = 0; return; } else //an den itan, tote simenei oti dilothike kai to tmp==ID tou aitimatos { reported = tmp; return; } } //An itan arnitiki apantisi, den epireazetai tipota return; } //Constructor initialising to NULL ypalilos::ypalilos():busy(0) { data.id = 0; data.onoma = ""; data.tel = 0; data.category = NOERROR; data.condition = 0; } /*H answer(pelaths*) gia enan ypalilo, einai i kiria diadikasia pou ulopoiei to interface *me ton xristi tou programatos kai tin epikinwnia me ton ypalilo. Energopoieitai kata tin *klisi ston ypalilo, dinei dinatotites opws mi apantisi, erwtiseis ston ypalilo, diaxeirisi *tis vasis ktl. Analoga to erwtima, i apantisi simenei: *1: se dilosi vlavis- Oti i vlavi dilothike kai i apantisi dinei ton kodiko tis *2: se erwtisi epilisis- I katafatiki apantisi simenei oti epilithike *Prosoxi kathws oi apantiseis DEN EKSARTONTAI ap tin pragmatikotita, gi afto prepei na didontai *ypefthina kai me prosoxi */ int ypalilos::answer(pelaths *cust, int id) { char rep; //Epilogi ipalilou int ans = 1;//Default katafatiki apantisi string tmp; //Ksekinaei to interface - Emfanisi klisis kai stoixeiwn cout << "-Pelatis me noumero: " << cust->phone_num << " Kalei, Apantisi?(y/n) :"; cin >> rep; if (rep == 'n') //Epilogi na min to sikosoume { cout << "Connection with Customer " << cust->phone_num << " refused" << endl << "Line idle..." << endl; return 0; //Epistrefete arnitiki apantisi } busy = 1; //An apantisoume, vazoume tin katastasi busy. //Se periptosi paralilwn diergasiwn tha eixe noima (leme twra..) cout << "Connection with Customer " << cust->phone_num << " established" << endl; if (id) //An dothike >0 simenei oti o pelatis rwtaei tin katastasi tou aitimatos tou gia ID=id cout << "O pelatis rwtaei an h vlavi me kwdiko " << id << " exei epilithei" << endl; else //Allios simenei oti den exei dilosei kapoia vlavi kai oti afto paei na kanei.. cout << "O pelatis thelei na dilwsei nea vlavi" << endl; //Efoson deiksame tin erwtisi tou pelati, proxwrame ston rolo tou ypalilou... //Deixnoume tis diathesimes energies (mporei na praksei asxeta me to ti thelei o pelatis) rep = menu(id); while (rep != '0'){ switch(rep){ case '1':{//Erwtisi tou onomatos tou kai egrafi sto record data.onoma = cust->get_onoma(); cout << " -" << data.onoma << endl; break;} case '2':{//Erwtisi provlimatos data.category = cust->get_error(); cout << " -" << data.category << endl; break;} case '3':{//Eggrafi record stin vasi db_add(); //klisi methodou break;} case '4':{//Diagrafi aitimatos me ID apo tin vasi db_del(id); break;} case '5':{//Anagnosi aitimatos me ID apo tin vasi db_rea(id); break;} case '6':{//Allagi aitimatos me ID apo tin vasi db_cha(id); break;} case '7':{//Ektiposi trexontos record cout << data.id << endl << data.onoma << endl << data.tel << endl << data.category << endl << data.condition << endl; break;} case '8':{//Katafatiki apantisi ston pelati cout << "Replying positively to " << cust->phone_num << endl << "Connection closed" << endl << "Line idle..." << endl; busy = 0; if (id == 0) ans == data.id; //reply the ID of record cout << "Giving ID to customer :" << ans << ". Is this correct? (y/n) " ; cin >> rep; //Teliki epiveveosi if (rep == 'y') return ans;break; cout << "Give the correct ID to say : " ; while (ans <= 0) cin >> ans; //Elegxos gia akuri timi return ans;break; } case '9':{//Arnitiki apantisi ston pelati cout << "Replying negative to " << cust->phone_num << endl << "Connection closed" << endl << "Line idle..." << endl; busy = 0;return 0;break; } } rep = menu(id); //Show menu with actions } //Kleisimo tilefonou cout << "Connection terminated" << endl << "Line idle..." << endl; busy = 0;return 0; } char ypalilos::menu(int id) { //Emfanisi tou menou ston ipalilo char tmp; cout << "\tTi tha thelate na kanete?" << endl; cout << "\t1: Erwtisi onomatos kai simplirosi eggrafis" << endl; cout << "\t2: Erwtisi provlimatos kai simplirosi eggrafis" << endl; cout << "\t3: Prosthiki eggrafis stin vasi dedomenwn" << endl; cout << "\t4: Diagrafi apo vasi dedomenwn (tis eggrafis ID:" << id << ")" << endl; cout << "\t5: Anagnosi apo vasi dedomenwn (tis eggrafis ID:" << id << ")" << endl; cout << "\t6: Allagi apo vasi dedomenwn (tis eggrafis ID:" << id << ")" << endl; cout << "\t7: Emfanisi trexousas eggrafis" << endl; cout << "\t8: Thetiki apantisi ston pelati" << endl; cout << "\t9: Arnitiki apantisi ston pelati" << endl; cout << "\t0: Klisimo tilefonou" << endl; cin >> tmp; return tmp; } #endif