47 #ifndef _MIRA_CHANNELMANAGER_H_ 48 #define _MIRA_CHANNELMANAGER_H_ 85 internal(iInternal) {}
94 return id == other.
id;
105 template<
typename Reflector>
108 r.interface(
"IChannelManager");
110 r.method(
"createChannel",
111 [
this](
const std::string& channelID,
const std::string&
type) {
113 auto ch = publish<void>(id,
"",
true,
type);
114 ChannelPromoteByTypename::instance().promoteChannel(ch,
type);
116 "Publish a channel with given type. If type is registered for automatic promotion, " 117 "the channel will be promoted to typed immediately, else remains untyped.",
118 "channelID",
"ID of channel to create",
"/namespace/Rect2i",
119 "type",
"Typename of channel type",
"mira::Rect<int,2>");
121 r.method(
"writeChannel",
122 [
this](
const std::string& channelID,
const json::Value& value,
123 Time timestamp, std::string frameID, uint32 sequenceID) {
124 writeChannel(channelID, value, std::move(timestamp),
125 std::move(frameID), sequenceID);
127 "Writes a json representation to the channel, " 128 "with specified timestamp, frame and sequence ID.",
129 "channelID",
"ID of channel to write to",
"/robot/RobotFrame",
132 "frameID",
"data frame ID",
"/robot/OdometryFrame",
133 "sequenceID",
"data sequence ID", 0);
134 r.method(
"writeChannel",
135 [
this](
const std::string& channelID,
const json::Value& value) {
136 writeChannel(channelID, value);
138 "Writes a json representation to the channel " 139 "with timestamp taken from clock 'now', default frame ID and increasing sequence ID.",
140 "channelID",
"ID of channel to write to",
"/robot/RobotFrame",
142 r.method(
"writeChannel",
143 [
this](
const std::string& channelID,
const json::Value& value,
Time timestamp) {
144 writeChannel(channelID, value, std::move(timestamp));
146 "Writes a json representation to the channel with specified timestamp, " 147 "default frame ID and increasing sequence ID.",
148 "channelID",
"ID of channel to write to",
"/robot/RobotFrame",
151 r.method(
"writeChannelWithFrameID",
152 [
this](
const std::string& channelID,
const json::Value& value, std::string frameID) {
153 writeChannel(channelID, value, std::move(frameID));
155 "Writes a json representation to the channel with specified frame ID, " 156 "timestamp taken from clock 'now' and increasing sequence ID.",
157 "channelID",
"ID of channel to write to",
"/robot/RobotFrame",
159 "frameID",
"data frame ID",
"/robot/OdometryFrame");
162 "Reads the latest channel data as json representation",
163 "channelID",
"ID of channel to read",
"/robot/RobotFrame");
165 "get list of channels",
166 "publishedOnly",
"if true, include only channels with (local) publishers",
false,
167 "subscribedOnly",
"if true, include only channels with (local) subscribers",
false);
193 const std::string& publisherID,
195 bool noAutoPromoteCheck =
false)
199 channel->fixateType();
201 if ( channel->isTyped() ) {
202 Typename channelType = channel->getTypename();
203 if ((channelType !=
type) &&
207 ((channelType[channelType.size()-1] !=
'*') || (typeName<T>() ==
"void")))
208 MIRA_THROW(XBadCast,
"Invalid publish<void> to typed channel '" << channelID <<
209 "'. Typename does not match. ('" <<
210 type <<
"' != '" << channel->getTypename() <<
"')");
212 if (mCheckChannelRegistrations && !noAutoPromoteCheck)
213 ChannelPromoteByTypename::instance().checkForPromoter(channel);
215 if (!channel->getTypename().empty() && (channel->getTypename() !=
type))
216 MIRA_THROW(XBadCast,
"Invalid publish<void> to untyped channel '" << channelID <<
217 "' (with set typename). Typename does not match. ('" <<
218 type <<
"' != '" << channel->getTypename() <<
"')");
220 channel->setTypename(
type);
224 insertPublishedChannel(channelID,
ChannelInfo(publisherID,
internal));
225 MIRA_LOG(
DEBUG) <<
"'" << publisherID <<
"' published channel '" << channelID <<
"'";
229 remotePublishChannel(channelID,
type);
272 const std::string& subscriberID,
273 const Duration& storageDuration,
bool internal)
277 assert(channel->getBuffer()!=NULL);
278 channel->getBuffer()->setStorageDuration(storageDuration);
279 insertSubscribedChannel(channelID,
ChannelInfo(subscriberID,
internal));
280 MIRA_LOG(
DEBUG) <<
"'" << subscriberID <<
"' subscribed to channel '" << channelID <<
"'";
283 remoteSubscribeChannel(channelID);
294 void removePublisherFromAllChannels(
const std::string& publisherID,
bool internal);
303 void removePublisher(
const std::string& channelID,
const std::string& publisherID,
bool internal);
309 void removeSubscriberFromAllChannels(
const std::string& subscriberID);
316 void removeSubscriber(
const std::string& channelID,
const std::string& subscriberID);
323 void deductNumSubscribers(
const std::string& channelID);
335 return channel_cast<T>(getAbstractChannel(channelID));
343 std::set<std::string> getChannels()
const;
356 std::set<std::string> getChannelList(
bool publishedOnly =
false,
357 bool subscribedOnly =
false)
const;
365 std::set<std::string> getSubscribedChannels(
bool includeInternal =
false)
const;
376 std::set<std::string> getSubscribedChannelsBy(
const std::string& subscriberID)
const;
381 bool isSubscribedOn(
const std::string& subscriberID,
382 const std::string& channelID)
const;
390 std::map<std::string, Typename> getPublishedChannels(
bool includeInternal =
false)
const;
401 std::set<std::string> getPublishedChannelsBy(
const std::string& publisherID)
const;
406 bool hasPublished(
const std::string& publisherID,
const std::string& channelID)
const;
414 std::list<std::pair<std::string, Typename>> getChannelsOfType(
const Typename&
type)
const;
422 template <
typename T>
424 return getChannelsOfType(typeName<T>());
434 bool hasSubscriber(
const std::string& channelID)
const;
443 bool hasPublisher(
const std::string& channelID,
bool includeInternal)
const;
448 uint32 getNrPublishers(
const std::string& channelID)
const;
453 std::set<std::string> getPublishers(
const std::string& channelID,
454 bool includeInternal =
false)
const;
459 uint32 getNrSubscribers(
const std::string& channelID)
const;
464 std::set<std::string> getSubscribers(
const std::string& channelID,
465 bool includeInternal =
false)
const;
471 bool hasChannel(
const std::string& channelID)
const;
477 int getTypeId(
const std::string& channelID)
const;
482 void setTypename(
const std::string& channelID,
const Typename& typenam);
488 Typename getTypename(
const std::string& channelID)
const;
494 void setTypeMeta(
const std::string& channelID,
TypeMetaPtr meta);
499 TypeMetaPtr getTypeMeta(
const std::string& channelID)
const;
506 Time getLastSlotTime(
const std::string& channelID)
const;
511 std::size_t getNrOfSlots(
const std::string& channelID)
const;
517 uint64 getNrOfDataChanges(
const std::string& channelID)
const;
527 obtainConcreteChannel<void>(channelID);
535 void setStorageDuration(
const std::string& channelID,
const Duration& storageDuration);
542 void setAutoIncreaseStorageDuration(
const std::string& channelID,
bool increase);
549 void setMinSlots(
const std::string& channelID, std::size_t minSlots);
556 void setMaxSlots(
const std::string& channelID, std::size_t maxSlots);
559 Duration getStorageDuration(
const std::string& channelID);
562 bool isAutoIncreasingStorageDuration(
const std::string& channelID);
565 std::size_t getMinSlots(
const std::string& channelID);
568 std::size_t getMaxSlots(
const std::string& channelID);
575 void writeChannel(
const std::string& channelID,
586 void writeChannel(
const std::string& channelID,
594 void writeChannel(
const std::string& channelID,
603 json::Value readChannel(
const std::string& channelID);
613 const AbstractChannel* getAbstractChannel(
const std::string& channelID)
const;
637 const AbstractChannel* getAbstractChannelNoLocking(
const std::string& channelID)
const;
649 boost::mutex::scoped_lock lock(mChannelsMutex);
654 channel = channel_cast<T>(getAbstractChannelNoLocking(channelID));
656 catch (XUnknownChannel&)
659 channel =
new ConcreteChannel<T>(channelID);
660 mChannels.insert(ChannelMap::value_type(channelID, channel));
667 void remotePublishChannel(
const std::string& channelID,
const Typename&
type);
668 void remoteUnpublishChannel(
const std::string& channelID);
669 void remoteSubscribeChannel(
const std::string& channelID);
670 void remoteUnsubscribeChannel(
const std::string& channelID);
676 void insertPublishedChannel(
const std::string& channelID,
const ChannelInfo& info);
677 void insertSubscribedChannel(
const std::string& channelID,
const ChannelInfo& info);
681 typedef std::map<std::string, AbstractChannelPtr > ChannelMap;
682 ChannelMap mChannels;
683 mutable boost::mutex mChannelsMutex;
688 mutable boost::mutex mSubscriberMutex;
689 mutable boost::mutex mPublisherMutex;
691 bool mAutoPromoteChannels;
692 bool mCheckChannelRegistrations;
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:93
bool operator==(const ChannelInfo &other) const
Definition: ChannelManager.h:92
Typedefs for OS independent basic data types.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
std::string Typename
Definition: Typename.h:60
#define MIRA_LOG(level)
Use this macro to log data.
Definition: LoggingCore.h:529
bool checkChannelRegistrationsEnabled()
Query if check for channel registrations in publish<T>(...) is enabled.
Definition: ChannelManager.h:246
#define MIRA_DEFINE_SERIALIZABLE_EXCEPTION(Ex, Base)
Macro for easily defining a new serializable exception class.
Definition: Exceptions.h:66
json::Value readChannel(const std::string &channelID)
Reads data from the channel with given id.
static ResourceName makeFullyQualified(const ResourceName &name, const ResourceName &ns)
Definition: ChannelReadWrite.h:65
void serialize(const std::string &name, const T &value, const std::string &comment="")
Serializes the specified object value under the given name.
Definition: Serializer.h:204
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
ConcreteChannel< T > * publish(const std::string &channelID, const std::string &publisherID, bool internal, const Typename &type, bool noAutoPromoteCheck=false)
Publishes a channel with the given id.
Definition: ChannelManager.h:192
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
void enableAutoPromoteChannels(bool enable)
Enable/disable automatic promotion of channel to typed in publish<void>(...)
Definition: ChannelManager.h:235
std::set< std::string > getChannelList(bool publishedOnly=false, bool subscribedOnly=false) const
Get a list of all channels managed by this manager, can be filtered for only those that have subscrib...
ChannelInfo()
Definition: ChannelManager.h:81
void enableCheckChannelRegistrations(bool enable)
Enable/disable check for registration of channel types in publish<T>(...) (T!=void) ...
Definition: ChannelManager.h:243
void reflect(Reflector &r)
Reflect method for serialization.
Definition: ChannelManager.h:106
Commonly used exception classes.
Includes, defines and functions for threads.
std::string id
Definition: ChannelManager.h:97
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Definition: AbstractChannel.h:70
Use this class to represent time durations.
Definition: Time.h:104
bool autoPromoteChannelsEnabled()
Query if automatic promotion of channel to typed in publish<void>(...) is enabled.
Definition: ChannelManager.h:238
ChannelInfo(const std::string &iID, bool iInternal=false)
Definition: ChannelManager.h:83
ConcreteChannel< T > * subscribe(const std::string &channelID, const std::string &subscriberID, const Duration &storageDuration, bool internal)
Subscribes to a channel with the given id.
Definition: ChannelManager.h:271
boost::shared_ptr< TypeMeta > TypeMetaPtr
Definition: MetaSerializer.h:309
Framework channel classes.
Informations about a channel that also provides ordering of channels by id.
Definition: ChannelManager.h:79
json_spirit::mValue Value
A value is an abstract description of data in JSON (underlying data can either be one of the JSON bas...
Definition: JSON.h:176
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:479
An exception that occurred whenever a channel does not exist.
Definition: ChannelManager.h:75
Typedefs for different Pose datatypes that are internally RigidTransforms.
Definition: LoggingCore.h:78
std::list< std::pair< std::string, Typename > > getChannelsOfType() const
Returns a list of all channels that match the specified type.
Definition: ChannelManager.h:423
bool operator<(const ChannelInfo &other) const
Definition: ChannelManager.h:87
void ensureChannel(const std::string &channelID)
Just make sure the specified channel exists (by creating it if needed), without changing any attribut...
Definition: ChannelManager.h:525
ConcreteChannel< T > * getConcreteChannel(const std::string &channelID)
Returns the existing channel with the given id.
Definition: ChannelManager.h:333