00001 /********************************************************************
00002 Description:
00003 part of the 3Dsia project
00004 created: xandi, 020400
00005
00006 History:
00007 date, name, changes, in funtion
00008 180400 StonedBones added dynamic packet length
00009 060400 xandi adapted it to work for the matrix all
00010
00011 ********************************************************************/
00012
00013
00014 /*...................................................................
00015 Description:
00016 Args:
00017 Returns:
00018 Created: xandi, 020400
00019 [ToDo:]
00020 Comments:
00021 The main problem is that this access function is just able
00022 to work with _one_ in and _one_ out buffer; when looking at
00023 coreThread we will see the problems with such a way of doing, when
00024 every client has its own two buffers plus the matrix buffers.
00025 can be solved by maps, but isn't beautiful... xandi, 040400
00026
00027 obove statement is obsolete.. xandi, 060400
00028 Changes:
00029 -------------------------------------------------------------------*/
00030
00031 /*
00032 GetAllObjects ( AtomType, AtomVal );
00033 GetAtom ( Object, AtomType );
00034 AddObject ( ObjectName );
00035 AddAtom ( Object, AtomType, AtomVal );
00036 */
00037
00038 #include <string.h>
00039 #include <pthread.h>
00040 #include <unistd.h>
00041
00042 #include <stdio.h>
00043
00044 #include "protocol.h"
00045 #include "buffer.h"
00046 #include "generic.h"
00047
00048 #include "databaseAccess.h"
00049
00050 #undef DEBUG2
00051
00052 extern Buffer* matrixBufIn;
00053
00054 DatabaseAccess::DatabaseAccess ( int ownerSock )
00055 {
00056 dataBaseIn = matrixBufIn;
00057 ownerSocket = ownerSock;
00058 myTimeStamp.tv_sec = 0;
00059 myTimeStamp.tv_usec = 0;
00060 }
00061
00062 /*...................................................................
00063 Description: GetAllObject Methods.. Used to get either all objects, all object
00064 that have a specific atom and/or all object that have an atom with a special value, or range of value
00065 The atoms can have various types (int,string,binary..)
00066 Created: xandi, 020400
00067 -------------------------------------------------------------------*/
00068
00069 /*...................................................................
00070 Description: getAllObjects
00071 Args:
00072 Returns: sends all those objects to the clientthread that requested it..
00073 Created: xandi, 040400
00074 [ToDo:]
00075 [Comments:]
00076 Changes:
00077 -------------------------------------------------------------------*/
00078
00079 void DatabaseAccess::getAllObjects ( void )
00080 {
00081 packet* pak;
00082 pak = new packet;
00083
00084
00085 pak->h.from = ownerSocket;
00086
00087 pak->h.TimeStamp.tv_sec = myTimeStamp.tv_sec;
00088 pak->h.TimeStamp.tv_usec = myTimeStamp.tv_usec;
00089
00090 pak->h.header[0] = ODB_GET_ALL_CHANGED;
00091
00092 #ifdef DEBUG
00093 fprintf ( stderr,"[dbAccess(%d).getAllObjects]\n",ownerSocket);
00094 #endif
00095
00096 dataBaseIn->write ( pak );
00097
00098 gettimeofday ( &myTimeStamp, NULL );
00099 }
00100
00101
00102
00103 void DatabaseAccess::getAtom ( int AtomID, int ObjectID, int AtomType )
00104 {
00105 packet* pak;
00106 pak = new packet;
00107
00108 pak->h.from = ownerSocket;
00109
00110 pak->h.header[0] = ODB_GET_ATOM;
00111 pak->h.header[1] = AtomID;
00112 pak->h.header[2] = ObjectID;
00113 pak->h.header[3] = AtomType;
00114
00115 #ifdef DEBUG2
00116 fprintf ( stderr,"[dbAccess(%d).getAtom] %d %d %d\n",ownerSocket,AtomID,ObjectID,AtomType);
00117 #endif
00118 dataBaseIn->write ( pak );
00119 }
00120
00121
00122 void DatabaseAccess::getObject ( int ObjectNr )
00123 {
00124 packet* pak;
00125 pak = new packet;
00126
00127 pak->h.from = ownerSocket;
00128
00129 pak->h.header[0] = ODB_GET_OBJECT;
00130 pak->h.header[1] = ObjectNr;
00131
00132 #ifdef DEBUG2
00133 fprintf(stderr,"[dbAccess(%d).getObject] ID --> %d <--\n",ownerSocket,ObjectNr );
00134 #endif
00135 dataBaseIn->write ( pak );
00136 }
00137
00138
00139
00140 /*...................................................................
00141 Description: AddObject
00142 Args:
00143 Returns: returns the ObjectID of the added Object
00144 Created: xandi, 040400
00145 [ToDo:]
00146 [Comments:]
00147 Changes:
00148 -------------------------------------------------------------------*/
00149
00150 void DatabaseAccess::addObject ( string objectName )
00151 {
00152 packet* pak;
00153 pak = new packet;
00154
00155 pak->h.from = ownerSocket;
00156
00157 pak->h.header[0] = ODB_ADD_OBJECT;
00158 pak->h.header[1] = CLT_OBJECT;
00159 pak->h.header[2] = -1;
00160
00161 pak->data = objectName;
00162
00163 #ifdef DEBUG
00164 fprintf ( stderr,"[coT(%d).addObject (string)] try to add an object %s to Matrix\n",ownerSocket,objectName.c_str() );
00165 #endif
00166 dataBaseIn->write ( pak );
00167 }
00168
00169 void DatabaseAccess::addObject ( int objectID, string objectName )
00170 {
00171 packet* pak;
00172 pak = new packet;
00173
00174 pak->h.from = ownerSocket;
00175
00176 pak->h.header[0] = ODB_ADD_OBJECT;
00177 pak->h.header[1] = CLT_OBJECT;
00178 pak->h.header[2] = objectID;
00179
00180 pak->data = objectName;
00181
00182 #ifdef DEBUG
00183 fprintf ( stderr,"[coT(%d).addObject (id,string)] try to add an object %s to Matrix\n",ownerSocket,objectName.c_str() );
00184 #endif
00185 dataBaseIn->write ( pak );
00186 }
00187
00188
00189 void DatabaseAccess::addEntity ( string entityName )
00190 {
00191 packet* pak;
00192 pak = new packet;
00193
00194 pak->h.from = ownerSocket;
00195
00196 pak->h.header[0] = ODB_ADD_OBJECT;
00197 pak->h.header[1] = CLT_ENTITY;
00198 pak->h.header[2] = -1;
00199
00200 pak->data = entityName;
00201
00202 #ifdef DEBUG
00203 fprintf ( stderr,"[coT(%d).addEntity (string)] try to add an entity %s to Matrix\n",ownerSocket,entityName.c_str() );
00204 #endif
00205 dataBaseIn->write ( pak );
00206 }
00207
00208 /*...................................................................
00209 Description: AddAtom Methods.. Used to add an atom to an object using
00210 various types (int,string,binary..)
00211 Created: xandi, 020400
00212 -------------------------------------------------------------------*/
00213
00214 /*...................................................................
00215 Description: AddAtom
00216 Args:
00217 int objectID: The Object ID of the object the atoms are to be added to
00218 int AtomType: Type of pre-defined atom
00219 string str: the content of the added atom
00220 Returns:
00221 Created: xandi, 020400
00222 [ToDo:]
00223 [Comments:]
00224 Changes:
00225 -------------------------------------------------------------------*/
00226
00227 void DatabaseAccess::addAtom ( int objectID, int atomType, string str )
00228 {
00229 packet* pak;
00230 pak = new packet;
00231
00232 pak->h.from = ownerSocket;
00233
00234 pak->h.header[0] = ODB_ADD_ATOM;
00235 pak->h.header[2] = objectID;
00236 pak->h.header[3] = atomType;
00237 pak->h.header[4] = -1;
00238 pak->data = str;
00239
00240 #ifdef DEBUG
00241 fprintf(stderr,"[coT(%d).addAtom (int,int,string)] - %d,%d,%s\n",ownerSocket,objectID,atomType,str.c_str());
00242 #endif
00243
00244 dataBaseIn->write ( pak );
00245 }
00246
00247
00248 /*...................................................................
00249 Description: AddAtom
00250 Args:
00251 int objectID: The Object ID of the object the atoms are to be added to
00252 int AtomType: Type of pre-defined atom
00253 int number: content of the added atom
00254 Returns:
00255 Created: xandi, 020400
00256 [ToDo:]
00257 [Comments:]
00258 Changes:
00259 -------------------------------------------------------------------*/
00260 void DatabaseAccess::addAtom ( int objectID, int atomType, long number )
00261 {
00262 packet* pak;
00263 pak = new packet;
00264
00265 pak->h.from = ownerSocket;
00266
00267 pak->h.header[0] = ODB_ADD_ATOM;
00268 pak->h.header[2] = objectID;
00269 pak->h.header[3] = atomType;
00270 pak->h.header[4] = -1;
00271
00272 char* temp; //sorry marc, but there is no other way :)
00273 temp = new char[sizeof(long)];
00274 memcpy ( temp, &number, sizeof (long) );
00275 pak->data.assign (temp, sizeof(long) );
00276
00277 #ifdef DEBUG
00278 fprintf(stderr,"[coT(%d).addAtom (int,int,long)] - %d,%d,%d\n",ownerSocket,objectID,atomType,(int)number);
00279 #endif
00280
00281 dataBaseIn->write ( pak );
00282 }
00283
00284
00285 /*...................................................................
00286 Description: AddAtom
00287 Args:
00288 int atomID: the ID of the atom
00289 int objectID: The Object ID of the object the atoms are to be added to
00290 int AtomType: Type of pre-defined atom
00291 string str: the content of the added atom
00292 Returns:
00293 Created: xandi, 020400
00294 [ToDo:]
00295 [Comments:]
00296 Changes:
00297 -------------------------------------------------------------------*/
00298
00299 void DatabaseAccess::addAtom ( int atomID, int objectID, int atomType, string str )
00300 {
00301 packet* pak;
00302 pak = new packet;
00303
00304 pak->h.from = ownerSocket;
00305
00306 pak->h.header[0] = ODB_ADD_ATOM;
00307 pak->h.header[2] = objectID;
00308 pak->h.header[3] = atomType;
00309 pak->h.header[4] = atomID;
00310 pak->data = str;
00311
00312 #ifdef DEBUG
00313 fprintf(stderr,"[coT(%d).addAtom (int,int,int,string)] - %d,%d,%d,%s\n",ownerSocket,atomID,objectID,atomType,str.c_str());
00314 #endif
00315
00316 dataBaseIn->write ( pak );
00317 }
00318
00319
00320 /*...................................................................
00321 Description: AddAtom
00322 Args:
00323 int atomID: the ID of the atom
00324 int objectID: The Object ID of the object the atoms are to be added to
00325 int AtomType: Type of pre-defined atom
00326 int number: content of the added atom
00327 Returns:
00328 Created: xandi, 020400
00329 [ToDo:]
00330 [Comments:]
00331 Changes:
00332 -------------------------------------------------------------------*/
00333 void DatabaseAccess::addAtom ( int atomID, int objectID, int atomType, long number )
00334 {
00335 packet* pak;
00336 pak = new packet;
00337
00338 pak->h.from = ownerSocket;
00339
00340 pak->h.header[0] = ODB_ADD_ATOM;
00341 pak->h.header[2] = objectID;
00342 pak->h.header[3] = atomType;
00343 pak->h.header[4] = atomID;
00344
00345 char* temp; //sorry marc, but there is no other way :)
00346 temp = new char[sizeof(long)];
00347 memcpy ( temp, &number, sizeof (long) );
00348 pak->data.assign (temp, sizeof(long) );
00349
00350 #ifdef DEBUG
00351 fprintf(stderr,"[coT(%d).addAtom (int,int,int,long)] - %d,%d,%d,%d\n",ownerSocket,atomID,objectID,atomType,(int)number);
00352 #endif
00353
00354 dataBaseIn->write ( pak );
00355 }
00356
00357
00358
00359 /*...................................................................
00360 Description: AddReason
00361 Args:
00362 int objectIDa: the ID of the object to which the other object one is bound to
00363 int objectIDb: the ID of the object which is the subject of the reason
00364 int reasonType:
00365 Returns:
00366 Created: xandi, 060500
00367 [ToDo:]
00368 [Comments:]
00369 Changes:
00370 -------------------------------------------------------------------*/
00371 void DatabaseAccess::addReason ( int objectIDa, int objectIDb, int reasonType )
00372 {
00373 packet* pak;
00374 pak = new packet;
00375
00376 pak->h.from = ownerSocket;
00377
00378 pak->h.header[0] = ODB_ADD_REASON;
00379 pak->h.header[1] = objectIDa;
00380 pak->h.header[2] = objectIDb;
00381 pak->h.header[3] = reasonType;
00382
00383 #ifdef DEBUG
00384 fprintf(stderr,"[coT(%d).addReason (int,int,int)] - %d,%d,%d\n",ownerSocket,objectIDa,objectIDb,reasonType);
00385 #endif
00386
00387 dataBaseIn->write ( pak );
00388 }
00389
00390 /*...................................................................
00391 Description: RemoveReason
00392 Args:
00393 int objectIDa: the ID of the object to which the other object one is bound to
00394 int objectIDb: the ID of the object which is the subject of the reason
00395 int reasonType:
00396 Returns:
00397 Created: xandi, 060500
00398 [ToDo:]
00399 [Comments:]
00400 Changes:
00401 -------------------------------------------------------------------*/
00402 void DatabaseAccess::removeReason ( int objectIDa, int objectIDb, int reasonType )
00403 {
00404 packet* pak;
00405 pak = new packet;
00406
00407 pak->h.from = ownerSocket;
00408
00409 pak->h.header[0] = ODB_REMOVE_REASON;
00410 pak->h.header[1] = objectIDa;
00411 pak->h.header[2] = objectIDb;
00412 pak->h.header[3] = reasonType;
00413
00414 #ifdef DEBUG
00415 fprintf(stderr,"[coT(%d).addReason (int,int,int)] - %d,%d,%d\n",ownerSocket,objectIDa,objectIDb,reasonType);
00416 #endif
00417
00418 dataBaseIn->write ( pak );
00419 }
00420