#include #include #include using namespace std; #include #include #include #include #include //Generator Includes #include #include "MessageGenerator.h" #include "SessionInfo.h" #include "vertex.h" #include "edge.h" #include "graph.h" #include "netHelper.h" //Receiver Includes #include "Receiver.h" #include "socket.h" #include "UDPserver.h" // receiver global junk UDPserver server(7000); // serverPort: need to send port data too Receiver receiver; std::string msg; // receiver global string to store received msg // end receiver global junk std::string destName = "rec"; std::string destDomain = "128.195.55.163"; // dunno what this does... std::string IP = "128.195.55.163"; // use real kphone's ip here! (e.g., .69) std::string port ="5060"; std::string protocol = "UDP"; std::string fromName = "snd"; std::string fromDomain = "128.195.55.196"; std::string callID = "snd@128.195.55.196"; int cuont = 1; // XXX: just kepeing track of number of iterations int numEdges = 0; Graph::Graph(int number_nodes) { graph = new Edge*[number_nodes]; for(int i=0; i < number_nodes; i++) graph[i] = new Edge[number_nodes]; this->size = 0; this->max_capacity = number_nodes; } int Graph::getSize() //get number of elements { return this->size; } int Graph::getCapacity() //get max number of nodes that can be fitted { return this->max_capacity; } int Graph::add(Edge *edge) { int row = -1; int col = -1; row = edge->getHead()->getId(); col = edge->getTail()->getId(); ////cout << "row: " << row << " col: " << col << endl; //graph[row * max_capacity + col] = edge; graph[row][col] = *edge; ////cout << "ID: " << graph[row][col].getHead()->getId(); //cout << "\n" << graph[row*max_capacity+col]->getId() << endl; //cout << graph[row*max_capacity+col]->getHead()->getId(); //cout << endl << edge << endl; //graph[row*max_capacity+col]->printInfo(); //cout << "\n" << graph << endl; size++; return 0; } void Graph::remove(Vertex *u, Vertex *v) { int row = u->getId(); int col = v->getId(); Edge *e = &graph[row][col]; graph[row][col] = -1; e->~Edge(); size--; return ; } void Graph::displayMatrix() { /*cout << "\n:: " << graph<< endl; cout << "yes\n"; cout << "ID: " << graph[1][2].getId() << "\n"; */ cout << "\t"; for(int i = 0; i < max_capacity; i++) cout << i << "\t\t"; cout << endl; for(int i = 0; i < max_capacity; i++) { cout << i << "\t"; for(int j = 0; j < max_capacity; j++) { //cout << graph[i*max_capacity+j]->getId() << "\t"; //cout << &graph[i][j] << " "; if(graph[i][j].getId() != -1) cout << "<" << graph[i][j].getId() << "," << graph[i][j].getHead()->getId() << "," << graph[i][j].getTail()->getId() << ">\t\t"; else cout << graph[i][j].getId() << "\t\t"; //graph[i][j].printInfo(); } cout << "\n\n"; } } void Graph::displayChild(int stateNumber) { int i = -1; int j = -1; if(stateNumber >= this->max_capacity) cout << "State Doesn't Exist"; cout << "SIZE: " << max_capacity << endl; for(int x = 0; x < max_capacity; x++) { i = stateNumber; j = x; if(graph[stateNumber][x].getId() != -1) { cout << "<" << graph[i][j].getId() << "," << graph[i][j].getHead()->getId() << "," << graph[i][j].getTail()->getId() << ">\t"; graph[i][j].printInfo(); cout << "\n"; } /* i = x; j = stateNumber; if(graph[x][stateNumber].getId() != -1) { cout << "<" << graph[i][j].getId() << "," << graph[i][j].getHead()->getId() << "," << graph[i][j].getTail()->getId() << ">\t\t"; } */ } } void Graph::printInfo() { cout << "GRAPH::< size = " << size << ", max = " << max_capacity << " >\n"; } ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// void printMsg() { bool hasMsg = false; // non-blocking network socket stream extraction is simulated // by using a polling function // you will have to check (server.hasMsg()) // this will wait 10 ms (configurable in socket.cpp) for a message // to arrive in the buffer // 10 ms should be more than enough time, since you really should only // be checking for messages after a message is supposed to be there // if (!server.hasMsg()), you must not do a server >> msg // or you will be I/O blocked until a message arrives in the // UDP buffer (which is possibly never) // server.hasMsg() returns 0 if there are no messages in the buffer // it returns -1 if an error occurred while polling // it's up to you to decide what to do if there are no messages. // here, I just cout an appropriate error if(server.hasMsg()) { server >> msg; hasMsg = true; } // else // msg = // "SIP/2.0 POO\nVia: \nFrom: \"snd\" ;tag=NULL\nCSeq: 1 INVITE\nCall-ID: NULL@NULL\nTo: \"rec\" ;tag=NULL\nContent-Length: 0\nUser-Agent: NULL\nContact: \"NULL\" \n"; // alternatively, we can set a dummy message as above // doesn't require testing if (server.hasMsg()) // this is probably less desirable, though if(hasMsg) { receiver.getMsg(msg); cout << "\n---------------------------------------------" << endl; // cout << "Parsed message:\n"; cout << "Message type: " << receiver.getMsgType(); cout << "\nCall ID: " << receiver.getCallID(); cout << "\nFrom IP address: " << receiver.getFromIP(); cout << "\nTo IP address: " << receiver.getToIP(); // cout << "\n\nActual message:\n"; // cout << msg; cout << "\n---------------------------------------------" << endl; } else { cout << "\n---------------------------------------------" << endl; cout << "No messages in the buffer!\n"; cout << "---------------------------------------------" << endl; } // test the GUI stuff // push Reject button if we discover a 180 ringing // int i = 0; // if(receiver.getMsgType() == "180 Ringing") // i = system("perl kphone_reject.pl"); } ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// void Graph::graph_auto() { string in, out; int total_edges = 20; int pstate[20]; int p = 0; int nextState = -1; int error = 0; int gui = 0; int irand = 0; int code = 0; //error code for gui int i = 0; bool hasMsg = false; int p1rand = 1, p2rand = 1; bool quit = false; //initialize the write file ofstream logWrite; logWrite.open ("log.txt"); int curState = 0; //generate seed //srand ( time(NULL) ); srand (100); do { //numEdges++; printf("\t! Total # of edges traversed: %d\n", numEdges++); if(curState == 0) { //system("xterm kphone &"); //system("sleep 3"); } error = 0; //error code = 0 gui = 0; //gui even 1 = yes, 0 = no //nextState = -1; //next possible state to reach p = 0; //possible nodes to goto out = "reset"; //hasMsg = false; p1rand = 0; p2rand = 0; for(i = 0; i < 20; i++) { pstate[i] = -1; } cout << "\tCurrent state (node): " << curState << endl; //find possible nodes cout << "\tPossible states (nodes): "; int curId; for(int x = 0; x < total_edges; x++) { curId = graph[curState][x].getId(); if(curId != -1) { //cout << curId << ", "; pstate[p] = graph[curState][x].getTail()->getId(); cout << pstate[p] << ", "; p++; } } //cout << "\n# States " << p << endl; for(int i = 0; i < 20; i++) cout << pstate[i] << ", "; irand = rand() % p + 1; nextState = pstate[irand - 1]; cout << "\n\tRandom number: " << irand; cout << "\n\tNext state: " << nextState; in = graph[curState][nextState].getInput(); // manual input /*cout << "\n\tInput: " << in << endl; cout << "\n\t::? "; cin >> in;*/ //generate the input part //send the input part //timer //don't worry because we are not testing for timeout //send message check & take action or not //cout << "\tSleeping for 2 seconds: zZzZz..." << endl; //system("sleep 2"); //cout << "\tDone sleeping." << endl; if(in == "offhook") { //int a; //cout << "ENTER CODE: OFFHOOK"; //cin>>out; gui = 1; cout << "\tPhone is OFFHOOK ...\n"; code = system("perl kphone_accept.pl"); if(code == -1) // error condition { cout << "\tError [OFF HOOK]\n"; error = code; } else cout << "\tOff Hooked!!!.\n"; out = "0"; system("sleep 1"); }/* else if(in == "cancel") { //cout << "ENTER CODE: CANCEL"; //cin>>out; gui = 1; cout << "CANCEL