forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
CpHandlerDoowon.cpp
1 
9 #include "CpHandler.h"
10 
11 namespace forest {
12 
20 pktx CpHandler::clientAddComtree(fAdr_t dest, int zipCode, CtlPkt& repCp) {
21  CtlPkt reqCp(CtlPkt::CLIENT_ADD_COMTREE,CtlPkt::REQUEST,0);
22  reqCp.zipCode = zipCode;
23  return sendRequest(reqCp,dest,repCp);
24 }
25 
33 pktx CpHandler::clientDropComtree(fAdr_t dest, comt_t comt,CtlPkt& repCp) {
34  CtlPkt reqCp(CtlPkt::CLIENT_DROP_COMTREE,CtlPkt::REQUEST,0);
35  reqCp.comtree = comt;
36  return sendRequest(reqCp,dest,repCp);
37 }
38 
49  ipa_t clientIp, ipp_t clientPort,CtlPkt& repCp) {
50  CtlPkt reqCp(CtlPkt::CLIENT_JOIN_COMTREE,CtlPkt::REQUEST,0);
51  reqCp.comtree = comt;
52  reqCp.ip1 = clientIp; reqCp.port1 = clientPort;
53  return sendRequest(reqCp,dest,repCp);
54 }
55 
66  ipa_t clientIp, ipp_t clientPort,CtlPkt& repCp) {
67  CtlPkt reqCp(CtlPkt::CLIENT_LEAVE_COMTREE,CtlPkt::REQUEST,0);
68  reqCp.comtree = comt;
69  reqCp.ip1 = clientIp; reqCp.port1 = clientPort;
70  return sendRequest(reqCp,dest,repCp);
71 }
72 
82 pktx CpHandler::addIface(fAdr_t dest, int iface, ipa_t ifip, RateSpec& rates,CtlPkt& repCp) {
83  CtlPkt reqCp(CtlPkt::ADD_IFACE,CtlPkt::REQUEST,0);
84  reqCp.iface = iface;
85  reqCp.ip1 = ifip;
86  reqCp.rspec1 = rates;
87  return sendRequest(reqCp,dest,repCp);
88 }
89 
97 pktx CpHandler::dropIface(fAdr_t dest, int iface,CtlPkt& repCp) {
98  CtlPkt reqCp(CtlPkt::DROP_IFACE,CtlPkt::REQUEST,0);
99  reqCp.iface = iface;
100  return sendRequest(reqCp,dest,repCp);
101 }
102 
112 pktx CpHandler::modIface(fAdr_t dest, int iface, ipa_t ifip, RateSpec& rates,CtlPkt& repCp) {
113  CtlPkt reqCp(CtlPkt::MOD_IFACE,CtlPkt::REQUEST,0);
114  reqCp.iface = iface;
115  reqCp.ip1 = ifip;
116  reqCp.rspec1 = rates;
117  return sendRequest(reqCp,dest,repCp);
118 }
119 
127 pktx CpHandler::getIface(fAdr_t dest, int iface,CtlPkt& repCp) {
128  CtlPkt reqCp(CtlPkt::GET_IFACE,CtlPkt::REQUEST,0);
129  reqCp.iface = iface;
130  return sendRequest(reqCp,dest,repCp);
131 }
132 
147 pktx CpHandler::addLink(fAdr_t dest,Forest::ntyp_t peerType, int iface, int lnk,
148  ipa_t peerIp, ipp_t peerPort, fAdr_t peerAdr,
149  uint64_t nonce, CtlPkt& repCp) {
150  CtlPkt reqCp(CtlPkt::ADD_LINK,CtlPkt::REQUEST,0);
151  reqCp.nodeType = peerType; reqCp.iface = iface; reqCp.link = lnk;
152  reqCp.ip1 = peerIp; reqCp.port1 = peerPort; reqCp.adr1 = peerAdr;
153  reqCp.nonce = nonce;
154  return sendRequest(reqCp,dest,repCp);
155 }
156 
168 pktx CpHandler::addLink(fAdr_t dest, Forest::ntyp_t peerType, int iface,
169  uint64_t nonce, CtlPkt& repCp) {
170  CtlPkt reqCp(CtlPkt::ADD_LINK,CtlPkt::REQUEST,0);
171  reqCp.nodeType = peerType; reqCp.iface = iface; reqCp.nonce = nonce;
172  return sendRequest(reqCp,dest,repCp);
173 }
174 
183 pktx CpHandler::dropLink(fAdr_t dest, int link, fAdr_t peerAdr, CtlPkt& repCp) {
184  CtlPkt reqCp(CtlPkt::DROP_LINK,CtlPkt::REQUEST,0);
185  if (link != 0) reqCp.link = link;
186  if (peerAdr != 0) reqCp.adr1 = peerAdr;
187  return sendRequest(reqCp,dest,repCp);
188 }
189 
198 pktx CpHandler::modLink(fAdr_t dest, int link, RateSpec& rates,CtlPkt& repCp) {
199  CtlPkt reqCp(CtlPkt::MOD_LINK,CtlPkt::REQUEST,0);
200  reqCp.link = link; reqCp.rspec1 = rates;
201  return sendRequest(reqCp,dest,repCp);
202 }
203 
211 pktx CpHandler::getLink(fAdr_t dest, int link,CtlPkt& repCp) {
212  CtlPkt reqCp(CtlPkt::GET_COMTREE,CtlPkt::REQUEST,0);
213  reqCp.link = link;
214  return sendRequest(reqCp,dest,repCp);
215 }
216 
224 pktx CpHandler::addComtree(fAdr_t dest, comt_t comtree,CtlPkt& repCp) {
225  CtlPkt reqCp(CtlPkt::ADD_COMTREE,CtlPkt::REQUEST,0);
226  reqCp.comtree = comtree;
227  return sendRequest(reqCp,dest,repCp);
228 }
229 
237 pktx CpHandler::dropComtree(fAdr_t dest, comt_t comtree,CtlPkt& repCp) {
238  CtlPkt reqCp(CtlPkt::DROP_COMTREE,CtlPkt::REQUEST,0);
239  reqCp.comtree = comtree;
240  return sendRequest(reqCp,dest,repCp);
241 }
242 
252 pktx CpHandler::modComtree(fAdr_t dest, comt_t comtree, int pLink,
253  int coreFlag,CtlPkt& repCp) {
254  CtlPkt reqCp(CtlPkt::MOD_COMTREE,CtlPkt::REQUEST,0);
255  reqCp.comtree = comtree;
256  reqCp.link = pLink; reqCp.coreFlag = coreFlag;
257  return sendRequest(reqCp,dest,repCp);
258 }
259 
267 pktx CpHandler::getComtree(fAdr_t dest, comt_t comtree,CtlPkt& repCp) {
268  CtlPkt reqCp(CtlPkt::GET_COMTREE,CtlPkt::REQUEST,0);
269  reqCp.comtree = comtree;
270  return sendRequest(reqCp,dest,repCp);
271 }
272 
282 pktx CpHandler::addComtreeLink(fAdr_t dest, comt_t comtree, int link,
283  int peerCoreFlag,CtlPkt& repCp) {
284  CtlPkt reqCp(CtlPkt::ADD_COMTREE_LINK,CtlPkt::REQUEST,0);
285  reqCp.comtree = comtree; reqCp.link = link;
286  reqCp.coreFlag = peerCoreFlag;
287  return sendRequest(reqCp,dest,repCp);
288 }
289 
299 pktx CpHandler::addComtreeLink(fAdr_t dest, comt_t comtree, fAdr_t peerAdr,
300  CtlPkt& repCp) {
301  CtlPkt reqCp(CtlPkt::ADD_COMTREE_LINK,CtlPkt::REQUEST,0);
302  reqCp.comtree = comtree; reqCp.adr1 = peerAdr;
303  return sendRequest(reqCp,dest,repCp);
304 }
305 
333 pktx CpHandler::dropComtreeLink(fAdr_t dest, comt_t comtree, int link,
334  fAdr_t peerAdr, CtlPkt& repCp) {
335  CtlPkt reqCp(CtlPkt::DROP_COMTREE_LINK,CtlPkt::REQUEST,0);
336  reqCp.comtree = comtree;
337  if (link == 0 && peerAdr == 0) {
338  logger->log("CpHandler::dropLink: link, peerAdr both 0", 2);
339  return false;
340  }
341  if (link != 0) reqCp.link = link;
342  else if (peerAdr != 0) reqCp.adr1 = peerAdr;
343  return sendRequest(reqCp,dest,repCp);
344 }
345 
372 pktx CpHandler::modComtreeLink( fAdr_t dest, comt_t comtree, int link,
373  RateSpec& rates,CtlPkt& repCp) {
374  CtlPkt reqCp(CtlPkt::MOD_COMTREE_LINK,CtlPkt::REQUEST,0);
375  reqCp.comtree = comtree;
376  reqCp.link = link; reqCp.rspec1 = rates;
377  return sendRequest(reqCp,dest,repCp);
378 }
379 
388 pktx CpHandler::getComtreeLink( fAdr_t dest, comt_t comtree, int link,CtlPkt& repCp) {
389  CtlPkt reqCp(CtlPkt::GET_COMTREE_LINK,CtlPkt::REQUEST,0);
390  reqCp.comtree = comtree; reqCp.link = link;
391  return sendRequest(reqCp,dest,repCp);
392 }
393 
402 pktx CpHandler::newSession(fAdr_t dest, ipa_t clientIp, RateSpec& rates, CtlPkt& repCp) {
403  CtlPkt reqCp(CtlPkt::NEW_SESSION,CtlPkt::REQUEST,0);
404  reqCp.ip1 = clientIp; reqCp.rspec1 = rates;
405  return sendRequest(reqCp,dest,repCp);
406 }
407 
416 pktx CpHandler::clientConnect(fAdr_t dest, fAdr_t clientAdr, fAdr_t rtrAdr,CtlPkt& repCp) {
417  CtlPkt reqCp(CtlPkt::CLIENT_CONNECT,CtlPkt::REQUEST,0);
418  reqCp.adr1 = clientAdr; reqCp.adr2 = rtrAdr;
419  return sendRequest(reqCp,dest,repCp);
420 }
421 
430 pktx CpHandler::clientDisconnect(fAdr_t dest, fAdr_t clientAdr, fAdr_t rtrAdr,CtlPkt& repCp) {
431  CtlPkt reqCp(CtlPkt::CLIENT_DISCONNECT,CtlPkt::REQUEST,0);
432  reqCp.adr1 = clientAdr; reqCp.adr2 = rtrAdr;
433  return sendRequest(reqCp,dest,repCp);
434 }
435 
442 pktx CpHandler::bootRouter(fAdr_t dest,CtlPkt& repCp) {
443  CtlPkt reqCp(CtlPkt::BOOT_ROUTER,CtlPkt::REQUEST,0);
444  return sendRequest(reqCp,dest,repCp);
445 }
446 
458 pktx CpHandler::configLeaf(fAdr_t dest, fAdr_t leafAdr, fAdr_t rtrAdr,
459  ipa_t rtrIp, ipp_t rtrPort, uint64_t nonce,
460  CtlPkt& repCp) {
461  CtlPkt reqCp(CtlPkt::CONFIG_LEAF,CtlPkt::REQUEST,0);
462  reqCp.adr1 = leafAdr; reqCp.adr2 = rtrAdr;
463  reqCp.ip1 = rtrIp; reqCp.port1 = rtrPort;
464  reqCp.nonce = nonce;
465  return sendRequest(reqCp,dest,repCp);
466 }
467 
476 pktx CpHandler::setLeafRange(fAdr_t dest, fAdr_t first, fAdr_t last,
477  CtlPkt& repCp) {
478  CtlPkt reqCp(CtlPkt::SET_LEAF_RANGE,CtlPkt::REQUEST,0);
479  reqCp.adr1 = first; reqCp.adr2 = last;
480  return sendRequest(reqCp,dest,repCp);
481 }
482 
489 pktx CpHandler::bootLeaf(fAdr_t dest, CtlPkt& repCp) {
490  CtlPkt reqCp(CtlPkt::BOOT_LEAF,CtlPkt::REQUEST,0);
491  return sendRequest(reqCp,dest,repCp);
492 }
493 
499 pktx CpHandler::bootComplete(fAdr_t dest,CtlPkt& repCp) {
500  CtlPkt reqCp(CtlPkt::BOOT_COMPLETE,CtlPkt::REQUEST,0);
501  return sendRequest(reqCp,dest,repCp);
502 }
503 
510 pktx CpHandler::bootAbort(fAdr_t dest,CtlPkt& repCp) {
511  CtlPkt reqCp(CtlPkt::BOOT_ABORT,CtlPkt::REQUEST,0);
512  return sendRequest(reqCp,dest,repCp);
513 }
514 
527 pktx CpHandler::sendRequest(CtlPkt& cp, fAdr_t dest, CtlPkt& repCp) {
528  pktx px = ps->alloc();
529  if (px == 0) {
530  logger->log("CpHandler::sendRequest: no packets "
531  "left in packet store\n",4,cp);
532  // terminates
533  }
534  Packet& p = ps->getPacket(px);
535  // set default seq# for requests - tells main thread to use "next" seq#
536  if (cp.mode == CtlPkt::REQUEST) cp.seqNum = 0;
537  cp.payload = p.payload();
538  int plen = cp.pack();
539  if (plen == 0) {
540  logger->log("CpHandler::sendRequest: packing error\n",4,cp);
541  // terminates
542  }
543  p.length = plen + Forest::OVERHEAD;
544  if (cp.type < CtlPkt::CLIENT_NET_SIG_SEP) {
545  p.type = Forest::CLIENT_SIG; p.comtree =Forest::CLIENT_SIG_COMT;
546  } else {
547  p.type = Forest::NET_SIG; p.comtree = Forest::NET_SIG_COMT;
548  }
549  p.flags = 0; p.dstAdr = dest; p.srcAdr = myAdr;
550  p.tunIp = tunIp; p.tunPort = tunPort;
551  p.pack();
552 
553  if (cp.mode != CtlPkt::REQUEST) {
554  outq->enq(px); return 0;
555  }
556  pktx reply = sendAndWait(px,cp);
557  if (reply != 0) repCp.reset(ps->getPacket(reply));
558  ps->free(px);
559  return reply;
560 }
561 
562 //Feng and Doowon
571 pktx CpHandler::getLinkSet(fAdr_t dest, CtlPkt& repCp, int firstLinkNum, int numOfLinks) {
572  CtlPkt reqCp(CtlPkt::GET_LINK_SET, CtlPkt::REQUEST, 0);
573  reqCp.firstLinkNum = firstLinkNum;
574  reqCp.numOfLinks = numOfLinks;
575  cout << __FUNCTION__ << endl;
576  return sendRequest(reqCp,dest,repCp);
577 }
578 
589 int CpHandler::sendAndWait(pktx px, CtlPkt& cp) {
590  Packet& p = ps->getPacket(px);
591  p.srcAdr = myAdr; p.pack();
592 
593  // make copy of packet and send the copy
594  pktx copy = ps->fullCopy(px);
595  if (copy == 0) {
596  logger->log("CpHandler::sendAndWait: no packets "
597  "left in packet store\n",4,p);
598  // terminates
599  }
600 
601  outq->enq(copy);
602 
603  for (int i = 1; i < 3; i++) {
604  pktx reply = inq->deq(1000000000); // 1 sec timeout
605  if (reply == Queue::TIMEOUT) {
606  // no reply, make new copy and send
607  pktx retry = ps->fullCopy(px);
608  if (retry == 0) {
609  logger->log("CpHandler::sendAndWait: no "
610  "packets left in packet store\n",
611  4,p);
612  // terminates
613  }
614  Packet& pr = ps->getPacket(retry);
615  cp.payload = pr.payload();
616  cp.seqNum = 1; // tag retry as a repeat
617  cp.pack();
618  pr.payErrUpdate();
619  outq->enq(retry);
620  } else {
621  Packet& pr = ps->getPacket(reply);
622  CtlPkt repCp(pr.payload(),pr.length-Forest::OVERHEAD);
623  repCp.unpack();
624  if (repCp.mode == CtlPkt::NEG_REPLY) {
625  logger->log("CpHandler::sendAndWait: negative "
626  "reply (" + repCp.errMsg +
627  ") to control packet",1,pr);
628  }
629  return reply;
630  }
631  }
632  logger->log("CpHandler::sendAndWait: no response to control packet",
633  2,p);
634 
635  return 0;
636 }
637 
638 
639 
645 void CpHandler::sendReply(CtlPkt& cp, fAdr_t dest) {
646  pktx px = ps->alloc();
647  if (px == 0) {
648  logger->log("CpHandler::sendRequest: no packets "
649  "left in packet store\n",4,cp);
650  // terminates
651  }
652  Packet& p = ps->getPacket(px);
653  cp.payload = p.payload();
654  int plen = cp.pack();
655  if (plen == 0) {
656  logger->log("CpHandler::sendRequest: packing error\n",4,cp);
657  // terminates
658  }
659  p.length = plen + Forest::OVERHEAD;
660  if (cp.type < CtlPkt::CLIENT_NET_SIG_SEP) {
661  p.type = Forest::CLIENT_SIG; p.comtree =Forest::CLIENT_SIG_COMT;
662  } else {
663  p.type = Forest::NET_SIG; p.comtree = Forest::NET_SIG_COMT;
664  }
665  p.flags = 0; p.dstAdr = dest; p.srcAdr = myAdr;
666  p.tunIp = tunIp; p.tunPort = tunPort;
667  p.pack();
668  outq->enq(px);
669 }
670 
676 void CpHandler::errReply(pktx px, CtlPkt& cp, const string& msg) {
677  Packet& p = ps->getPacket(px);
678 
679  pktx px1 = ps->alloc();
680  Packet& p1 = ps->getPacket(px1);
681  CtlPkt cp1(cp.type,CtlPkt::NEG_REPLY,cp.seqNum);
682  cp1.errMsg = msg;
683  cp1.payload = p1.payload();
684 
685  p1.length = Forest::OVERHEAD + cp1.pack();
686  p1.type = p.type; p1.flags = 0; p1.comtree = p.comtree;
687  p1.dstAdr = p.srcAdr; p1.srcAdr = myAdr;
688  p1.pack();
689 
690  outq->enq(px1);
691 }
692 
693 } // ends namespace
694