pub struct Topic {
pub address: Address,
pub channel: Channel<Command>,
pub subscribers: SubscriberBucket,
pub cache: Vec<Transaction>,
/* private fields */
}Expand description
The main structure for representing Topics in the system. It will be the main interaction point for the user. Each Topic the user has will also require a copy of the same Topic in the Handler Thread. The only difference between the two is the opposing Channel, with which the two can communicate. TODO: The thread might require a dedicated struct. TODO: Add methods for fetching fields.
Fields§
§address: AddressThroughout the entire system all components have the same Address type. Each Topic also has a uniqe Address, which can be generated through any number of ways.
channel: Channel<Command>Since each Topic can receive messages individually a dedicated Channel (mpsc connection) is required.
subscribers: SubscriberBucketList of subscribers
cache: Vec<Transaction>The socket can get overread so a cache is required.
Implementations§
Source§impl Topic
impl Topic
Sourcepub fn new(
address: Address,
channel: Channel<Command>,
subscribers: Vec<Address>,
public: Address,
) -> Self
pub fn new( address: Address, channel: Channel<Command>, subscribers: Vec<Address>, public: Address, ) -> Self
Creates a new Topic with a given Channel. This Topic is meant to be used both by the User and the Handler Thread. This function is not meant to be called by the user, since it requires the linked Channel to be stored on the Handler therad. Instead new Topics have to be created through the interface.
Sourcepub fn recv(&mut self) -> Option<Transaction>
pub fn recv(&mut self) -> Option<Transaction>
Blocking call to receive a Message from a Topic. It will only return once a Message from the system (usually from another user) is available or the Channel is unavailable. Since commands are sent over the same Channel the recv method takes a mutable reference to the Topic and can add / remove subscribers.
(Should it receive a Send message it will simply report an error.)
Sourcepub fn try_recv(&mut self) -> Option<Transaction>
pub fn try_recv(&mut self) -> Option<Transaction>
Behaves the same as “recv” but is non-blocking. Internally it still uses a loop to filter out non-user messages and will return on a User message or no message at all.
Sourcepub fn broadcast(&mut self, body: Vec<u8>) -> Result<(), Error>
pub fn broadcast(&mut self, body: Vec<u8>) -> Result<(), Error>
The main function for sending Messages to all subscribed
users. It takes in a Vec
Sourcepub fn unsubscribe(&mut self)
pub fn unsubscribe(&mut self)
In the future this should be replaced by an automatic Drop implementation, currently a manual “unsubscribe” function is required to inform other users about the change. It simply sends an Unsubscribe action to each subscriber.