Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef KOGMO_RTDB_CONN_HXX
00010 #define KOGMO_RTDB_CONN_HXX
00011
00012 #include <string.h>
00013 #include <iostream>
00014 #include <string>
00015 #include <stdexcept>
00016 #include <stdlib.h>
00017
00018 namespace KogniMobil
00019 {
00020
00021
00022
00023
00024
00025
00026 class DBError : public std::runtime_error
00027 {
00028 public:
00029 DBError (const std::string& what = "unknown error") : std::runtime_error (what) {}
00030 DBError (int err) : std::runtime_error (std::string(strerror(-err))) {}
00031 };
00032
00033
00034
00035 #ifndef HAVE_DEFINED_setstatus
00036 extern "C" {
00037 int
00038 kogmo_rtdb_setstatus (kogmo_rtdb_handle_t *db_h, uint32_t status, _const char* msg, uint32_t flags);
00039 }
00040 #endif
00041
00042 #define HAVE_DBC_SetStatus
00043
00044
00045
00046
00047
00048
00049
00050 class RTDBConn
00051 {
00052 friend class RTDBObj;
00053 public:
00054 kogmo_rtdb_handle_t *dbc;
00055 protected:
00056 bool connected;
00057 Timestamp cycle_ts;
00058 float cycle_period;
00059 kogmo_rtdb_objid_t proc_id;
00060 private:
00061 void operator= ( const RTDBConn& src);
00062 public:
00063
00064 RTDBConn (const char* procname, const float& cycletime = 0.033,
00065 const char *dbhost = "", const unsigned int& flags = 0)
00066 {
00067 kogmo_rtdb_connect_info_t dbinfo;
00068 int err = kogmo_rtdb_connect_initinfo (&dbinfo, dbhost, procname, cycletime);
00069 if ( err < 0 )
00070 throw DBError(err);
00071 dbinfo.flags = flags;
00072 dbinfo.version = getenv("KOGMO_REV") ? atoi(getenv("KOGMO_REV")) : 0;
00073 proc_id = kogmo_rtdb_connect (&dbc, &dbinfo);
00074 if ( proc_id < 0 )
00075 throw DBError(proc_id);
00076 connected = true;
00077 cycle_period = cycletime;
00078 cycle_ts.now();
00079 };
00080 RTDBConn (kogmo_rtdb_handle_t *db_h)
00081 {
00082 dbc = db_h;
00083 connected = false;
00084 };
00085 RTDBConn ( const RTDBConn& src)
00086 {
00087 dbc = src.dbc;
00088 connected = false;
00089
00090 };
00091
00092 ~RTDBConn ()
00093 {
00094 if (!connected)
00095 return;
00096 int err = kogmo_rtdb_disconnect (dbc, NULL);
00097 if ( err < 0 )
00098 throw DBError(err);
00099 }
00100 kogmo_rtdb_objid_t getProcID () const
00101 {
00102 return proc_id;
00103 }
00104 kogmo_rtdb_objid_t getOID () const
00105 {
00106 kogmo_rtdb_objid_t proclistoid,procoid;
00107 proclistoid = kogmo_rtdb_obj_searchinfo (dbc, "processes", KOGMO_RTDB_OBJTYPE_C3_PROCESSLIST, 0, 0, 0, NULL, 1);
00108 if ( proclistoid < 0 )
00109 throw DBError(proclistoid);
00110 procoid = kogmo_rtdb_obj_searchinfo (dbc, "", KOGMO_RTDB_OBJTYPE_C3_PROCESS, proclistoid, proc_id, 0, NULL, 1);
00111 if ( procoid < 0 )
00112 throw DBError(procoid);
00113 return procoid;
00114 }
00115 void CycleDone (const unsigned int& flags = 0)
00116 {
00117 SetStatus(KOGMO_RTDB_PROCSTATUS_CYCLEDONE, "finished work for this cycle", flags);
00118 }
00119 void SetStatus_Ok(const char *msg="")
00120 {
00121 SetStatus(KOGMO_RTDB_PROCSTATUS_CYCLEDONE, msg);
00122 }
00123 void SetStatus_Waiting(const char *msg="")
00124 {
00125 SetStatus(KOGMO_RTDB_PROCSTATUS_WAITING, msg);
00126 }
00127 void SetStatus_Busy(const char *msg="")
00128 {
00129 SetStatus(KOGMO_RTDB_PROCSTATUS_BUSY, msg);
00130 }
00131 void SetStatus_Failure(const char *msg="")
00132 {
00133 SetStatus(KOGMO_RTDB_PROCSTATUS_FAILURE, msg);
00134 }
00135 void SetStatus_Warning(const char *msg="")
00136 {
00137 SetStatus(KOGMO_RTDB_PROCSTATUS_WARNING, msg);
00138 }
00139 private:
00140 void SetStatus (const unsigned int& status, const char *msg, const unsigned int& flags = 0)
00141 {
00142 int err = kogmo_rtdb_setstatus(dbc, status, msg, flags);
00143 if ( err < 0 )
00144 throw DBError(err);
00145 }
00146 public:
00147 void SleepUntil (Timestamp ts)
00148 {
00149 int err = kogmo_rtdb_sleep_until(dbc, ts);
00150 if ( err < 0 )
00151 throw DBError(err);
00152 }
00153 void Sleep (const double& secs)
00154 {
00155 Timestamp ts;
00156 ts.now();
00157 ts+=secs;
00158 int err = kogmo_rtdb_sleep_until(dbc, ts);
00159 if ( err < 0 )
00160 throw DBError(err);
00161 }
00162 void WaitNextCycle ()
00163 {
00164 SetStatus_Waiting("waiting for next cycle");
00165 cycle_ts+=cycle_period;
00166 SleepUntil (cycle_ts);
00167 cycle_ts.now();
00168 SetStatus_Busy("at work");
00169 }
00170 Timestamp getTimestamp ()
00171 {
00172 return Timestamp(kogmo_rtdb_timestamp_now(dbc));
00173 }
00174 void setTimestamp (Timestamp currTime)
00175 {
00176 kogmo_rtdb_timestamp_set(dbc, currTime.timestamp());
00177 }
00178 Timestamp getCycleTimestamp () const
00179 {
00180 return cycle_ts;
00181 }
00182 kogmo_rtdb_handle_t* getHandle (void) const
00183 {
00184 return dbc;
00185 };
00186 };
00187
00188 const int RTDBConnFlag_NoWait = ( KOGMO_RTDB_CONNECT_FLAGS_TRYNOWAIT | KOGMO_RTDB_CONNECT_FLAGS_LIVEONERR );
00189
00190
00191
00192
00193 };
00194
00195 #endif