knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::TokenBroadcaster Class Reference

#include <TokenBroadcaster.h>

Inheritance diagram for knowrob::TokenBroadcaster:
Collaboration diagram for knowrob::TokenBroadcaster:

Public Member Functions

 TokenBroadcaster ()
 
 ~TokenBroadcaster () override
 
void addSubscriber (const std::shared_ptr< Channel > &subscriber)
 
void removeSubscriber (const std::shared_ptr< Channel > &subscriber)
 
 TokenBroadcaster ()
 
 ~TokenBroadcaster () override
 
void addSubscriber (const std::shared_ptr< Channel > &subscriber)
 
void removeSubscriber (const std::shared_ptr< Channel > &subscriber)
 
- Public Member Functions inherited from knowrob::TokenStream
 TokenStream ()
 
virtual ~TokenStream ()
 
 TokenStream (const TokenStream &)=delete
 
virtual void close ()
 
bool isOpened () const
 
 TokenStream ()
 
virtual ~TokenStream ()
 
 TokenStream (const TokenStream &)=delete
 
virtual void close ()
 
bool isOpened () const
 

Protected Member Functions

void push (const TokenPtr &tok) override
 
virtual void pushToBroadcast (const TokenPtr &tok)
 
void push (const TokenPtr &tok) override
 
virtual void pushToBroadcast (const TokenPtr &tok)
 
- Protected Member Functions inherited from knowrob::TokenStream
virtual void push (Channel &channel, const TokenPtr &tok)
 
virtual void push (Channel &channel, const TokenPtr &tok)
 

Protected Attributes

std::list< std::shared_ptr< Channel > > subscribers_
 
std::mutex mtx_
 
- Protected Attributes inherited from knowrob::TokenStream
std::list< std::shared_ptr< Channel > > channels_
 
std::atomic< bool > isOpened_
 
std::mutex channel_mutex_
 

Detailed Description

A broadcaster of query results.

Definition at line 17 of file TokenBroadcaster.h.

Constructor & Destructor Documentation

◆ TokenBroadcaster() [1/2]

TokenBroadcaster::TokenBroadcaster ( )

Definition at line 12 of file TokenBroadcaster.cpp.

◆ ~TokenBroadcaster() [1/2]

TokenBroadcaster::~TokenBroadcaster ( )
override

Definition at line 15 of file TokenBroadcaster.cpp.

15  {
16  if (isOpened()) {
17  for (auto &x: subscribers_) {
18  x->push(EndOfEvaluation::get());
19  }
20  }
21 }
std::list< std::shared_ptr< Channel > > subscribers_
bool isOpened() const
Definition: TokenStream.cpp:44

◆ TokenBroadcaster() [2/2]

knowrob::TokenBroadcaster::TokenBroadcaster ( )

◆ ~TokenBroadcaster() [2/2]

knowrob::TokenBroadcaster::~TokenBroadcaster ( )
override

Member Function Documentation

◆ addSubscriber() [1/2]

void TokenBroadcaster::addSubscriber ( const std::shared_ptr< Channel > &  subscriber)

Add a subscriber to this broadcast. The subscriber will receive input from the broadcast after this call.

Parameters
subscribera query result stream.

Definition at line 23 of file TokenBroadcaster.cpp.

23  {
24  std::lock_guard<std::mutex> lock(mtx_);
25  subscribers_.push_back(subscriber);
26 }

◆ addSubscriber() [2/2]

void knowrob::TokenBroadcaster::addSubscriber ( const std::shared_ptr< Channel > &  subscriber)

Add a subscriber to this broadcast. The subscriber will receive input from the broadcast after this call.

Parameters
subscribera query result stream.

◆ push() [1/2]

void TokenBroadcaster::push ( const TokenPtr tok)
overrideprotectedvirtual

◆ push() [2/2]

void knowrob::TokenBroadcaster::push ( const TokenPtr tok)
overrideprotectedvirtual

Implements knowrob::TokenStream.

Reimplemented in knowrob::TokenBuffer.

◆ pushToBroadcast() [1/2]

void TokenBroadcaster::pushToBroadcast ( const TokenPtr tok)
protectedvirtual

Reimplemented in knowrob::NegationStage, and knowrob::NegationStage.

Definition at line 37 of file TokenBroadcaster.cpp.

37  {
38  // broadcast the query result to all subscribers.
39  // for now only allow one broadcast at a time: if multiple
40  // broadcasts are performed, there are all sorts of concurrency problems
41  // for stages in query pipelines, so we better avoid it for now.
42  // Also protect the list of subscribers with a mutex.
43  //std::lock_guard<std::shared_mutex> lock(mtx_);
44  std::lock_guard<std::mutex> lock(mtx_);
45  for (auto &x: subscribers_) {
46  x->push(tok);
47  }
48 }

◆ pushToBroadcast() [2/2]

virtual void knowrob::TokenBroadcaster::pushToBroadcast ( const TokenPtr tok)
protectedvirtual

◆ removeSubscriber() [1/2]

void TokenBroadcaster::removeSubscriber ( const std::shared_ptr< Channel > &  subscriber)

Remove a previously added subscriber.

Parameters
subscribera query result stream.

Definition at line 28 of file TokenBroadcaster.cpp.

28  {
29  std::lock_guard<std::mutex> lock(mtx_);
30  subscribers_.remove(subscriber);
31 }

◆ removeSubscriber() [2/2]

void knowrob::TokenBroadcaster::removeSubscriber ( const std::shared_ptr< Channel > &  subscriber)

Remove a previously added subscriber.

Parameters
subscribera query result stream.

Member Data Documentation

◆ mtx_

std::mutex knowrob::TokenBroadcaster::mtx_
protected

Definition at line 38 of file TokenBroadcaster.h.

◆ subscribers_

std::list< std::shared_ptr< Channel > > knowrob::TokenBroadcaster::subscribers_
protected

Definition at line 37 of file TokenBroadcaster.h.


The documentation for this class was generated from the following files: