Main Page   Compound List   File List   Compound Members   File Members  

async.c

00001 /* DICElib (DIstributed CAVE Engine library) 
00002  * Copyright (c) 2001 Bruno Barberi Gnecco <brunobg@lsi.usp.br>
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to
00006  * deal in the Software without restriction, including without limitation the
00007  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00008  * sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies of the Software, its documentation and marketing & publicity
00013  * materials, and acknowledgment shall be given in the documentation, materials
00014  * and software packages that this Software was used.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00019  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00020  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  */
00023 
00024 /* NOTE TO DEVELOPERS:
00025    Async variables are currently unused, because no architecture for mixing
00026    the sync and async comm has been defined yet. Possible solutions include
00027    signals (which are partly implemented and under #ifdef USE_ASYNC), or
00028    perhaps threads and two sockets. 
00029    This file, however, has been kept uptodate with changes that may affect it. */
00030 
00031 #include "_dicelib.h"
00032 #include <string.h>
00033 #include "hash.h"
00034 
00035 #if 0
00036 
00037 static HashTable dice_async_var;
00038 
00042 struct dice_async {
00043         char                    *name;          
00044         char                    *type;          
00045         void                    *value;         
00046         void                    *nextvalue;     
00047         int                     locked;         
00048 };
00049 
00054 void dice_async_free ( void *data ) {
00055         struct dice_async       *da;
00056 
00057         if ( !data )
00058                 return;
00059 
00060         da = (struct dice_async *)data;
00061         dice_free(da->name);
00062         dice_type_free(da->type, da->value);
00063         dice_type_free(da->type, da->nextvalue);
00064         dice_free(da->type);
00065         free(da);
00066 }
00067 
00071 int dice_async_init ( void ) {
00072         if ( hash_init(&dice_async_var, 128, NULL) == -1 )
00073                 return -1;
00074         
00075         return 0;
00076 }
00077 
00081 void dice_async_end ( void ) {
00082         hash_free(&dice_async_var, dice_async_free);
00083 }
00084 
00085 #undef FUNCTION
00086 #define FUNCTION                "dice_async_new"
00087 
00099 int dice_async_new ( char *name, char *type, void *value ) {
00100         struct dice_async               *da;
00101         dice_packet                     packet;
00102 
00103         _dice_debug(3, dice_printf(FUNCTION, "(%s,%s,%p)\n",
00104                         name, type, value);)
00105 
00106         if ( !name || !dice_type_is_valid(type) ) {
00107                 _dice_debug(1, dice_printf(FUNCTION, "Invalid type or NULL name.\n");)
00108                 return -1;
00109         }
00110         if ( hash_data(&dice_async_var, da->name) ) {
00111                 _dice_debug(1, dice_printf(FUNCTION, "Variable already exists.\n");)
00112                 return -1;
00113         }
00114 
00115         da = (struct dice_async *)malloc(sizeof(struct dice_async));
00116         if ( !da ) {
00117                 _dice_debug(1, dice_printf(FUNCTION, "NULL malloc.\n");)
00118                 return -1;
00119         }
00120 
00121         da->name = strdup(name);
00122         if ( !da->name ) {
00123                 _dice_debug(1, dice_printf(FUNCTION, "NULL strdup.\n");)
00124         }
00125         da->type = strdup(type);
00126         if ( !da->type ) {
00127                 _dice_debug(1, dice_printf(FUNCTION, "NULL strdup 2.\n");)
00128         }
00129         da->value = value;
00130         da->nextvalue = NULL;
00131         da->locked = 0;
00132 
00133         return (hash_insert(&dice_async_var, da->name, da) == -1 ? -1 : 0);
00134 }
00135 
00136 #undef FUNCTION
00137 #define FUNCTION                "dice_async_delete"
00138 
00145 void dice_async_delete ( char *name ) {
00146         struct dice_async               *da;
00147 
00148         _dice_debug(3, dice_printf(FUNCTION, "(%s)\n", name);)
00149 
00150         if ( !name ) {
00151                 _dice_debug(2, dice_printf(FUNCTION, "NULL name\n");)
00152                 return;
00153         }
00154         da = hash_del(&dice_async_var, name);
00155         if ( !da ) {
00156                 _dice_debug(2, dice_printf(FUNCTION, " variable not found.\n");)
00157                 return;
00158         }
00159 
00160         dice_async_free((void *)da);
00161 }
00162 
00163 #undef FUNCTION
00164 #define FUNCTION                "dice_async_update"
00165 
00178 int dice_async_update ( char *name, char *type, void *value ) {
00179         struct dice_async       *da;
00180 
00181         _dice_debug(3, dice_printf(FUNCTION, "(%s,%s,%p)\n", 
00182                         name, type, value);)
00183 
00184         if ( !name ) {
00185                 _dice_debug(1, dice_printf(FUNCTION, "NULL name\n");)
00186                 return -1;
00187         }
00188         da = hash_data(&dice_async_var, name);
00189         if ( !da ) {
00190                 return dice_async_new(name, type, value);
00191         }
00192 
00193         if ( da->locked ) 
00194                 da->nextvalue = value;
00195         else
00196                 da->value = value;
00197 
00198         return 0;
00199 }
00200 
00201 /*
00202  * Public functions
00203  */
00204 
00205 #undef FUNCTION
00206 #define FUNCTION                "DICE_async_new"
00207 
00220 int DICE_async_new ( char *name, char *type, void *value ) {
00221         struct dice_async       *da;
00222         DICE_packet             packet;
00223 
00224         _dice_debug(3, dice_printf(FUNCTION, "(%s,%s,%p)\n",
00225                         name, type, value);)
00226 
00227         if ( dice_data.original_only == 1 && dice_data.is_original == 0 )
00228                 return -3;
00229 
00230         /* check arguments */
00231         if ( !name || !dice_type_is_valid(type) ) {
00232                 _dice_debug(1, dice_printf(FUNCTION, "Invalid type or NULL name\n");)
00233                 return -1;
00234         }
00235 
00236         /* check if it already exists */
00237         da = (struct dice_async *)hash_data(&dice_async_var, name);
00238         if ( da ) {
00239                 _dice_debug(2, dice_printf(FUNCTION, 
00240                                 "variable %s already exists.\n", name);)
00241                 return -2;
00242         }
00243 
00244         /* ok, post it */
00245         packet.variable.packtype = VARIABLE_ASYNC;
00246         packet.variable.name = name;
00247         packet.variable.type = type;
00248         packet.variable.value = value;
00249         return dice_packet_send(&packet, dice_data.controlsock);
00250 }
00251 
00252 #undef FUNCTION
00253 #define FUNCTION                "DICE_async_delete"
00254 
00266 int DICE_async_delete ( char *name ) {
00267         DICE_packet             packet;
00268 
00269         _dice_debug(3, dice_printf(FUNCTION, "(%s)\n", name);)
00270 
00271         if ( dice_data.original_only == 1 && dice_data.is_original == 0 )
00272                 return -3;
00273 
00274         if ( !name ) {
00275                 _dice_debug(1, dice_printf(FUNCTION, "NULL name\n");)
00276                 return -1;
00277         }
00278 
00279         /* check if it exists */
00280         da = (struct dice_async *)hash_data(&dice_async_var, name);
00281         if ( !da ) {
00282                 _dice_debug(2, dice_printf(FUNCTION, 
00283                                 "variable %s doesn't exist.\n", name);)
00284                 return -2;
00285         }
00286 
00287         /* send it */
00288         packet.variable_free.packtype = VARIABLE_FREE_ASYNC;
00289         packet.variable_free.name = name;
00290         dice_packet_send(&packet, dice_data.controlsock);
00291         return 0;
00292 }
00293 
00294 #undef FUNCTION
00295 #define FUNCTION                "DICE_async_update"
00296 
00309 int DICE_async_update ( char *name, void *value ) {
00310         dice_packet             packet;
00311 
00312         _dice_debug(3, dice_printf(FUNCTION, "(%s,%p)\n", 
00313                         name, value);)
00314 
00315         if ( dice_data.original_only == 1 && dice_data.is_original == 0 )
00316                 return -3;
00317 
00318         if ( !name ) {
00319                 _dice_debug(1, dice_printf(FUNCTION, "NULL name\n");)
00320                 return -1;
00321         }
00322 
00323         /* check if it exists */
00324         da = (struct dice_async *)hash_data(&dice_async_var, name);
00325         if ( !da ) {
00326                 _dice_debug(2, dice_printf(FUNCTION, 
00327                                 "variable %s doesn't exist.\n", name);)
00328                 return -2;
00329         }
00330 
00331         /* ok, post it */
00332         packet.variable.packtype = VARIABLE_ASYNC;
00333         packet.variable.name = name;
00334         packet.variable.type = "";
00335         packet.variable.value = value;
00336         return dice_packet_send(&packet, dice_data.controlsock);
00337 }
00338 
00339 #undef FUNCTION
00340 #define FUNCTION                "DICE_async_get"
00341 
00346 void *DICE_async_get ( char *name ) {
00347         struct dice_async       *da;
00348 
00349         _dice_debug(3, dice_printf(FUNCTION, "(%s)\n", name);)
00350         if ( !name ) {
00351                 _dice_debug(1, dice_printf(FUNCTION, "NULL name\n");)
00352                 return NULL;
00353         }
00354         da = hash_data(&dice_async_var, name);
00355         
00356         if ( !da ) {
00357                 _dice_debug(2, dice_printf(FUNCTION, "not found\n");)
00358                 return NULL;
00359         }
00360         
00361         return da->value;
00362 }
00363 
00364 #undef FUNCTION
00365 #define FUNCTION                "DICE_async_lock"
00366 
00377 int DICE_async_lock ( char *name ) {
00378         struct dice_async       *da;
00379 
00380         _dice_debug(3, dice_printf(FUNCTION, "(%s)\n", name);)
00381         if ( !name ) {
00382                 _dice_debug(1, dice_printf(FUNCTION, "NULL name\n");)
00383                 return -1;
00384         }
00385 
00386         da = hash_data(&dice_async_var, name);
00387         if ( !da ) {
00388                 _dice_debug(2, dice_printf(FUNCTION, "not found\n");)
00389                 return -2;
00390         }
00391 
00392         da->locked = 1;
00393         return 0;
00394 }
00395 
00396 #undef FUNCTION
00397 #define FUNCTION                "DICE_async_unlock"
00398 
00409 int DICE_async_unlock ( char *name ) {
00410         struct dice_async       *da;
00411         
00412         _dice_debug(3, dice_printf(FUNCTION, "(%s)\n", name);)
00413 
00414         if ( !name ) {
00415                 _dice_debug(1, dice_printf(FUNCTION, "NULL name\n");)
00416                 return -1;
00417         }
00418         
00419         da = hash_data(&dice_async_var, name);
00420         if ( !da ) {
00421                 _dice_debug(2, dice_printf(FUNCTION, "not found\n");)
00422                 return -2;
00423         }
00424 
00425         if ( da->locked == 1 && da->nextvalue != NULL) {
00426                 da->value = da->nextvalue;
00427                 da->nextvalue = NULL;
00428 
00429         }
00430         da->locked = 0; 
00431 
00432         return 0;
00433 }
00434 
00435 #endif

Generated at Sun Dec 9 16:13:18 2001 for dicelib by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001