gramods
Loading...
Searching...
No Matches
SyncNode.hh
1
2#ifndef GRAMODS_NETWORK_SYNCNODE
3#define GRAMODS_NETWORK_SYNCNODE
4
5#include <gmNetwork/config.hh>
6
7// Required before gmCore/OFactory.hh for some compilers
8#include <gmCore/io_size.hh>
9
10#include <gmNetwork/Protocol.hh>
11
12#include <gmCore/Object.hh>
13#include <gmCore/OFactory.hh>
14#include <gmCore/PreConditionViolation.hh>
15
16#include <memory>
17#include <set>
18
19BEGIN_NAMESPACE_GMNETWORK;
20
34 : public gmCore::Object {
35
36public:
37
38 SyncNode();
39 ~SyncNode();
40
57 void addPeer(std::string address);
58
64 void setLocalPeerIdx(size_t idx);
65
69 size_t getLocalPeerIdx();
70
78 void setExitWhenAPeerIsDisconnected(bool on);
79
87 void setTimeoutDelay(float t);
88
93 float getTimeoutDelay();
94
99 void waitForConnection();
100
104 bool isConnected();
105
109 std::size_t getPeersCount();
110
114 std::set<std::size_t> getConnectedPeers();
115
128 template<typename TYPE>
129 TYPE * getProtocol() {
130 static_assert(std::is_base_of<Protocol, TYPE>::value,
131 "Specified protocol is not a Protocol");
132
133 std::string name = typeid(TYPE).name();
134 Protocol * protocol = getProtocol(name);
135
136 if (protocol) {
137 TYPE * typed_protocol = dynamic_cast<TYPE*>(protocol);
138 if (typed_protocol == nullptr)
140 ("Requested protocol exists but has wrong type.");
141 return typed_protocol;
142 }
143
144 try {
145
146 std::shared_ptr<TYPE> typed_protocol = std::make_shared<TYPE>();
147 addProtocol(name, typed_protocol);
148
149 return static_cast<TYPE*>(getProtocol(name));
150
151 } catch (std::bad_weak_ptr &) {
153 ("SyncNode instance must be held by a std::shared_ptr");
154 }
155 }
156
161 Protocol * getProtocol(std::string name);
162
167 Protocol * getProtocol(char id);
168
174 void addProtocol(std::string name, std::shared_ptr<Protocol> prot);
175
179 void sendMessage(Protocol::Message mess);
180
184 void initialize() override;
185
190 virtual std::string getDefaultKey() override { return "syncNode"; }
191
192 GM_OFI_DECLARE;
193
194private:
195
196 struct Impl;
197 std::unique_ptr<Impl> _impl;
198
199};
200
201END_NAMESPACE_GMNETWORK;
202
203#endif
Base type for objects in the Gramods package for standardized handling of construction,...
Definition Object.hh:42
TODO: write this.
Definition Protocol.hh:21
A handler of network communication with multiple peers.
Definition SyncNode.hh:34
TYPE * getProtocol()
Returns an instance of the templated protocol.
Definition SyncNode.hh:129
virtual std::string getDefaultKey() override
Returns the default key, in Configuration, for the Object.
Definition SyncNode.hh:190
Standard exception for violation of pre conditions in a call to a function or object.
Definition PreConditionViolation.hh:16
Data entity communicated by the connection to the designated protocol.
Definition Protocol.hh:43