Mailbox.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <swarmio/Node.h>
4 #include <swarmio/Endpoint.h>
5 #include <swarmio/data/Message.pb.h>
6 
7 namespace swarmio
8 {
13  class SWARMIO_API Mailbox
14  {
15  private:
16 
22 
23  protected:
24 
30  : _endpoint(nullptr) { }
31 
37  Mailbox(Endpoint* endpoint)
38  {
39  _endpoint = endpoint;
40  }
41 
47  {
48  if (_endpoint != nullptr)
49  {
50  _endpoint->RegisterMailbox(this);
51  }
52  }
53 
58  Mailbox(Mailbox&& other)
59  {
60  // Copy over endpoint reference
61  _endpoint = other.GetEndpoint();
62  }
63 
70  void FinishMovingTo(Mailbox* other)
71  {
72  if (_endpoint != nullptr)
73  {
74  _endpoint->ReplaceMailbox(this, other);
75  _endpoint = nullptr;
76  }
77  }
78 
79  public:
80 
85  Mailbox(const Mailbox&) = delete;
86 
92  Mailbox& operator=(const Mailbox&) = delete;
93 
102  virtual bool ReceiveMessage(const Node* sender, const data::Message* message) { return false; }
103 
109  virtual void NodeWasDiscovered(const Node* node) noexcept { }
110 
116  virtual void NodeDidJoin(const Node* node) noexcept { }
117 
123  virtual void NodeWillLeave(const Node* node) noexcept { }
124 
130  virtual void MailboxWasConnected() noexcept { }
131 
137  virtual void MailboxWillBeDisconnected() noexcept { }
138 
145  {
146  return _endpoint;
147  }
148 
153  virtual void Disconnect()
154  {
155  if (_endpoint != nullptr)
156  {
157  _endpoint->UnregisterMailbox(this);
158  _endpoint = nullptr;
159  }
160  }
161 
165  virtual ~Mailbox()
166  {
167  Disconnect();
168  }
169  };
170 }
void FinishMovingTo(Mailbox *other)
Called when message handling should be passed onto the new instance.
Definition: Mailbox.h:70
virtual void RegisterMailbox(Mailbox *mailbox)=0
Register a Mailbox to receive messages.
virtual ~Mailbox()
Destroy the Mailbox object.
Definition: Mailbox.h:165
Endpoint * _endpoint
Associated endpoint.
Definition: Mailbox.h:21
virtual void ReplaceMailbox(Mailbox *oldMailbox, Mailbox *newMailbox)=0
Relocate a mailbox to another in-memory location.
virtual void NodeWasDiscovered(const Node *node) noexcept
Called when a new Node has been discovered.
Definition: Mailbox.h:109
virtual void NodeDidJoin(const Node *node) noexcept
Called when a new Node has joined the group.
Definition: Mailbox.h:116
void FinishConstruction()
Called when the last constructor has finished its job.
Definition: Mailbox.h:46
virtual void MailboxWillBeDisconnected() noexcept
Called right before the mailbox is disconnected from its endpoint or if the attached endpoint is abou...
Definition: Mailbox.h:137
virtual void MailboxWasConnected() noexcept
Called when the mailbox is attached to an already running endpoint or if the attached endpoint has ju...
Definition: Mailbox.h:130
virtual void UnregisterMailbox(Mailbox *mailbox)=0
Unregister a Mailbox from receiving messages.
virtual bool ReceiveMessage(const Node *sender, const data::Message *message)
Delivery point of all messages.
Definition: Mailbox.h:102
Abstract base class for Endpoint implementations.
Definition: Endpoint.h:25
Mailbox()
Construct a disconnected Mailbox.
Definition: Mailbox.h:29
Endpoint * GetEndpoint()
Get the associated Endpoint.
Definition: Mailbox.h:144
Mailbox(Mailbox &&other)
Move a Mailbox object.
Definition: Mailbox.h:58
Mailbox(Endpoint *endpoint)
Construct a new Mailbox object.
Definition: Mailbox.h:37
virtual void NodeWillLeave(const Node *node) noexcept
Called when a Node signals that it will leave.
Definition: Mailbox.h:123
Represents a Node the Endpoint knows about and can send messages to.
virtual void Disconnect()
Disconnect this Mailbox from the Endpoint.
Definition: Mailbox.h:153
Abstract base class for Mailbox implementations.
Definition: Mailbox.h:13


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48