11 using namespace forest;
39 int main(
int argc,
char *argv[]) {
40 ipa_t cmIp,
myIp;
int finTime, worldSize;
45 (sscanf(argv[3],
"%d", &worldSize) != 1) ||
46 sscanf(argv[6],
"%d", &finTime) != 1)
47 fatal(
"usage: Monitor myIp worldSize uname pword finTime");
49 Monitor mon(cmIp, myIp,worldSize);
50 if (!mon.
init(argv[4],argv[5])) {
51 fatal(
"Monitor: initialization failure");
60 Monitor::Monitor(ipa_t cmIp1, ipa_t myIp1,
int worldSize1) : cmIp(cmIp1),
61 myIp(myIp1), worldSize(min(worldSize1,MAX_WORLD)) {
64 mySubs =
new set<int>;
66 cornerX = 0; cornerY = 0;
67 viewSize = min(10,worldSize);
90 if (!
login(uname, pword))
return false;
111 cerr <<
"Avatar::login: cannot open/configure socket to "
116 string s =
"login: " + uname +
"\npassword: " + pword +
"\nover\n";
120 if (!buf.
readLine(s0) || s0 !=
"login successful" ||
125 s =
"newSession\nover\n";
155 if (!buf.
readLine(s0) || s0 !=
"overAndOut")
164 cout <<
"nonce=" <<
nonce << endl;
180 bool waiting4switch =
false;
181 finishTime *= 1000000;
182 while (now <= finishTime) {
184 if (newComt != 0 && newComt !=
comt) {
186 waiting4switch =
true;
191 if (!waiting4switch) {
266 if (cp.type == CtlPkt::CLIENT_LEAVE_COMTREE) {
267 if (cp.mode == CtlPkt::POS_REPLY) {
273 }
else if (cp.mode == CtlPkt::NEG_REPLY) {
292 if (cp.type == CtlPkt::CLIENT_JOIN_COMTREE) {
293 if (cp.mode == CtlPkt::POS_REPLY) {
296 }
else if (cp.mode == CtlPkt::NEG_REPLY) {
314 fatal(
"Monitor::send2comtCtl: no packets left to allocate");
317 CtlPkt cp(joinLeave,CtlPkt::REQUEST,
seqNum,p.payload());
323 fatal(
"Monitor::send2comtCtl: control packet packing error");
339 if (rv == -1) fatal(
"Monitor::sendToRouter: failure in sendto");
347 if (px == 0)
return 0;
351 if (nbytes < 0) {
ps->
free(px);
return 0; }
385 fatal(
"can't make connection socket nonblocking");
386 bool status;
int ndVal = 1;
387 status = setsockopt(
listenSock,IPPROTO_TCP,TCP_NODELAY,
388 (
void *) &ndVal,
sizeof(
int));
390 cerr <<
"setsockopt for no-delay failed\n";
396 int nbytes = read(
connSock, buf, 5);
398 if (errno == EAGAIN)
return 0;
399 fatal(
"Monitor::check4command: error in read call");
400 }
else if (nbytes == 0) {
404 }
else if (nbytes < 5) {
405 fatal(
"Monitor::check4command: incomplete command");
408 uint32_t param = ntohl(*((
int*) &buf[1]));
421 case 'c':
return param;
422 default: fatal(
"unrecognized command from remote display");
454 for (
int yi = cornerY; yi < cornerY +
viewSize; yi++) {
458 mySubs->insert(g); glist.push_back(g);
469 set<int>::iterator gp;
471 glist.push_back(*gp);
480 if (glist.size() == 0)
return;
484 uint32_t *pp = p.payload();
487 list<int>::iterator gp;
488 for (gp = glist.begin(); gp != glist.end(); gp++) {
491 pp[0] = htonl(nsub-1); pp[nsub] = 0;
499 pp[nsub] = htonl(-g);
501 pp[0] = htonl(nsub); pp[nsub+1] = 0;
515 if (glist.size() == 0)
return;
519 uint32_t *pp = p.payload();
522 list<int>::iterator gp;
523 for (gp = glist.begin(); gp != glist.end(); gp++) {
524 int g = *gp; nunsub++;
526 pp[0] = 0; pp[1] = htonl(nunsub-1);
534 pp[nunsub+1] = htonl(-g);
536 pp[0] = 0; pp[1] = htonl(nunsub);
551 if (
comt == 0)
return;
554 set<int>::iterator gp;
560 yi >= cornerY && yi < cornerY +
viewSize)
565 list<int>::iterator p;
566 for (p = glist.begin(); p != glist.end(); p++)
574 for (
int yi = cornerY; yi < cornerY +
viewSize; yi++) {
577 mySubs->insert(g); glist.push_back(g);
594 uint32_t *pp = p.payload();
603 for (
int i = 0; i <
NUMITEMS; i++) buf[i] = pp[i];
604 buf[0] = htonl(now); buf[1] = htonl(p.
srcAdr);
605 buf[8] = htonl(
comt);
607 int nbytes = NUMITEMS*
sizeof(uint32_t);
608 char* bp = (
char *) buf;
610 int n = write(
connSock, (
void *) bp, nbytes);
612 bp += n; nbytes -= n;
624 p.payload()[0] = htonl((uint32_t) (
nonce >> 32));
625 p.payload()[1] = htonl((uint32_t) (
nonce & 0xffffffff));
627 p.
comtree = Forest::CONNECT_COMT;
634 if (now > resendTime) {
635 if (resendCount > 3) {
ps->
free(px);
return false; }
637 resendTime += 1000000;
641 if (rx == 0) { sleep(100000);
continue; }
656 p.payload()[0] = htonl((uint32_t) (
nonce >> 32));
657 p.payload()[1] = htonl((uint32_t) (
nonce & 0xffffffff));
666 if (now > resendTime) {
667 if (resendCount > 3) {
ps->
free(px);
return false; }
669 resendTime += 1000000;
673 if (rx == 0) { sleep(100000);
continue; }