Some code fixes done to multicast service classes.
[tourist.git] / App / include / Tourist / App / CacheXferWorker.h
blob2e6ef2f1a4b095b151a33703810a73212900f122
1 #ifndef CACHEEXPORTERWORKER_
2 #define CACHEEXPORTERWORKER_
4 #include "Tourist/LocalNode.h"
5 #include "Tourist/Util/Util.h"
6 #include "Tourist/AvlCache.h"
7 #include "Tourist/Message/RemoteNode.h"
8 #include "Tourist/Message/MessageDispatcher.h"
9 #include "Tourist/Message/MessageNotification.h"
10 #include "Poco/Runnable.h"
11 #include "Poco/AutoPtr.h"
12 #include "Poco/NotificationQueue.h"
14 using Tourist::Message::RemoteNode;
15 using Tourist::Message::MessageDispatcher;
16 using Tourist::Message::MessageNotification;
18 using Poco::Runnable;
19 using Poco::NotificationQueue;
20 using Poco::AutoPtr;
22 namespace Tourist {
23 //forwar declaration of LocalNode
24 class LocalNode;
25 namespace Message {
26 class MsgCacheXferRequest;
30 namespace Tourist {
31 namespace App {
33 class CacheXferWorker : public Runnable
35 public:
37 /**
38 * Creates a worker thread.
40 * @param contactNode Remote node to ask for nodes.
41 * @param threadName Name of the thread
42 * @param request Encapsulate the request parameters.
43 * @param dispatcher Instance of message bus to use for message communication
45 CacheXferWorker (RemoteNode *contactNode, string threadName,
46 Tourist::Message::MsgCacheXferRequest *request, MessageDispatcher *dispatcher,
47 int timeout = 5);
49 CacheXferWorker (RemoteNode *contactNode, string threadName, \
50 Tourist::Message::MsgCacheXferRequest *remoteRequest, MessageDispatcher *dispatcher,
51 LocalNode *localnode, int timeout = 5);
53 ~CacheXferWorker();
55 //void changeState(int event);
57 void stopWorker();
59 void startWorker();
61 bool isStopped();
63 //call by thread
64 void run();
66 //Return the container holding the result of cache exchange.
67 NodeSet getNodes();
69 //Return the status of operation performed by this worker thread.
70 int getStatus();
72 /**
73 * Callback method from the message bus (MessageDispatcher)
74 * This method enquee the message object as delivered by dispatcher
75 * thread this is to ensure the no delay for message delivery task.
77 void onMessage(const AutoPtr<MessageNotification>& notification);
79 int getTaskType();
81 static int TASK_REMOTE_TO_LOCAL;//Transfer routing cache from remote node to local.
82 static int TASK_LOCAL_TO_REMOTE;//Transfer routing cache from local node to remote.
84 static int STATUS_UNKNOWN; //Initial transfer status
85 static int STATUS_TIMEDOUT;// Transfer failed as operation timedout. Usually this means
86 // an error in communication.
87 static int STATUS_TIMEDOUT_PARTIAL; //Transfer failed as operation timedout. Usually this means
88 // that there was some partial transfer of cache.
89 static int STATUS_CONNECT_FAILED; //indicates that remote node is not connected.
90 static int STATUS_OK;
92 //< state codes declaration > //
93 const static int AWAIT_RESPONSE; //indicates that worker is waiting for the transfer to finish.
94 const static int PROCESSING_RESPONSE; //indicates that worker is/was processing response
96 //<event code>//
97 static int FINISHED;
98 static int RESPONSE_RECVD;
99 static int RESPONSE_OK;
100 static int RESPONSE_PARTIAL;
101 static int STOPPED; //indicate that controller asked for stopping this thread
103 void duplicate();
105 void release();
107 private:
109 //called to clean-up any resources used by this
110 //thread.
111 void cleanUp();
113 void processResponse();
115 void processRequest();
117 RemoteNode *contactNode;
119 //suggest if this worker thread should run or not.
120 //No gurantte is made that if the thread will actually be running
121 //or not
122 bool shouldbeRunning;
124 //Indicates whether the thread is actually stopped or not
125 bool stopped;
127 //Name of the worker thread
128 string threadName;
130 Logger &logger;
132 //Result of worker threads are put in this set to let controller
133 //fetch it.
134 NodeSet result;
136 //status are mentioned in the controller class 'CacheXfer'
137 int state, status;
139 int timeout;
141 //Notificaiton
142 NotificationQueue queue;
144 //Type of work this worker thread is going to do. Currently
145 //these tasks includes (a) Asking a remote node to transfer its cache
146 //with a specified cache type and range of levels. (b) To transfer
147 //cache of LocalNode to remote node.
148 int taskType;
150 Tourist::Message::MsgCacheXferRequest *xferRequest;
152 MessageDispatcher *dispatcher;
154 Tourist::LocalNode *node;
156 //for reference counting
157 int rc;
158 static int ref_count;
161 } // namespace App
162 } // namespace Tourist
165 #endif /*CACHEEXPORTERWORKER_*/