21 #ifndef G_PUBLISHER__H
22 #define G_PUBLISHER__H
38 class PublisherChannel ;
39 class PublisherSubscription ;
40 class PublisherSubscriber ;
42 G_EXCEPTION( PublisherError ,
"publish/subscribe error" ) ;
70 explicit Publisher(
const std::string & channel_name ,
bool auto_cleanup =
true ) ;
73 Publisher(
const std::string & channel_name ,
Item publisher_info ,
bool auto_cleanup =
true ) ;
89 void publish(
const char * p ,
size_t n ,
const char * type =
nullptr ) ;
92 void publish(
const std::vector<char> & ,
const char * type =
nullptr ) ;
95 void publish(
const std::vector<std::pair<const char*,size_t> > & ,
const char * type ) ;
98 void publish(
const std::vector<std::vector<char> > & ,
const char * type ) ;
101 static std::vector<std::string>
list( std::vector<std::string> * others =
nullptr ) ;
105 static void purge(
const std::string & channel_name ) ;
110 static std::string
delete_(
const std::string & channel_name ) ;
114 static Item info(
const std::string & channel_name ,
bool all_slots =
true ) ;
123 void publishStart() ;
130 unique_ptr<SharedMemory> m_shmem_data ;
131 unique_ptr<PublisherInfo> m_info ;
132 std::vector<const char*> m_data_p ;
133 std::vector<size_t> m_data_n ;
143 const std::string & socket_path_prefix = std::string() ) ;
168 bool receive(
size_t ,
int , std::vector<char> & , std::string * =
nullptr ,
G::EpochTime * =
nullptr ) ;
174 std::string
name()
const ;
184 unique_ptr<SharedMemory> m_shmem_data ;
185 std::string m_path_prefix ;
206 bool receive( std::vector<char> & buffer , std::string * type_p =
nullptr ,
G::EpochTime * time_p =
nullptr ) ;
211 bool peek( std::vector<char> & buffer , std::string * type_p =
nullptr ,
G::EpochTime * time_p =
nullptr ) ;
249 bool receive( std::vector<char> & buffer , std::string * type_p =
nullptr ,
G::EpochTime * time_p =
nullptr ) ;
255 bool peek( std::vector<char> & buffer , std::string * type_p =
nullptr ,
G::EpochTime * time_p =
nullptr ) ;
258 std::string
name()
const ;
261 std::string
info()
const ;
267 unsigned int age()
const ;
281 std::string m_channel_name ;
282 std::string m_channel_info ;
285 std::string m_path_prefix ;
286 unique_ptr<PublisherChannel> m_channel ;
287 unique_ptr<PublisherSubscriber> m_subscriber ;
PublisherSubscriber * subscribe()
Subscribes to the publisher returning a new()ed object.
PublisherSubscriber(PublisherChannel &, size_t slot_id, int socket_fd)
Pseudo-private constructor, used by G::PublisherChannel.
A subsecond-resolution timestamp based on a time_t.
bool receive(std::vector< char > &buffer, std::string *type_p=nullptr, G::EpochTime *time_p=nullptr)
Does a read for new publish()ed data.
bool peek(std::vector< char > &buffer, std::string *type_p=nullptr, G::EpochTime *time_p=nullptr)
Does a receive() but without requiring a publication event.
A publication-channel subscriber endpoint.
bool open()
Tries to initialise the object after lazy construction or after close().
static std::string delete_(const std::string &channel_name)
Deletes the channel.
Publisher(const std::string &channel_name, bool auto_cleanup=true)
Constructor for a publisher.
std::string name() const
Returns the channel name, as passed in to the constructor.
static std::vector< std::string > list(std::vector< std::string > *others=nullptr)
Returns a list of channel names.
PublisherChannel(const std::string &channel_name, const std::string &socket_path_prefix=std::string())
Constructor.
std::string name() const
Returns the channel name, as passed in to the constructor.
An easy-to-use combination of a G::PublisherChannel object and a single G::PublisherSubscriber.
bool peek(std::vector< char > &buffer, std::string *type_p=nullptr, G::EpochTime *time_p=nullptr)
Does a receive() but without requiring a publication event.
int fd() const
Returns the named socket file descriptor.
int fd() const
Returns the subscriber's file descriptor or minus one.
size_t id() const
Returns the slot id.
A named publisher channel that can be subscribed to.
PublisherSubscription(const std::string &channel_name, bool lazy=false)
Constructor.
~PublisherChannel()
Destructor.
void releaseSlot(size_t slot_id)
Used by PublisherSubscriber.
A POSIX shared memory class.
static void purge(const std::string &channel_name)
Tries to clean up any failed slots in the named channel.
void publish(const char *p, size_t n, const char *type=nullptr)
Publishes a chunk of data to subscribers.
A variant class holding a string, an item map keyed by name, or an ordered list of items...
A broadcast communication channel between unrelated processes using shared memory.
static Item info(const std::string &channel_name, bool all_slots=true)
Returns a variant containing information about the state of the channel.
bool receive(size_t, int, std::vector< char > &, std::string *=nullptr, G::EpochTime *=nullptr)
Used by PublisherSubscriber.
bool receive(std::vector< char > &buffer, std::string *type_p=nullptr, G::EpochTime *time_p=nullptr)
Does a read for new publish()ed data.
unsigned int age() const
Returns the age of the latest data in seconds, or zero.
std::string info() const
Returns some channel metadata as a short json string (see G::Item::str()).
~PublisherSubscriber()
Destructor.
void close()
Closes the channel subscription, releasing resources and becoming inactive.