new protocol

This commit is contained in:
Xisco Bosch 2017-03-22 14:44:39 +01:00
parent 1080531cbe
commit 197462939f
8 changed files with 72 additions and 34 deletions

View File

@ -31,25 +31,29 @@ namespace carla {
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

View File

@ -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:

View File

@ -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<Protocol>(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;
}
}
}

View File

@ -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<std::string, std::string> _worldThread;
std::atomic_bool _needRestart;
std::atomic_bool _needsRestart;
std::atomic<Mode> _mode;

View File

@ -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

View File

@ -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:

View File

@ -1,5 +1,15 @@
// Client Messages
message RequestNewEpisode {
optional bool request =1;
}
message EpisodeStart {

View File

@ -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;