00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #ifndef DM_CLIENT_H_
00047 #define DM_CLIENT_H_
00048
00049 #include <Ice/Ice.h>
00050 #include <vector>
00051 #include "Server/DistributedMap.h"
00052 #include <IceUtil/Handle.h>
00053 #include "TopicLearner/Synchronizer_Helper.h"
00054 #include "Client.h"
00055 #include "boost/unordered_map.hpp"
00056 #include "constants.h"
00057
00058 using namespace std;
00059 using namespace GlobalTable;
00060
00061 struct Cookie: public Ice::LocalObject {
00062 string entity;
00063 int msg_id;
00064 };
00065 typedef IceUtil::Handle<Cookie> CookiePtr;
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 class PNGCallback: public IceUtil::Shared {
00087 public:
00088
00089 PNGCallback(Synchronizer_Helper& sync_helper, int num_msgs) :
00090 _sync_helper(sync_helper), _num_msgs(num_msgs) {
00091 _num_synchs = 0;
00092 _waiting = -1;
00093 _status = new bool[_num_msgs];
00094 for (int i = 0; i < _num_msgs; i++) {
00095 _status[i] = true;
00096 }
00097 }
00098
00099 ~PNGCallback() {
00100 delete[] _status;
00101 }
00102
00103
00104 bool wait_till_done(int msg_id) {
00105 if (_status[msg_id])
00106 return true;
00107 IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
00108 while (!_status[msg_id]) {
00109 try {
00110 _waiting = msg_id;
00111 _monitor.wait();
00112 _waiting = -1;
00113 } catch (const Ice::Exception& ex) {
00114 _waiting = -1;
00115 throw ex;
00116 }
00117 }
00118 return true;
00119 }
00120
00121
00122 void set_done(int msg_id, bool status) {
00123 _status[msg_id] = status;
00124 }
00125
00126
00127 int num_synchs() {
00128 return _num_synchs;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 void finished(const Ice::AsyncResultPtr& r) {
00138 DistributedMapPrx dist_map = DistributedMapPrx::uncheckedCast(
00139 r->getProxy());
00140 string global_cnts;
00141 CookiePtr cookie = CookiePtr::dynamicCast(r->getCookie());
00142 int msg_id = cookie->msg_id;
00143 string entity = cookie->entity;
00144 try {
00145 dist_map->end_putNget(global_cnts, r);
00146 } catch (const Ice::Exception& ex) {
00147 throw ex;
00148 }
00149 _sync_helper.end_putNget(entity, global_cnts);
00150 ++_num_synchs;
00151 IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
00152 _status[msg_id] = true;
00153 if (_waiting == msg_id) {
00154 _monitor.notify();
00155 }
00156 }
00157
00158 private:
00159 Synchronizer_Helper& _sync_helper;
00160 int _num_msgs, _num_synchs;
00161 bool *_status;
00162 int _waiting;
00163 IceUtil::Monitor<IceUtil::Mutex> _monitor;
00164 };
00165 typedef IceUtil::Handle<PNGCallback> PNGCallbackPtr;
00166
00167
00168
00169
00170
00171
00172 class DM_Client: public Client {
00173 public:
00174
00175
00176
00177
00178
00179
00180
00181
00182 DM_Client(int num_entities, const string& servers,
00183 Synchronizer_Helper& sync_helper);
00184 virtual ~DM_Client();
00185
00186
00187 void put(const string& entity, const string& delta);
00188 void set(const string& entity, const string& counts);
00189 bool get(const string& entity, string& counts);
00190 bool remove(const string& entity, string& counts);
00191
00192
00193 void begin_putNget(const string& entity, const string& delta);
00194
00195
00196 void wait_for_all();
00197
00198 void wait_till_done();
00199
00200 int get_num_servers();
00201
00202 private:
00203 void add_server(const string& servant_name, const string& server_endpoint);
00204 inline size_t get_server(const string& s);
00205
00206 private:
00207 Ice::CommunicatorPtr _ic;
00208 vector<DistributedMapPrx> _dist_maps;
00209 Ice::CallbackPtr _cb;
00210 PNGCallbackPtr _png_cb;
00211 int NUM_SERVERS, _num_entities;
00212 boost::hash<string> _hasher;
00213 size_t _cur_msg_id, _max_msgs;
00214 };
00215
00216 #endif