forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
ComtreeRegister.cpp
Go to the documentation of this file.
1 
9 #include "ComtreeRegister.h"
10 
11 namespace forest {
12 
13 
15 ComtreeRegister::ComtreeRegister(int maxComtrees) : maxComt(maxComtrees) {
16  cvec = new Comtree[maxComt+1];
17  comtMap = new IdMap(maxComt);
18  maxCtx = 0;
19 }
20 
23  delete [] cvec; delete comtMap;
24  pthread_mutex_destroy(&mapLock);
25 }
26 
29  if (pthread_mutex_init(&mapLock,NULL) != 0) return false;
30  for (int ctx = 1; ctx <= maxComt; ctx++) {
31  cvec[ctx].busyBit = false;
32  if (pthread_cond_init(&cvec[ctx].busyCond,NULL) != 0)
33  return false;
34  }
35  return true;
36 }
37 
46  lockMap();
47  int ctx = comtMap->getId(key(comt));
48  if (ctx == 0) { unlockMap(); return 0; }
49  while (cvec[ctx].busyBit) { // wait until client's entry is not busy
50  pthread_cond_wait(&cvec[ctx].busyCond,&mapLock);
51  if (ctx == 0) { unlockMap(); return 0; }
52  }
53  cvec[ctx].busyBit = true; // set busyBit to lock entry
54  unlockMap();
55  return ctx;
56 }
57 
63  lockMap();
64  cvec[ctx].busyBit = false;
65  pthread_cond_signal(&cvec[ctx].busyCond);
66  unlockMap();
67 }
68 
74  lockMap();
75  int ctx = comtMap->firstId();
76  if (ctx == 0) { unlockMap(); return 0; }
77  while (cvec[ctx].busyBit) {
78  pthread_cond_wait(&cvec[ctx].busyCond,&mapLock);
79  ctx = comtMap->firstId();
80  if (ctx == 0) {
81  pthread_cond_signal(&cvec[ctx].busyCond);
82  unlockMap(); return 0;
83  }
84  }
85  cvec[ctx].busyBit = true;
86  unlockMap();
87  return ctx;
88 }
89 
98  lockMap();
99  int nuCtx = comtMap->nextId(ctx);
100  if (nuCtx == 0) {
101  cvec[ctx].busyBit = false;
102  pthread_cond_signal(&cvec[ctx].busyCond);
103  unlockMap();
104  return 0;
105  }
106  while (cvec[nuCtx].busyBit) {
107  pthread_cond_wait(&cvec[nuCtx].busyCond,&mapLock);
108  nuCtx = comtMap->nextId(ctx);
109  if (nuCtx == 0) {
110  cvec[ctx].busyBit = false;
111  pthread_cond_signal(&cvec[ctx].busyCond);
112  pthread_cond_signal(&cvec[nuCtx].busyCond);
113  unlockMap();
114  return 0;
115  }
116  }
117  cvec[nuCtx].busyBit = true;
118  cvec[ctx].busyBit = false;
119  pthread_cond_signal(&cvec[ctx].busyCond);
120  unlockMap();
121  return nuCtx;
122 }
123 
136  lockMap();
137  if (ctx == 0) ctx = comtMap->addPair(key(comt));
138  else ctx = comtMap->addPair(key(comt),ctx);
139  if (ctx == 0) { unlockMap(); return 0;}
140  cvec[ctx].busyBit = true;
141  unlockMap();
142 
143  cvec[ctx].comt = comt;
144  maxCtx = max(ctx,maxCtx);
145  return ctx;
146 }
147 
154  lockMap();
155  comtMap->dropPair(key(getComtree(ctx)));
156  cvec[ctx].busyBit = false;
157  pthread_cond_signal(&cvec[ctx].busyCond);
158  unlockMap();
159 }
160 
168 bool ComtreeRegister::readEntry(istream& in, int ctx) {
169  comt_t comt, string owner, pwd, cfgString, axsString;
170  fAdr_t rootZip, super;
171  int repInterval; time_t start;
172 
173  ss << getComtree(ctx) << ", " << getOwner(ctx) << ", ";
174  ss << Forest::fAdr2string(getRootZip(ctx),s) << ", ";
175  ss << Forest::fAdr2string(getSuper(ctx),s) << ", ";
176 
177  if (!in.good()) return false;
178  if (Misc::verify(in,'+')) {
179  if (!Misc::readNum(in, comt) ||
180  !Misc::verify(in,',') ||
181  !Misc::readWord(in, owner) ||
182  !Misc::verify(in,',') ||
183  !Forest::readForestAdr(in, rootZip) ||
184  !Misc::verify(in,',') ||
185  !Forest::readForestAdr(in, super) ||
186  !Misc::verify(in,',') ||
187  !Misc::readWord(in, cfgString) ||
188  !Misc::verify(in,',') ||
189  !Misc::readString(in, axsString) ||
190  !Misc::verify(in,',') ||
191  !Misc::readWord(in, pwd) ||
192  !Misc::verify(in,',') ||
193  !Misc::readNum(in, repInterval) ||
194  !Misc::verify(in,',') ||
195  !Misc::readNum(in, start)) {
196  return false;
197  }
198  Misc::cflush(in,'\n');
199  } else if (Misc::verify(in,'-')) {
200  maxCtx = max(ctx, maxCtx);
201  Misc::cflush(in,'\n'); return true;
202  } else {
203  Misc::cflush(in,'\n'); return false;
204  }
205 
206  Forest::ConfigMode cfg;
207  if (cfgString == "static") cfg = STATIC;
208  else if (cfgString == "leafAdjust") cfg = LEAFADJUST;
209  else if (cfgString == "stepAdjust") cfg = STEPADJUST;
210  else cfg = NUL_CFG;
211 
213  if (axsString == "open") axs = OPEN;
214  else if (axsString == "byPermission") axs = BYPERMISSION;
215  else if (axsString == "byPassword") axs = BYPASSWORD;
216  else axs = NUL_AXS;
217 
218  if (addComtree(comt, ctx) == 0) return false;
219  setOwner(ctx,owner);
220  setRootZip(ctx,rootZip); setSuper(ctx,super);
221  setConfigMode(ctx) = cfg; setAccessMethod(ctx) = axs;
222  setPassword(ctx,password);
223  setReportInterval(ctx) = repInterval; setStartTime(ctx) = start;
224  releaseComtree(ctx);
225  return true;
226 }
227 
239 bool ComtreeRegister::read(istream& in) {
240  int i = 0;
241  while (readEntry(in,i)) i++;
242  cout << "read " << i << " client records, producing "
243  << clients->getNumIn() << "table entries\n";
244  return true;
245 }
246 
253 string& ComtreeRegister::comtree2string(int ctx, string& s) const {
254  stringstream ss;
255  ss << getComtree(ctx) << ", " << getOwner(ctx) << ", ";
256  ss << Forest::fAdr2string(getRootZip(ctx),s) << ", ";
257  ss << Forest::fAdr2string(getSuper(ctx),s) << ", ";
258 
259  Forest::ConfigMode cfg = getConfigMode(ctx);
260  switch (cfg) {
261  case STATIC: ss << "static"; break;
262  case LEAFADJUST: ss << "leafAdjust"; break;
263  case STEPADJUST: ss << "stepAdjust"; break;
264  default: ss << "-";
265  }
266  ss << ", ";
267 
269  switch (axs) {
270  case OPEN: ss << "open"; break;
271  case BYPERMISSION: ss << "byPermission"; break;
272  case BYPASSWORD: ss << "byPassword"; break;
273  default: ss << "-";
274  }
275  ss << ", ";
276 
277  ss << getPassword(ctx) << ", " << getReportInterval(ctx) << ", "
278  << getStartTime(ctx) << endl;
279 
280  s = ss.str();
281  return s;
282 }
283 
288 string& ComtreeRegister::toString(string& s) {
289  string s1; s = "";
290  for (int ctx = firstComtree(); ctx != 0; ctx = nextComtree(ctx))
291  s += client2string(ctx,s1);
292  return s;
293 }
294 
299 void ComtreeRegister::write(ostream& out) {
300  string s;
301  for (int ctx = firstComtree(); ctx != 0; ctx = nextComtree(ctx))
302  out << client2string(ctx,s);
303 }
304 
305 } // ends namespace
306