bool sendReceiveObjectTest(){ cout << "sendReceiveObjectTest() ...\n"; Object tosend('c', 34, true, "cinco"); pid_t child; // client code if ((child=fork())==0){ try { TcpSocket toServer; cout << " Forked child waiting a bit\n"; sleep(1); cout << " Forked child connecting to server\n"; toServer.connect("127.0.0.1",LOCALPORT); cout << " Forked child connected: " << toServer << endl; cout << " Forked child sending to server\n"; toServer.write(reinterpret_cast<char *>(&tosend), sizeof(Object)); cout << " Forked child closing connection to server\n"; toServer.close(); cout << " Forked child exiting\n"; exit(0); } catch (std::exception& e) { cout << " Forked child exception: " << e.what() << endl; exit(-1); } return false; // for clarity } // server code TcpSocket* connectedSocket; try { TcpSocket serverSocket; connectedSocket = bindListenAccept(LOCALPORT, serverSocket); std::string clientmessage; cout << " Reading one object from client\n"; // Read the object Object received; int len = connectedSocket->read(reinterpret_cast<char *>(&received), sizeof(Object)); cout << " Read: " << len << " bytes from " << *connectedSocket << endl; if (!(tosend.compare(received) == 0)) throw std::runtime_error("objects dont match"); cout << " Server read " << received << endl; checkChild(child); serverSocket.close(); connectedSocket->close(); cout << "Done!\n"; delete connectedSocket; return true; } catch (std::exception& e) { cout << " Server exception: " << e.what() << endl; cout << "Failed!\n"; checkChild(child, true); delete connectedSocket; return false; } }
bool HttpServer::send_response(TcpSocket &cl_sock, Yb::ILogger &logger, int code, const string &desc, const string &body, const string &cont_type) { try { ostringstream out; out << "HTTP/1.0 " << code << " " << desc << "\nContent-Type: " << cont_type << "\nContent-Length: " << body.size() << "\n\n" << body; logger.debug("HTTP response:\n" + out.str()); cl_sock.write(out.str()); cl_sock.close(true); return true; } catch (const std::exception &ex) { logger.error(string("exception: ") + ex.what()); return false; } }
int main(int argc, char* argv[]) { startRobogen(); interrupted = false; if (argc < 2) { std::cerr << "Please, provide server port." << std::endl; exitRobogen(EXIT_FAILURE); } // Parameters: <PORT> int port = std::atoi(argv[1]); if (!port) { std::cerr << "The first argument must be a server port." << std::endl; exitRobogen(EXIT_FAILURE); } bool visualize = false; bool startPaused = false; for (int currentArg=2; currentArg<argc; currentArg++) { if (std::string(argv[currentArg]).compare("--visualization") == 0) { visualize = true; } else if (std::string(argv[currentArg]).compare("--pause") == 0) { startPaused = true; } } if (startPaused && !visualize) { std::cerr << "Cannot start paused without visualization enabled." << std::endl; exitRobogen(EXIT_FAILURE); } TcpSocket socket; bool rc = socket.create(port); if (!rc) { std::cerr << "Cannot listen for incoming connections on port " << port << std::endl; exitRobogen(EXIT_FAILURE); } boost::random::mt19937 rng; rng.seed(port); #ifdef QT5_ENABLED QCoreApplication a(argc, argv); #endif while (!interrupted) { // Wait for client to connect std::cout << "Waiting for clients..." << std::endl; rc = socket.accept(); if (rc) { std::cout << "Client connected..." << std::endl; while (true) { try { // --------------------------------------- // Decode solution // --------------------------------------- ProtobufPacket<robogenMessage::EvaluationRequest> packet; // 1) Read packet header std::vector<unsigned char> headerBuffer; socket.read(headerBuffer, ProtobufPacket<robogenMessage::EvaluationRequest>::HEADER_SIZE); unsigned int packetSize = packet.decodeHeader(headerBuffer); // 2) Read packet size std::vector<unsigned char> payloadBuffer; socket.read(payloadBuffer, packetSize); packet.decodePayload(payloadBuffer); // --------------------------------------- // Decode configuration file // --------------------------------------- boost::shared_ptr<RobogenConfig> configuration = ConfigurationReader::parseRobogenMessage( packet.getMessage()->configuration()); if (configuration == NULL) { std::cerr << "Problems parsing the configuration file. Quit." << std::endl; exitRobogen(EXIT_FAILURE); } // --------------------------------------- // Setup environment // --------------------------------------- boost::shared_ptr<Scenario> scenario = ScenarioFactory::createScenario(configuration); if (scenario == NULL) { exitRobogen(EXIT_FAILURE); } std::cout << "-----------------------------------------------" << std::endl; // --------------------------------------- // Run simulations // --------------------------------------- Viewer *viewer = NULL; if(visualize) { viewer = new Viewer(startPaused); } unsigned int simulationResult = runSimulations(scenario, configuration, packet.getMessage()->robot(), viewer, rng); if(viewer != NULL) { delete viewer; } if (simulationResult == SIMULATION_FAILURE) { exitRobogen(EXIT_FAILURE); } // --------------------------------------- // Compute fitness // --------------------------------------- double fitness; if (simulationResult == CONSTRAINT_VIOLATED) { fitness = MIN_FITNESS; } else { fitness = scenario->getFitness(); } std::cout << "Fitness for the current solution: " << fitness << std::endl << std::endl; // --------------------------------------- // Send reply to EA // --------------------------------------- boost::shared_ptr<robogenMessage::EvaluationResult> evalResultPacket( new robogenMessage::EvaluationResult()); evalResultPacket->set_fitness(fitness); evalResultPacket->set_id(packet.getMessage()->robot().id()); ProtobufPacket<robogenMessage::EvaluationResult> evalResult; evalResult.setMessage(evalResultPacket); std::vector<unsigned char> sendBuffer; evalResult.forge(sendBuffer); socket.write(sendBuffer); } catch (boost::system::system_error& e) { socket.close(); exitRobogen(EXIT_FAILURE); } } } else { std::cerr << "Cannot connect to client. Exiting." << std::endl; socket.close(); exitRobogen(EXIT_FAILURE); } } exitRobogen(EXIT_SUCCESS); }