#18 Cleaning up a bit

This commit is contained in:
nsubiron 2017-04-19 13:00:12 +02:00
parent f1733c6c76
commit 1d3b05109f
7 changed files with 130 additions and 344 deletions

View File

@ -1,9 +1,13 @@
#include "CarlaServer.h"
#include <carla/server/Server.h>
#include <carla/server/CarlaCommunication.h>
namespace carla {
Image::Image() {}
Image::~Image() {}
Reward_Values::Reward_Values() {}
Reward_Values::~Reward_Values() {}
@ -12,10 +16,13 @@ namespace carla {
Scene_Values::~Scene_Values() {}
class CarlaServer::Pimpl : public carla::server::Server {
class CarlaServer::Pimpl {
public:
Pimpl(uint32_t worldPort, uint32_t writePort, uint32_t readPort) :
carla::server::Server(worldPort, writePort, readPort) {}
template<typename... Args>
Pimpl(Args&&... args) : communication(std::forward<Args>(args)...) {}
carla::server::CarlaCommunication communication;
};
CarlaServer::CarlaServer(uint32_t writePort, uint32_t readPort, uint32_t worldPort) :
@ -23,69 +30,77 @@ namespace carla {
CarlaServer::~CarlaServer() {}
bool CarlaServer::init(uint32_t LevelCount) {
if (!worldConnected() && !clientConnected() && !serverConnected()) return false;
_pimpl->sendWorld(static_cast<uint32_t>(Mode::NUMBER_OF_MODES), LevelCount);
bool CarlaServer::init(uint32_t levelCount) {
if (!worldConnected() && !clientConnected() && !serverConnected())
return false;
_pimpl->communication.sendWorld(levelCount);
return true;
}
bool CarlaServer::tryReadSceneInit(Mode &mode, uint32_t &scene, bool &readed) {
if (!worldConnected()) return false;
readed = _pimpl->tryReadSceneInit(mode, scene);
bool CarlaServer::tryReadSceneInit(uint32_t &scene, bool &readed) {
if (!worldConnected())
return false;
readed = _pimpl->communication.tryReadSceneInit(scene);
return true;
}
bool CarlaServer::tryReadEpisodeStart(uint32_t &startIndex, uint32_t &endIndex, bool &readed) {
if (!worldConnected()) return false;
readed = _pimpl->tryReadEpisodeStart(startIndex, endIndex);
if (!worldConnected())
return false;
readed = _pimpl->communication.tryReadEpisodeStart(startIndex, endIndex);
return true;
}
bool CarlaServer::tryReadControl(float &steer, float &throttle, bool &readed) {
if (!clientConnected()) return false;
readed = _pimpl->tryReadControl(steer, throttle);
if (!clientConnected())
return false;
readed = _pimpl->communication.tryReadControl(steer, throttle);
return true;
}
bool CarlaServer::newEpisodeRequested(bool &newEpisode) {
if (!worldConnected()) return false;
newEpisode = _pimpl->tryReadRequestNewEpisode();
if (!worldConnected())
return false;
newEpisode = _pimpl->communication.tryReadRequestNewEpisode();
return true;
}
bool CarlaServer::sendReward(Reward_Values *values) {
std::unique_ptr<Reward_Values> ptr(values);
if (!serverConnected()) return false;
_pimpl->sendReward(std::move(ptr));
if (!serverConnected())
return false;
_pimpl->communication.sendReward(std::move(ptr));
return true;
}
bool CarlaServer::sendSceneValues(const Scene_Values &values) {
if (!worldConnected()) return false;
_pimpl->sendSceneValues(values);
if (!worldConnected())
return false;
_pimpl->communication.sendScene(values);
return true;
}
bool CarlaServer::sendEndReset() {
if (!worldConnected()) return false;
_pimpl->sendEndReset();
if (!worldConnected())
return false;
_pimpl->communication.sendReset();
return true;
}
bool CarlaServer::worldConnected(){
return _pimpl->worldConnected() && !_pimpl->needsRestart();
return _pimpl->communication.worldConnected() && !_pimpl->communication.needsRestart();
}
bool CarlaServer::clientConnected(){
return _pimpl->clientConnected() && !_pimpl->needsRestart();
return _pimpl->communication.clientConnected() && !_pimpl->communication.needsRestart();
}
bool CarlaServer::serverConnected(){
return _pimpl->serverConnected() && !_pimpl->needsRestart();
return _pimpl->communication.serverConnected() && !_pimpl->communication.needsRestart();
}
bool CarlaServer::needsRestart(){
return _pimpl->needsRestart();
return _pimpl->communication.needsRestart();
}
} // namespace carla

View File

@ -27,12 +27,14 @@ namespace carla {
uint8_t A;
};
enum ImageType{
enum ImageType {
IMAGE,
DEPTH,
};
struct Image {
Image();
~Image();
std::vector<Color> image;
ImageType type;
uint32_t width, height;
@ -61,14 +63,8 @@ namespace carla {
float intersect_other_lane;
/// Percentage of the car off-road.
float intersect_offroad;
/// Width and height of the images.
//uint32_t image_width, image_height;
/// RGB images.
/// Captured images.
std::vector<Image> images;
//std::vector<Color> image_rgb_1;
/// Depth images.
//std::vector<Color> image_depth_0;
//std::vector<Color> image_depth_1;
};
struct Scene_Values {
@ -76,18 +72,10 @@ namespace carla {
~Scene_Values();
/// Possible world positions to spawn the player.
std::vector<Vector2D> possible_positions;
/// Projection matrices of the cameras (1 in mode mono, 2 in stereo).
/// Projection matrices of the cameras.
std::vector<std::array<float, 16u>> projection_matrices;
};
enum class Mode : int8_t {
MONO,
STEREO,
NUMBER_OF_MODES,
INVALID = -1
};
/// Asynchronous TCP server. Uses three ports, one for sending messages
/// (write), one for receiving messages (read), and one for world level
/// commands (world).
@ -109,13 +97,11 @@ namespace carla {
/// Initialize the server.
///
/// @param LevelCount Number of levels available.
bool init(uint32_t LevelCount);
bool init(uint32_t levelCount);
/// Try to read if the client has selected an scene and mode. Return false
/// if the queue is empty.
///
/// If returned mode INVALID, ignore scene.
bool tryReadSceneInit(Mode &mode, uint32_t &scene, bool &readed);
bool tryReadSceneInit(uint32_t &scene, bool &readed);
/// Try to read if the client has selected an end & start point. Return
/// false if the queue is empty.

View File

@ -10,16 +10,6 @@ namespace server {
// -- Static methods ---------------------------------------------------------
std::mutex _generalMutex;
static Mode getMode(int modeInt) {
switch (modeInt) {
case 0: return Mode::MONO;
case 1: return Mode::STEREO;
default: return Mode::INVALID;
}
}
template<typename ERROR_CODE>
static void logTCPError(const std::string &text, const ERROR_CODE &errorCode) {
std::cerr << "CarlaConnection - TCP Server: " << text << ": " << errorCode.message() << std::endl;
@ -141,9 +131,11 @@ namespace server {
}
static void ReconnectAll(CarlaCommunication &communication) {
std::lock_guard<std::mutex> lock(_generalMutex);
/// @todo This blocks every CarlaCommunication with a single mutex.
static std::mutex MUTEX;
std::lock_guard<std::mutex> lock(MUTEX);
if (!communication.NeedsRestart()) {
if (!communication.needsRestart()) {
std::cout << " ---- RECONNECT ALL ...." << std::endl;
@ -165,7 +157,7 @@ namespace server {
communication.getClientThread().restart();
}
communication.Restart();
communication.restart();
}
}
@ -174,7 +166,6 @@ namespace server {
_server(writePort),
_client(readPort),
_needsRestart(false),
_mode(Mode::MONO),
_proto(std::make_unique<Protocol>(this)),
_worldThread{
[this]() { return worldReceiveThread(this->_world, this->_worldThread); },
@ -213,13 +204,14 @@ namespace server {
return true;
}
void CarlaCommunication::sendWorld(const uint32_t modes, const uint32_t scenes) {
void CarlaCommunication::sendWorld(const uint32_t scenes) {
//ClearThreads
_worldThread.clear();
_clientThread.clear();
_serverThread.clear();
World world;
constexpr int modes = 0; /// @todo #18
_proto->LoadWorld(world, modes, scenes);
auto message = std::make_unique<std::string>();
@ -253,8 +245,7 @@ namespace server {
}
}
bool CarlaCommunication::tryReadSceneInit(Mode &mode, uint32_t &scene) {
mode = Mode::INVALID;
bool CarlaCommunication::tryReadSceneInit(uint32_t &scene) {
scene = 0u;
std::unique_ptr<std::string> info = _worldThread.tryPop();
@ -264,11 +255,7 @@ namespace server {
if (!sceneInit.ParseFromString(*info)) { return false; }
mode = getMode(sceneInit.mode());
scene = sceneInit.scene();
_mode = mode;
return true;
}
@ -303,17 +290,14 @@ namespace server {
void CarlaCommunication::restartServer() {
_server.close();
//_server = TCPServer(_serverPort);
}
void CarlaCommunication::restartWorld() {
_world.close();
//_world = TCPServer(_worldPort);
}
void CarlaCommunication::restartClient() {
_client.close();
//_client = TCPServer(_clientPort);
}
void CarlaCommunication::checkRestart() {
@ -347,15 +331,11 @@ namespace server {
return _server.Connected();
}
Mode CarlaCommunication::GetMode() {
return _mode;
}
bool CarlaCommunication::NeedsRestart() {
bool CarlaCommunication::needsRestart() {
return _needsRestart;
}
void CarlaCommunication::Restart() {
void CarlaCommunication::restart() {
_needsRestart = true;
}

View File

@ -36,10 +36,9 @@ namespace server {
void sendReset();
//void sendWorld(const World &world);
void sendWorld(const uint32_t modes, const uint32_t scenes);
void sendWorld(const uint32_t scenes);
bool tryReadSceneInit(Mode &mode, uint32_t &scene);
bool tryReadSceneInit(uint32_t &scene);
bool tryReadEpisodeStart(uint32_t &start_index, uint32_t &end_index);
@ -63,10 +62,9 @@ namespace server {
std::mutex getGeneralMutex();
Mode GetMode();
bool needsRestart();
bool NeedsRestart();
void Restart();
void restart();
private:
@ -78,8 +76,6 @@ namespace server {
std::atomic_bool _needsRestart;
std::atomic<Mode> _mode;
const std::unique_ptr<Protocol> _proto;
thread::AsyncReadWriteJobQueue< std::string, std::string> _worldThread;

View File

@ -1,88 +0,0 @@
// CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
#include "Server.h"
#include "CarlaCommunication.h"
#include "carla_protocol.pb.h"
#include <iostream>
#include <memory>
namespace carla {
namespace server {
// -- CarlaServer ------------------------------------------------------------
Server::Server(uint32_t worldPort, uint32_t writePort, uint32_t readPort) :
_communication(std::make_unique<CarlaCommunication>(worldPort, writePort, readPort)){}
Server::~Server() {
}
void Server::sendReward( std::unique_ptr<Reward_Values> values) {
//Reward reward;
//_proto->LoadReward(reward, values);
//_communication->sendReward(reward);
_communication->sendReward(std::move(values));
}
void Server::sendSceneValues(const Scene_Values &values) {
_communication->sendScene(values);
}
void Server::sendEndReset() {
_communication->sendReset();
}
void Server::sendWorld(const uint32_t modes, const uint32_t scenes) {
//World world;
//_proto->LoadWorld(world, modes, scenes);
//_communication->sendWorld(world);
_communication->sendWorld(modes, scenes);
}
bool Server::tryReadControl(float &steer, float &gas) {
return _communication->tryReadControl(steer, gas);
}
bool Server::tryReadSceneInit(Mode &mode, uint32_t &scene) {
return _communication->tryReadSceneInit(mode, scene);
}
bool Server::tryReadEpisodeStart(uint32_t &start_index, uint32_t &end_index) {
return _communication->tryReadEpisodeStart(start_index, end_index);
}
bool Server::tryReadRequestNewEpisode(){
return _communication->tryReadRequestNewEpisode();
}
Mode Server::GetMode() const {
return _communication->GetMode();
}
void Server::SetScene(int scene) {
_scene = scene;
}
bool Server::worldConnected() const{
return _communication->worldConnected();
}
bool Server::clientConnected() const{
return _communication->clientConnected();
}
bool Server::serverConnected() const{
return _communication->serverConnected();
}
bool Server::needsRestart() const {
return _communication->NeedsRestart();
}
} // namespace server
} // namespace carla

View File

@ -1,88 +0,0 @@
// CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
#pragma once
#include <carla/NonCopyable.h>
#include <carla/CarlaServer.h>
#include <atomic>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
namespace carla {
namespace server {
class CarlaCommunication;
/// Asynchronous TCP server. Uses two ports, one for sending messages (write)
/// and one for receiving messages (read).
///
/// Writing and reading are executed in two different threads. Each thread has
/// its own queue of messages.
///
/// Note that a new socket is created for every connection (every write and
/// read).
class Server : private NonCopyable {
public:
/// Starts two threads for writing and reading.
explicit Server(uint32_t worldPort, uint32_t writePort, uint32_t readPort);
~Server();
///// Send values of the current player status
void sendReward(std::unique_ptr<Reward_Values> values);
//// Send the values of the generated scene
void sendSceneValues(const Scene_Values &values);
//// Send a signal to the client to notify that the car is ready
void sendEndReset();
void sendWorld(uint32_t modes, uint32_t scenes);
///// Try to read the response of the client. Return false if the queue
///// is empty.
bool tryReadControl(float &steer, float &gas);
////Try to read if the client has selected an scene and mode. Return false if the queue is empty
bool tryReadSceneInit(Mode &mode, uint32_t &scene);
////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;
void SetScene(int scene);
int GetScene() const;
bool worldConnected() const;
bool clientConnected() const;
bool serverConnected() const;
bool needsRestart() const;
private:
//std::mutex _mutex;
std::atomic_int _scene;
const std::unique_ptr<CarlaCommunication> _communication;
//const std::unique_ptr<Protocol> _proto;
};
} // namespace server
} // namespace carla

View File

@ -43,8 +43,7 @@ static std::vector<carla::Color> makeImage(uint32_t width, uint32_t height) {
return image;
}
std::unique_ptr<carla::Reward_Values> makeReward(){
std::unique_ptr<carla::Reward_Values> makeReward() {
auto reward = std::make_unique<carla::Reward_Values>();
const uint32_t imageWidth = 512u;
@ -60,7 +59,7 @@ std::unique_ptr<carla::Reward_Values> makeReward(){
reward->intersect_other_lane = 0.5f;
reward->intersect_offroad = 0.5f;
for (int i = 0; i < 4; ++i){
for (int i = 0; i < 4; ++i) {
carla::Image img;
img.image = makeImage(imageWidth, imageHeight);
img.width = imageWidth;
@ -91,67 +90,57 @@ int main(int argc, char *argv[]) {
const uint32_t worldPort = toInt(argv[1u]);
const uint32_t writePort = toInt(argv[2u]);
const uint32_t readPort = toInt(argv[3u]);
// const uint32_t mode = toInt(argv[4u]);
// const uint32_t scene = toInt(argv[5u]);
// This already starts the two threads.
carla::CarlaServer server(writePort, readPort, worldPort);
// Let's simulate the game loop.
for (;;){
if (server.init(1u)){
for (;;) {
if (server.init(1u)) {
{
carla::Mode mode;
uint32_t scene;
bool error = false, readed = false;
do{
error = !server.tryReadSceneInit(mode, scene, readed);
}while(!readed && !error);
do {
error = !server.tryReadSceneInit(scene, readed);
} while (!readed && !error);
if (error) std::cerr << "ERROR while sending SceneValues" << std::endl;
else std::cout << "Received: mode = " << (mode == carla::Mode::MONO ? "MONO" : "STEREO")
<< ", scene = " << scene << std::endl;
if (error) {
std::cerr << "ERROR while sending SceneValues" << std::endl;
}
else std::cout << "Received: scene = " << scene << std::endl;
}
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});
const std::array<float, 16u> pMatrix = {{ 10.0 }};
sceneValues.projection_matrices.push_back(pMatrix);
/* std::cout << "POSSIBLE POSITIONS "<< std::endl;
for (int i=0; i<sceneValues.possible_positions.size(); ++i){
std::cout << " x: " << sceneValues.possible_positions[i].x << " y: " << sceneValues.possible_positions[i].y << std::endl;
}*/
if (!server.sendSceneValues(sceneValues)) std::cerr << "ERROR while sending SceneValues" << std::endl;
std::cout << "New episode" << std::endl;
{
uint32_t start, end;
bool error = false, readed = false;
do{
error = !server.tryReadEpisodeStart(start, end, readed);
}while (!readed && !error);
if (error) std::cerr << "ERROR while reading EpisodeStart" << std::endl;
else std::cout << "Received: startIndex = " << start << ", endIndex = " << end << std::endl;
if (!server.sendSceneValues(sceneValues)) {
std::cerr << "ERROR while sending SceneValues" << std::endl;
}
if (!server.sendEndReset()) std::cerr << "ERROR while sending EndReset" << std::endl;
std::cout << "New episode" << std::endl;
{
uint32_t start, end;
bool error = false, readed = false;
do {
error = !server.tryReadEpisodeStart(start, end, readed);
} while (!readed && !error);
if (error) {
std::cerr << "ERROR while reading EpisodeStart" << std::endl;
} else {
std::cout << "Received: startIndex = " << start << ", endIndex = " << end << std::endl;
}
}
if (!server.sendEndReset()) {
std::cerr << "ERROR while sending EndReset" << std::endl;
}
while (true) {
float steer, gas;
@ -172,38 +161,34 @@ int main(int argc, char *argv[]) {
uint32_t startPoint, endPoint;
bool error = false, readed = false;
do{
do {
error = !server.tryReadEpisodeStart(startPoint, endPoint, readed);
}while (!readed && !error);
} while (!readed && !error);
if (error) {
std::cerr << "ERROR while reading EpisodeStart" << std::endl;
break;
}
else{
} else {
std::cout << "--> Start: " << startPoint << " End: " << endPoint << " <--" << std::endl;
server.sendEndReset();
}
}else {
} else {
bool readed = false;
if (!server.tryReadControl(steer, gas, readed)){
std::cerr << "ERROR while reading Control" << std::endl;
break;
}
else if (readed)
} else if (readed) {
std::cout << "CONTROL --> gas: " << gas << " steer: " << steer << std::endl;
}
if (!server.sendReward(makeReward().release())) {
std::cerr << "ERROR while sending Reward" << std::endl;
break;
}
}
}
std::cout << " ----- RESTARTING -----" << std::endl;
}
}