Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

xmlstorage.cpp

Go to the documentation of this file.
00001 //xmlstorage.cpp
00002 
00003 #include <string.h>
00004 #include <iostream.h>
00005 
00006 #include "xmlstorage.h"
00007 #include "Storage.h"
00008 #include "charstr.h"
00009 #include "xmldata.h"
00010 #include "messages.h"
00011 #include "List.h"
00012 
00013 //used by x2o(x, TaskStatus)
00014 int x2o(CharStr *x, task_state &o)
00015 {
00016   if (strcmp(x->str, p_Buffered->str) == 0)
00017     o = task_buffered;
00018   else if (strcmp(x->str, p_Refused->str) == 0)
00019     o = task_refused;
00020   else if (strcmp(x->str, p_Started->str) == 0)
00021     o = task_started;
00022   else if (strcmp(x->str, p_Crashed->str) == 0)
00023     o = task_crashed;
00024   else if  (strcmp(x->str, p_Stopped->str) == 0)
00025     o = task_stopped;
00026   else if  (strcmp(x->str, p_Ok->str) == 0)
00027     o = task_ok;
00028   else { o = task_internal_error; return 0; }
00029   return 1;
00030 }
00031        
00032 int x2o(XMLData *x, TaskStatus &o)
00033 {
00034     x->reset();
00035     CharStr *err = x->sub(p_Error)->getString();
00036     o._cs_error(err);
00037     if (x2o(x->sub(p_TaskID), o.taskId))
00038       if (x2o(x->sub(p_Status)->getString(), o.state))
00039         return 1;
00040     return 0;
00041 }
00042 
00043 //used by o2x(TaskStatus)
00044 CharStr *o2x(const task_state o)
00045 {
00046     if (o == task_buffered)
00047         return p_Buffered;
00048     else if (o == task_refused)
00049         return p_Refused;
00050     else if (o == task_started)
00051         return p_Started;
00052     else if (o == task_crashed)
00053         return p_Crashed;
00054     else if (o == task_stopped)
00055         return p_Stopped;
00056     else if (o == task_ok)
00057         return p_Ok;
00058     else 
00059         return CharStr::Error;
00060 }
00061 
00062 XMLData *o2x(const TaskStatus &o)
00063 {
00064     XMLData *ts= new XMLData(p_Message, o2x(o.taskId));
00065     ts->setAttrib(p_Type, p_M_TASK_STATUS);
00066     ts->add(new XMLData(p_Status, o2x(o.state)));
00067     if (o._cs_error() != CharStr::Error) ts->add(new XMLData(p_Error, o._cs_error()));
00068 
00069     return ts;
00070 }
00071 
00072 XMLData *o2x(char *jobName, const int jobId)
00073 {
00074     return new XMLData(p_JobID, 
00075            new XMLData(p_Name, CharStr::create(jobName),
00076            new XMLData(p_ID, jobId)));
00077 }
00078 
00079 int x2o(CharStr *x, job_state &o)
00080 {
00081     if (strcmp(x->str, p_Abandoned->str) == 0) o = job_abandoned;
00082     else if (strcmp(x->str, p_Running->str) == 0) o = job_running;
00083     else if (strcmp(x->str, p_Refused->str) == 0) o = job_refused;
00084     else if (strcmp(x->str, p_Stopped->str) == 0) o = job_stopped;
00085     else { o = job_internal_error; return 0; }
00086     return 1;
00087 }
00088 
00089 int x2o(XMLData *x, ReserveId &o)
00090 {
00091     XMLData *p;
00092     x->reset();
00093     p = x->sub(p_Name);
00094     if (p == XMLData::Nil) return 0;
00095     o._cs_name(p->getString());
00096     p = x->sub(p_ID);
00097     if (p == XMLData::Nil) return 0;
00098     o.id = p->getLong();
00099     return 1;
00100 }
00101 
00102 int x2o(XMLData *x, Reservation &o)
00103 {
00104     x->reset();
00105     if (!x2o(x->sub(p_ReserveID), o.resID)) return 0;
00106     if (x->sub(p_Parallel) != XMLData::Nil)
00107         o.parallel = 1;
00108     else 
00109         o.parallel = 0;
00110     XMLData *p = x->sub(p_SlavesRequired);
00111     if (p == XMLData::Nil) return 0;
00112     o.slavesRequired = p->getLong();
00113     return 1;
00114 }
00115 
00116 int x2o(XMLData *x, JobStatus &o)
00117 {
00118   //int x2o(XMLData *x, JobId &o);
00119   
00120     x->reset();
00121     //    clog << "yo";
00122     //    x->print(clog);
00123     //    x->sub(p_JobID)->print(clog);
00124     //    if (!x2o(x->sub(p_JobID),o.jobID)) return 0;
00125     //    clog << "yo2";
00126     if (!x2o(x->sub(p_Status)->getString(), o.state)) return 0;  // TODO What is this?
00127     o.nRunning = x->sub(p_TasksRunning)->getLong();
00128     o.nWaiting = x->sub(p_TasksWaiting)->getLong();
00129     o.nReservations = 0;
00130     if (o.reservations) delete [] o.reservations;
00131     if (x->sub(p_ReserveID) != XMLData::Nil)
00132     {
00133         return x2o(x->sub(), o.resID);
00134     }
00135     else 
00136     {
00137         int rv = 1;
00138         List collectRes = List();
00139         Reservation *r;
00140         x->reset();        
00141         while (x->sub(p_Reservation) != XMLData::Nil)
00142         {
00143             r = new Reservation();
00144             rv *= x2o(x->sub(), *r);
00145             collectRes.Add(r);
00146             o.nReservations++;
00147         }        
00148         if (o.nReservations)
00149         {
00150             o.reservations = new pReservation[o.nReservations];
00151             for (int i = 0; i < o.nReservations; i++)
00152             {
00153                 o.reservations[i] = (Reservation *)collectRes.Get_First();
00154                 collectRes.Remove(o.reservations[i]);
00155             }
00156         }
00157         return rv;
00158     }
00159 }
00160 
00161 //used by o2x(JobStatus)
00162 CharStr *o2x(const job_state o)
00163 {
00164   if (o == job_running) return p_Running;
00165   else if (o == job_abandoned) return p_Abandoned;
00166   else if (o == job_refused) return p_Refused;
00167   else if (o == job_stopped) return p_Stopped;
00168   return CharStr::Error;
00169 }
00170 
00171 XMLData *o2x(const ReserveId &o)
00172 {
00173     return new XMLData(p_ReserveID, 
00174            new XMLData(p_Name, o._cs_name(),
00175            new XMLData(p_ID, o.id)));
00176 }
00177 
00178 XMLData *o2x(const Reservation &o)
00179 {
00180     XMLData *res = new XMLData(p_Reservation, o2x(o.resID));
00181     if (o.parallel) res->add(new XMLData(p_Parallel, XMLData::Nil));
00182     res->add(new XMLData(p_SlavesRequired, o.slavesRequired));
00183     return res;
00184 }
00185 
00186 XMLData *o2x(const JobStatus &o)
00187 {
00188     XMLData *jobstat = new XMLData(p_JobStatus, 
00189            new XMLData(p_Status, o2x(o.state),
00190            new XMLData(p_TasksRunning, o.nRunning,
00191            new XMLData(p_TasksWaiting, o.nWaiting))));
00192     //if (o.resID != NULL)
00193     if (o.resID.id != 0)
00194         jobstat->add(o2x(o.resID));
00195     else if (o.reservations != NULL)
00196         for (int i = 0; i < o.nReservations; i++)
00197             jobstat->add(o2x(*o.reservations[i]));
00198     return jobstat;
00199 }
00200 
00201 XMLData *o2x(const TaskId &o)
00202 {
00203     XMLData *taskID = new XMLData(p_TaskID, o2x(o.jId));
00204     taskID->add(new XMLData(p_ID, o.id));
00205     return taskID;
00206 }
00207 
00208 int x2o(XMLData *x, TaskId &o)
00209 {
00210     x->reset();
00211     if (!x2o(x->sub(p_JobID), o.jId)) return 0;
00212     XMLData *p = x->sub(p_ID);
00213     if (p == XMLData::Nil) return 0;
00214     o.id = p->getLong();
00215     return 1;
00216 }
00217 
00218 int x2o(XMLData *x, JobId &o)
00219 {
00220     x->reset();
00221     XMLData *p = x->sub(p_Name);
00222     if (p == XMLData::Nil) return 0;
00223     o._cs_name(p->getString());
00224     p = x->sub(p_ID);
00225     if (p == XMLData::Nil) return 0;
00226     o.id = p->getLong();
00227     return 1;
00228 }
00229 
00230 XMLData *o2x(const JobId &o)
00231 {
00232     return  new XMLData(p_JobID,
00233                  new XMLData(p_Name, o._cs_name(),
00234                  new XMLData(p_ID, o.id)));
00235 }
00236 
00237 int x2o(XMLData *x, TaskInfo &o)
00238 {
00239     x->reset();
00240     XMLData *p = x->sub(p_OS);
00241     if (p == XMLData::Nil) return 0;
00242     o._cs_os(p->getString());
00243     p = x->sub(p_CPU);
00244     if (p == XMLData::Nil) return 0;
00245     o._cs_cpu(p->getString());
00246     CharStr *pcs = p->getAttrib(p_Speed);
00247     if (pcs == CharStr::Error) o.cpu_speed = 0;
00248     else o.cpu_speed = atoi(pcs->str);
00249     p = x->sub(p_URL);
00250     if (p == XMLData::Nil) return 0;
00251     o._cs_url(p->getString());
00252     p = x->sub(p_Memory);
00253     if (p == XMLData::Nil) return 0;
00254     o.mem_unit = convert_unit(p->getAttrib(p_Unit));
00255     o.mem = p->getLong();
00256     p = x->sub(p_Disk);
00257     if (p == XMLData::Nil) return 0;
00258     o.disk_unit = convert_unit(p->getAttrib(p_Unit));
00259     o.disk = p->getLong();
00260     p = x->sub(p_TimeOut);
00261     if (p == XMLData::Nil) return 0;
00262     o.timeout = p->getLong();
00263     // also convert software
00264     int softCount = 0;
00265     while (XMLData::Nil != x->sub(p_Software)) softCount++;
00266     x->reset();
00267     if (softCount > 0)
00268     {
00269       o.nSoftInfos = softCount;
00270       SoftwareInfo *si = new SoftwareInfo[softCount];
00271       while (softCount-- > 0)
00272       {
00273         XMLData *y = x->sub(p_Software);
00274         si[softCount]._cs_version(y->getAttrib(p_Version));
00275         si[softCount]._cs_name(y->getString());
00276       }
00277       o.softwareInfos = si;
00278     }
00279     o._cs_userData(x->getString(p_UserData));
00280     return 1;
00281 }
00282 
00283 CharStr *convert_unit(long unit)
00284 {
00285     if (unit == 1024) return p_KB;
00286     else if (unit == 1024*1024) return p_MB;
00287     else return new CharStr(unit);
00288 }
00289 
00290 long convert_unit(CharStr *unit)
00291 {
00292 //note: if your platform doesn't support strcasecmp or stricmp, just use strcmp()
00293 #ifdef _WIN32
00294     if (stricmp(unit->str, p_KB->str) == 0)
00295         return 1024;
00296     else if (stricmp(unit->str, p_MB->str) == 0)
00297         return 1024*1024;
00298     else 
00299         return atol(unit->str);
00300 #else
00301     if (strcasecmp(unit->str, p_KB->str) == 0)
00302         return 1024;
00303     else if (strcasecmp(unit->str, p_MB->str) == 0)
00304         return 1024*1024;
00305     else 
00306         return atol(unit->str);
00307 #endif
00308 }
00309     
00310 
00311 XMLData *o2x(const TaskInfo &o)
00312 {
00313    int i;
00314    XMLData *softInfos;
00315    if (o._cs_userData() != CharStr::Error)
00316        softInfos = new XMLData(p_UserData, o._cs_userData());
00317    else
00318        softInfos = XMLData::Nil;
00319    if (o.nSoftInfos)
00320    {
00321       for (i = 0; i < o.nSoftInfos; i++)
00322       {
00323         if (o.softwareInfos[i]._cs_name() != CharStr::Error)
00324           softInfos = new XMLData(p_Software, o.softwareInfos[i]._cs_name(), softInfos, new XMLAttrib(p_Version, o.softwareInfos[i]._cs_version()));
00325       }
00326    }
00327 
00328     return new XMLData(p_TaskInfo, 
00329         new XMLData(p_OS, o._cs_os(), 
00330         new XMLData(p_CPU, o._cs_cpu(), 
00331         new XMLData(p_Memory, o.mem,
00332         new XMLData(p_Disk, o.disk,
00333         new XMLData(p_TimeOut, o.timeout,
00334         new XMLData(p_URL, o._cs_url(), softInfos)), 
00335       new XMLAttrib(p_Unit, convert_unit(o.disk_unit))),
00336       new XMLAttrib(p_Unit, convert_unit(o.mem_unit))),
00337       new XMLAttrib(p_Speed, o.cpu_speed)))); 
00338 }
00339 
00340 int x2o(XMLData *x, SlaveInfo &o)
00341 {
00342     x->reset();
00343     o._cs_version(x->sub(p_Version)->getString());
00344     XMLData *p = x->sub(p_OS);
00345     if (p == XMLData::Nil) return 0;
00346     o._cs_os(p->getString());
00347     p = x->sub(p_CPU);
00348     if (p == XMLData::Nil) return 0;
00349     o._cs_cpu(p->getString());
00350     CharStr *cs = p->getAttrib(p_Speed);
00351     if (cs == CharStr::Error) o.cpu_speed = 0;
00352     else o.cpu_speed = atoi(cs->str);
00353     p = x->sub(p_Memory);
00354     if (p == XMLData::Nil) return 0;
00355     o.mem = p->getLong();
00356     o.mem_unit = convert_unit(p->getAttrib(p_Unit));
00357     p = x->sub(p_Disk);
00358     if (p == XMLData::Nil) return 0;
00359     o.disk = p->getLong();
00360     o.disk_unit = convert_unit(p->getAttrib(p_Unit));
00361     o.addr.setIP(x->sub(p_Address)->getString()->str); 
00362     // also convert software
00363     int softCount = 0;
00364     while (XMLData::Nil != x->sub(p_Software)) softCount++;
00365     x->reset();
00366     if (softCount > 0)
00367     {
00368       o.nSoftInfos = softCount;
00369       SoftwareInfo *si = new SoftwareInfo[softCount];
00370       while (softCount-- > 0)
00371       {
00372         p = x->sub(p_Software);
00373         si[softCount]._cs_version(p->getAttrib(p_Version));
00374         si[softCount]._cs_name(p->getString());
00375       }
00376       o.softwareInfos = si;
00377     }
00378     return 1;
00379 }
00380 
00381 
00382 XMLData *o2x(const SlaveInfo &o)
00383 {
00384    int i;
00385    XMLData *softInfos;
00386    if (o.nSoftInfos)
00387    {
00388       softInfos = XMLData::Nil;
00389       for (i = 0; i < o.nSoftInfos; i++)
00390       {
00391         if (o.softwareInfos[i]._cs_name() != CharStr::Error)
00392           softInfos = new XMLData(p_Software, o.softwareInfos[i]._cs_name(), softInfos, new XMLAttrib(p_Version, o.softwareInfos[i]._cs_version()));
00393       }
00394    }
00395    else softInfos = XMLData::Nil;
00396 
00397     XMLData *slaveInfo = new XMLData(p_SlaveInfo, XMLData::Nil);
00398     if (o._cs_version() != CharStr::Error) 
00399         slaveInfo->add(new XMLData(p_Version, o._cs_version()));
00400     slaveInfo->add(new XMLData(p_OS, o._cs_os()));
00401     if (o.cpu_speed == 0)
00402         slaveInfo->add(new XMLData(p_CPU, o._cs_cpu()));
00403     else
00404         slaveInfo->add(new XMLData(p_CPU, o._cs_cpu(), XMLData::Nil, new XMLAttrib(p_Speed, o.cpu_speed)));
00405     slaveInfo->add(new XMLData(p_Memory, o.mem, XMLData::Nil, new XMLAttrib(p_Unit, convert_unit(o.mem_unit))));
00406     slaveInfo->add(new XMLData(p_Disk, o.disk, XMLData::Nil, new XMLAttrib(p_Unit, convert_unit(o.disk_unit))));
00407     if (softInfos != XMLData::Nil)
00408       slaveInfo->add(softInfos);
00409     char ip[26];
00410     o.addr.getIP(ip);
00411     slaveInfo->add(new XMLData(p_Address, ip));
00412 
00413     return slaveInfo;
00414 }
00415 
00416 int x2o(XMLData *x, UserInfo &o)
00417 {
00418     x->reset();
00419     XMLData *p = x->sub(p_User);
00420     if (p == XMLData::Nil) return 0;
00421     o._cs_user(p->getString());
00422     p = x->sub(p_Pswd);
00423     if (p == XMLData::Nil) return 0;
00424     o._cs_passwd(p->getString());
00425     return 1;
00426 }
00427 
00428 XMLData *o2x(const UserInfo &o)
00429 {
00430     return new XMLData(p_UserInfo, 
00431         new XMLData(p_User, o._cs_user(),
00432         new XMLData(p_Pswd, o._cs_passwd())));
00433 }
00434 
00435 int x2o(XMLData *x, TaskFinish &o)
00436 {
00437     x->reset();
00438     if (!x2o(x->sub(p_TaskID), o.taskId)) return 0;
00439     XMLData *d = x->sub(XMLData::CDATA);
00440     if (d != XMLData::Nil)
00441       o._cs_data (d->getString());
00442     return 1;
00443 }
00444 
00445 XMLData *o2x(const TaskFinish &o)
00446 {
00447     XMLData *x = xmlmsg(p_M_TASK_FINISH, o2x(o.taskId));
00448     if (o.data()) x->add(new XMLData(XMLData::CDATA, o._cs_data()));
00449     return x;
00450 }
00451 
00452 int x2o(XMLData *x, SlaveAvail &o)
00453 {
00454     x->reset();
00455     XMLData *p = x->sub(p_SlavesRequired);
00456     if (p == XMLData::Nil) return 0;
00457     o.slavesRequired = p->getLong();
00458     if (!x2o(x->sub(p_ReserveID), o.reserveID)) return 0;
00459     o.slaveInfos = new pSlaveInfo[o.slavesRequired];
00460     for (int i = 0; i < o.slavesRequired; i++)
00461     {
00462         o.slaveInfos[i] = new SlaveInfo();
00463         if (!x2o(x->sub(p_SlaveInfo), *o.slaveInfos[i])) return 0;
00464     }
00465     return 1;
00466 }
00467 
00468 
00469 XMLData *o2x(const slave_state &o)
00470 {
00471     CharStr *s;
00472     
00473     switch (o) {
00474         case slave_ready: s = p_Ready; break;
00475         case slave_busy: s = p_Busy; break;
00476         case slave_disabled: s = p_Disabled; break;
00477         case slave_off: s = p_Off; break;
00478         default: s = CharStr::Error; break;
00479     }
00480 
00481     return new XMLData(p_Status, s);    
00482 }
00483 
00484 int x2o(CharStr *x, slave_state &o)
00485 {
00486     if (strcmp(x->str, p_Ready->str) == 0) o = slave_ready;
00487     else if (strcmp(x->str, p_Busy->str) == 0) o = slave_busy;
00488     else if (strcmp(x->str, p_Disabled->str) == 0) o = slave_disabled;
00489     else if (strcmp(x->str, p_Off->str) == 0) o = slave_off;
00490     else { o = slave_internal_error; return 0; }
00491     return 1;
00492 }
00493 
00494 int x2o(XMLData *x, slave_state &o)
00495 {
00496     const char *s = x->getString()->str;
00497     if (strcmp(s, p_Ready->str) == 0) o = slave_ready;
00498     else if (strcmp(s, p_Busy->str) == 0) o = slave_busy;
00499     else if (strcmp(s, p_Disabled->str) == 0) o = slave_disabled;
00500     else if (strcmp(s, p_Off->str) == 0) o = slave_off;
00501     else { o = slave_internal_error; return 0; }
00502     return 1;
00503 }
00504 
00505 CharStr *o2x(const task_ctrl_action o)
00506 {
00507     if (o == stop_task) return p_Stop;
00508     else if (o == control_task) return p_Control;
00509     else return CharStr::Error;
00510 }
00511 
00512 int x2o(CharStr *x, task_ctrl_action &o)
00513 {
00514     if (strcmp(x->str, p_Stop->str) == 0) o = stop_task;
00515     else if (strcmp(x->str, p_Control->str) == 0) o = control_task;
00516     else { o = task_ctrl_internal_error; return 0; }
00517     return 1;
00518 }
00519 
00520 CharStr *o2x(const job_ctrl_action o)
00521 {
00522     if (o == stop_job) return p_Stop;
00523     else if (o == get_job_status) return p_GetStatus;
00524     else if (o == stop_all_jobs_name) return p_StopAllName;
00525     else if (o == stop_all_jobs_user) return p_StopAllUser;
00526     else return CharStr::Error;
00527 }
00528 
00529 int x2o(CharStr *x, job_ctrl_action &o)
00530 {
00531     if (strcmp(x->str, p_Stop->str) == 0) o = stop_job;
00532     else if (strcmp(x->str, p_GetStatus->str) == 0) o = get_job_status;
00533     else if (strcmp(x->str, p_StopAllUser->str) == 0) o = stop_all_jobs_user;
00534     else if (strcmp(x->str, p_StopAllName->str) == 0) o = stop_all_jobs_name;
00535     else { o = job_ctrl_internal_error; return 0; }
00536     return 1;
00537 }
00538 
00539 int x2o(CharStr *x, slave_ctrl_action &o)
00540 {
00541     if (strcmp(x->str, p_Upgrade->str) == 0) o = upgrade_slave;
00542     else if (strcmp(x->str, p_Disable->str) == 0) o = disable_slave;
00543     else if (strcmp(x->str, p_Enable->str) == 0) o = enable_slave;
00544     else if (strcmp(x->str, p_Shutdown->str) == 0) o = shutdown_slave;
00545     else { o = slave_ctrl_internal_error; return 0; }
00546     return 1;
00547 }
00548 
00549 int x2o(CharStr *x, client_state &o)
00550 {
00551     if (strcmp(x->str, p_On->str) == 0) o = client_on;
00552     else if (strcmp(x->str, p_Off->str) == 0) o = client_off;
00553     else { o = client_internal_error; return 0; }
00554     return 1;
00555 }
00556 
00557 XMLData *xmlmsg(CharStr *m_Type, XMLData *contents)
00558 {
00559     return new XMLData(p_Message, contents, XMLData::Nil, new XMLAttrib(p_Type, m_Type));
00560 }
00561 
00562 int msgType(XMLData *msg, CharStr *m_Type)
00563 {
00564     if (!strcmp(msg->getAttrib(p_Type->str)->str, m_Type->str)) return 1;
00565     else return 0;
00566 }
00567 
00568 CharStr *msgType(XMLData *msg)
00569 {
00570   return msg->getAttrib(p_Type->str);
00571 }
00572 
00573 
00574 XMLData *xTInfo2xSInfo(XMLData *t)
00575 {
00576     XMLData *s = new XMLData(t, 0);
00577     s->setTag(p_SlaveInfo);
00578     s->remove(p_TimeOut->str);
00579     s->remove(p_URL->str);
00580     s->remove(p_UserData->str);
00581     return s;
00582 }
00583 
00584 XMLData *xTInfo2xSInfoAll(XMLData *t)
00585 {
00586     XMLData *s0, *s2, *s = XMLData::Nil;
00587     
00588     while (t != XMLData::Nil)
00589     {
00590        s2 = xTInfo2xSInfo(t);
00591        if (s == XMLData::Nil) s0 = s2;
00592        else s->chainWith(s2);
00593        s = s2;
00594        t = t->getNextElem();
00595     }
00596     return s0;
00597 }

Generated on Mon Nov 25 12:46:33 2002 for qadpz by doxygen1.2.18