00001 #include "auc-md.h"
00002 #include "masterDaemon.h"
00003 #include "../server/Listener.cpp"
00004 #include "../server/EventSender2.cpp"
00005 using namespace std;
00006
00011 int getHandle() {
00012
00013 map<int,mdLiveClient*>::iterator it;
00014
00015 bool collision = thisConfig->allClients.size() > 0 ? false: true, found;
00016 int value; srand ( time(NULL) );
00017
00018 value = (rand() % (MAX_DEVICE * 10)) + 1;
00019
00020 while(collision) {
00021 for(found = false, it = thisConfig->allClients.begin();
00022 it != thisConfig->allClients.end() && !found;
00023 found = (it->first == value ? true : false), it++);
00024 if (!found) collision = false;
00025 else value = (rand() % (MAX_DEVICE * 10)) + 1;
00026
00027 }
00028 return value;
00029
00030 }
00031 template<class T> T* mdDevice<T>::registeR(md_device t) {
00032
00033 T *value=NULL;
00034 int h = getHandle();
00035
00036 if (value=mdDevice<T>::validateClient(h)) {
00037 theseLogs->logN(2,"Handle %d assigned to new client of type: %s",clientTypes[t]);
00038 } else {
00039 theseLogs->logN(2,"Validation failed for client type: %s",clientTypes[t]);
00040 return value;
00041 }
00042
00043 value->handle = h;
00044 value->create();
00045 thisConfig->allClients[h] = value;
00046 return value ;
00047
00048 }
00049 template<class T>
00050 void mdDevice<T>::lxi_control(T *device, std::string fullText) {
00051
00052 T *target = device;
00053 char *ip,*port,*command,*timeout,*argv[5];
00054
00055 argv[1] = ip = (char *)malloc(32);
00056 argv[2] = port = (char *)malloc(16);
00057 argv[3] = timeout = (char *)malloc(32);
00058 argv[4] = command = (char *)malloc(1024);
00059
00060 sprintf(ip,"--ip %s",target->ip.c_str());
00061 sprintf(port,"--port %s",target->port.c_str());
00062 sprintf(timeout,"--timeout %s",target->timeout.c_str());
00063 sprintf(command,"--scpi %s",fullText.c_str());
00064
00065 lxi_control(5,argv);
00066
00067 free(ip);
00068 free(port);
00069 free(timeout);
00070 free(command);
00071
00072 }
00073 mdClientServer* mdClientServer::validateClient(int handle, mdResponse &r) {
00074
00075 bool isNew=true;
00076 int i,m=-1,n=-1;
00077 mdClientServer *value=NULL;
00078
00079 if (!thisConfig->nClievers) {m =0; isNew = true;}
00080 else for(i=0;i<MAX_CLIEVER && isNew;i++)
00081 {if (m < 0 && thisConfig->clievers[i].empty()) m = i;
00082 if (ip.address().to_string() == thisConfig->clievers[i]) isNew = false;
00083 }
00084
00085 if (isNew && thisConfig->nClievers < MAX_CLIEVER)
00086 { for (n=i=0;i<thisConfig->nClievers && n < 0;i++)
00087 if (!thisConfig->cliever[i]) n = i;
00088 thisConfig->nClievers++;
00089 thisConfig->cliever[n] = value = this;
00090 mdStdDevIdx = n + 1;
00091 ip = r.ip;
00092 thisConfig->clievers[n] = ip.address().to_string();
00093 }
00094 else {
00095 theseLogs->logN(1,"Either a Cliever already active at %s or limit number (%d) reached.",ip.address().to_string().c_str(),MAX_CLIEVER);
00096 }
00097 return value;
00098
00099 }
00100 mdMachine* mdMachine::validateClient(int handle, const mdClientBirth &c, mdResponse &r) {
00101
00102 char *cp;
00103 mdMachine *value=NULL;
00104
00105 if (!theMachine) {
00106 if (c.dg.hdr.dgType.clieverGroup) {
00107 theseLogs->logN(1,"Machine specified non-zero cliever group(%d) in GDOLMS 1.x, rejected.",c.dg.hdr.dgType.clieverGroup);
00108 goto done;
00109 }
00110 if (!thisConfig->cliever[c.dg.hdr.dgType.clieverGroup]) {
00111 theseLogs->logN(1,"The cliever for this device group (%d) is not online, machine birth rejected.",c.dg.hdr.dgType.clieverGroup);
00112 goto done;
00113 }
00114 strcpy(r.reply.dg.payLoad,thisConfig->clievers[c.dg.hdr.dgType.clieverGroup].c_str());
00115 cp = &r.reply.dg.payLoad[0] + strlen(r.reply.dg.payLoad) + 1;
00116 *((unsigned short *)cp) = thisConfig->cliever[c.dg.hdr.dgType.clieverGroup]->ip.port();
00117 theMachine = value = this;
00118 mdStdDevIdx = MAX_CLIEVER + 1;
00119 }
00120
00121 done:
00122 return value;
00123
00124 }
00125 mdInstrument* mdInstrument::validateClient(int handle, const mdClientBirth &c, mdResponse &r) {
00126
00127 mdInstrument *value=NULL;
00128
00129 if (thisConfig->instruments.size() < MAX_INSTRUMENTS) {
00130 thisConfig->instruments[handle] = value = this;
00131
00132 }
00133 else theseLogs->logN(1,"Too many instruments, configured limit is: %d.",MAX_INSTRUMENTS);
00134
00135 return value;
00136
00137 }
00138 mdDataClient* mdDataClient::validateClient(int handle) {
00139
00140 mdDataClient *value=NULL;
00141
00142 if (thisConfig->clients.size() < MAX_DATACLIENTS) {
00143 thisConfig->clients[handle] = value = this;
00144 }
00145 else theseLogs->logN(1,"Too many non-device clients, configured limit is: %d.",MAX_DATACLIENTS);
00146
00147 return value;
00148
00149 }
00150 std::string mdDeviceFabrik::newFromAPI(md_device type,std::string thisSpecialOne) {
00151
00152 }
00153 void mdDeviceFabrik::newFromHeartbeat(const mdClientBirth &thisOne) {
00154
00155 const char *kind,*outcome;
00156 void *resultat;
00157
00158 int i,mdStdDevIdx;
00159 md_device thisKind;
00160 mdCB *newControlBlock;
00161 mdLiveClient *newAllMap;
00162 mdClientServer *newCliever;
00163 mdMachine *newMachine;
00164 mdInstrument *newInstrument;
00165 mdResponse *result = new mdResponse(thisService->bg,thisOne.ip);
00166
00167 int maybe=getHandle();
00168
00169 result->dCat = MD_NEWBORN;
00170 result->reply.dg.hdr = thisOne.dg.hdr;
00171 result->reply.dg.hdr.dgType.isAckNak = true;
00172 result->reply.dg.hdr.dgType.value = true;
00173 result->ip = thisOne.ip;
00174
00175 switch(thisOne.dg.hdr.clientType) {
00176 case MDDEV_CD:
00177 thisKind = MDDEV_CD;
00178 kind = "cliever";
00179 newCliever = new mdClientServer();
00180 if (resultat = newCliever = newCliever->validateClient( maybe, *result )) {
00181 newCliever->ip = thisOne.ip;
00182 mdStdDevIdx = newCliever->mdStdDevIdx;
00183 }
00184 else delete newCliever;
00185 break;
00186 case MACHINE:
00187 thisKind = MACHINE;
00188 kind = "machine";
00189 newMachine = new mdMachine();
00190 if (resultat = newMachine = newMachine->validateClient( maybe, thisOne, *result )) {
00191 theMachine = newMachine;
00192 newMachine->ip = thisOne.ip;
00193 mdStdDevIdx = MAX_CLIEVER + thisOne.dg.hdr.dgType.clieverGroup;
00194 }
00195 else delete newMachine;
00196 break;
00197 case MDDEV_INSTRUMENT:
00198 thisKind = MDDEV_INSTRUMENT;
00199 kind = "instrument";
00200 newInstrument = new mdInstrument();
00201 if (!(resultat = newInstrument = newInstrument->validateClient( maybe, thisOne, *result )))
00202 delete newInstrument;
00203 else {newInstrument->ip = thisOne.ip;
00204 for (i=0;i<MAX_INSTRUMENTS && thisService->instruments[i];i++);
00205 thisService->instruments[i] = maybe;
00206 mdStdDevIdx = 2+i;
00207 }
00208 break;
00209 }
00210
00211 outcome = resultat ? "succeeded" : "failed";
00212 theseLogs->logN(2,"The %s instantiation request %s.",kind,outcome);
00213
00214 if (!resultat) { result->reply.dg.hdr.dgType.value = false;
00215 result->mdStdDevIdx = MDDEV_MD;
00216 }
00217 else { newAllMap = new mdLiveClient();
00218 newAllMap->devType = thisKind;
00219 newAllMap->mdStdDevIdx = mdStdDevIdx;
00220 thisConfig->allClients[maybe] = newAllMap;
00221 result->reply.dg.hdr.sinkHandle = maybe;
00222 result->mdStdDevIdx = mdStdDevIdx;
00223 cb[mdStdDevIdx] = newControlBlock = new mdCB;
00224 newControlBlock->handle = maybe;
00225 }
00226
00227 result->send();
00228
00229 }
00230 void mdMachine::registerCmd(const char *cmdName,const mdIncoming &thisOne) {
00231
00232 const char *msg;
00233 char *name;
00234 int value = OK;
00235 std::string arg = std::string(cmdName);
00236 std::map<int,mdLiveClient*>::iterator iter = thisConfig->allClients.find(thisOne.dg.hdr.handle);
00237 mdResponse *result = new mdResponse(thisService->bg,thisOne.ip);
00238
00239 result->reply.dg.hdr = thisOne.dg.hdr;
00240 result->dCat = DV_MDQUERY;
00241
00242 if( iter == thisConfig->allClients.end() ) {
00243 theseLogs->logN(1,"Cmd reg for device whose handle (%d) absent, ignored.", thisOne.dg.hdr.handle );
00244 value = MDERR_NOTREADY;
00245 goto done;
00246 }
00247
00248 result->mdStdDevIdx = iter->second->mdStdDevIdx;
00249
00250 if (cmds.empty()) {
00251 theseLogs->logN(1,"attempt to register '%s' but device not ready to accept command registration.",cmdName);
00252 value = MDERR_NOTREADY;
00253 goto done;
00254 }
00255
00256 if( cmds.find(arg) == cmds.end() ) {
00257 theseLogs->logN(1,"attempt to register '%s' whose rules has not yet been defined.",cmdName);
00258 value = MDERR_MISSING;
00259 goto done;
00260 }
00261
00262
00263
00264 if (cmds[arg]->getHandler())
00265 {value = MDERR_CONFLICT; goto done;}
00266 else{
00267 cmds[arg]->setHandler(cmds[arg]);
00268 result->reply.dg.hdr.dgSubType = MDDG_REGSCPI;
00269 }
00270
00271 done:
00272
00273 if (value == OK) {
00274 msg = cmdName;
00275 result->reply.dg.hdr.dgType.value = 1;
00276 }
00277 else msg = thisConfig->err[value];
00278
00279 result->reply.dg.hdr.msgType = MDDG_MDQUERY;
00280 name = (char *)(&result->reply.dg.payLoad[0] + result->reply.dg.hdr.primeOffset);
00281
00282 strcpy(name,msg);
00283 result->reply.dg.hdr.payloadSize = result->reply.dg.hdr.primeOffset + strlen(name) + 1;
00284 result->send();
00285
00286 }
00287
00288
00289
00290 void mdInstrument::registerCmd(const char *cmdName,const mdIncoming &thisOne) {
00291
00292 const char *msg;
00293 char *name;
00294 int value = OK;
00295 std::string arg = std::string(cmdName);
00296 std::map<int,mdLiveClient*>::iterator iter = thisConfig->allClients.find(thisOne.dg.hdr.handle);
00297 mdResponse *result = new mdResponse(thisService->bg,thisOne.ip);
00298
00299 result->reply.dg.hdr = thisOne.dg.hdr;
00300 result->dCat = DV_MDQUERY;
00301
00302 if( iter == thisConfig->allClients.end() ) {
00303 theseLogs->logN(1,"Cmd reg for device whose handle (%d) absent, ignored.", thisOne.dg.hdr.handle );
00304 value = MDERR_NOTREADY;
00305 goto done;
00306 }
00307
00308 result->mdStdDevIdx = iter->second->mdStdDevIdx;
00309
00310 if (cmds.empty()) {
00311 theseLogs->logN(1,"attempt to register '%s' but device not ready to accept command registration.",cmdName);
00312 value = MDERR_NOTREADY;
00313 goto done;
00314 }
00315
00316 if( cmds.find(arg) == cmds.end() ) {
00317 theseLogs->logN(1,"attempt to register '%s' which has no rules basis.",cmdName);
00318 value = MDERR_MISSING;
00319 goto done;
00320 }
00321
00322
00323
00324 if (cmds[arg]->getHandler())
00325 {value = MDERR_CONFLICT; goto done;}
00326 else{
00327 cmds[arg]->setHandler(cmds[arg]);
00328 result->reply.dg.hdr.dgSubType = MDDG_REGSCPI;
00329 }
00330
00331 done:
00332
00333 if (value == OK) {
00334 msg = cmdName;
00335 result->reply.dg.hdr.dgType.value = 1;
00336 }
00337 else msg = thisConfig->err[value];
00338
00339 result->reply.dg.hdr.msgType = MDDG_MDQUERY;
00340 name = (char *)(&result->reply.dg.payLoad[0] + result->reply.dg.hdr.primeOffset);
00341
00342 strcpy(name,msg);
00343 result->reply.dg.hdr.payloadSize = result->reply.dg.hdr.primeOffset + strlen(name) + 1;
00344 result->send();
00345
00346 }