2012-06-07 16:43:12 -04:00
|
|
|
#ifndef RSGXSNETSERVICE_H
|
|
|
|
#define RSGXSNETSERVICE_H
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "rsnxs.h"
|
|
|
|
#include "rsgds.h"
|
|
|
|
#include "rsnxsobserver.h"
|
2012-07-05 17:26:14 -04:00
|
|
|
#include "pqi/p3linkmgr.h"
|
2012-06-07 16:43:12 -04:00
|
|
|
#include "serialiser/rsnxsitems.h"
|
|
|
|
|
|
|
|
#include "pqi/p3cfgmgr.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* This represents a transaction made
|
|
|
|
* with the NxsNetService in all states
|
2012-06-11 17:56:23 -04:00
|
|
|
* of operation until completion
|
2012-06-16 09:59:40 -04:00
|
|
|
*
|
2012-06-07 16:43:12 -04:00
|
|
|
*/
|
|
|
|
class NxsTransaction
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
static const uint8_t FLAG_STATE_STARTING; // when
|
2012-06-11 17:56:23 -04:00
|
|
|
static const uint8_t FLAG_STATE_RECEIVING; // begin receiving items for incoming trans
|
|
|
|
static const uint8_t FLAG_STATE_SENDING; // begin sending items for outgoing trans
|
|
|
|
static const uint8_t FLAG_STATE_COMPLETED;
|
|
|
|
static const uint8_t FLAG_STATE_FAILED;
|
|
|
|
static const uint8_t FLAG_STATE_WAITING_CONFIRM;
|
|
|
|
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
NxsTransaction();
|
|
|
|
~NxsTransaction();
|
|
|
|
|
|
|
|
uint32_t mFlag; // current state of transaction
|
|
|
|
uint32_t mTimestamp;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* this contains who we
|
|
|
|
* c what peer this transaction involves.
|
|
|
|
* c The type of transaction
|
|
|
|
* c transaction id
|
|
|
|
* c timeout set for this transaction
|
|
|
|
* c and itemCount
|
|
|
|
*/
|
|
|
|
RsNxsTransac* mTransaction;
|
|
|
|
std::list<RsNxsItem*> mItems; // items received or sent
|
|
|
|
};
|
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
|
|
|
|
class RsNxsNetMgr
|
|
|
|
{
|
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
virtual std::string getOwnId() = 0;
|
|
|
|
virtual void getOnlineList(std::set<std::string>& ssl_peers) = 0;
|
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
};
|
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
//class RsNxsNetMgrImpl : public RsNxsNetMgrImpl
|
|
|
|
//{
|
2012-06-11 17:56:23 -04:00
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
//public:
|
|
|
|
|
|
|
|
// RsNxsNetMgrImpl(p3LinkMgr* lMgr);
|
2012-06-11 17:56:23 -04:00
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
// std::string getOwnId();
|
|
|
|
// void getOnlineList(std::set<std::string>& ssl_peers);
|
|
|
|
|
|
|
|
//};
|
2012-06-11 17:56:23 -04:00
|
|
|
|
|
|
|
|
2012-06-07 16:43:12 -04:00
|
|
|
/// keep track of transaction number
|
|
|
|
typedef std::map<uint32_t, NxsTransaction*> TransactionIdMap;
|
|
|
|
|
|
|
|
/// to keep track of peers active transactions
|
|
|
|
typedef std::map<std::string, TransactionIdMap > TransactionsPeerMap;
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
2012-07-12 16:18:58 -04:00
|
|
|
* This class implements the RsNetWorkExchangeService
|
|
|
|
* using transactions to handle synchrnisation of Nxs items between
|
|
|
|
* peers in a network
|
|
|
|
* Transactions requires the maintenance of several states between peers, and whether
|
|
|
|
*
|
|
|
|
* Thus a data structure maintains: peers, and their active transactions
|
|
|
|
* Then for each transaction it needs to be noted if this is an outgoing or incoming transaction
|
|
|
|
* Outgoing transaction are in 3 different states:
|
|
|
|
* 1. START 2. INITIATED 3. SENDING 4. END
|
|
|
|
* Incoming transaction are also in 3 different states
|
|
|
|
* 1. START 2. RECEIVING 3. END
|
2012-06-07 16:43:12 -04:00
|
|
|
*/
|
2012-07-05 17:26:14 -04:00
|
|
|
class RsGxsNetService : public RsNetworkExchangeService, public p3ThreadedService,
|
2012-06-07 16:43:12 -04:00
|
|
|
public p3Config
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-07-12 16:18:58 -04:00
|
|
|
|
2012-06-07 16:43:12 -04:00
|
|
|
/*!
|
|
|
|
* only one observer is allowed
|
|
|
|
* @param servType service type
|
|
|
|
* @param gds The data service which allows read access to a service/store
|
|
|
|
* @param nxsObs observer will be notified whenever new messages/grps
|
|
|
|
* arrive
|
|
|
|
*/
|
2012-07-05 17:26:14 -04:00
|
|
|
RsGxsNetService(uint16_t servType, RsGeneralDataService* gds, RsNxsNetMgr* netMgr, RsNxsObserver* nxsObs = NULL);
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-07-12 16:18:58 -04:00
|
|
|
virtual ~RsGxsNetService();
|
|
|
|
|
2012-06-07 16:43:12 -04:00
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Use this to set how far back synchronisation of messages should take place
|
|
|
|
* @param age the max age a sync item can to be allowed in a synchronisation
|
|
|
|
*/
|
|
|
|
void setSyncAge(uint32_t age);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Explicitly requests all the groups contained by a peer
|
|
|
|
* Circumvents polling of peers for message
|
|
|
|
* @param peerId id of peer
|
|
|
|
*/
|
2012-06-11 17:56:23 -04:00
|
|
|
void requestGroupsOfPeer(const std::string& peerId){ return;}
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* get messages of a peer for a given group id, this circumvents the normal
|
|
|
|
* polling of peers for messages of given group id
|
|
|
|
* @param peerId Id of peer
|
|
|
|
* @param grpId id of group to request messages for
|
|
|
|
*/
|
2012-06-26 15:52:01 -04:00
|
|
|
void requestMessagesOfPeer(const std::string& peerId, const std::string& grpId){ return; }
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* pauses synchronisation of subscribed groups and request for group id
|
|
|
|
* from peers
|
|
|
|
* @param enabled set to false to disable pause, and true otherwise
|
|
|
|
*/
|
|
|
|
void pauseSynchronisation(bool enabled);
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Request for this message is sent through to peers on your network
|
|
|
|
* and how many hops from them you've indicated
|
|
|
|
* @param msgId the messages to retrieve
|
|
|
|
* @return request token to be redeemed
|
|
|
|
*/
|
2012-06-11 17:56:23 -04:00
|
|
|
int requestMsg(const std::string& msgId, uint8_t hops){ return 0;}
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Request for this group is sent through to peers on your network
|
|
|
|
* and how many hops from them you've indicated
|
|
|
|
* @param enabled set to false to disable pause, and true otherwise
|
|
|
|
* @return request token to be redeemed
|
|
|
|
*/
|
2012-06-26 15:52:01 -04:00
|
|
|
int requestGrp(const std::list<std::string>& grpId, uint8_t hops){ return 0;}
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-07-12 16:18:58 -04:00
|
|
|
/* p3Config methods */
|
|
|
|
|
|
|
|
public:
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-07-12 16:18:58 -04:00
|
|
|
bool loadList(std::list<RsItem *>& load);
|
|
|
|
bool saveList(bool &cleanup, std::list<RsItem *>&);
|
|
|
|
RsSerialiser *setupSerialiser();
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* initiates synchronisation
|
|
|
|
*/
|
2012-06-11 17:56:23 -04:00
|
|
|
int tick();
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Processes transactions and job queue
|
|
|
|
*/
|
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* called when
|
|
|
|
* items are deemed to be waiting in p3Service item queue
|
|
|
|
*/
|
|
|
|
void recvNxsItemQueue();
|
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
|
|
|
|
/** S: Transaction processing **/
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/*!
|
2012-06-11 17:56:23 -04:00
|
|
|
* These process transactions which are in a wait state
|
|
|
|
* Also moves transaction which have been completed to
|
|
|
|
* the completed transactions list
|
2012-06-07 16:43:12 -04:00
|
|
|
*/
|
2012-06-11 17:56:23 -04:00
|
|
|
void processTransactions();
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/*!
|
2012-06-11 17:56:23 -04:00
|
|
|
* Process completed transaction, which either simply
|
|
|
|
* retires a transaction or additionally generates a response
|
|
|
|
* to the completed transaction
|
2012-06-07 16:43:12 -04:00
|
|
|
*/
|
2012-06-11 17:56:23 -04:00
|
|
|
void processCompletedTransactions();
|
|
|
|
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/*!
|
2012-06-11 17:56:23 -04:00
|
|
|
* Process a transaction item, assumes a general lock
|
|
|
|
* @param item the transaction item to process
|
2012-06-07 16:43:12 -04:00
|
|
|
*/
|
2012-06-11 17:56:23 -04:00
|
|
|
bool locked_processTransac(RsNxsTransac* item);
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* This adds a transaction
|
|
|
|
* completeted transaction list
|
|
|
|
* If this is an outgoing transaction, transaction id is
|
|
|
|
* decrement
|
|
|
|
* @param trans transaction to add
|
|
|
|
*/
|
|
|
|
void locked_completeTransaction(NxsTransaction* trans);
|
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
/*!
|
|
|
|
* This retrieves a unique transaction id that
|
|
|
|
* can be used in an outgoing transaction
|
|
|
|
*/
|
2012-06-07 16:43:12 -04:00
|
|
|
uint32_t getTransactionId();
|
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
/*!
|
|
|
|
* This attempts to push the transaction id counter back if you have
|
|
|
|
* active outgoing transactions in play
|
|
|
|
*/
|
2012-06-07 16:43:12 -04:00
|
|
|
bool attemptRecoverIds();
|
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
/*!
|
|
|
|
* The cb listener is the owner of the grps
|
|
|
|
* @param grps
|
|
|
|
*/
|
2012-07-12 16:18:58 -04:00
|
|
|
//void notifyListenerGrps(std::list<RsNxsGrp*>& grps);
|
2012-06-11 17:56:23 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* The cb listener is the owner of the msgs
|
|
|
|
* @param msgs
|
|
|
|
*/
|
2012-07-12 16:18:58 -04:00
|
|
|
//void notifyListenerMsgs(std::list<RsNxsMsg*>& msgs);
|
2012-06-11 17:56:23 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @param tr transaction responsible for generating msg request
|
|
|
|
*/
|
|
|
|
void genReqMsgTransaction(NxsTransaction* tr);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @param tr transaction responsible for generating grp request
|
|
|
|
*/
|
|
|
|
void genReqGrpTransaction(NxsTransaction* tr);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @param tr transaction to add
|
|
|
|
*/
|
|
|
|
bool locked_addTransaction(NxsTransaction* tr);
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
void cleanTransactionItems(NxsTransaction* tr) const;
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
/** E: Transaction processing **/
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
/** S: item handlers **/
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
/*!
|
|
|
|
* This attempts handles transaction items
|
|
|
|
* ownership of item is left with callee if this method returns false
|
|
|
|
* @param item transaction item to handle
|
|
|
|
* @return false if transaction could not be handled, ownership of item is left with callee
|
|
|
|
*/
|
|
|
|
bool handleTransaction(RsNxsItem* item);
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
/*!
|
|
|
|
* Handles an nxs item for group synchronisation
|
2012-07-12 16:18:58 -04:00
|
|
|
* by startin a transaction and sending a list
|
|
|
|
* of groups held by user
|
|
|
|
* @param item contains grp sync info
|
2012-06-11 17:56:23 -04:00
|
|
|
*/
|
|
|
|
void handleRecvSyncGroup(RsNxsSyncGrp* item);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Handles an nxs item for msgs synchronisation
|
|
|
|
* @param item contaims msg sync info
|
|
|
|
*/
|
|
|
|
void handleRecvSyncMessage(RsNxsSyncMsg* item);
|
|
|
|
|
|
|
|
/** E: item handlers **/
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
|
2012-07-12 16:18:58 -04:00
|
|
|
void syncWithPeers();
|
|
|
|
|
2012-06-07 16:43:12 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
/*** transactions ***/
|
|
|
|
|
|
|
|
/// active transactions
|
2012-06-11 17:56:23 -04:00
|
|
|
TransactionsPeerMap mTransactions;
|
2012-06-07 16:43:12 -04:00
|
|
|
|
|
|
|
/// completed transactions
|
|
|
|
std::list<NxsTransaction*> mComplTransactions;
|
|
|
|
|
2012-07-12 16:18:58 -04:00
|
|
|
/// transaction id counter
|
2012-06-07 16:43:12 -04:00
|
|
|
uint32_t mTransactionN;
|
|
|
|
|
|
|
|
/*** transactions ***/
|
|
|
|
|
|
|
|
/*** synchronisation ***/
|
2012-06-11 17:56:23 -04:00
|
|
|
std::list<RsNxsSyncGrp*> mSyncGrp;
|
|
|
|
std::list<RsNxsSyncMsg*> mSyncMsg;
|
2012-06-07 16:43:12 -04:00
|
|
|
/*** synchronisation ***/
|
|
|
|
|
|
|
|
RsNxsObserver* mObserver;
|
|
|
|
RsGeneralDataService* mDataStore;
|
|
|
|
uint16_t mServType;
|
2012-06-11 17:56:23 -04:00
|
|
|
uint32_t mTransactionTimeOut;
|
2012-06-07 16:43:12 -04:00
|
|
|
|
2012-06-11 17:56:23 -04:00
|
|
|
std::string mOwnId;
|
2012-07-05 17:26:14 -04:00
|
|
|
|
|
|
|
RsNxsNetMgr* mNetMgr;
|
|
|
|
|
2012-06-07 16:43:12 -04:00
|
|
|
/// for other members save transactions
|
|
|
|
RsMutex mNxsMutex;
|
|
|
|
|
2012-07-12 16:18:58 -04:00
|
|
|
uint32_t mSyncTs;
|
|
|
|
const uint32_t mSYNC_PERIOD;
|
|
|
|
|
2012-06-07 16:43:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RSGXSNETSERVICE_H
|