CARLA
ServerSideSensor.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
8 
9 #include "carla/Logging.h"
11 
12 #include <exception>
13 
14 namespace carla {
15 namespace client {
16 
18  if (IsAlive() && IsListening()) {
20  "sensor object went out of the scope but the sensor is still alive",
21  "in the simulation:",
22  GetDisplayId());
23  }
24  if (IsListening() && GetEpisode().IsValid()) {
25  try {
26  Stop();
27  } catch (const std::exception &e) {
28  log_error("exception trying to stop sensor:", GetDisplayId(), ':', e.what());
29  }
30  }
31  }
32 
34  log_debug(GetDisplayId(), ": subscribing to stream");
35  GetEpisode().Lock()->SubscribeToSensor(*this, std::move(callback));
36  _is_listening = true;
37  }
38 
40  if (!_is_listening) {
42  "attempting to unsubscribe from stream but sensor wasn't listening:",
43  GetDisplayId());
44  return;
45  }
46  GetEpisode().Lock()->UnSubscribeFromSensor(*this);
47  _is_listening = false;
48  }
49 
51  if (IsListening()) {
52  Stop();
53  }
54  return Actor::Destroy();
55  }
56 
57 } // namespace client
58 } // namespace carla
void Listen(CallbackFunctionType callback) override
Register a callback to be executed each time a new measurement is received.
static void log_error(Args &&... args)
Definition: Logging.h:110
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
bool IsListening() const override
Return whether this Sensor instance is currently listening to the associated sensor in the simulator...
static void log_debug(Args &&...)
Definition: Logging.h:75
const std::string & GetDisplayId() const
Definition: ActorState.h:33
virtual bool Destroy()
Tell the simulator to destroy this Actor, and return whether the actor was successfully destroyed...
Definition: Actor.cpp:91
static void log_warning(Args &&... args)
Definition: Logging.h:96
bool Destroy() override
Tell the simulator to destroy this Actor, and return whether the actor was successfully destroyed...
std::function< void(SharedPtr< sensor::SensorData >)> CallbackFunctionType
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
bool IsAlive() const
Definition: client/Actor.h:108
void Stop() override
Stop listening for new measurements.