/*! * \brief Mouvement d'amelioration de la solution : deplacement d'un sommet * * \param ind1 : indice du client a inserer * \param tour1 : tournee du client a inserer * \param ind2 : indice du client avant ou apres lequel on insere * \param tour2 : tournee du client avant ou apres lequel on insere * \param instance : instance sur laquelle on travaille. * \param avant : indique si l'on doit inserer avant ou apres ind2 * * \return 0 si c'est ok -1 sinon */ int Solution::shift(unsigned ind1, unsigned tour1, unsigned ind2, unsigned tour2, Instance * instance, bool avant) { sommet_t ** vect = instance->getVect(); int res = 0; int tmp; if (_load[tour2] + vect[ind1]->_demande <= instance->getCapacityV()) { // on recupere l'indice de tour1 tmp = delClient(ind1, vect, tour1); // s'il a ete modifie et que la deuxieme tournee est apres la premiere, la deuxieme tournee aussi est en dessous if (tmp != (int)tour1 && tour2 > tour1) --tour2; if (avant) { if (_first[tour2] != ind2) { if (faisable(instance, ind2, std::max(_depart[_pred[ind2]], vect[ind1]->_debut), avant)) { insertClient(ind1, ind2, tour2, avant, vect); } else res = -1; } else if (faisable(instance, ind2, vect[ind1]->_debut, avant)) { insertClient(ind1, ind2, tour2, avant, vect); } else res = -1; } else { if (faisable(instance, ind2, std::max(_depart[ind2], vect[ind1]->_debut) + vect[ind1]->_duree, avant)) { insertClient(ind1, ind2, tour2, avant, vect); } else res = -1; } } else res = -1; return res; }
ServerHeartbeat::ServerHeartbeat(zmq::context_t& context, const HeartbeatSettings& settings) : msFrequency_(settings.msRate), msPollingFrequency_(settings.msPollRate), msTimeout_(settings.msTimeout), running_(true) { // Create the router's socket std::unique_ptr<zmq::socket_t> heartbeat(new zmq::socket_t(context, ZMQ_PAIR)); heartbeat->connect(SERVER_HB_SOCKET_ADDR.c_str()); router_.add_socket(SocketID::HEARTBEAT, heartbeat); // Specify functionality auto onRcvHELLO = [&] (const Message&m) { insertClient(m, clients_, lastHeartbeats_);}; auto onRcvGOODBYE = [&] (const Message&m) { deleteClient(m, clients_, lastHeartbeats_);}; auto onRcvHEARTBEAT = [&] (const Message&m) { receiveHeartbeat(m, lastHeartbeats_);}; auto timeouts = [&] () { monitorTimeouts(clients_, lastHeartbeats_, router_, msTimeout_);}; auto send = [&] () { sendHeartbeats(clients_, lastSendTime_, router_, msFrequency_);}; // Bind to router router_(SocketID::HEARTBEAT).onRcvHELLO.connect(onRcvHELLO); router_(SocketID::HEARTBEAT).onRcvGOODBYE.connect(onRcvGOODBYE); router_(SocketID::HEARTBEAT).onRcvHEARTBEAT.connect(onRcvHEARTBEAT); router_(SocketID::HEARTBEAT).onPoll.connect(timeouts); router_(SocketID::HEARTBEAT).onPoll.connect(send); router_(SocketID::HEARTBEAT).onFailedSend.connect(failedSendServer); LOG(INFO)<< "Server heartbeat initialising"; lastSendTime_ = std::chrono::high_resolution_clock::now(); router_.start(msPollingFrequency_, running_); // milliseconds between polling loops }
bool IpcSocketDispatcher::registerListener(IDispatcherClient * client) { LOG4CPLUS_TRACE_METHOD(mLogger, __PRETTY_FUNCTION__); LOG4CPLUS_INFO(mLogger, "registering listener to addr: " + client->getAddress()); insertClient(client); MutexLocker lock(mListenersMutex); tListenersMap::iterator iter = mListeners.find(client->getAddress()); if (iter != mListeners.end()) { return false; } mListeners[client->getAddress()] = client; return true; }
tPipeWriterPtr IpcSocketDispatcher::connectToServer(IDispatcherClient * client, DirectionID& receivedDirectionID) { LOG4CPLUS_TRACE_METHOD(mLogger, __PRETTY_FUNCTION__); LOG4CPLUS_INFO(mLogger, "trying to connect to addr: " + client->getAddress()); receivedDirectionID = -1; insertClient(client); IDispatcherClient * server = NULL; { MutexLocker lock(mListenersMutex); tListenersMap::iterator iter = mListeners.find(client->getAddress()); if (iter != mListeners.end()) { LOG4CPLUS_INFO(mLogger, "Found server for address: " + client->getAddress()); server = (*iter).second; } } if (server == NULL) { return tPipeWriterPtr(); } receivedDirectionID = server->onClientConnected(client); return server->getPipeWriter(); }
/*! * \brief Mouvement d'amelioration de la solution : echange de deux sommets * * \param ind1 : indice du premier client * \param tour1 : tournee du premier client * \param ind2 : indice du deuxieme client * \param tour2 : tournee du deuxieme client * \param instance : instance sur laquelle on travaille. * * \return 0 si c'est ok -1 sinon */ int Solution::swap(unsigned ind1, unsigned tour1, unsigned ind2, unsigned tour2, Instance * instance) { sommet_t ** vect = instance->getVect(); int res = 0; //On regarde si ça colle au niveau des charges if (_load[tour1] - vect[ind1]->_demande + vect[ind2]->_demande > instance->getCapacityV()) res = -1; if (!res && _load[tour2] - vect[ind2]->_demande + vect[ind1]->_demande > instance->getCapacityV()) res = -1; // On regarde si ça colle au niveau des fenetres de temps // si c'est le premier ça colle forcement au niveau du temps, sinon on regarde le pred if (!res && _pred[ind2] != 0 && _depart[_pred[ind2]] > vect[ind1]->_fin) res = -1; if (!res && _pred[ind1] != 0 && _depart[_pred[ind1]] > vect[ind2]->_fin) res = -1; // si c'est le dernier ça colle forcement aussi if (!res && _succ[ind1] != 0 && vect[ind2]->_debut > vect[_succ[ind1]]->_fin) res = -1; if (!res && _succ[ind2] != 0 && vect[ind1]->_debut > vect[_succ[ind2]]->_fin) res = -1; if (!res) { // sauvegarde des suivants et precedants unsigned savep1 = _pred[ind1]; unsigned saven1 = _succ[ind1]; unsigned savep2 = _pred[ind2]; unsigned saven2 = _succ[ind2]; // a supprimer bool toDel1 = false; bool toDel2 = false; if (_nbClt[tour1] != 1) delClient(ind1, vect, tour1); else toDel1 = true; if (_nbClt[tour2] != 1) delClient(ind2, vect, tour2); else toDel2 = true; bool avant = true; if (saven1 == 0) { saven1 = savep1; savep1 = _pred[savep1]; avant = false; } if (toDel1) { _pred[ind2] = 0; _succ[ind2] = 0; _first[tour1] = ind2; _last[tour1] = ind2; } else { if (faisable(instance, saven1, std::max(_depart[savep1], vect[ind2]->_debut), avant)) { if (saven1 != 0) insertClient(ind2, saven1, tour1, true, vect); else insertClient(ind2, _last[tour1], tour1, false, vect); } else res = -1; } avant = true; if (saven2 == 0) { saven2 = savep2; savep2 = _pred[savep2]; avant = false; } if (toDel2) { _pred[ind1] = 0; _succ[ind1] = 0; _first[tour2] = ind1; _last[tour2] = ind1; } else { if (faisable(instance, saven2, std::max(_depart[savep2], vect[ind1]->_debut), avant)) { if (saven2 != 0) insertClient(ind1, saven2, tour2, true, vect); else insertClient(ind1, _last[tour2], tour2, false, vect); } else res = -1; } } return res; }
int main(int argc, char *argv[]){ //Verificação de argumentos da main para o -t if(argc >= 2 && strcmp(argv[1], "-t") == 0 ) port = atoi(argv[2]); fd=socket(AF_INET,SOCK_DGRAM,0); memset((void*)&serveraddr,(int)'\0', sizeof(serveraddr)); serveraddr.sin_family=AF_INET; serveraddr.sin_addr.s_addr=htonl(INADDR_ANY); serveraddr.sin_port=htons((u_short)port); if(bind(fd,(struct sockaddr*)&serveraddr,sizeof(serveraddr))== -1){ printf("deu erro no bind\n"); exit(-1); } while(1){ addrlen=sizeof(clientaddr); if(recvfrom(fd,buffer,sizeof(buffer),0, (struct sockaddr*)&clientaddr, &addrlen) == -1){ printf("erro na recepção de mensagem do servidor\n"); exit(-1); } if(strncmp("REG", buffer, 3)==0){ i = 0; while(buffer[i+3]!='\n'){ bufferid[i] = buffer[i+4]; i++; } ip = strtok(bufferid, " "); portstr = strtok(NULL, "\n"); sl = insertServer(ip, portstr, sl); auxServer = searchServer(ip, portstr, sl); printf("Server: %s %s - #%d\n", auxServer->ip, auxServer->port, auxServer->intNr++); addrlen=sizeof(serveraddr); if(sendto(fd,str,strlen(str)+1,0,(struct sockaddr*)&clientaddr,addrlen) == -1){ printf("erro no envio de mensagem do servidor\n"); exit(-1); } } if(strncmp("UPD", buffer, 3)==0){ i = 0; while(buffer[i+3]!='\n'){ bufferid[i] = buffer[i+4]; i++; } ip = strtok(bufferid, " "); portstr = strtok(NULL, " "); name = strtok(NULL, "\n"); if((auxClient = searchClient(name, cl)) == NULL){ cl = insertClient(name, cl); auxClient = cl; } auxServer = searchServer(ip, portstr, sl); printf("Server: %s %s - #%d\nClient: %s - #%d\n", auxServer->ip, auxServer->port, auxServer->intNr++, auxClient->name, auxClient->intNr++); addrlen=sizeof(serveraddr); if(sendto(fd,str,strlen(str)+1,0,(struct sockaddr*)&clientaddr,addrlen) == -1){ printf("erro no envio de mensagem do servidor\n"); exit(-1); } } } close(fd); }