void Socket::innerReset() { if (0 != m_zmqSocket) { delete(m_zmqSocket); } m_zmqSocket = new zmq::socket_t(m_zmqContext, m_socketType); int aLinger = 10; // linger 0.01 second max after being closed m_zmqSocket->setsockopt(ZMQ_LINGER, &aLinger, sizeof(aLinger)); if (ZMQ_SUB == m_socketType) { string atag; m_zmqSocket->setsockopt(ZMQ_SUBSCRIBE, atag.c_str(), atag.size()); } if (ConnectionMode::BIND == m_connectionMode) { m_zmqSocket->bind(m_url.c_str()); ORWELL_LOG_INFO("Puller binds on " << m_url.c_str()); } else { assert(ConnectionMode::CONNECT == m_connectionMode); m_zmqSocket->connect(m_url.c_str()); ORWELL_LOG_INFO("Subscriber connects to '" << m_url.c_str() << "'"); } }
void ProcessRegister::execute() { ORWELL_LOG_INFO("ProcessRegister::execute"); orwell::messages::Register const & aRegisterMsg = static_cast< orwell::messages::Register const & >(*m_msg); std::string const & aClientID = getArgument("RoutingID"); std::string aTemporaryRobotId = aRegisterMsg.temporary_robot_id(); std::shared_ptr< orwell::game::Robot > aRobot = m_game->getRobotWithoutRealRobot(aTemporaryRobotId); Registered aRegistered; std::string aRobotId; if (aRobot) { aRobotId = aRobot->getRobotId(); aRobot->setHasRealRobot(true); aRobot->setVideoUrl(aRegisterMsg.video_url()); aRegistered.set_team(aRobot->getTeam().getName()); } aRegistered.set_robot_id(aRobotId); RawMessage aReply(aClientID, "Registered", aRegistered.SerializeAsString()); m_publisher->send(aReply); if (aRobot) { ORWELL_LOG_INFO("ProcessRegister::execute success"); } else { ORWELL_LOG_INFO("ProcessRegister::execute failure"); } }
int main() { orwell::support::GlobalLogger::Create("test_agent", "test_agent.log", true); log4cxx::NDC ndc("test_agent"); ORWELL_LOG_INFO("Test starts\n"); orwell::Application::CommandLineParameters aCommandLineArguments; aCommandLineArguments.m_agentPort = 9004; aCommandLineArguments.m_tickInterval = 1; aCommandLineArguments.m_gameDuration = 100; aCommandLineArguments.m_dryRun = false; aCommandLineArguments.m_broadcast = false; orwell::Application::Parameters aParameters; Arguments aArguments = Common::GetArguments( aCommandLineArguments, true); orwell::Application::ReadParameters( aArguments.m_argc, aArguments.m_argv, aParameters); TestAgent aTestAgent(aParameters.m_commandLineParameters.m_agentPort.get()); std::thread aApplicationThread(Application, aParameters); aTestAgent.sendCommand("stop application"); aApplicationThread.join(); orwell::support::GlobalLogger::Clear(); return 0; }
static void Stopper() { TestAgent aTestAgent(9003); ORWELL_LOG_INFO("create subscriber"); zmq::context_t aContext(1); orwell::com::Receiver aSubscriber("tcp://127.0.0.1:9001", ZMQ_SUB, orwell::com::ConnectionMode::CONNECT, aContext); aTestAgent.sendCommand("ping", std::string("pong")); aTestAgent.sendCommand("add team TEAM"); aTestAgent.sendCommand("add robot toto TEAM"); aTestAgent.sendCommand("set robot toto video_url fake"); aTestAgent.sendCommand("register robot toto"); ExpectGameTime(Equals(2), aTestAgent); aTestAgent.sendCommand("start game"); usleep(100 * 1000); bool aIsRunning = boost::lexical_cast< bool >( aTestAgent.sendCommand("get game running", boost::none)); ORWELL_ASSERT_TRUE(aIsRunning, "Game should be running."); usleep(1000 * 1000); ExpectGameTime(MoreThan(0), aTestAgent); ExpectGameTime(LessThan(2), aTestAgent); usleep(1000 * 1000); aIsRunning = boost::lexical_cast< bool >( aTestAgent.sendCommand("get game running", boost::none)); ORWELL_ASSERT_TRUE((!aIsRunning), "Game should not be running any more."); aTestAgent.sendCommand("stop application"); }
void ProcessRobotState::execute() { std::string const & aDestination = getArgument("RoutingID").second; orwell::messages::RobotState const & aRobotStateMsg = static_cast<orwell::messages::RobotState const & >(*m_msg); ORWELL_LOG_INFO("ProcessRobotState::execute : simple relay"); RawMessage aForward(aDestination, "RobotState", aRobotStateMsg.SerializeAsString()); m_publisher->send( aForward ); }
int main() { orwell::support::GlobalLogger::Create( "test_stop_after_game_duration", "test_stop_after_game_duration.log", true); log4cxx::NDC ndc("test_main_plus"); ORWELL_LOG_INFO("Test starts\n"); std::thread aApplicationThread(Application); std::thread aAgentThread(Stopper); aApplicationThread.join(); aAgentThread.join(); orwell::support::GlobalLogger::Clear(); return g_status; }
void ProcessHello::execute() { ORWELL_LOG_INFO("ProcessHello::execute"); orwell::messages::Hello const & anHelloMsg = static_cast<orwell::messages::Hello const & >(*m_msg); std::string const & aClientID = getArgument("RoutingID"); string aNewPlayerName = anHelloMsg.name(); bool const aPlayerAddedSuccess = m_game->addPlayer( aNewPlayerName ); bool aFailure = not aPlayerAddedSuccess; if (aPlayerAddedSuccess) { std::shared_ptr< ::orwell::game::Robot > aAvailableRobot = m_game->getRobotForPlayer(aNewPlayerName); aFailure = true; if (aAvailableRobot.get() != nullptr) { ORWELL_LOG_INFO( "Player " << aNewPlayerName << " is now linked to robot " << aAvailableRobot->getName() ); std::shared_ptr< game::Player > aPlayer = m_game->accessPlayer(aNewPlayerName); if (nullptr != aPlayer) { aPlayer->setRobot(aAvailableRobot); aAvailableRobot->setPlayer(aPlayer); Welcome aWelcome; aWelcome.set_robot(aAvailableRobot->getName()); aWelcome.set_team("team_red"); //currently stupidly hard coded aWelcome.set_id(aAvailableRobot->getRobotId()); aWelcome.set_video_address("localhost"); aWelcome.set_video_port(aAvailableRobot->getVideoRetransmissionPort()); RawMessage aReply(aClientID, "Welcome", aWelcome.SerializeAsString()); m_publisher->send(aReply); aFailure = false; if (m_game->getAvailableRobot() == nullptr) { m_game->start(); } } else { // there is no reason for the player not to be found // but we consider Goodbye would be sent. } } else { ORWELL_LOG_INFO("No robot available for player (" << aNewPlayerName << ")"); } } if (aFailure) { ORWELL_LOG_WARN( "Impossible to process Hello ; player added with success :" << aPlayerAddedSuccess); Goodbye aGoodbye; RawMessage aReply(aClientID, "Goodbye", aGoodbye.SerializeAsString()); m_publisher->send(aReply); } }
void Socket::reset() { ORWELL_LOG_INFO("reset the socket."); }
void ProcessRobotState::execute() { std::string const & aDestination = getArgument("RoutingID"); if (not m_game->getHasRobotById(aDestination)) { ORWELL_LOG_WARN("This is an invalid destination: " << aDestination); return; } orwell::messages::ServerRobotState const & aRobotStateMsg = static_cast<orwell::messages::ServerRobotState const & >(*m_msg); //ORWELL_LOG_INFO("ProcessRobotState::execute : simple relay"); for (int i = 0; i < aRobotStateMsg.rfid_size() ; ++i) { std::shared_ptr< orwell::game::Item > aItem = game::Item::GetItemByRfid(aRobotStateMsg.rfid(i).rfid()); if (not aItem) { continue; } switch (aRobotStateMsg.rfid(i).status()) { case messages::Status::ON : ORWELL_LOG_INFO("Robot " << aDestination << " records contact with RFID " << aRobotStateMsg.rfid(i).rfid() << " at " << aRobotStateMsg.rfid(i).timestamp()); m_game->robotIsInContactWith(aDestination, aItem); break; case messages::Status::OFF : ORWELL_LOG_INFO("Robot " << aDestination << " stops contact with RFID " << aRobotStateMsg.rfid(i).rfid() << " at " << aRobotStateMsg.rfid(i).timestamp()); m_game->robotDropsContactWith(aDestination, aItem); break; } } for (int i = 0; i < aRobotStateMsg.colour_size() ; ++i) { std::shared_ptr< orwell::game::Item > aItem = game::Item::GetItemByColor(aRobotStateMsg.colour(i).colour()); if (not aItem) { continue; } switch (aRobotStateMsg.colour(i).status()) { case messages::Status::ON : ORWELL_LOG_INFO("Robot " << aDestination << " records contact with Colour tag " << aRobotStateMsg.colour(i).colour() << " at " << aRobotStateMsg.colour(i).timestamp()); m_game->robotIsInContactWith(aDestination, aItem); break; case messages::Status::OFF : ORWELL_LOG_INFO("Robot " << aDestination << " stops contact with Colour tag " << aRobotStateMsg.colour(i).colour() << " at " << aRobotStateMsg.colour(i).timestamp()); m_game->robotDropsContactWith(aDestination, aItem); break; } } //todo : what do we forward to the player ? // forward this message to each controler //RawMessage aForward(aDestination, "RobotState", aRobotStateMsg.SerializeAsString()); //m_publisher->send( aForward ); }