HttpOdb Class Reference

Access to ODB through the MIDAS HTTP server mhttpd. More...

#include <HttpOdb.h>

Inheritance diagram for HttpOdb:
Inheritance graph
[legend]
Collaboration diagram for HttpOdb:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 HttpOdb (const char *url)
 Contructor from a base URL.
virtual ~HttpOdb ()
 Destructor.
int odbReadAny (const char *name, int index, int tid, void *buf, int bufsize=0)
 Read value of arbitrary type.
int odbReadInt (const char *name, int index, int defaultValue)
 Read an integer value, midas type TID_INT.
uint32_t odbReadUint32 (const char *name, int index, uint32_t defaultValue)
 Read an unsigned 32-bit integer value, midas type TID_DWORD.
bool odbReadBool (const char *name, int index, bool defaultValue)
 Read a boolean value, midas type TID_BOOL.
double odbReadDouble (const char *name, int index, double defaultValue)
 Read an 64-bit floating point value, midas type TID_DOUBLET.
float odbReadFloat (const char *name, int index, float defaultValue)
 Read an 32-bit floating point value, midas type TID_FLOAT.
const char * odbReadString (const char *name, int index, const char *defaultValue)
 Read a string value, midas type TID_STRING.
int odbReadArraySize (const char *name)
 Read size of an array.

Protected Member Functions

const char * jkey (const char *path)
const char * jget (const char *path, int index)

Protected Attributes

const char * fUrl
int fDebug
char fRequestBuf [1024]
char fReplyBuf [1024]

Detailed Description

Access to ODB through the MIDAS HTTP server mhttpd.

To enable ODB access in mhttpd, create an ODB string entry "/Custom/secret.html!" with blank value.

Then create an HttpOdb class with URL "http://hostname:port/CS/secret.html" and use the odb access functions as required.

Note that HTTP access is not optimized for speed. It can be slow, can result in high CPU utilization by mhttpd and is subject to lengthy delays if mhttpd is busy with something else (i.e. starting a run or generating history plots).

In this usage, the string "secret.html" functions as an access password.

Definition at line 27 of file HttpOdb.h.


Constructor & Destructor Documentation

HttpOdb::HttpOdb ( const char *  url  ) 

Contructor from a base URL.

Definition at line 17 of file HttpOdb.cxx.

References fDebug, and fUrl.

00018 {
00019   fDebug = 0;
00020   fUrl = strdup(url);
00021 }

HttpOdb::~HttpOdb (  )  [virtual]

Destructor.

Definition at line 23 of file HttpOdb.cxx.

References fUrl.

00024 {
00025   if (fUrl)
00026     free((void*)fUrl);
00027   fUrl = NULL;
00028 }


Member Function Documentation

const char * HttpOdb::jget ( const char *  path,
int  index 
) [protected]

Definition at line 35 of file HttpOdb.cxx.

References fDebug, fReplyBuf, fRequestBuf, and fUrl.

Referenced by odbReadBool(), odbReadDouble(), odbReadFloat(), odbReadInt(), odbReadString(), and odbReadUint32().

00036 {
00037   // index "-1" is special for "return whole array"
00038   if (index < 0)
00039     return NULL;
00040 
00041   TUrl xurl(fUrl);
00042 
00043   TSocket s(xurl.GetHost(), xurl.GetPort());
00044 
00045   //char *req = "GET /CS/F000.html?cmd=jset&odb=/equipment/mscb/settings/FGD/Feb_demand/power[0]&value=y HTTP/1.1\r\n\r\n";
00046   //char *req = "GET /CS/F000.html?cmd=jget&odb=/runinfo/run number HTTP/1.1\r\n\r\n";
00047 
00048   sprintf(fRequestBuf,"GET /%s?cmd=jget&odb=%s[%d] HTTP/1.1\r\n\r\n", xurl.GetFileAndOptions(), path, index);
00049 
00050   if (fDebug)
00051     printf("Sending [%s]\n", fRequestBuf);
00052 
00053   s.SendRaw(fRequestBuf, strlen(fRequestBuf));
00054   int rd = s.RecvRaw(fReplyBuf, sizeof(fReplyBuf));
00055 
00056   if (fDebug)
00057     printf("Received %d [%s]\n", rd, fReplyBuf);
00058 
00059   if (rd < 10)
00060     return NULL;
00061 
00062   if (strstr(fReplyBuf, "200 Document follows") == NULL)
00063     return NULL;
00064 
00065   fReplyBuf[rd] = 0;
00066 
00067   const char* p = strstr(fReplyBuf,"\r\n\r\n");
00068   if (!p)
00069     return NULL;
00070 
00071   p += 4;
00072 
00073   if (strcmp(p, "<DB_NO_KEY>") == 0)
00074     return NULL;
00075 
00076   if (strcmp(p, "<DB_OUT_OF_RANGE>") == 0)
00077     return NULL;
00078 
00079   if (strcmp(p, "<unknown>") == 0)
00080     return NULL;
00081 
00082   if (strstr(p, "<html>") != NULL)
00083     {
00084       fprintf(stderr, "HttpOdb::jget: Bad mhttpd response: %s\n", p);
00085       return NULL;
00086     }
00087 
00088   if (fDebug)
00089     printf("----> mhttpd %s[%d] return [%s]\n", path, index, p);
00090   
00091   return p;
00092 }

Here is the caller graph for this function:

const char * HttpOdb::jkey ( const char *  path  )  [protected]

Definition at line 94 of file HttpOdb.cxx.

References fDebug, fReplyBuf, fRequestBuf, and fUrl.

Referenced by odbReadArraySize().

00095 {
00096   TUrl xurl(fUrl);
00097 
00098   TSocket s(xurl.GetHost(), xurl.GetPort());
00099 
00100   //char *req = "GET /CS/F000.html?cmd=jset&odb=/equipment/mscb/settings/FGD/Feb_demand/power[0]&value=y HTTP/1.1\r\n\r\n";
00101   //char *req = "GET /CS/F000.html?cmd=jget&odb=/runinfo/run number HTTP/1.1\r\n\r\n";
00102 
00103   sprintf(fRequestBuf,"GET /%s?cmd=jkey&odb=%s HTTP/1.1\r\n\r\n", xurl.GetFileAndOptions(), path);
00104 
00105   if (fDebug)
00106     printf("Sending [%s]\n", fRequestBuf);
00107 
00108   s.SendRaw(fRequestBuf, strlen(fRequestBuf));
00109   int rd = s.RecvRaw(fReplyBuf, sizeof(fReplyBuf));
00110 
00111   if (fDebug)
00112     printf("Received %d [%s]\n", rd, fReplyBuf);
00113 
00114   if (rd < 10)
00115     return NULL;
00116 
00117   if (strstr(fReplyBuf, "200 Document follows") == NULL)
00118     return NULL;
00119 
00120   fReplyBuf[rd] = 0;
00121 
00122   const char* p = strstr(fReplyBuf,"\r\n\r\n");
00123   if (!p)
00124     return NULL;
00125 
00126   p += 4;
00127 
00128   if (strcmp(p, "<DB_NO_KEY>") == 0)
00129     return NULL;
00130 
00131   if (strcmp(p, "<DB_OUT_OF_RANGE>") == 0)
00132     return NULL;
00133 
00134   if (strcmp(p, "<unknown>") == 0)
00135     return NULL;
00136 
00137   if (strstr(p, "<html>") != NULL)
00138     {
00139       fprintf(stderr, "HttpOdb::jget: Bad mhttpd response: %s\n", p);
00140       return NULL;
00141     }
00142 
00143   if (fDebug)
00144     printf("----> mhttpd %s return [%s]\n", path, p);
00145   
00146   return p;
00147 }

Here is the caller graph for this function:

int HttpOdb::odbReadAny ( const char *  name,
int  index,
int  tid,
void *  buf,
int  bufsize = 0 
) [virtual]

Read value of arbitrary type.

Implements VirtualOdb.

Definition at line 149 of file HttpOdb.cxx.

00149 { assert(!"Not implemented!"); }

int HttpOdb::odbReadArraySize ( const char *  name  )  [virtual]

Read size of an array.

Implements VirtualOdb.

Definition at line 201 of file HttpOdb.cxx.

References jkey().

00202 {
00203   const char* reply = jkey(name);
00204   if (!reply)
00205     return 0;
00206   const char* p = strstr(reply, "\n");
00207   if (p)
00208     p = strstr(p+1, "\n");
00209   if (p)
00210     return atoi(p);
00211   return 1;
00212 }

Here is the call graph for this function:

bool HttpOdb::odbReadBool ( const char *  name,
int  index,
bool  defaultValue 
) [virtual]

Read a boolean value, midas type TID_BOOL.

Implements VirtualOdb.

Definition at line 183 of file HttpOdb.cxx.

References jget().

00184 {
00185   const char* reply = jget(name, index);
00186   if (!reply)
00187     return defaultValue;
00188   if (*reply == 'n')
00189     return false;
00190   return true;
00191 }

Here is the call graph for this function:

double HttpOdb::odbReadDouble ( const char *  name,
int  index,
double  defaultValue 
) [virtual]

Read an 64-bit floating point value, midas type TID_DOUBLET.

Implements VirtualOdb.

Definition at line 167 of file HttpOdb.cxx.

References jget().

00168 {
00169   const char* reply = jget(name, index);
00170   if (!reply)
00171     return defaultValue;
00172   return atof(reply);
00173 }

Here is the call graph for this function:

float HttpOdb::odbReadFloat ( const char *  name,
int  index,
float  defaultValue 
) [virtual]

Read an 32-bit floating point value, midas type TID_FLOAT.

Implements VirtualOdb.

Definition at line 159 of file HttpOdb.cxx.

References jget().

00160 {
00161   const char* reply = jget(name, index);
00162   if (!reply)
00163     return defaultValue;
00164   return atof(reply);
00165 }

Here is the call graph for this function:

int HttpOdb::odbReadInt ( const char *  name,
int  index,
int  defaultValue 
) [virtual]

Read an integer value, midas type TID_INT.

Implements VirtualOdb.

Definition at line 175 of file HttpOdb.cxx.

References jget().

00176 {
00177   const char* reply = jget(name, index);
00178   if (!reply)
00179     return defaultValue;
00180   return atoi(reply);
00181 }

Here is the call graph for this function:

const char * HttpOdb::odbReadString ( const char *  name,
int  index,
const char *  defaultValue 
) [virtual]

Read a string value, midas type TID_STRING.

Implements VirtualOdb.

Definition at line 193 of file HttpOdb.cxx.

References jget().

00194 {
00195   const char* reply = jget(name, index);
00196   if (!reply)
00197     return defaultValue;
00198   return reply;
00199 }

Here is the call graph for this function:

uint32_t HttpOdb::odbReadUint32 ( const char *  name,
int  index,
uint32_t  defaultValue 
) [virtual]

Read an unsigned 32-bit integer value, midas type TID_DWORD.

Implements VirtualOdb.

Definition at line 151 of file HttpOdb.cxx.

References jget().

00152 {
00153   const char* reply = jget(name, index);
00154   if (!reply)
00155     return defaultValue;
00156   return strtoul(reply, NULL, 0);
00157 }

Here is the call graph for this function:


Member Data Documentation

int HttpOdb::fDebug [protected]

Definition at line 31 of file HttpOdb.h.

Referenced by HttpOdb(), jget(), and jkey().

char HttpOdb::fReplyBuf[1024] [protected]

Definition at line 55 of file HttpOdb.h.

Referenced by jget(), and jkey().

char HttpOdb::fRequestBuf[1024] [protected]

Definition at line 54 of file HttpOdb.h.

Referenced by jget(), and jkey().

const char* HttpOdb::fUrl [protected]

Definition at line 30 of file HttpOdb.h.

Referenced by HttpOdb(), jget(), jkey(), and ~HttpOdb().


The documentation for this class was generated from the following files:

Generated on 12 Feb 2016 for ROOT Analyzer by  doxygen 1.6.1