ZG  "1.00
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
ZGPeerSession.h
Go to the documentation of this file.
1 #ifndef ZGPeerSession_h
2 #define ZGPeerSession_h
3 
4 #include "reflector/StorageReflectSession.h"
5 #include "system/DetectNetworkConfigChangesSession.h" // for INetworkConfigChangesTarget
6 
8 #include "zg/ZGPeerID.h"
9 #include "zg/ZGPeerSettings.h"
10 #include "zg/ZGStdinSession.h" // for ITextCommandReceiver
11 
12 #include "zg/private/PZGBeaconData.h"
13 #include "zg/private/PZGDatabaseState.h"
14 #include "zg/private/PZGDatabaseUpdate.h"
15 #include "zg/private/PZGUpdateBackOrderKey.h"
16 
17 namespace zg_private
18 {
19  class PZGNetworkIOSession; // forward declaration
20 };
21 
22 namespace zg
23 {
24 
28 class ZGPeerSession : public StorageReflectSession, public ITextCommandReceiver, public INetworkConfigChangesTarget, public INetworkTimeProvider
29 {
30 public:
34  ZGPeerSession(const ZGPeerSettings & peerSettings);
35 
39  virtual status_t AttachedToServer();
40 
42  virtual void AboutToDetachFromServer();
43 
47  virtual void EndSession();
48 
53  virtual uint64 GetPulseTime(const PulseArgs & args);
54 
58  virtual void Pulse(const PulseArgs & args);
59 
61  virtual bool IsReadyForTextCommands() const {return _iAmFullyAttached;}
62 
67  virtual bool TextCommandReceived(const String & text);
68 
70  bool IAmTheSeniorPeer() const;
71 
73  const ZGPeerSettings & GetPeerSettings() const {return _peerSettings;}
74 
80  virtual void NetworkInterfacesChanged(const Hashtable<String, Void> & optInterfaceNames) {(void) optInterfaceNames;}
81 
83  virtual void ComputerIsAboutToSleep() {_iAmFullyAttached = false;} // we'll re-fully-attach after we wake up again
84 
86  virtual void ComputerJustWokeUp() {/* empty */}
87 
89  bool IAmFullyAttached() const {return _iAmFullyAttached;}
90 
92  virtual void LocalSeniorPeerStatusChanged();
93 
95  const ZGPeerID & GetLocalPeerID() const {return _localPeerID;}
96 
98  const ZGPeerID & GetSeniorPeerID() const {return _seniorPeerID;}
99 
105  virtual uint64 GetNetworkTime64() const {return _iAmFullyAttached ? GetNetworkTime64ForRunTime64(GetRunTime64()) : 0;}
106 
110  virtual uint64 GetRunTime64ForNetworkTime64(uint64 networkTime64TimeStamp) const {return (networkTime64TimeStamp==MUSCLE_TIME_NEVER)?MUSCLE_TIME_NEVER:(networkTime64TimeStamp-GetToNetworkTimeOffset());}
111 
115  virtual uint64 GetNetworkTime64ForRunTime64(uint64 runTime64TimeStamp) const {return (runTime64TimeStamp==MUSCLE_TIME_NEVER)?MUSCLE_TIME_NEVER:(runTime64TimeStamp+GetToNetworkTimeOffset());}
116 
120  virtual int64 GetToNetworkTimeOffset() const;
121 
126  const Hashtable<ZGPeerID, ConstMessageRef> & GetOnlinePeers() const {return _onlinePeers;}
127 
131  bool IsPeerOnline(const ZGPeerID & peerID) const;
132 
137  uint64 GetEstimatedLatencyToPeer(const ZGPeerID & peerID) const;
138 
139 protected:
146  status_t RequestResetDatabaseStateToDefault(uint32 whichDatabase);
147 
155  status_t RequestReplaceDatabaseState(uint32 whichDatabase, const MessageRef & newDatabaseStateMsg);
156 
165  status_t RequestUpdateDatabaseState(uint32 whichDatabase, const MessageRef & databaseUpdateMsg);
166 
168  virtual void MessageReceivedFromGateway(const MessageRef & msg, void * userData);
169 
174  virtual void ResetLocalDatabaseToDefault(uint32 whichDatabase, uint32 & dbChecksum) = 0;
175 
187  virtual ConstMessageRef SeniorUpdateLocalDatabase(uint32 whichDatabase, uint32 & dbChecksum, const ConstMessageRef & seniorDoMsg) = 0;
188 
197  virtual status_t JuniorUpdateLocalDatabase(uint32 whichDatabase, uint32 & dbChecksum, const ConstMessageRef & juniorDoMsg) = 0;
198 
203  virtual MessageRef SaveLocalDatabaseToMessage(uint32 whichDatabase) const = 0;
204 
212  virtual status_t SetLocalDatabaseFromMessage(uint32 whichDatabase, uint32 & dbChecksum, const ConstMessageRef & newDBStateMsg) = 0;
213 
219  virtual uint32 CalculateLocalDatabaseChecksum(uint32 whichDatabase) const = 0;
220 
228  virtual String GetLocalDatabaseContentsAsString(uint32 whichDatabase) const;
229 
235  virtual void PeerHasComeOnline(const ZGPeerID & peerID, const ConstMessageRef & peerInfo);
236 
242  virtual void PeerHasGoneOffline(const ZGPeerID & peerID, const ConstMessageRef & peerInfo);
243 
248  virtual void SeniorPeerChanged(const ZGPeerID & oldSeniorPeerID, const ZGPeerID & newSeniorPeerID);
249 
256  status_t SendMulticastUserMessageToAllPeers(const MessageRef & msg);
257 
262  status_t SendUnicastUserMessageToAllPeers(const MessageRef & msg);
263 
269  status_t SendUnicastUserMessageToPeer(const ZGPeerID & destinationPeerID, const MessageRef & msg);
270 
274  void PrintDatabaseStateInfo(int32 whichDatabase = -1) const;
275 
279  void PrintDatabaseUpdateLog(int32 whichDatabase = -1) const;
280 
281 private:
282  void ScheduleSetBeaconData();
283  void ShutdownChildSessions();
284  status_t SendRequestToSeniorPeer(uint32 whichDatabase, uint32 whatCode, const MessageRef & userMsg);
285  status_t HandleDatabaseUpdateRequest(const ZGPeerID & fromPeerID, const MessageRef & msg, bool isMessageMeantForSeniorPeer);
286  status_t SendDatabaseUpdateViaMulticast(const zg_private::ConstPZGDatabaseUpdateRef & dbUp);
287  status_t RequestBackOrderFromSeniorPeer(const zg_private::PZGUpdateBackOrderKey & ubok);
288  zg_private::ConstPZGBeaconDataRef GetNewSeniorBeaconData() const;
289 
290  // These methods are called from the PZGNetworkIOSession code
291  void MessageReceivedFromPeer(const ZGPeerID & peerID, const MessageRef & msg);
292  void BeaconDataChanged(const zg_private::ConstPZGBeaconDataRef & beaconData);
293  void BackOrderResultReceived(const zg_private::PZGUpdateBackOrderKey & ubok, const zg_private::ConstPZGDatabaseUpdateRef & optUpdateData);
294  zg_private::ConstPZGDatabaseUpdateRef GetDatabaseUpdateByID(uint32 whichDatabase, uint64 updateID) const;
295 
296  const ZGPeerSettings _peerSettings;
297 
298  const ZGPeerID _localPeerID;
299  ZGPeerID _seniorPeerID;
300 
301  AbstractReflectSessionRef _networkIOSession;
302  bool _iAmFullyAttached;
303 
304 #ifndef DOXYGEN_SHOULD_IGNORE_THIS
305  friend class zg_private::PZGDatabaseState;
306  friend class zg_private::PZGNetworkIOSession;
307 #endif
308 
309  Queue<zg_private::PZGDatabaseState> _databases;
310  bool _setBeaconDataPending;
311 
312  Hashtable<ZGPeerID, ConstMessageRef> _onlinePeers;
313 };
314 DECLARE_REFTYPES(ZGPeerSession);
315 
316 }; // end namespace zg
317 
318 #endif
virtual uint32 CalculateLocalDatabaseChecksum(uint32 whichDatabase) const =0
This method is used for sanity-checking.
This immutable class holds various read-only settings that will be used to define the peer's behavior...
This is an abstract interface for an object that can provide us with network-clock-time values...
status_t RequestReplaceDatabaseState(uint32 whichDatabase, const MessageRef &newDatabaseStateMsg)
Call this if you want to request that the specified database be completely replaced with a new state ...
virtual uint64 GetPulseTime(const PulseArgs &args)
Returns the time (in microseconds, using the timebase of GetRunTime64()) when Pulse() should next be ...
status_t RequestUpdateDatabaseState(uint32 whichDatabase, const MessageRef &databaseUpdateMsg)
Call this if you want to request that the specified database be incrementally updated to a new state ...
uint64 GetEstimatedLatencyToPeer(const ZGPeerID &peerID) const
Gets our current estimate of the one-way network latency between us and the specified peer...
virtual status_t AttachedToServer()
Called when this session object is attached to the ReflectServer object.
bool IsPeerOnline(const ZGPeerID &peerID) const
Returns true iff if the specified peer is currently online.
status_t RequestResetDatabaseStateToDefault(uint32 whichDatabase)
Call this if you want to request that the specified database be reset back to its well-known default ...
virtual uint64 GetNetworkTime64ForRunTime64(uint64 runTime64TimeStamp) const
Given a local-time-clock-value (i.e.
virtual void Pulse(const PulseArgs &args)
Called at (approximately) the time specified by GetPulseTime().
virtual status_t JuniorUpdateLocalDatabase(uint32 whichDatabase, uint32 &dbChecksum, const ConstMessageRef &juniorDoMsg)=0
This method will only be called on junior peers.
virtual ConstMessageRef SeniorUpdateLocalDatabase(uint32 whichDatabase, uint32 &dbChecksum, const ConstMessageRef &seniorDoMsg)=0
This method will only be called on the senior peer.
void PrintDatabaseUpdateLog(int32 whichDatabase=-1) const
Prints the database update-log(s) to stdout.
Interface for an object that can receive text commands from the user.
virtual void EndSession()
Call this to request the termination of this session and its detachment from the ReflectServer.
bool IAmFullyAttached() const
Returns true iff this peer is fully attached to the system (i.e.
Definition: ZGPeerSession.h:89
const Hashtable< ZGPeerID, ConstMessageRef > & GetOnlinePeers() const
Returns a reference to a read-only table of the peers that are currently online in the system...
virtual void NetworkInterfacesChanged(const Hashtable< String, Void > &optInterfaceNames)
Called when the set of available network interfaces on this computer has changed. ...
Definition: ZGPeerSession.h:80
ZGPeerSession(const ZGPeerSettings &peerSettings)
Constructor.
virtual void PeerHasComeOnline(const ZGPeerID &peerID, const ConstMessageRef &peerInfo)
Called when a new peer has joined our group.
The zg namespace contains the public API of the ZG library.
virtual void SeniorPeerChanged(const ZGPeerID &oldSeniorPeerID, const ZGPeerID &newSeniorPeerID)
Called when the senior peer of our peer group has changed.
virtual void AboutToDetachFromServer()
Called just before this session object is detached from the ReflectServer object. ...
const ZGPeerID & GetSeniorPeerID() const
Returns the ZGPeerID of the senior peer of this system, or an invalid ZGPeerID if there currently is ...
Definition: ZGPeerSession.h:98
virtual MessageRef SaveLocalDatabaseToMessage(uint32 whichDatabase) const =0
This method should be implemented to save the state of the specified local database into a Message...
virtual void LocalSeniorPeerStatusChanged()
Called whenever this peer has gained or lost senior-peer status (current status is indicated by the r...
virtual void ComputerJustWokeUp()
Called just after this computer woke up from sleep (e.g.
Definition: ZGPeerSession.h:86
A ZGPeerID is a 128-bit integer that uniquely represents a particular peer in a ZG system...
Definition: ZGPeerID.h:18
status_t SendUnicastUserMessageToPeer(const ZGPeerID &destinationPeerID, const MessageRef &msg)
Tries to send the given Message to a specific peers via TCP unicast.
virtual void ComputerIsAboutToSleep()
Called just before this computer goes to sleep (e.g.
Definition: ZGPeerSession.h:83
virtual bool IsReadyForTextCommands() const
Default implementation returns true iff we are currently fully attached to the ZG system...
Definition: ZGPeerSession.h:61
virtual void PeerHasGoneOffline(const ZGPeerID &peerID, const ConstMessageRef &peerInfo)
Called when a peer has left our group.
virtual uint64 GetNetworkTime64() const
Returns the current time according to the network-time-clock, in microseconds.
const ZGPeerID & GetLocalPeerID() const
Returns the ZGPeerID being used by this local peer.
Definition: ZGPeerSession.h:95
virtual bool TextCommandReceived(const String &text)
Default implementation handles some standard ZG commands such as "print peers" or "print sessions"...
virtual String GetLocalDatabaseContentsAsString(uint32 whichDatabase) const
This method may be implemented to return a human-readable representation of the specified database's ...
const ZGPeerSettings & GetPeerSettings() const
Returns the ZGPeerSettings object, as passed to our constructor.
Definition: ZGPeerSession.h:73
virtual status_t SetLocalDatabaseFromMessage(uint32 whichDatabase, uint32 &dbChecksum, const ConstMessageRef &newDBStateMsg)=0
This method should be implemented to replace the current state of the specified local database with t...
virtual void MessageReceivedFromGateway(const MessageRef &msg, void *userData)
Implemented as a no-op, since by default this session doesn't have a gateway.
virtual int64 GetToNetworkTimeOffset() const
Returns the number of microseconds that should be added to a GetRunTime64() value to turn it into a G...
virtual void ResetLocalDatabaseToDefault(uint32 whichDatabase, uint32 &dbChecksum)=0
Must be implemented to reset local state of the specified database to its well-known default state...
virtual uint64 GetRunTime64ForNetworkTime64(uint64 networkTime64TimeStamp) const
Given a network-time-clock-value (i.e.
bool IAmTheSeniorPeer() const
Returns true iff this peer is currently considered to be the senior peer of the system.
status_t SendMulticastUserMessageToAllPeers(const MessageRef &msg)
Tries to send the given Message to all peers (except this one) via UDP multicast. ...
void PrintDatabaseStateInfo(int32 whichDatabase=-1) const
Prints various database-state information to stdout.
The zg_private namespace is an undocumented namespace where the ZG library keeps all of its private i...
Definition: ZGPeerSession.h:17
This is the class that a user program would typically subclass from and add to a ReflectServer in ord...
Definition: ZGPeerSession.h:28
status_t SendUnicastUserMessageToAllPeers(const MessageRef &msg)
Tries to send the given Message to all peers (except this one) via multiple instances of TCP unicast...