2 #ifndef GRAMODS_NETWORK_SYNCNODE
3 #define GRAMODS_NETWORK_SYNCNODE
5 #include <gmNetwork/config.hh>
8 #include <gmCore/io_size.hh>
10 #include <gmNetwork/Protocol.hh>
12 #include <gmCore/Object.hh>
13 #include <gmCore/OFactory.hh>
14 #include <gmCore/PreConditionViolation.hh>
19 BEGIN_NAMESPACE_GMNETWORK;
57 void addPeer(std::string address);
64 void setLocalPeerIdx(
size_t idx);
69 size_t getLocalPeerIdx();
78 void setExitWhenAPeerIsDisconnected(
bool on);
87 void setTimeoutDelay(
float t);
93 float getTimeoutDelay();
99 void waitForConnection();
109 std::size_t getPeersCount();
114 std::set<std::size_t> getConnectedPeers();
128 template<
typename TYPE>
130 static_assert(std::is_base_of<Protocol, TYPE>::value,
131 "Specified protocol is not a Protocol");
133 std::string name =
typeid(TYPE).name();
134 Protocol * protocol = getProtocol(name);
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;
146 std::shared_ptr<TYPE> typed_protocol = std::make_shared<TYPE>();
147 addProtocol(name, typed_protocol);
149 return static_cast<TYPE*
>(getProtocol(name));
151 }
catch (std::bad_weak_ptr &) {
153 (
"SyncNode instance must be held by a std::shared_ptr");
161 Protocol * getProtocol(std::string name);
174 void addProtocol(std::string name, std::shared_ptr<Protocol> prot);
184 void initialize()
override;
197 std::unique_ptr<Impl> _impl;
201 END_NAMESPACE_GMNETWORK;
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
virtual std::string getDefaultKey() override
Returns the default key, in Configuration, for the Object.
Definition: SyncNode.hh:190
TYPE * getProtocol()
Returns an instance of the templated protocol.
Definition: SyncNode.hh:129
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