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 #include <CodaObject.hxx>
00026
00027
00028 #ifdef VXWORKS
00029 #include <vxWorks.h>
00030 #include <taskLib.h>
00031 #include <sysLib.h>
00032 #include <hostLib.h>
00033 #endif
00034
00035
00036 using namespace std;
00037 using namespace cmsg;
00038 using namespace codaObject;
00039
00040
00041
00042 int CodaObject::debug=0;
00043
00044
00045
00046
00047
00048
00049
00061 CodaObject::CodaObject(const string& udl, const string& Name, const string& Descr, const string &cClass,
00062 const cMsgSubscriptionConfig *scfg, int connTimeout) throw(CodaException)
00063 : UDL(udl), name(Name), descr(Descr), state("unknown"), myscfg(scfg), rcConn(NULL),
00064 codaCBD(new cMsgDispatcher<CodaObject>(this,&CodaObject::codaObjectCallback)), connTimeout(connTimeout),
00065 CodaObjectDaLogSubject("daLogSubject"), status("unknown"), config("unknown") {
00066
00067
00068
00069 if(debug!=0)cout << "Entering CodaObject constructor with udl " << udl << " ..." << endl;
00070
00071
00072
00073 objectType = "CodaObject";
00074
00075
00076
00077 codaClass = checkCodaClass(cClass);
00078
00079
00080
00081 char c[256];
00082 if(gethostname(c,sizeof(c))==0) {
00083 hostName=string(c);
00084 } else {
00085 hostName="unknown";
00086 }
00087
00088
00089
00090 char *u=getenv("USER");
00091 if(u!=NULL) {
00092 userName=string(u);
00093 } else {
00094 userName="unknown";
00095 }
00096
00097
00098
00099 if(debug!=0)cout << "...CodaObject constructor connecting..." << endl;
00100 bool conn=false;
00101 rcConn = new cMsg(UDL, name, descr);
00102 if(debug!=0)cout << "UDL,name,descr " << UDL << " " << name << " "<< descr << endl;
00103 for(int i=0; i<connTimeout; i+=2) {
00104 try {
00105 if(debug!=0)cout << "about to try." << endl;
00106 rcConn->connect();
00107 if(debug!=0)cout << "tried." << endl;
00108 conn=true;
00109 break;
00110 } catch (cMsgException &e) {
00111 if((e.returnCode==CMSG_NETWORK_ERROR)||(e.returnCode==CMSG_TIMEOUT)) {
00112 string info = "CodaObject constructor...attempting to establish run control connection";;
00113 cout << info << endl;
00114 daLogMsg(info,DALOG_INFO);
00115 #ifdef VXWORKS
00116 taskDelay(2*sysClkRateGet());
00117 #else
00118 sleep(2);
00119 #endif
00120 } else {
00121 string err = "?CodaObject constructor...error connecting to run control, UDL is: " + UDL;
00122 cerr << err << endl;
00123 daLogMsg(err,DALOG_ERROR);
00124 throw(CodaException(e.what()));
00125 }
00126 } catch (...) {
00127 string err = "?CodaObject constructor...unknown exception while connecting to run control, UDL is: " + UDL;
00128 cerr << err << endl;
00129 daLogMsg(err,DALOG_ERROR);
00130 throw(CodaException(err));
00131 }
00132 }
00133 if(!conn) {
00134 string err = "?CodaObject constructor...failed to connect to run control, UDL is: " + UDL;
00135 cerr << err << endl;
00136 daLogMsg(err,DALOG_ERROR);
00137 throw(CodaException(err));
00138 }
00139
00140
00141
00142 if(debug!=0)cout << "...CodaObject constructor subscribing..." << endl;
00143 try {
00144 rcConn->subscribe(name, "coda/info/*", codaCBD, NULL, myscfg);
00145 rcConn->subscribe("CodaObject", "coda/info/*", codaCBD, NULL, myscfg);
00146 } catch (cMsgException &e) {
00147 string err = "?CodaObject constructor...unable to subscribe";
00148 cerr << err << endl;
00149 daLogMsg(err,DALOG_ERROR);
00150 throw(CodaException(err));
00151 } catch (...) {
00152 string err = "?CodaObject constructor...unknown exception, unable to subscribe";
00153 cerr << err << endl;
00154 daLogMsg(err,DALOG_ERROR);
00155 throw(CodaException(err));
00156 }
00157
00158 if(debug!=0)cout << "...leaving CodaObject constructor" << endl;
00159 }
00160
00161
00162
00163
00164
00169 CodaObject::~CodaObject(void) throw() {
00170
00171 if(debug!=0)cout << "Entering CodaObject destructor..." << endl;
00172
00173 if(codaCBD!=NULL) {
00174 delete(codaCBD);
00175 codaCBD=NULL;
00176 }
00177
00178 if(rcConn!=NULL) {
00179 rcConn->disconnect();
00180 rcConn=NULL;
00181 }
00182
00183 if(debug!=0)cout << "...leaving CodaObject destructor" << endl;
00184 }
00185
00186
00187
00188
00189
00197 string CodaObject::checkCodaClass(const string &cClass) const throw() {
00198
00199 string s = cClass;
00200 std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
00201
00202 if((s=="roc")||(s=="eb")||(s=="er")||(s=="ts")||(s=="et")||(s=="ana"))return(cClass);
00203 return("USER");
00204 }
00205
00206
00207
00208
00209
00213 void CodaObject::startProcessing(void) throw(CodaException) {
00214
00215 if(debug!=0)cout << "Entering CodaObject startProcessing..." << endl;
00216
00217
00218 sendResponse(NULL,name,"rc/report/status",status,0);
00219
00220
00221 rcConn->start();
00222
00223 if(debug!=0)cout << "...leaving CodaObject startProcessing" << endl;
00224 }
00225
00226
00227
00228
00229
00236 void CodaObject::changeStatus(const string& newStatus) throw(CodaException) {
00237 if(debug!=0)cout << "CodaObject changeStatus, new status is " << newStatus << endl;
00238 status=newStatus;
00239 sendResponse(NULL,name,"rc/response/getStatus",status,0);
00240 }
00241
00242
00243
00244
00245
00246
00250 void CodaObject::stopProcessing(void) throw(CodaException) {
00251
00252 if(debug!=0)cout << "Entering CodaObject stopProcessing..." << endl;
00253
00254
00255 rcConn->stop();
00256
00257 if(debug!=0)cout << "...leaving CodaObject stopProcessing" << endl;
00258 }
00259
00260
00261
00262
00263
00271 void CodaObject::codaObjectCallback(cMsgMessage *msg, void *userArg) throw(CodaException) {
00272
00273 string s = msg->getType();
00274 std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) tolower);
00275
00276 if(debug!=0)cout << "codaObjectCallback received message, type is " << s << "..." << endl;
00277
00278
00279 if(s=="coda/info/getobjecttype") {
00280 sendResponse(msg,name,"rc/response/getObjectType",objectType,0);
00281
00282 } else if(s=="coda/info/getstate") {
00283 sendResponse(msg,name,"rc/response/getState",state,0);
00284
00285 } else if(s=="coda/info/getcodaclass") {
00286 sendResponse(msg,name,"rc/response/getCodaClass",codaClass,0);
00287
00288 } else if(s=="coda/info/getstatus") {
00289 sendResponse(msg,name,"rc/response/getStatus",status,0);
00290
00291 } else if(s=="coda/info/getconfig") {
00292 sendResponse(msg,name,"rc/response/getConfig",config,0);
00293
00294 } else {
00295
00296 userMsgHandler(msg,userArg);
00297 return;
00298 }
00299
00300 delete(msg);
00301 }
00302
00303
00304
00305
00306
00313 void CodaObject::changeState(const string& newState) throw(CodaException) {
00314 if(state!=newState) {
00315 cout << "CodaObject changeState, new state is " << newState << endl;
00316 state=newState;
00317 sendResponse(NULL,name,"rc/response/getState",state,0);
00318 }
00319 }
00320
00321
00322
00323
00324
00332 int CodaObject::daLogMsg(const string& text, int sevid, const string& daLogSubject) const throw(CodaException) {
00333
00334 stringstream ss;
00335
00336 if(rcConn==NULL) {
00337 throw(CodaException("?CodaObject::daLogMsg...connection not established yet"));
00338 return 1;
00339 }
00340
00341
00342
00343 cMsgMessage m;
00344
00345
00346 if(!daLogSubject.empty()) {
00347 m.setSubject(daLogSubject);
00348 } else if(!CodaObjectDaLogSubject.empty()) {
00349 m.setSubject(CodaObjectDaLogSubject);
00350 } else {
00351 m.setSubject("");
00352 }
00353
00354
00355 ss << "rc/report/dalog/" << sevid << ends;
00356 m.setType(ss.str());
00357
00358
00359
00360 m.setUserInt(sevid);
00361
00362
00363
00364 m.setText(text);
00365
00366
00367 ss.str("");
00368 if((sevid>=1)&&(sevid<=4)) {
00369 ss << "INFO";
00370 } else if((sevid>=5)&&(sevid<=8)) {
00371 ss << "WARN";
00372 } else if((sevid>=9)&&(sevid<=12)) {
00373 ss << "ERROR";
00374 } else if((sevid>=13)&&(sevid<=16)) {
00375 ss << "SEVERE";
00376 } else {
00377 ss << "unknown";
00378 }
00379 ss << "." << (sevid-1)%4+1 << ends;
00380 m.add("severityText",ss.str());
00381
00382
00383
00384 daLogMsgFill(m);
00385
00386
00387
00388 try {
00389 rcConn->send(m);
00390 rcConn->flush();
00391 } catch (cMsgException e) {
00392 string err = "?CodaObject::daLogMsg unable to send message\n" + e.toString();
00393 cerr << err << endl;
00394 throw(CodaException(err));
00395 return 2;
00396 } catch (...) {
00397 string err = "?CodaObject::daLogMsg...unknown exception, unable to send message\n";
00398 cerr << err << endl;
00399 throw(CodaException(err));
00400 return 3;
00401 }
00402 return 0;
00403 }
00404
00405
00406
00407
00408
00414 void CodaObject::daLogMsgFill(cMsgMessage &m) const throw(CodaException) {
00415
00416 m.add("codaName",name);
00417
00418 time_t now = time(NULL);
00419 m.add("tod",string(ctime(&now)));
00420 m.add("hostName",getHostName());
00421 m.add("userName",getUserName());
00422 }
00423
00424
00425
00426
00427
00437 void CodaObject::sendResponse(const cMsgMessage *msg, const string& subject, const string& type,
00438 const string& text, int userInt) const
00439 throw(CodaException) {
00440
00441
00442 cMsgMessage m;
00443 m.setSubject(subject);
00444 m.setType(type);
00445 m.setText(text);
00446 m.setUserInt(userInt);
00447
00448
00449 if((msg!=NULL)&&(msg->isGetRequest())) m.makeResponse(msg);
00450
00451
00452 try {
00453 rcConn->send(m);
00454 rcConn->flush();
00455 } catch (cMsgException e) {
00456 string err = "?CodaObject::sendResponse unable to send response\n" + e.toString();
00457 cerr << err << endl;
00458 daLogMsg(err,DALOG_ERROR);
00459 throw(CodaException(err));
00460 } catch (...) {
00461 string err = "?CodaObject::sendResponse, unknown exception, unable to send response\n";
00462 cerr << err << endl;
00463 daLogMsg(err,DALOG_ERROR);
00464 throw(CodaException(err));
00465 }
00466 }
00467
00468
00469
00470
00471
00477 string CodaObject::getStatus(void) const throw() {
00478 return(status);
00479 }
00480
00481
00482
00483
00489 string CodaObject::getObjectType(void) const throw() {
00490 return(objectType);
00491 }
00492
00493
00494
00495
00496
00502 string CodaObject::getCodaClass(void) const throw() {
00503 return(codaClass);
00504 }
00505
00506
00507
00508
00509
00515 string CodaObject::getHostName(void) const throw() {
00516 return(hostName);
00517 }
00518
00519
00520
00521
00522
00528 string CodaObject::getUserName(void) const throw() {
00529 return(userName);
00530 }
00531
00532
00533
00534
00535
00541 string CodaObject::getState(void) const throw() {
00542 return(state);
00543 }
00544
00545
00546
00547
00548
00554 string CodaObject::getConfig(void) const throw() {
00555 return(config);
00556 }
00557
00558
00559
00560
00561
00569 void CodaObject::userMsgHandler(cMsgMessage *msg, void *userArg) throw(CodaException) {
00570 string warn = "?CodaObject::userMsgHandler...default vsn ignoring unknown message type: " + msg->getType();
00571 cerr << warn << endl;
00572 daLogMsg(warn,DALOG_WARN);
00573 }
00574
00575
00576
00577
00578
00585 string CodaObject::userStatus(void) const throw() {
00586 return("");
00587 }
00588
00589
00590
00591
00592
00593
00594