diff --git a/Source/CarlaServer/source/carla/CarlaServer.cpp b/Source/CarlaServer/source/carla/CarlaServer.cpp index da0c9e6d2..7cc604cfa 100644 --- a/Source/CarlaServer/source/carla/CarlaServer.cpp +++ b/Source/CarlaServer/source/carla/CarlaServer.cpp @@ -30,26 +30,30 @@ namespace carla { bool CarlaServer::tryReadControl(float &steer, float &throttle) { return _pimpl->tryReadControl(steer, throttle); } + + bool CarlaServer::tryReadRequestNewEpisode(){ + return _pimpl->tryReadRequestNewEpisode(); + } bool CarlaServer::sendReward(const Reward_Values &values) { - if (needRestart()) return false; + if (needsRestart()) return false; _pimpl->sendReward(values); return true; } bool CarlaServer::sendSceneValues(const Scene_Values &values) { - if (needRestart()) return false; + if (needsRestart()) return false; _pimpl->sendSceneValues(values); return true; - } + } bool CarlaServer::sendEndReset() { - if (needRestart()) return false; + if (needsRestart()) return false; _pimpl->sendEndReset(); return true; } - bool CarlaServer::worldConnected(){ + /*bool CarlaServer::worldConnected(){ return _pimpl->worldConnected(); } @@ -63,6 +67,10 @@ namespace carla { bool CarlaServer::needRestart() { return _pimpl->needRestart(); + }*/ + + bool CarlaServer::needsRestart(){ + return _pimpl->needsRestart() && _pimpl->worldConnected() && _pimpl->clientConnected() && _pimpl->serverConnected(); } } // namespace carla diff --git a/Source/CarlaServer/source/carla/CarlaServer.h b/Source/CarlaServer/source/carla/CarlaServer.h index 8f660d49c..546e5a25d 100644 --- a/Source/CarlaServer/source/carla/CarlaServer.h +++ b/Source/CarlaServer/source/carla/CarlaServer.h @@ -108,6 +108,8 @@ namespace carla { /// is empty. bool tryReadControl(float &steer, float &throttle); + bool tryReadRequestNewEpisode(); + /// Send values of the current player status. bool sendReward(const Reward_Values &values); @@ -127,7 +129,7 @@ namespace carla { bool worldConnected(); // Returns true if the server needs to restart the communication protocol - bool needRestart(); + bool needsRestart(); private: diff --git a/Source/CarlaServer/source/carla/server/CarlaCommunication.cpp b/Source/CarlaServer/source/carla/server/CarlaCommunication.cpp index 510a51b0c..216c14e6d 100644 --- a/Source/CarlaServer/source/carla/server/CarlaCommunication.cpp +++ b/Source/CarlaServer/source/carla/server/CarlaCommunication.cpp @@ -153,7 +153,7 @@ namespace server { - if (!communication.NeedRestart()){ + if (!communication.NeedsRestart()){ std::cout << " ---- RECONNECT ALL ...." << std::endl; @@ -187,7 +187,7 @@ namespace server { _world(worldPort), _server(writePort), _client(readPort), - _needRestart(false), + _needsRestart(false), _proto(std::make_unique(this)), _worldThread { [this]() { return worldReceiveThread(this->_world, this->_worldThread); }, @@ -214,9 +214,9 @@ namespace server { } CarlaCommunication::~CarlaCommunication(){ - delete &_worldThread; - delete &_serverThread; - delete &_clientThread; + //delete &_worldThread; + //delete &_serverThread; + //delete &_clientThread; } void CarlaCommunication::sendReward(const Reward_Values &values) { @@ -243,7 +243,7 @@ namespace server { void CarlaCommunication::sendWorld(const uint32_t modes,const uint32_t scenes) { - _needRestart = false; + _needsRestart = false; World world; _proto->LoadWorld(world, modes, scenes); @@ -312,6 +312,15 @@ namespace server { return true; } + bool CarlaCommunication::tryReadRequestNewEpisode(){ + std::string request; + if(!_worldThread.tryPop(request)) return false; + + RequestNewEpisode reqEpisode; + + return reqEpisode.ParseFromString(request); + } + void CarlaCommunication::restartServer(){ _server.close(); //_server = TCPServer(_serverPort); @@ -355,12 +364,12 @@ namespace server { return _mode; } - bool CarlaCommunication::NeedRestart(){ - return _needRestart; + bool CarlaCommunication::NeedsRestart(){ + return _needsRestart; } void CarlaCommunication::Restart(){ - _needRestart = true; + _needsRestart = true; } } } diff --git a/Source/CarlaServer/source/carla/server/CarlaCommunication.h b/Source/CarlaServer/source/carla/server/CarlaCommunication.h index a2aa302dc..57161655e 100644 --- a/Source/CarlaServer/source/carla/server/CarlaCommunication.h +++ b/Source/CarlaServer/source/carla/server/CarlaCommunication.h @@ -43,6 +43,8 @@ namespace server { bool tryReadEpisodeStart(uint32_t &start_index, uint32_t &end_index); + bool tryReadRequestNewEpisode(); + void restartServer(); void restartWorld(); @@ -61,7 +63,7 @@ namespace server { Mode GetMode(); - bool NeedRestart(); + bool NeedsRestart(); void Restart(); private: @@ -81,7 +83,7 @@ namespace server { thread::AsyncReadWriteJobQueue _worldThread; - std::atomic_bool _needRestart; + std::atomic_bool _needsRestart; std::atomic _mode; diff --git a/Source/CarlaServer/source/carla/server/Server.cpp b/Source/CarlaServer/source/carla/server/Server.cpp index 6d773cb0b..0699a3c20 100644 --- a/Source/CarlaServer/source/carla/server/Server.cpp +++ b/Source/CarlaServer/source/carla/server/Server.cpp @@ -66,6 +66,10 @@ namespace server { return _communication->tryReadEpisodeStart(start_index, end_index); } + bool Server::tryReadRequestNewEpisode(){ + return _communication->tryReadRequestNewEpisode(); + } + Mode Server::GetMode() const { return _communication->GetMode(); } @@ -86,8 +90,8 @@ namespace server { return _communication->serverConnected(); } - bool Server::needRestart() const { - return _communication->NeedRestart(); + bool Server::needsRestart() const { + return _communication->NeedsRestart(); } } // namespace server diff --git a/Source/CarlaServer/source/carla/server/Server.h b/Source/CarlaServer/source/carla/server/Server.h index 4f5687fc2..08b34a759 100644 --- a/Source/CarlaServer/source/carla/server/Server.h +++ b/Source/CarlaServer/source/carla/server/Server.h @@ -53,6 +53,9 @@ namespace server { ////Try to read if the client has selected an end & start point. Return false if the queue is empty bool tryReadEpisodeStart(uint32_t &start_index, uint32_t &end_index); + + bool tryReadRequestNewEpisode(); + void setMode(Mode mode); Mode GetMode() const; @@ -67,7 +70,7 @@ namespace server { bool serverConnected() const; - bool needRestart() const; + bool needsRestart() const; private: diff --git a/Source/CarlaServer/source/carla/server/carla_protocol.proto b/Source/CarlaServer/source/carla/server/carla_protocol.proto index 3d7165608..e7f7e8dbe 100644 --- a/Source/CarlaServer/source/carla/server/carla_protocol.proto +++ b/Source/CarlaServer/source/carla/server/carla_protocol.proto @@ -1,5 +1,15 @@ // Client Messages + + +message RequestNewEpisode { + + + optional bool request =1; + + +} + message EpisodeStart { diff --git a/Source/CarlaServer/source/test/async_server.cpp b/Source/CarlaServer/source/test/async_server.cpp index 19fcdf27b..6d6b0ed66 100644 --- a/Source/CarlaServer/source/test/async_server.cpp +++ b/Source/CarlaServer/source/test/async_server.cpp @@ -89,7 +89,7 @@ int main(int argc, char *argv[]) { for (;;){ - if (server.worldConnected()){ + if (!server.needsRestart()){ server.init(1u); @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) { carla::Mode mode; uint32_t scene; - while(!server.needRestart() && !server.tryReadSceneInit(mode, scene)); + while(!server.needsRestart() && !server.tryReadSceneInit(mode, scene)); std::cout << "Received: mode = " << (mode == carla::Mode::MONO ? "MONO" : "STEREO") @@ -105,9 +105,9 @@ int main(int argc, char *argv[]) { << scene << std::endl; } + carla::Scene_Values sceneValues; { - carla::Scene_Values sceneValues; sceneValues.possible_positions.push_back({0.0f, 0.0f}); sceneValues.possible_positions.push_back({1.0f, 2.0f}); sceneValues.possible_positions.push_back({3.0f, 4.0f}); @@ -124,21 +124,22 @@ int main(int argc, char *argv[]) { std::cout << "New episode" << std::endl; uint32_t start, end; - while (!server.needRestart() && !server.tryReadEpisodeStart(start, end)); + while (!server.needsRestart() && !server.tryReadEpisodeStart(start, end)); std::cout << "Received: startIndex = " << start << ", endIndex = " << end << std::endl; } server.sendEndReset(); - while (!server.needRestart()) { - if (server.clientConnected() && server.serverConnected()){ - float steer, gas; - uint32_t startPoint, endPoint; - if (server.tryReadEpisodeStart(startPoint, endPoint)) { - std::cout << "-------- RESET --------" << std::endl; - std::cout << "--> Start: " << startPoint << " End: " << endPoint << " <--" << std::endl; - server.sendEndReset(); - } else { + while (!server.needsRestart()) { + float steer, gas; + uint32_t startPoint, endPoint; + if (server.tryReadRequestNewEpisode()){ + std::cout << "-------- RESET --------" << std::endl; + server.sendSceneValues(sceneValues); + while (!server.needsRestart() && !server.tryReadEpisodeStart(startPoint, endPoint)); + std::cout << "--> Start: " << startPoint << " End: " << endPoint << " <--" << std::endl; + server.sendEndReset(); + }else { if (server.tryReadControl(steer, gas)) { std::cout << "Steer: " << steer << " Gas: " << gas << std::endl; } @@ -146,7 +147,6 @@ int main(int argc, char *argv[]) { reward.timestamp = timestamp++; server.sendReward(reward); } - } } std::cout << " ----- RESTARTING -----" << std::endl;