/** * Check if there are messages from the connected nodes, update the details for the nodes and send it to the clients. */ void SpaceBroadcaster::update() { // ofLog(OF_LOG_NOTICE, "num connected clients %d ", getNumClients()); if (isServerActive()) { //for each client lets answer their message for (int i = 0; i < getNumClients(); i++) { string msg = receive(i); if (!msg.empty()) { handleMessage(i, msg); } } } }
uint32_t MSRAccessor::getNumInstances(){ uint32_t num_instances; getNumClients(connect, &num_instances); return num_instances; }
bool gmshLocalNetworkClient::run() { new_connection: setPid(0); // dummy pid, should be non-negative onelabGmshServer *server = new onelabGmshServer(this); int sock = server->LaunchClient(); if(sock < 0){ // could not establish the connection: aborting server->Shutdown(); delete server; return false; } Msg::StatusBar(true, "Running '%s'...", _name.c_str()); setGmshServer(server); while(1) { if(getExecutable().empty() && !CTX::instance()->solver.listen){ // we stopped listening to the special "Listen" client break; } // loop over all the clients (usually only one, but can be more if we // spawned subclients) and check if data is available for one of them bool stop = false, haveData = false; gmshLocalNetworkClient *c = 0; std::vector<gmshLocalNetworkClient*> toDelete; for(int i = 0; i < getNumClients(); i++){ c = getClient(i); if(c->getPid() < 0){ if(c == this){ // the "master" client stopped stop = true; break; } else{ // this subclient is not active anymore: shut down and delete its // server and mark the client for deletion GmshServer *s = c->getGmshServer(); c->setGmshServer(0); c->setFather(0); if(s){ s->Shutdown(); delete s; } toDelete.push_back(c); continue; } } GmshServer *s = c->getGmshServer(); if(!s){ Msg::Error("Abnormal server termination (no valid server)"); stop = true; break; } else{ int ret = s->NonBlockingWait(0.001, -1.); if(ret == 0){ // we have data from this particular client haveData = true; break; } else if(ret == 3){ // pass to the next client continue; } else{ // an error occurred stop = true; break; } } } for(unsigned int i = 0; i < toDelete.size(); i++){ removeClient(toDelete[i]); delete toDelete[i]; } // break the while(1) if the master client has stopped or if we encountered // a problem if(stop) break; // if data is available try to get the message from the corresponding // client; break the while(1) if we could not receive the message if(haveData && !c->receiveMessage(this)) break; // break the while(1) if the master client has stopped if(c == this && c->getPid() < 0) break; } // we are done running the (master) client: delete the servers and the // subclients, if any remain (they should have been deleted already). std::vector<gmshLocalNetworkClient*> toDelete; for(int i = 0; i < getNumClients(); i++){ gmshLocalNetworkClient *c = getClient(i); GmshServer *s = c->getGmshServer(); c->setGmshServer(0); c->setFather(0); if(s){ s->Shutdown(); delete s; } if(c != this){ if(c->getPid() > 0) Msg::Error("Subclient %s was not stopped correctly", c->getName().c_str()); toDelete.push_back(c); } } for(unsigned int i = 0; i < toDelete.size(); i++){ removeClient(toDelete[i]); delete toDelete[i]; } Msg::StatusBar(true, "Done running '%s'", _name.c_str()); if(getExecutable().empty()){ Msg::Info("Client disconnected: starting new connection"); goto new_connection; } return true; }