int sendStages() { unsigned int i, stage; map<int,string>::iterator it; for (it=coreToHost.begin();it!=coreToHost.end();it++) { if (verbose) cout << "Sending stages to " << it->second << endl; int s = connectionForCore[it->first]; unsigned int stages = (unsigned int)stagesFor[it->first].size(); ReassignHeader header; header.stages = htonl(stages); sendThing(header,s); for (stage=0; stage<stages;stage++) { ReassignStage stg; stg.count = htonl(stagesFor[it->first][stage].size()); sendThing(stg,s); vector<ReassignEntry>& entries=stagesFor[it->first][stage]; for (i=0;i<entries.size();i++) sendThing(entries[i],s); } ReassignCmd response; if (!receiveCmd(response,s) || response.cmd != OK) { cerr << "Bad response from " << it->first <<": "<<response.cmd << endl; return false; } } return true; }
int InterpreterDBG::receiveCmd() { if(clientsock < 0) { return CMDContinue; } string cmd = receiveIncomingData(); int ret; if(cmd == "step_into") { ret = CMDStepInto; } else if(cmd == "step_over") { ret = CMDStepOver; } else if(cmd == "step_out") { ret = CMDStepOut; } else if(cmd == "continue") { ret = CMDContinue; } else if(cmd.find("breakpoint") != string::npos) { processBreakpointCMD(cmd); ret = receiveCmd(); } else { //cerr << PACKAGE << ": comando desconhecido: \"" << cmd << "\"" << endl; ret = CMDStepInto; } return ret; }
int InterpreterDBG::nextCmd(int line, Variables& v, list<pair<string, int> >& stk) { sendStackInfo(stk); sendVariables(v.getGlobals(), stk, true); sendVariables(v.getLocals(), stk, false); return receiveCmd(); }
void* opimServer::acceptClients( void* ptr ) { Client* client; int sock; sockaddr_in theirAddr; int cmd; while ( true ) { sock = listenFor( _socket, theirAddr ); cout << "Got connection from " << inet_ntoa( theirAddr.sin_addr ) << " on socket " << sock << "!\n" << flush; if ( sock != -1 ) { client = new Client( _db, sock, theirAddr ); cmd = receiveCmd( client->socket() ); switch ( cmd ) { case login: if ( client->login() ) //login successful? { pthread_mutex_lock( &_clientsMutex ); //need to tell the other clients that someone has logged in. updateClientsLists( addClient, client ); _clients.push_back( client ); client->setSendSocket( listenFor( _socket, theirAddr ) ); updateClientsLists( fullList, client ); _dt->startThread( boost::bind( &opimServer::handleClient, this, _1 ), (void*)client ); pthread_mutex_unlock( &_clientsMutex ); cout << "Started listening to client " << client->ip() << "!" << endl << flush; } else delete client; break; case reg: client->registerMe(); break; default: cout << "Invaild startup command from " << client->ip() << ": " << cmd << endl << flush; } } } return 0; }
int doStage(int stage) { map<int,string>::iterator it; ReassignCmd start, response; start.cmd=START_STAGE; start.arg = stage; vector<int> sockets; for (it=coreToHost.begin();it!=coreToHost.end();it++) { int s = connectionForCore[it->first]; sockets.push_back(s); sendCmd(start,s); } for (it=coreToHost.begin();it!=coreToHost.end();it++) { int s = connectionForCore[it->first]; if (!receiveCmd(response,s) || response.cmd != OK) return false; } return true; }
int main(int argc, char* argv[]) { char cmdBuf[SOCK_BUFF_SIZE]; printf("Quantum Test\nQEP %s\nQF %s, QF/Linux port %s\n", QEP_getVersion(), QF_getVersion(), QF_getPortVersion()); QF_init(); QF_psInit(subscrSto, Q_DIM(subscrSto)); QF_poolInit(poolArray, sizeof(poolArray), sizeof(GenEvt)); applicationStart(QSIZE); for (;;) { // Get the incoming string command from the dmsTerminal or the GUI receiveCmd(cmdBuf); // Attempt to parse the command string for IMPL or ACTIVE calls char tmpBuf[USER_ENTRY_SIZE]; char smName[USER_ENTRY_SIZE]; char attrName[USER_ENTRY_SIZE]; char valStr[USER_ENTRY_SIZE]; // scan for IMPL['SM'].set("attr", True/False), allowing blanks // IMPL['trans'].set('guard', False) int cnt = sscanf(strcpy(tmpBuf, cmdBuf), "IMPL[%*['\"]%[^'\"]%*['\"]].set(%*['\"]%[^'\"]%*['\"],%[^)])", smName, attrName, valStr); if (cnt > 0) { // found an IMPL attribute setter! setGuardAttribute(smName, attrName, valStr); continue; } char *word; word = strtok(cmdBuf, " "); // We assume the first word contains the signal that is to be published, // and the remaining words are data to be used to populate the event. GenEvt *event; int signal = strtoul(word, NULL, 10); if (signal == DURING) { QF_tick(); QF_publish( Q_NEW(QEvent, DURING) ); } else { event = Q_NEW(GenEvt, signal); // Loop through the remaining words and populate the event int i = 0; do { word = strtok('\0', " "); if (word) { Q_ASSERT(i<EVENT_SIZE); event->data[i] = strtoul(word, NULL, 16); } i = i + 1; } while (word); QF_publish((QEvent *)event); } QF_run(); } }
void* opimServer::handleClient( void* arg ) { Client* client = (Client*)arg; int cmd; while ( true ) { cmd = receiveCmd( client->socket() ); if ( cmd == discon || cmd == disconFlood ) //client disconnected? { pthread_mutex_lock( &_clientsMutex ); for ( clientIt it = 0 ; it != _clients.size() ; ++it ) //find the client in the clients list and remove them. { if ( _clients[it]->id() == client->id() ) { _clients.erase( _clients.begin() + it ); break; } } for ( convoIt it = 0 ; it != _convos.size() ; ++it ) //find and remove all conversations containing the disconnecting client. { if ( _convos[it]->inConvo( client ) ) { _convos.erase( _convos.begin() + it ); delete _convos[it]; --it; } } updateClientsLists( removeClient, client ); cout << client->name() << " disconnects\n" << flush; cout << _clients.size() << " client(s) now connected.\n" << flush; delete client; pthread_mutex_unlock( &_clientsMutex ); break; } else if ( cmd == startConvo ) //client wants to start a conversation with another client? { sendCmd( client->socket(), ack ); int clientId = atoi( receiveMessage( client->socket() ).c_str() ); cout << client->name() << " wants to talk to client " << clientId << endl << flush; if ( clientId != client->id() ) //not starting a conversation with themselves? { Query q( *_db ); q.get_result( makeSql( "select 1 from connected_users where user_id = ?", longToString( clientId ).c_str() ) ); if ( q.num_rows() ) //other client online? { pthread_mutex_lock( &_clientsMutex ); Client* otherClient = getClientById( clientId ); if ( otherClient != 0 ) //client found in clients list? { if ( notInAConvo( client, otherClient ) ) { convo* c = new convo(); c->addClient( client ); c->addClient( otherClient ); _convos.push_back( c ); sendCmd( otherClient->sendSock(), startConvo ); sendCmd( client->socket(), ack ); sendMessage( client->socket(), otherClient->ip() ); cout << "Conversation started between " << client->name() << " and " << otherClient->name() << endl << flush; } else { sendCmd( client->socket(), nak ); sendMessage( client->socket(), "Already in a conversation" ); cout << client->name() << " already in conversation with " << otherClient->name() << endl << flush; } } else { sendCmd( client->socket(), nak ); sendMessage( client->socket(), "Client is not online" ); cout << "Other client is not online\n" << flush; } pthread_mutex_unlock( &_clientsMutex ); } else { sendCmd( client->socket(), nak ); sendMessage( client->socket(), "Client is not online" ); cout << client->name() << " other client is not online\n" << flush; } q.free_result(); } else { sendCmd( client->socket(), nak ); sendMessage( client->socket(), "You cannot start a conversation with yourself" ); cout << client->name() << " tries to start a conversation with themself\n" << flush; } } else { sendCmd( client->socket(), nak ); sendMessage( client->socket(), "Command not recongnized" ); cout << client->name() << " send unrecongnized command: " << cmd << endl << flush; } } return 0; }