1 #ifndef ZGPeerSession_h
2 #define ZGPeerSession_h
4 #include "reflector/StorageReflectSession.h"
5 #include "system/DetectNetworkConfigChangesSession.h"
12 #include "zg/private/PZGBeaconData.h"
13 #include "zg/private/PZGDatabaseState.h"
14 #include "zg/private/PZGDatabaseUpdate.h"
15 #include "zg/private/PZGUpdateBackOrderKey.h"
19 class PZGNetworkIOSession;
58 virtual void Pulse(
const PulseArgs & args);
126 const Hashtable<ZGPeerID, ConstMessageRef> &
GetOnlinePeers()
const {
return _onlinePeers;}
187 virtual ConstMessageRef
SeniorUpdateLocalDatabase(uint32 whichDatabase, uint32 & dbChecksum,
const ConstMessageRef & seniorDoMsg) = 0;
197 virtual status_t
JuniorUpdateLocalDatabase(uint32 whichDatabase, uint32 & dbChecksum,
const ConstMessageRef & juniorDoMsg) = 0;
212 virtual status_t
SetLocalDatabaseFromMessage(uint32 whichDatabase, uint32 & dbChecksum,
const ConstMessageRef & newDBStateMsg) = 0;
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;
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;
301 AbstractReflectSessionRef _networkIOSession;
302 bool _iAmFullyAttached;
304 #ifndef DOXYGEN_SHOULD_IGNORE_THIS
305 friend class zg_private::PZGDatabaseState;
306 friend class zg_private::PZGNetworkIOSession;
309 Queue<zg_private::PZGDatabaseState> _databases;
310 bool _setBeaconDataPending;
312 Hashtable<ZGPeerID, ConstMessageRef> _onlinePeers;
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.
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. ...
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 ...
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.
A ZGPeerID is a 128-bit integer that uniquely represents a particular peer in a ZG system...
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.
virtual bool IsReadyForTextCommands() const
Default implementation returns true iff we are currently fully attached to the ZG system...
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.
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.
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...
This is the class that a user program would typically subclass from and add to a ReflectServer in ord...
status_t SendUnicastUserMessageToAllPeers(const MessageRef &msg)
Tries to send the given Message to all peers (except this one) via multiple instances of TCP unicast...