00001 #ifdef INCLMD
00002 #include "auc-md.h"
00003 #endif
00004 #ifdef INCLDV
00005 #include "md_device.h"
00006 #else
00007 #include "masterDaemon.h"
00008 #endif
00009
00010 class mdResponse;
00011
00012 using namespace std;
00013
00014 unsigned short mdOperationalDataElement::pack(char *framePtr) {
00015
00016 unsigned short value = 0;
00017
00018 return value;
00019
00020 }
00021 mdOperationalDataElement::mdOperationalDataElement() {
00022 stringValue = std::string("");
00023 ode = new mdODEPOD;
00024 ode->bitValue = false;
00025 ode->realValue = 0.0;
00026 ode->intValue = 0;
00027 ode->hasSource = false;
00028 ode->realtime = true;
00029 ode->referenceType = 's';
00030 ode->sValSize = ode->sVal = 0;
00031 }
00032 mdOperationalDataElement::mdOperationalDataElement(mdODEPOD *shared) {
00033 stringValue = std::string("");
00034 ode = shared;
00035 }
00036
00037 #ifndef INCLDV
00038 void mdOperationalDataElement::source(mdResponse *mdr) {
00039
00040 int sValSize;
00041 mdODEPOD *flat = (mdODEPOD *)&mdr->reply.dg.payLoad[0];
00042
00043 memcpy(flat,&this->ode,sizeof(mdODEPOD));
00044 strcpy(&flat->sVal,this->stringValue.c_str());
00045 sValSize = this->stringValue.length();
00046 flat->sValSize = sValSize + 1;
00047
00048 mdr->reply.dg.hdr.primeOffset = mdr->reply.dg.hdr.payloadSize = sizeof(mdODEPOD) + sValSize;
00049
00050 }
00051 #endif
00052 void mdOperationalDataElement::unpack(char **framePtr) {
00053
00054
00055 }
00056 #ifndef INCLDV
00057 std::string mdState::create(int deviceHandle,std::string &typeSig,std::string &dataName) {
00058
00059 mdObservable neuName;
00060 mdOperationalDataElement *newName;
00061 std::string rc("OK");
00062
00063 if (dataName.at(0) != '_')
00064 return neuName.create(deviceHandle,typeSig,dataName);
00065
00066 if (thisConfig->allClients[deviceHandle]->devType == MDDEV_INSTRUMENT) {
00067
00068 if ( thisConfig->instruments[deviceHandle]->state.localODEs.find(dataName) !=
00069 thisConfig->instruments[deviceHandle]->state.localODEs.end() ) {
00070
00071 rc = std::string("Dataname already defined.");
00072 }
00073 else {
00074
00075 newName = &(thisConfig->instruments[deviceHandle]->state.localODEs[dataName]);
00076 newName->ode->referenceType = typeSig[0];
00077
00078 }}
00079 else if (thisConfig->allClients[deviceHandle]->devType == MACHINE)
00080 {
00081 if ( theMachine->state.localODEs.find(dataName) !=
00082 theMachine->state.localODEs.end() ) {
00083
00084 rc = std::string("Dataname already defined.");
00085 }
00086 else {
00087
00088 newName = &(theMachine->state.localODEs[dataName]);
00089 newName->ode->referenceType = typeSig[0];
00090
00091 }
00092 }
00093 else rc = NOT_OK;
00094
00095 if (rc == "OK")
00096 theseLogs->logNdebug(MAX_DEBUG/1000,2,"New ODE: '%s' defined by device: %d.",dataName.c_str(),deviceHandle);
00097 else
00098 theseLogs->logNdebug(0,3,"Failed New ODE: '%s' attemptd by device: %d: %s.",dataName.c_str(),deviceHandle,rc.c_str());
00099
00100 return rc;
00101
00102 }
00103 xmlrpc_c::value *mdState::get(int deviceHandle,std::string &dataname) {
00104
00105 map<string, xmlrpc_c::value> returnData;
00106 mdObservable focus;
00107 xmlrpc_c::value *gotten;
00108
00109 try {
00110
00111 if (thisConfig->allClients[deviceHandle]->devType == MDDEV_INSTRUMENT) {
00112
00113 if ( thisConfig->instruments[deviceHandle]->state.observables.find(dataname) ==
00114 thisConfig->instruments[deviceHandle]->state.observables.end() )
00115 {
00116 theseLogs->logNdebug(100,2,"Unknown datum: '%s' requested from device: %d.",dataname.c_str(),deviceHandle);
00117 returnData["dataname"] = xmlrpc_c::value_string("not found");
00118 gotten = new xmlrpc_c::value_struct(returnData);
00119 }
00120 else {focus = thisConfig->instruments[deviceHandle]->state.observables[dataname];
00121 gotten = focus.shipIt(returnData,dataname);
00122 }
00123 }
00124 else if (thisConfig->allClients[deviceHandle]->devType == MACHINE) {
00125
00126 if ( theMachine->state.observables.find(dataname) ==
00127 theMachine->state.observables.end() )
00128 {
00129 theseLogs->logNdebug(100,2,"Unknown datum: '%s' requested from device: %d.",dataname.c_str(),deviceHandle);
00130 returnData["dataname"] = xmlrpc_c::value_string("not found");
00131 gotten = new xmlrpc_c::value_struct(returnData);
00132 }
00133 else {focus = theMachine->state.observables[dataname];
00134 gotten = focus.shipIt(returnData,dataname);
00135 }
00136 }
00137
00138 }
00139 catch(exception &e) {
00140 theseLogs->logN(1,"fault - mdState.get: %s",e.what());
00141 }
00142
00143 return gotten;
00144
00145 }
00146 std::string mdState::set(int deviceHandle,xmlrpc_c::cstruct &inbound) {
00147
00148 std::string dataname("dataname"),svalue("sValue"),rc("NOT OK");
00149 mdObservable *focus;
00150
00151 if (inbound.find(dataname) == inbound.end() || inbound.find(svalue) == inbound.end()) {
00152 if (inbound.find(dataname) == inbound.end())
00153 theseLogs->logNdebug(0,1,"Missing dataname in update attempt from device: %d.",deviceHandle);
00154 if (inbound.find(svalue) == inbound.end())
00155 theseLogs->logNdebug(0,1,"Missing string value in update attempt from device: %d.",deviceHandle);
00156 return rc;
00157 }
00158
00159 try {dataname = std::string(xmlrpc_c::value_string(inbound["dataname"]));
00160
00161 if (thisConfig->allClients[deviceHandle]->devType == MDDEV_INSTRUMENT) {
00162
00163 if ( thisConfig->instruments[deviceHandle]->state.observables.find(dataname) !=
00164 thisConfig->instruments[deviceHandle]->state.observables.end() )
00165 {
00166 focus = &thisConfig->instruments[deviceHandle]->state.observables[dataname];
00167 rc = focus->setWith(inbound);
00168 }
00169 else {
00170 theseLogs->logNdebug(1000,2,"Unknown datum: '%s' update attempt from device: %d.",dataname.c_str(),deviceHandle);
00171 }
00172 } else if (thisConfig->allClients[deviceHandle]->devType == MACHINE)
00173 {if ( theMachine->state.observables.find(dataname) !=
00174 theMachine->state.observables.end() )
00175 {
00176 focus = &theMachine->state.observables[dataname];
00177 rc = focus->setWith(inbound);
00178 }
00179 else {
00180 theseLogs->logNdebug(1000,2,"Unknown datum: '%s' update attempt from device: %d.",dataname.c_str(),deviceHandle);
00181 }
00182 }
00183 }
00184 catch(exception &e) {
00185 theseLogs->logN(1,"fault - mdState.set: %s",e.what());
00186 }
00187
00188 return rc;
00189
00190 }
00191 void mdState::registerData(const char *dataName,const mdIncoming &thisOne) {
00192
00193 const char *msg;
00194 char *name;
00195 int value = OK;
00196 std::string arg = std::string(dataName);
00197 std::map<int,mdLiveClient*>::iterator iter = thisConfig->allClients.find(thisOne.dg.hdr.handle);
00198 mdResponse *result = new mdResponse(thisService->bg,thisOne.ip);
00199
00200 result->reply.dg.hdr = thisOne.dg.hdr;
00201 result->dCat = DV_MDQUERY;
00202
00203 if( iter == thisConfig->allClients.end() ) {
00204 theseLogs->logN(1,"Query for device whose handle (%d) absent, ignored.", thisOne.dg.hdr.handle );
00205 value = MDERR_NOTREADY;
00206 goto done;
00207 }
00208
00209 result->mdStdDevIdx = iter->second->mdStdDevIdx;
00210
00211 if (dataName[0] != '_') {
00212
00213 if (observables.empty()) {
00214 theseLogs->logN(1,"attempt to register '%s' but device not ready to accept data element registration.",dataName);
00215 value = MDERR_NOTREADY;
00216 goto done;
00217 }
00218
00219 if( observables.find(arg) == observables.end() ) {
00220 theseLogs->logN(1,"attempt to register '%s' which does not yet exist.",dataName);
00221 value = MDERR_MISSING;
00222 goto done;
00223 }
00224
00225 mdObservable sourced = observables[arg];
00226 if (sourced.obs->hasSource) {value = MDERR_CONFLICT; goto done;}
00227 sourced.obs->hasSource=true;
00228 sourced.source(result);
00229 result->reply.dg.hdr.dgSubType = MDDG_REGOBS;
00230
00231 } else {
00232
00233 if (localODEs.empty()) {
00234 theseLogs->logN(1,"attempt to register '%s' but device not ready to accept data element registration.",dataName);
00235 value = MDERR_NOTREADY;
00236 goto done;
00237 }
00238
00239 if( localODEs.find(arg) == localODEs.end() ) {
00240 theseLogs->logN(1,"attempt to register '%s' which does not yet exist.",dataName);
00241 value = MDERR_MISSING;
00242 goto done;
00243 }
00244
00245 mdOperationalDataElement sourced = localODEs[arg];
00246 if (sourced.ode->hasSource)
00247 {value = MDERR_CONFLICT; goto done;}
00248 sourced.ode->hasSource=true;
00249 sourced.source(result);
00250 result->reply.dg.hdr.dgSubType = MDDG_REGODE;
00251
00252 }
00253
00254 done:
00255
00256 if (value == OK) {
00257 msg = dataName;
00258 result->reply.dg.hdr.dgType.value = 1;
00259 }
00260 else msg = thisConfig->err[value];
00261
00262 result->reply.dg.hdr.msgType = MDDG_MDQUERY;
00263 name = (char *)(&result->reply.dg.payLoad[0] + result->reply.dg.hdr.primeOffset);
00264
00265 strcpy(name,msg);
00266 result->reply.dg.hdr.payloadSize = result->reply.dg.hdr.primeOffset + strlen(name) + 1;
00267 result->send();
00268
00269 }
00270
00271 #endif