00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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
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
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
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
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
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
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
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