10 #include "CommonDefs.h"
11 #include "PacketHeader.h"
31 int main(
int argc,
char *argv[]) {
35 fatal(
"usage: fConsole netMgrIp");
41 fatal(
"can't connect to NetMgr");
51 string line; getline(cin,line);
52 if (line.find(
"quit") == 0)
break;
53 if (line.find(
"clear") == 0) {
54 target = 0; cpTemplate.
reset();
continue;
56 if (line.find(
"show") == 0) {
63 for (
int i = CPA_START+1; i < CPA_END; i++) {
64 CpAttrIndex ii = CpAttrIndex(i);
65 if (cpTemplate.isSet(ii)) {
74 list<string> tokenList;
77 CpTypeIndex reqType = CPT_START;
79 if (reqType == CPT_START)
continue;
82 cout <<
"no target defined for command\n";
88 reqPkt.setCpType(reqType);
89 reqPkt.setRrType(REQUEST);
94 cout <<
"missing one or more required "
96 }
else if (!
sendReqPkt(sock,reqPkt,target,replyPkt)) {
97 cout <<
"no valid reply received\n";
99 if (replyPkt.getRrType() == POS_REPLY) {
100 posResponse(replyPkt);
103 cout <<
"error reported: "
104 << replyPkt.getErrMsg() << endl;
108 cout <<
"cannot recognize command\n";
113 void posResponse(CtlPkt& cp) {
114 bool printedSomething =
false;
115 CpTypeIndex cpType = cp.getCpType();
116 for (
int i = CPA_START+1; i < CPA_END; i++) {
117 CpAttrIndex ii = CpAttrIndex(i);
118 if (!CpType::isRepAttr(cpType,ii))
continue;
119 printedSomething =
true;
120 cout << CpAttr::getName(ii) <<
"=";
121 int32_t val = cp.getAttr(ii);
122 if (ii == COMTREE_OWNER || ii == LEAF_ADR ||
123 ii == PEER_ADR || ii == PEER_DEST ||
127 }
else if (ii == LOCAL_IP || ii == PEER_IP || ii == RTR_IP) {
129 }
else if (ii == PEER_TYPE) {
131 cout << Forest::nodeType2string((ntyp_t) val,s);
137 if (printedSomething) cout << endl;
151 buffer_t reqBuf, replyBuf;
152 PacketHeader reqHdr, replyHdr;
157 reqHdr.setLength(pleng);
158 reqHdr.setPtype(NET_SIG);
162 reqHdr.setDstAdr(target);
166 if (Np4d::sendBuf(sock, (
char *) &reqBuf[0], pleng) != pleng)
167 fatal(
"can't send control packet to NetMgr");
169 for (
int i = 0; i < 3; i++) {
175 cout <<
"."; cout.flush();
176 Np4d::sendBuf(sock, (
char *) &reqBuf[0], pleng);
179 replyHdr.unpack(replyBuf);
183 return replyHdr.getSrcAdr() == target;
196 for (
int i = CPA_START+1; i < CPA_END; i++) {
197 CpAttrIndex ii = CpAttrIndex(i);
198 if (CpType::isReqAttr(type, ii)) {
199 if (cpTemplate.isSet(ii)) {
200 reqPkt.setAttr(ii,cpTemplate.getAttr(ii));
201 }
else if (CpType::isReqReqAttr(type, ii)) {
218 list<string> wordList;
224 list<string>::iterator p = wordList.begin();
226 while (p != wordList.end()) {
227 if ((*p)[0] ==
'=')
break;
232 if (pos == 0)
return;
233 if (p != wordList.end() && pos == 1) {
235 p = wordList.begin();
237 int last = (p == wordList.end() ? pos-1 : pos-2);
240 p = wordList.begin();
241 for (
int i = 0; i <= last; i++) {
243 if (i < last) s +=
" ";
245 tokenList.push_back(s);
251 while (p != wordList.end()) {
252 list<string>::iterator pp, ppp;
253 pp = p; pp++;
if (pp == wordList.end())
return;
254 ppp = pp; ppp++;
if (ppp == wordList.end())
return;
255 if ((*pp)[0] !=
'=')
return;
257 s += *p; s +=
"="; s+= *ppp;
258 tokenList.push_back(s);
272 char white[] =
" \t\n";
char delim[] =
" =\t\n";
277 int start = line.find_first_not_of(white,pos);
278 if (start == (
int) string::npos)
return;
280 if (line[start] ==
'=') {
281 wordList.push_back(
"=");
283 }
else if (isalnum(line[start]) || line[start] ==
'-') {
284 end = line.find_first_of(delim,start);
285 string s = (end == (int) string::npos ?
287 line.substr(start,end-start));
288 wordList.push_back(s);
305 list<string>::iterator p = tokenList.begin();
306 if (p == tokenList.end())
return true;
307 if ((*p).find(
'=') == string::npos) {
309 for (
int i = CPT_START+1; true ; i++) {
310 CpTypeIndex ii = CpTypeIndex(i);
311 if (i == CPT_END)
return false;
312 if ((*p).compare(CpType::getName(ii)) == 0)
314 if ((*p).compare(CpType::getAbbrev(ii)) == 0)
322 while (p != tokenList.end()) {
323 int pos = (*p).find(
'=');
324 string attrib = (*p).substr(0,pos);
325 for (
int i = CPA_START+1; true ; i++) {
326 CpAttrIndex ii = CpAttrIndex(i);
329 if (attrib.compare(
"target") == 0)
break;
332 if (attrib.compare(CpAttr::getName(ii)) == 0)
353 CpTypeIndex& cpType,
CtlPkt& cpTemplate) {
355 list<string>::iterator p = tokenList.begin();
356 if ((*p).find(
'=') == string::npos) {
357 for (
int i = CPT_START+1; i < CPT_END ; i++) {
358 CpTypeIndex ii = CpTypeIndex(i);
359 if ((*p).compare(CpType::getName(ii)) == 0) {
362 if ((*p).compare(CpType::getAbbrev(ii)) == 0) {
371 for (; p != tokenList.end(); p++) {
372 int pos = (*p).find(
'=');
373 string leftSide = (*p).substr(0,pos);
374 string rightSide = (*p).substr(pos+1);
375 CpAttrIndex attr = CPA_START;
376 for (
int i = CPA_START+1; i < CPA_END ; i++) {
377 CpAttrIndex ii = CpAttrIndex(i);
378 if (leftSide.compare(CpAttr::getName(ii)) == 0) {
383 if ((*p).find(
"target") == 0) {
390 case DEST_ADR:
case LEAF_ADR:
391 case PEER_ADR:
case PEER_DEST:
394 if (fa != 0) cpTemplate.setAttr(attr, fa);
396 case LOCAL_IP:
case PEER_IP:
case RTR_IP:
399 if (ipa != 0) cpTemplate.setAttr(attr, ipa);
403 nt = Forest::getNodeType(rightSide);
404 if (nt != UNDEF_NODE)
405 cpTemplate.setAttr(attr, nt);
408 uint32_t value = atoi(rightSide.c_str());
409 cpTemplate.setAttr(attr, value);