21#include <condition_variable>
31#define DEBUG_PRINT_TM 0
32#define IP_DATA_BUFFER_SIZE 80
35namespace traffic_manager {
37using namespace constants::SpeedThreshold;
38using namespace constants::PID;
62 std::make_move_iterator(released.begin()),
63 std::make_move_iterator(released.end()));
67 std::vector<ActorPtr> released;
68 std::vector<TrafficManagerBase *> to_delete;
70 std::scoped_lock<std::mutex> lock(
_mutex);
74 to_delete.push_back(tm.second);
78 for (
auto *tm_ptr : to_delete) {
84 std::vector<ActorPtr> released;
86 std::scoped_lock<std::mutex> lock(
_mutex);
88 tm.second->Reset(episode_id);
95 std::vector<ActorPtr> released;
97 std::scoped_lock<std::mutex> lock(
_mutex);
99 tm.second->SynchronousTick();
107 std::vector<ActorPtr> released;
109 std::scoped_lock<std::mutex> lock(
_mutex);
115 if(tm_ptr !=
nullptr) {
126 class EpisodeResetThread {
129 void Post(uint64_t episode_id, std::function<
void()> on_done) {
130 std::unique_lock<std::mutex> lock(_mutex);
131 _queue.emplace_back(episode_id, std::move(on_done));
133 if (!_running || _owner != CurrentProcess()) {
135 _owner = CurrentProcess();
136 std::thread(&EpisodeResetThread::Run,
this).detach();
143 static long CurrentProcess() {
147 return static_cast<long>(::getpid());
152 std::unique_lock<std::mutex> lock(_mutex);
154 _cv.wait(lock, [
this]() {
return !_queue.empty(); });
155 auto item = std::move(_queue.front());
157 const bool reset = !_has_last || item.first != _last_episode_id;
158 _last_episode_id = item.first;
164 }
catch (
const std::exception &e) {
165 log_error(
"failed to reset the Traffic Manager for the new episode:", e.what());
178 std::condition_variable _cv;
179 std::deque<std::pair<uint64_t, std::function<void()>>> _queue;
180 uint64_t _last_episode_id = 0u;
181 bool _has_last =
false;
182 bool _running =
false;
186 EpisodeResetThread &GetEpisodeResetThread() {
188 static EpisodeResetThread *instance =
new EpisodeResetThread();
195 GetEpisodeResetThread().Post(episode_id, std::move(on_done));
203 auto GetLocalIP = [=](
const uint16_t sport)-> std::pair<std::string, uint16_t> {
204 std::pair<std::string, uint16_t> localIP;
205 int sock = socket(AF_INET, SOCK_DGRAM, 0);
208 std::cout <<
"Error number 1: " << errno << std::endl;
209 std::cout <<
"Error message: " << strerror(errno) << std::endl;
213 sockaddr_in loopback;
214 std::memset(&loopback, 0,
sizeof(loopback));
215 loopback.sin_family = AF_INET;
216 loopback.sin_addr.s_addr = INADDR_LOOPBACK;
217 loopback.sin_port = htons(9);
218 err = connect(sock,
reinterpret_cast<sockaddr*
>(&loopback),
sizeof(loopback));
221 std::cout <<
"Error number 2: " << errno << std::endl;
222 std::cout <<
"Error message: " << strerror(errno) << std::endl;
225 socklen_t addrlen =
sizeof(loopback);
226 err = getsockname(sock,
reinterpret_cast<struct sockaddr*
> (&loopback), &addrlen);
229 std::cout <<
"Error number 3: " << errno << std::endl;
230 std::cout <<
"Error message: " << strerror(errno) << std::endl;
236 localIP = std::pair<std::string, uint16_t>(std::string(buffer), sport);
239 std::cout <<
"Error number 4: " << errno << std::endl;
240 std::cout <<
"Error message: " << strerror(errno) << std::endl;
255 const std::vector<float> longitudinal_param = LONGITUDIAL_PARAM;
256 const std::vector<float> longitudinal_highway_param = LONGITUDIAL_HIGHWAY_PARAM;
257 const std::vector<float> lateral_param = LATERAL_PARAM;
258 const std::vector<float> lateral_highway_param = LATERAL_HIGHWAY_PARAM;
259 const float perc_difference_from_limit = INITIAL_PERCENTAGE_SPEED_DIFFERENCE;
261 std::pair<std::string, uint16_t> serverTM;
266 longitudinal_highway_param,
268 lateral_highway_param,
269 perc_difference_from_limit,
274 serverTM = GetLocalIP(port);
277 episode_proxy.
Lock()->AddTrafficManagerRunning(serverTM);
281 std::cout <<
"NEW@: Registered TM at "
282 << serverTM.first <<
":"
283 << serverTM.second <<
" ..... SUCCESS."
288 _tm_map.insert(std::make_pair(port, tm_ptr));
298 if(episode_proxy.
Lock()->IsTrafficManagerRunning(port)) {
301 std::pair<std::string, uint16_t> serverTM =
302 episode_proxy.
Lock()->GetTrafficManagerRunning(port);
312 if(tm_ptr !=
nullptr) {
316 std::cout <<
"OLD@: Registered TM at "
317 << serverTM.first <<
":"
318 << serverTM.second <<
" ..... TRY "
325 _tm_map.insert(std::make_pair(port, tm_ptr));
339 std::cout <<
"OLD@: Registered TM at "
340 << serverTM.first <<
":"
341 << serverTM.second <<
" ..... FAILED "
#define SOCK_INVALID_INDEX
< socket sockaddr_in getsockname
#define IP_DATA_BUFFER_SIZE
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
virtual std::vector< ActorPtr > TakeReleasedActors()
Vehicles released since the last call; the caller drops them with no TM lock held.
virtual void ShutDown()=0
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
TrafficManagerLocal(std::vector< float > longitudinal_PID_parameters, std::vector< float > longitudinal_highway_PID_parameters, std::vector< float > lateral_PID_parameters, std::vector< float > lateral_highway_PID_parameters, float perc_decrease_from_limit, cc::detail::EpisodeProxy &episode_proxy, uint16_t &RPCportTM)
Private constructor for singleton lifecycle management.
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
TrafficManagerRemote(const std::pair< std::string, uint16_t > &_serverTM, carla::client::detail::EpisodeProxy &episodeProxy)
Constructor store remote location information.
void HealthCheckRemoteTM()
Method to check server is alive or not.
static std::map< uint16_t, TrafficManagerBase * > _tm_map
static void RequestReset(uint64_t episode_id, std::function< void()> on_done)
Non-blocking: reset all TMs once per episode on the reset thread, then call on_done.
TrafficManagerBase * GetTM(uint16_t port) const
void CreateTrafficManagerServer(carla::client::detail::EpisodeProxy episode_proxy, uint16_t port)
bool CreateTrafficManagerClient(carla::client::detail::EpisodeProxy episode_proxy, uint16_t port)
static void Reset(uint64_t episode_id)
EpisodeProxyImpl< EpisodeProxyPointerType::Strong > EpisodeProxy
static void AppendReleased(std::vector< ActorPtr > &out, TrafficManagerBase &tm)
This file contains definitions of common data structures used in traffic manager.