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 #include "ShortIdBlock.H"
00029 #include "ShortIdSRPC.H"
00030 #include "SRPC.H"
00031 #include "VestaConfig.H"
00032 #include <Thread.H>
00033 #include <pthread.h>
00034 #include <assert.h>
00035
00036 using std::cerr;
00037 using std::endl;
00038
00039
00040 static pthread_once_t once = PTHREAD_ONCE_INIT;
00041 static SRPC* srpc;
00042 static Basics::mutex mu;
00043
00044 extern "C"
00045 {
00046 static void
00047 ShortIdClientImport()
00048 {
00049 Text port, host;
00050 try {
00051 port = VestaConfig::get_Text("Repository", "ShortIdSRPC_port");
00052 host = VestaConfig::get_Text("Repository", "ShortIdSRPC_host");
00053 srpc = NEW_CONSTR(SRPC, (SRPC::caller, port, host));
00054 } catch (VestaConfig::failure f) {
00055 cerr << f.msg << endl;
00056 throw;
00057 }
00058 }
00059 }
00060
00061 ShortIdBlock*
00062 ShortIdBlock::acquire(bool leafflag) throw(SRPC::failure)
00063 {
00064 pthread_once(&once, ShortIdClientImport);
00065 ShortIdBlock *bk = NEW(ShortIdBlock);
00066 int size = sizeof(bk->bits);
00067 mu.lock();
00068 srpc->start_call(ShortIdSRPC::AcquireShortIdBlock,
00069 ShortIdSRPC::version);
00070 srpc->send_int((int) leafflag);
00071 srpc->send_end();
00072 bk->start = bk->next = (ShortId) srpc->recv_int();
00073 bk->leaseExpires = (time_t) srpc->recv_int();
00074 srpc->recv_bytes_here((char *) bk->bits, size);
00075 assert(size == sizeof(bk->bits));
00076 srpc->recv_end();
00077 mu.unlock();
00078 return bk;
00079 }
00080
00081 bool
00082 ShortIdBlock::renew(ShortIdBlock* bk) throw(SRPC::failure)
00083 {
00084 pthread_once(&once, ShortIdClientImport);
00085 mu.lock();
00086 srpc->start_call(ShortIdSRPC::RenewShortIdBlock,
00087 ShortIdSRPC::version);
00088 srpc->send_int((int) bk->start);
00089 srpc->send_end();
00090 bk->leaseExpires = (time_t) srpc->recv_int();
00091 bool res = (bool) srpc->recv_int();
00092 srpc->recv_end();
00093 mu.unlock();
00094 return res;
00095 }
00096
00097 void
00098 ShortIdBlock::release(ShortIdBlock* bk) throw(SRPC::failure)
00099 {
00100 pthread_once(&once, ShortIdClientImport);
00101 mu.lock();
00102 srpc->start_call(ShortIdSRPC::ReleaseShortIdBlock,
00103 ShortIdSRPC::version);
00104 srpc->send_int((int) bk->start);
00105 srpc->send_end();
00106 srpc->recv_end();
00107 mu.unlock();
00108 }
00109
00110 int
00111 ShortIdBlock::keepDerived(ShortIdsFile ds, time_t dt, bool force)
00112 throw(SRPC::failure)
00113 {
00114 pthread_once(&once, ShortIdClientImport);
00115 mu.lock();
00116 srpc->start_call(ShortIdSRPC::KeepDerived,
00117 ShortIdSRPC::version);
00118 srpc->send_int((int) ds);
00119 srpc->send_int((int) dt);
00120 srpc->send_int((int) force);
00121 srpc->send_end();
00122 int res = srpc->recv_int();
00123 srpc->recv_end();
00124 mu.unlock();
00125 return res;
00126 }
00127
00128 void
00129 ShortIdBlock::checkpoint() throw(SRPC::failure)
00130 {
00131 pthread_once(&once, ShortIdClientImport);
00132 mu.lock();
00133 srpc->start_call(ShortIdSRPC::Checkpoint,
00134 ShortIdSRPC::version);
00135 srpc->send_end();
00136 srpc->recv_end();
00137 mu.unlock();
00138 }
00139
00140 void
00141 ShortIdBlock::getWeedingState(ShortIdsFile& ds, time_t& dt,
00142 ShortIdsFile& ss, time_t& st,
00143 bool& sourceWeedInProgress,
00144 bool& deletionsInProgress,
00145 bool& deletionsDone,
00146 bool& checkpointInProgress)
00147 throw(SRPC::failure)
00148 {
00149 pthread_once(&once, ShortIdClientImport);
00150 mu.lock();
00151 srpc->start_call(ShortIdSRPC::GetWeedingState,
00152 ShortIdSRPC::version);
00153 srpc->send_end();
00154 ds = (ShortIdsFile) srpc->recv_int();
00155 dt = (time_t) srpc->recv_int();
00156 ss = (ShortIdsFile) srpc->recv_int();
00157 st = (time_t) srpc->recv_int();
00158 sourceWeedInProgress = (bool) srpc->recv_int();
00159 deletionsInProgress = (bool) srpc->recv_int();
00160 deletionsDone = (bool) srpc->recv_int();
00161 checkpointInProgress = (bool) srpc->recv_int();
00162 srpc->recv_end();
00163 mu.unlock();
00164 }
00165