This is a convenience class; it is the same as a ZGPeerSession except that it also knows how to create and manage some user-provided IDatabaseObjects (one per database) so that the subclass doesn't have to implement all of the Message<->IDatabaseObject plumbing manually. More...
#include <ZGDatabasePeerSession.h>
Public Member Functions | |
ZGDatabasePeerSession (const ZGPeerSettings &peerSettings) | |
Constructor. More... | |
virtual status_t | AttachedToServer () |
Overridden to create and set up our IDatabaseObjects. More... | |
IDatabaseObject * | GetDatabaseObject (uint32 whichDatabase) |
Convenience method: Returns a read/write pointer to the specified IDatabaseObject that we hold. More... | |
const IDatabaseObject * | GetDatabaseObject (uint32 whichDatabase) const |
Convenience method: Returns a read-only pointer to the specified IDatabaseObject that we hold. More... | |
virtual void | AboutToDetachFromServer () |
Called just before this session object is detached from the ReflectServer object. More... | |
virtual void | EndSession () |
Call this to request the termination of this session and its detachment from the ReflectServer. More... | |
virtual uint64 | GetPulseTime (const PulseArgs &args) |
Returns the time (in microseconds, using the timebase of GetRunTime64()) when Pulse() should next be called. More... | |
virtual void | Pulse (const PulseArgs &args) |
Called at (approximately) the time specified by GetPulseTime(). More... | |
virtual bool | IsReadyForTextCommands () const |
Default implementation returns true iff we are currently fully attached to the ZG system. More... | |
virtual bool | TextCommandReceived (const String &text) |
Default implementation handles some standard ZG commands such as "print peers" or "print sessions". More... | |
bool | IAmTheSeniorPeer () const |
Returns true iff this peer is currently considered to be the senior peer of the system. More... | |
const ZGPeerSettings & | GetPeerSettings () const |
Returns the ZGPeerSettings object, as passed to our constructor. More... | |
virtual void | NetworkInterfacesChanged (const Hashtable< String, Void > &optInterfaceNames) |
Called when the set of available network interfaces on this computer has changed. More... | |
virtual void | ComputerIsAboutToSleep () |
Called just before this computer goes to sleep (e.g. More... | |
virtual void | ComputerJustWokeUp () |
Called just after this computer woke up from sleep (e.g. More... | |
bool | IAmFullyAttached () const |
Returns true iff this peer is fully attached to the system (i.e. More... | |
virtual void | LocalSeniorPeerStatusChanged () |
Called whenever this peer has gained or lost senior-peer status (current status is indicated by the return value of IAmTheSeniorPeer()) More... | |
const ZGPeerID & | GetLocalPeerID () const |
Returns the ZGPeerID being used by this local peer. More... | |
const ZGPeerID & | GetSeniorPeerID () const |
Returns the ZGPeerID of the senior peer of this system, or an invalid ZGPeerID if there currently is no senior peer (that we know of). More... | |
virtual uint64 | GetNetworkTime64 () const |
Returns the current time according to the network-time-clock, in microseconds. More... | |
virtual uint64 | GetRunTime64ForNetworkTime64 (uint64 networkTime64TimeStamp) const |
Given a network-time-clock-value (i.e. More... | |
virtual uint64 | GetNetworkTime64ForRunTime64 (uint64 runTime64TimeStamp) const |
Given a local-time-clock-value (i.e. More... | |
virtual int64 | GetToNetworkTimeOffset () const |
Returns the number of microseconds that should be added to a GetRunTime64() value to turn it into a GetNetworkTime64() value, or subtracted to do the inverse operation. More... | |
const Hashtable< ZGPeerID, ConstMessageRef > & | GetOnlinePeers () const |
Returns a reference to a read-only table of the peers that are currently online in the system. More... | |
bool | IsPeerOnline (const ZGPeerID &peerID) const |
Returns true iff if the specified peer is currently online. More... | |
uint64 | GetEstimatedLatencyToPeer (const ZGPeerID &peerID) const |
Gets our current estimate of the one-way network latency between us and the specified peer. More... | |
Protected Member Functions | |
virtual IDatabaseObjectRef | CreateDatabaseObject (uint32 whichDatabase)=0 |
This will be called as part of the startup sequence. More... | |
virtual void | ResetLocalDatabaseToDefault (uint32 whichDatabase, uint32 &dbChecksum) |
Must be implemented to reset local state of the specified database to its well-known default state. More... | |
virtual ConstMessageRef | SeniorUpdateLocalDatabase (uint32 whichDatabase, uint32 &dbChecksum, const ConstMessageRef &seniorDoMsg) |
This method will only be called on the senior peer. More... | |
virtual status_t | JuniorUpdateLocalDatabase (uint32 whichDatabase, uint32 &dbChecksum, const ConstMessageRef &juniorDoMsg) |
This method will only be called on junior peers. More... | |
virtual MessageRef | SaveLocalDatabaseToMessage (uint32 whichDatabase) const |
This method should be implemented to save the state of the specified local database into a Message. More... | |
virtual status_t | SetLocalDatabaseFromMessage (uint32 whichDatabase, uint32 &dbChecksum, const ConstMessageRef &newDBStateMsg) |
This method should be implemented to replace the current state of the specified local database with the new state represented by the passed-in Message. More... | |
virtual uint32 | CalculateLocalDatabaseChecksum (uint32 whichDatabase) const |
This method is used for sanity-checking. More... | |
virtual String | GetLocalDatabaseContentsAsString (uint32 whichDatabase) const |
This method may be implemented to return a human-readable representation of the specified database's current contents as a String. More... | |
status_t | RequestResetDatabaseStateToDefault (uint32 whichDatabase) |
Call this if you want to request that the specified database be reset back to its well-known default state. More... | |
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 that is specified by the passed-in Message. More... | |
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 via a specified passed-in Message. More... | |
virtual void | MessageReceivedFromGateway (const MessageRef &msg, void *userData) |
Implemented as a no-op, since by default this session doesn't have a gateway. More... | |
virtual void | PeerHasComeOnline (const ZGPeerID &peerID, const ConstMessageRef &peerInfo) |
Called when a new peer has joined our group. More... | |
virtual void | PeerHasGoneOffline (const ZGPeerID &peerID, const ConstMessageRef &peerInfo) |
Called when a peer has left our group. More... | |
virtual void | SeniorPeerChanged (const ZGPeerID &oldSeniorPeerID, const ZGPeerID &newSeniorPeerID) |
Called when the senior peer of our peer group has changed. More... | |
status_t | SendMulticastUserMessageToAllPeers (const MessageRef &msg) |
Tries to send the given Message to all peers (except this one) via UDP multicast. More... | |
status_t | SendUnicastUserMessageToAllPeers (const MessageRef &msg) |
Tries to send the given Message to all peers (except this one) via multiple instances of TCP unicast. More... | |
status_t | SendUnicastUserMessageToPeer (const ZGPeerID &destinationPeerID, const MessageRef &msg) |
Tries to send the given Message to a specific peers via TCP unicast. More... | |
void | PrintDatabaseStateInfo (int32 whichDatabase=-1) const |
Prints various database-state information to stdout. More... | |
void | PrintDatabaseUpdateLog (int32 whichDatabase=-1) const |
Prints the database update-log(s) to stdout. More... | |
bool | ParseGenericTextCommand (const String &cmd) |
Tries to handle any text commands that can be handled generically; that is, any commands that can be handled without knowing anything about this particular application. More... | |
This is a convenience class; it is the same as a ZGPeerSession except that it also knows how to create and manage some user-provided IDatabaseObjects (one per database) so that the subclass doesn't have to implement all of the Message<->IDatabaseObject plumbing manually.
Definition at line 14 of file ZGDatabasePeerSession.h.
ZGDatabasePeerSession | ( | const ZGPeerSettings & | peerSettings | ) |
Constructor.
peerSettings | the ZGPeerSettings that this system is to use. |
|
virtualinherited |
Called just before this session object is detached from the ReflectServer object.
|
virtual |
Overridden to create and set up our IDatabaseObjects.
Reimplemented from ZGPeerSession.
|
protectedvirtual |
This method is used for sanity-checking.
It should be implemented to scan the specified local database and return a checksum representing all of the data in the database. This checksum should match the checksums returned/updated by the SeniorUpdateLocalDatabase() and JuniorUpdateLocalDatabase() functions for the same database state.
Implements ZGPeerSession.
|
inlinevirtualinherited |
Called just before this computer goes to sleep (e.g.
due to closing a laptop lid)
Definition at line 83 of file ZGPeerSession.h.
|
inlinevirtualinherited |
Called just after this computer woke up from sleep (e.g.
due to opening a laptop lid)
Definition at line 86 of file ZGPeerSession.h.
|
protectedpure virtual |
This will be called as part of the startup sequence.
It should create a new IDatabaseObject that will represent the specified database and return a reference to it, for the ZGDatabasePeerSession to manage.
whichDatabase | The index of the database that we need an object to represent. |
Implemented in ChoirSession.
|
virtualinherited |
Call this to request the termination of this session and its detachment from the ReflectServer.
Overridden to do some thread-shutdown work to avoid race conditions.
|
inline |
Convenience method: Returns a read/write pointer to the specified IDatabaseObject that we hold.
whichDatabase | The index of the database that we need an object to represent. |
Definition at line 28 of file ZGDatabasePeerSession.h.
|
inline |
Convenience method: Returns a read-only pointer to the specified IDatabaseObject that we hold.
whichDatabase | The index of the database that we need an object to represent. |
Definition at line 33 of file ZGDatabasePeerSession.h.
|
inherited |
Gets our current estimate of the one-way network latency between us and the specified peer.
peerID | The peer ID to get the latency of |
|
protectedvirtual |
This method may be implemented to return a human-readable representation of the specified database's current contents as a String.
This string will be printed to stdout after a checksum error has occurred, to make it easier to debug the update logic (i.e. by comparing the database state before and after the checksum-mismatched database was replaced, to see what is different). The default implementation of this method just returns "(GetLocalDatabaseContentsAsString unimplemented)".
whichDatabase | The index of the database to return a string for. |
Reimplemented from ZGPeerSession.
|
inlineinherited |
Returns the ZGPeerID being used by this local peer.
Returned value is not valid until after ZGPeerSession::AttachedToServer() returns B_NO_ERROR.
Definition at line 95 of file ZGPeerSession.h.
|
inlinevirtualinherited |
Returns the current time according to the network-time-clock, in microseconds.
The intent of this clock is to be the same on all peers in the system. However, this means that it may occasionally change (break monotonicity) in order to synchronize with the other peers in the system. Note that this function will return 0 if we aren't currently fully attached to the system, since before then we don't know the network time.
Implements INetworkTimeProvider.
Definition at line 105 of file ZGPeerSession.h.
References ZGPeerSession::GetNetworkTime64ForRunTime64().
|
inlinevirtualinherited |
Given a local-time-clock-value (i.e.
one using the same time-base as returned by GetRunTime64()), returns the approximately equivalent network-time-value (i.e. one using the same time-base as returned by GetNetworkTime64())
Implements INetworkTimeProvider.
Definition at line 115 of file ZGPeerSession.h.
References ZGPeerSession::GetToNetworkTimeOffset().
Referenced by ZGPeerSession::GetNetworkTime64().
|
inlineinherited |
Returns a reference to a read-only table of the peers that are currently online in the system.
The keys in the table are the peer's IDs, and the values are the peers' attributes (may be NULL if they didn't advertise any) The ordering of the entries in the table is not significant (in particular, it doesn't reflect the ordering of the peers' seniority, at least not for now).
Definition at line 126 of file ZGPeerSession.h.
|
inlineinherited |
Returns the ZGPeerSettings object, as passed to our constructor.
Definition at line 73 of file ZGPeerSession.h.
|
virtualinherited |
Returns the time (in microseconds, using the timebase of GetRunTime64()) when Pulse() should next be called.
args | Context information for this call (including the current time and the time at which Pulse() was supposed to be called at) |
Reimplemented in ChoirSession.
|
inlinevirtualinherited |
Given a network-time-clock-value (i.e.
one using the same time-base as returned by GetNetworkTime64()), returns the approximately-equivalent local-time-clock-value (i.e. one using the same time-base as returned by GetRunTime64())
Implements INetworkTimeProvider.
Definition at line 110 of file ZGPeerSession.h.
References ZGPeerSession::GetToNetworkTimeOffset().
|
inlineinherited |
Returns the ZGPeerID of the senior peer of this system, or an invalid ZGPeerID if there currently is no senior peer (that we know of).
Definition at line 98 of file ZGPeerSession.h.
|
virtualinherited |
Returns the number of microseconds that should be added to a GetRunTime64() value to turn it into a GetNetworkTime64() value, or subtracted to do the inverse operation.
Note that this value will vary from one moment to the next!
Implements INetworkTimeProvider.
Referenced by ZGPeerSession::GetNetworkTime64ForRunTime64(), and ZGPeerSession::GetRunTime64ForNetworkTime64().
|
inlineinherited |
Returns true iff this peer is fully attached to the system (i.e.
has completed enumeration of which other peers are online, etc)
Definition at line 89 of file ZGPeerSession.h.
|
inherited |
Returns true iff this peer is currently considered to be the senior peer of the system.
|
inherited |
Returns true iff if the specified peer is currently online.
peerID | The peer ID to check on. |
|
inlinevirtualinherited |
Default implementation returns true iff we are currently fully attached to the ZG system.
Implements ITextCommandReceiver.
Definition at line 61 of file ZGPeerSession.h.
|
protectedvirtual |
This method will only be called on junior peers.
It must be implemented to update the junior peer's local database according to the instructions contained in (juniorDoMsg).
whichDatabase | The index of the database to update (e.g. 0 for the first database, 1 for the second, and so on) |
dbChecksum | Passed in as the database's current checksum value. On return, this should be set to the database's new (post-update) checksum value. |
juniorDoMsg | Reference to a Message instructing the junior peer how his lcoal database should be updated. (The contents and semantics of this Message will be determined by logic in the subclass of this class; they are not specified by the ZGPeerSession class itself) |
Implements ZGPeerSession.
|
virtualinherited |
Called whenever this peer has gained or lost senior-peer status (current status is indicated by the return value of IAmTheSeniorPeer())
|
protectedvirtualinherited |
Implemented as a no-op, since by default this session doesn't have a gateway.
|
inlinevirtualinherited |
Called when the set of available network interfaces on this computer has changed.
Default implementation is a no-op.
optInterfaceNames | An optional list of network-interface names (e.g. "eth0", "eth1") that are known to have changed. If empty, then you should assume that any or all network interfaces have changed. |
Definition at line 80 of file ZGPeerSession.h.
|
protectedinherited |
Tries to handle any text commands that can be handled generically; that is, any commands that can be handled without knowing anything about this particular application.
This logic is broken out into a separate function so that it can be called by the stdin-text handlers of various daemons.
cmd | the command text to parse |
|
protectedvirtualinherited |
Called when a new peer has joined our group.
peerID | The unique ID of the peer who is now online. (Note that this ID may be our own ID!) |
peerInfo | An optional Message containing some additional information about that peer, if that peer specified any additional information in his ZGPeerSettings object. May be a NULL reference otherwise. |
Reimplemented in ChoirSession.
|
protectedvirtualinherited |
Called when a peer has left our group.
peerID | The unique ID of the peer who is now offline. |
peerInfo | An optional Message containing some additional information about that peer, if that peer specified any additional information in his ZGPeerSettings object. May be a NULL reference otherwise. |
Reimplemented in ChoirSession.
|
protectedinherited |
Prints various database-state information to stdout.
Useful for debugging purposes.
whichDatabase | Index of the database to print out state for, or leave set to -1 to print out info about all databases. |
|
protectedinherited |
Prints the database update-log(s) to stdout.
Useful for debugging purposes.
whichDatabase | Index of the database to print out the update-log for, or leave set to -1 to print out the update-logs of all databases. |
|
virtualinherited |
Called at (approximately) the time specified by GetPulseTime().
args | Context information for this call (including the current time and the time at which this method was supposed to be called at) |
Reimplemented in ChoirSession.
|
protectedinherited |
Call this if you want to request that the specified database be completely replaced with a new state that is specified by the passed-in Message.
The new database state will be created by calling SetLocalDatabaseFromMessage(newDatabaseStateMsg) on the senior peer. Note that this method only sends the request; the actual database-replace will happen (if it happens) some time after this method returns.
whichDatabase | the index of the database whose state should be replaced. |
newDatabaseStateMsg | a Message containing instructions/data that SetLocalDatabaseFromMessage() can use later on to create a new database state. |
|
protectedinherited |
Call this if you want to request that the specified database be reset back to its well-known default state.
The well-known default state is defined by the implementation of the subclass's ResetLocalDatabaseToDefault() method. Note that this method only sends the request; the actual reset will happen (if it happens) some time after this method returns.
whichDatabase | the index of the database that should be reset |
|
protectedinherited |
Call this if you want to request that the specified database be incrementally updated to a new state via a specified passed-in Message.
The new database state will be created by calling SeniorUpdateLocalDatabase(databaseUpdateMsg) on the senior peer, and then the Message returned by that method call will be used to update the local database of all of the other peers. Note that this method only sends the request; the actual database-update will happen (if it happens) some time after this method returns.
whichDatabase | the index of the database whose state should be updated. |
databaseUpdateMsg | a Message containing instructions/data that SeniorUpdateLocalDatabase() can use later on to transition the database to a new database state. |
|
protectedvirtual |
Must be implemented to reset local state of the specified database to its well-known default state.
whichDatabase | The index of the database to reset (e.g. 0 for the first database, 1 for the second, and so on) |
dbChecksum | Passed in as the database's current checksum value. On return, this should be set to the database's new checksum value. |
Implements ZGPeerSession.
|
protectedvirtual |
This method should be implemented to save the state of the specified local database into a Message.
whichDatabase | The index of the database to save (e.g. 0 for the first database, 1 for the second, and so on) |
Implements ZGPeerSession.
|
protectedinherited |
Tries to send the given Message to all peers (except this one) via UDP multicast.
The PacketTunnelIOGateway mechanism is used so that large Messages can be transmitted as well as small ones. Delivery is not guaranteed, however.
msg | The Message object to send. MessageReceivedFromPeer() will be called on each peer when the Message arrives there. |
|
protectedinherited |
Tries to send the given Message to all peers (except this one) via multiple instances of TCP unicast.
msg | The Message object to send. MessageReceivedFromPeer() will be called on each peer when the Message arrives there. |
|
protectedinherited |
Tries to send the given Message to a specific peers via TCP unicast.
destinationPeerID | The ZGPeerID of the peer we want this Message to be sent to. |
msg | The Message object to send. MessageReceivedFromPeer() will be called on the peer when the Message arrives there. |
|
protectedvirtualinherited |
Called when the senior peer of our peer group has changed.
oldSeniorPeerID | The unique ID of the peer who was the senior peer but no longer is. (May be a zero/invalid if there wasn't any senior peer before) |
newSeniorPeerID | The unique ID of the peer who is now the senior peer. (May be a zero/invalid if there is no senior peer anymore) |
Reimplemented in ChoirSession.
|
protectedvirtual |
This method will only be called on the senior peer.
It must be implemented to update the senior peer's local database and return a MessageRef that the system can later use to update same database on the various junior peers in the same way later on.
whichDatabase | The index of the database to update (e.g. 0 for the first database, 1 for the second, and so on) |
dbChecksum | Passed in as the database's current checksum value. On return, this should be set to the database's new (post-update) checksum value. |
seniorDoMsg | Reference to a Message instructing the senior peer how his local database should be updated. (The contents and semantics of this Message will be determined by logic in the subclass of this class; they are not specified by the ZGPeerSession class itself) |
Implements ZGPeerSession.
|
protectedvirtual |
This method should be implemented to replace the current state of the specified local database with the new state represented by the passed-in Message.
whichDatabase | The index of the database to replace (e.g. 0 for the first database, 1 for the second, and so on) |
dbChecksum | Passed in as the database's current checksum value. On return, this should be set to the database's new (post-update) checksum value. |
newDBStateMsg | A Message holding the contents of the new database we want to replace the current database with. |
Implements ZGPeerSession.
|
virtualinherited |
Default implementation handles some standard ZG commands such as "print peers" or "print sessions".
text | A text string was received via our stdin stream. |
Implements ITextCommandReceiver.