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
00032
00033
00034
00035
00036
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #ifndef _WIN32
00040 #include <unistd.h>
00041 #include "config.h"
00042 #else
00043 #include "winconfig.h"
00044 #endif
00045 #include "include/mc_platform.h"
00046 #include "include/df.h"
00047
00048
00049 int
00050 df_Add(struct df_s* df, struct df_node_s* node)
00051 {
00052 int err;
00053
00054 SIGNAL(
00055 df->cond,
00056 df->lock,
00057
00058 err = ListAdd(df->service_list, (void*) node);
00059 if (err == MC_SUCCESS)
00060 df->num_entries++;
00061 );
00062 return err;
00063 }
00064
00065 int
00066 df_AddRequest(struct df_s* df, struct df_request_list_node_s* node)
00067 {
00068 int err;
00069
00070 SIGNAL(
00071 df->request_list->cond,
00072 df->request_list->lock,
00073
00074 err = ListAdd(
00075 df->request_list->request_list,
00076 (void*)node );
00077 df->request_list->size++;
00078 );
00079 return err;
00080 }
00081
00082 int
00083 df_Destroy(df_p df)
00084 {
00085 df_node_p df_node;
00086 MUTEX_LOCK(df->lock);
00087 while ( (df_node = (df_node_p)ListPop(df->service_list)) != NULL) {
00088 df_node_Destroy(df_node);
00089 }
00090 ListTerminate(df->service_list);
00091 df_request_list_Destroy(df->request_list);
00092 MUTEX_DESTROY(df->lock);
00093 COND_DESTROY(df->cond);
00094 free(df->lock);
00095 free(df->cond);
00096 free(df->waiting_lock);
00097 free(df->waiting_cond);
00098 free(df);
00099 return MC_SUCCESS;
00100 }
00101
00102 df_p
00103 df_Initialize(mc_platform_p mc_platform)
00104 {
00105 df_p df;
00106 df = (df_p)malloc(sizeof(df_t));
00107
00108 df->mc_platform = mc_platform;
00109
00110
00111 df->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00112 MUTEX_INIT(df->lock);
00113
00114
00115 df->cond = (COND_T*)malloc(sizeof(COND_T));
00116 COND_INIT(df->cond);
00117
00118
00119 df->service_list = ListInitialize();
00120
00121
00122 df->request_list = df_request_list_New();
00123
00124 df->num_entries = 0;
00125 df->waiting = 0;
00126 df->waiting_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00127 MUTEX_INIT(df->waiting_lock);
00128 df->waiting_cond = (COND_T*)malloc(sizeof(COND_T));
00129 COND_INIT(df->waiting_cond);
00130 return df;
00131 }
00132
00133 int
00134 df_ProcessRequest(
00135 struct mc_platform_s* global
00136 )
00137 {
00138 int return_code;
00139 int handler_code;
00140 enum df_request_list_index_e request_code;
00141 df_request_list_node_t *request;
00142 if (
00143 (
00144 request = df_request_list_Pop( global->df->request_list )
00145 ) == NULL
00146 )
00147 {
00148 printf("Empty.\n");
00149 return MC_ERR_EMPTY;
00150 }
00151
00152
00153 #define REQUEST(name, string, description) \
00154 if ( !strcmp(request->command, string ) ) { \
00155 return_code = MC_SUCCESS; \
00156 handler_code = request_handler_##name( \
00157 global, \
00158 request->data ); \
00159 request_code = REQUEST_##name; \
00160 } else
00161 #include "include/df_request.x.h"
00162 #undef REQUEST
00163 {
00164 fprintf(stderr, "No such register command: %s. %s:%d\n",
00165 request->command,
00166 __FILE__,
00167 __LINE__ );
00168 return MC_ERR_INVALID;
00169 }
00170
00171 return handler_code;
00172 }
00173
00174
00175
00176
00177
00178 int df_SearchForService(
00179 df_p df,
00180 const char* searchstring,
00181 char*** agent_names,
00182 char*** service_names,
00183 int** agent_ids,
00184 int* num_entries)
00185 {
00186 int i=0;
00187 int j=0;
00188 int found_entries=0;
00189 listNode_p list_node;
00190 df_node_p df_node;
00191
00192
00193 if(df->num_entries < 1) {
00194 *num_entries = 0;
00195 return MC_ERR_NOT_FOUND;
00196 }
00197
00198
00199 MUTEX_LOCK(df->lock);
00200
00201
00202
00203 list_node = df->service_list->listhead;
00204 while (list_node != NULL) {
00205
00206
00207 MUTEX_LOCK( ((df_node_p)(list_node->node_data))->lock );
00208 df_node = (df_node_p)list_node->node_data;
00209 for(i = 0; i < df_node->num_services; i++) {
00210 if ( strstr(df_node->service_names[i], searchstring) ) {
00211
00212 found_entries++;
00213 }
00214 }
00215 MUTEX_UNLOCK(df_node->lock);
00216 list_node = list_node->next;
00217 }
00218 if (found_entries == 0) {
00219
00220 MUTEX_UNLOCK(df->lock);
00221 *num_entries = 0;
00222 return MC_ERR_NOT_FOUND;
00223 }
00224
00225
00226 *agent_names = (char**)malloc(sizeof(char*) * found_entries);
00227 *service_names = (char**)malloc(sizeof(char*) * found_entries);
00228 *agent_ids = (int*)malloc(sizeof(int) * found_entries);
00229
00230 list_node = df->service_list->listhead;
00231 while (list_node != NULL) {
00232
00233
00234 MUTEX_LOCK( ((df_node_p)(list_node->node_data))->lock );
00235 df_node = (df_node_p)list_node->node_data;
00236 for(i = 0; i < df_node->num_services; i++) {
00237 if ( strstr(df_node->service_names[i], searchstring) ) {
00238
00239
00240 (*agent_names)[j] = (char*)malloc(
00241 sizeof(char) * (strlen(df_node->agent_name)+1)
00242 );
00243 strcpy((*agent_names)[j], df_node->agent_name);
00244
00245 (*service_names)[j] = (char*)malloc(
00246 sizeof(char) * (strlen((df_node->service_names)[i])+1)
00247 );
00248 strcpy((*service_names)[j], (df_node->service_names)[i]);
00249
00250 (*agent_ids)[j] = df_node->agent_id;
00251 j++;
00252 }
00253 }
00254 MUTEX_UNLOCK(df_node->lock);
00255 list_node = list_node->next;
00256 }
00257 MUTEX_UNLOCK(df->lock);
00258 *num_entries = found_entries;
00259 return MC_SUCCESS;
00260 }
00261
00262 void
00263 df_Start(mc_platform_p mc_platform)
00264 {
00265 #ifndef _WIN32
00266 pthread_attr_t attr;
00267 pthread_attr_init(&attr);
00268 if (mc_platform->stack_size[MC_THREAD_DF] != -1) {
00269 pthread_attr_setstacksize
00270 (
00271 &attr,
00272 mc_platform->stack_size[MC_THREAD_DF]
00273 );
00274 }
00275 #else
00276 int stack_size;
00277 if (mc_platform->stack_size[MC_THREAD_DF] < 1) {
00278
00279 stack_size = mc_platform->stack_size[MC_THREAD_DF]+1;
00280 } else {
00281 stack_size = mc_platform->stack_size[MC_THREAD_DF];
00282 }
00283 #endif
00284 THREAD_CREATE
00285 (
00286 &mc_platform->df->thread,
00287 df_Thread,
00288 mc_platform
00289 );
00290 }
00291
00292
00293 int
00294 df_request_list_node_Destroy(df_request_list_node_p node)
00295 {
00296 MUTEX_DESTROY(node->lock);
00297 free(node->lock);
00298 COND_DESTROY(node->cond);
00299 free(node->cond);
00300 free(node);
00301 return MC_SUCCESS;
00302 }
00303
00304 df_request_list_node_p
00305 df_request_list_node_New(void)
00306 {
00307 df_request_list_node_p node;
00308 node = (df_request_list_node_p)
00309 malloc(sizeof(df_request_list_node_t));
00310 CHECK_NULL(node, return NULL;);
00311 node->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00312 MUTEX_INIT(node->lock);
00313 node->cond = (COND_T*)malloc(sizeof(COND_T));
00314 COND_INIT(node->cond);
00315 node->data_size = 0;
00316 node->command = NULL;
00317 node->data = NULL;
00318 return node;
00319 }
00320
00321
00322 int
00323 df_request_list_Destroy(df_request_list_p df_request_list)
00324 {
00325 df_request_list_node_p node;
00326 while
00327 (
00328 (
00329 node =
00330 (df_request_list_node_p)ListPop
00331 (
00332 df_request_list->request_list
00333 )
00334 ) != NULL
00335 )
00336 {
00337 df_request_list_node_Destroy(node);
00338 }
00339 ListTerminate(df_request_list->request_list);
00340 MUTEX_DESTROY(df_request_list->lock);
00341 free(df_request_list->lock);
00342 COND_DESTROY(df_request_list->cond);
00343 free(df_request_list->cond);
00344 free(df_request_list);
00345 return MC_SUCCESS;
00346 }
00347
00348 df_request_list_p
00349 df_request_list_New(void)
00350 {
00351 df_request_list_p new_list;
00352 new_list = (df_request_list_p)malloc(sizeof(df_request_list_t));
00353 CHECK_NULL(new_list, return NULL;);
00354
00355
00356 new_list->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00357 CHECK_NULL(new_list->lock, return NULL;);
00358 new_list->cond = (COND_T*)malloc(sizeof(COND_T));
00359 CHECK_NULL(new_list->cond, return NULL;);
00360
00361 MUTEX_INIT(new_list->lock);
00362 COND_INIT(new_list->cond);
00363
00364 new_list->size=0;
00365
00366 new_list->request_list = ListInitialize();
00367 if (new_list->request_list == NULL) {
00368 return NULL;
00369 } else
00370 return new_list;
00371 }
00372
00373 df_request_list_node_p
00374 df_request_list_Pop(df_request_list_p requests)
00375 {
00376 df_request_list_node_t *node;
00377 MUTEX_LOCK( requests->lock );
00378 if (requests->size <= 0) {
00379 MUTEX_UNLOCK( requests->lock );
00380 return NULL;
00381 }
00382 node = (df_request_list_node_t*)ListPop(requests->request_list);
00383 requests->size--;
00384 MUTEX_UNLOCK( requests->lock );
00385 return node;
00386 }
00387
00388
00389 df_request_search_p
00390 df_request_search_New(void)
00391 {
00392 df_request_search_p search;
00393 search = (df_request_search_p)malloc(sizeof(df_request_search_t));
00394 CHECK_NULL(search, return NULL;);
00395 search->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00396 CHECK_NULL(search->lock, return NULL;);
00397 search->cond = (COND_T*)malloc(sizeof(COND_T));
00398 CHECK_NULL(search->cond, return NULL;);
00399 MUTEX_INIT(search->lock);
00400 COND_INIT(search->cond);
00401 return search;
00402 }
00403
00404 int
00405 df_request_search_Destroy(df_request_search_p node)
00406 {
00407 MUTEX_DESTROY(node->lock);
00408 free(node->lock);
00409 COND_DESTROY(node->cond);
00410 free(node->cond);
00411
00412 free(node);
00413 return MC_SUCCESS;
00414 }
00415
00416
00417 int
00418 df_node_Destroy(df_node_p df_node)
00419 {
00420 int i;
00421 MUTEX_LOCK(df_node->lock);
00422 free(df_node->agent_name);
00423 for(i = 0; i < df_node->num_services; i++) {
00424 free(df_node->service_names[i]);
00425 }
00426 free(df_node->service_names);
00427 free(df_node);
00428 return MC_SUCCESS;
00429 }
00430
00431 #ifndef _WIN32
00432 void* df_Thread(void* arg)
00433 #else
00434 DWORD WINAPI df_Thread( LPVOID arg )
00435 #endif
00436 {
00437 int err_code;
00438 mc_platform_p global = (mc_platform_p)arg;
00439 while(1) {
00440 MUTEX_LOCK(global->df->request_list->lock);
00441 MUTEX_LOCK(global->quit_lock);
00442 while
00443 (
00444 (global->df->request_list->size <= 0) &&
00445 !global->quit
00446 )
00447 {
00448 MUTEX_UNLOCK(global->quit_lock);
00449
00450 MUTEX_LOCK(global->df->waiting_lock);
00451 global->df->waiting = 1;
00452 COND_BROADCAST(global->df->waiting_cond);
00453 MUTEX_UNLOCK(global->df->waiting_lock);
00454
00455 COND_WAIT
00456 (
00457 global->df->request_list->cond,
00458 global->df->request_list->lock
00459 );
00460 MUTEX_LOCK(global->quit_lock);
00461 }
00462
00463 MUTEX_LOCK(global->df->waiting_lock);
00464 global->df->waiting = 0;
00465 COND_BROADCAST(global->df->waiting_cond);
00466 MUTEX_UNLOCK(global->df->waiting_lock);
00467 if
00468 (
00469 global->df->request_list->size == 0 && global->quit
00470 ) {
00471 MUTEX_UNLOCK(global->quit_lock);
00472 MUTEX_UNLOCK(global->df->request_list->lock);
00473 THREAD_EXIT();
00474 }
00475 MUTEX_UNLOCK(global->quit_lock);
00476 MUTEX_UNLOCK(global->df->request_list->lock);
00477 if (
00478 (err_code = df_ProcessRequest(
00479 global
00480 )) != MC_SUCCESS ) {
00481 fprintf(stderr,
00482 "Error Code %d: %s:%d\n",
00483 err_code,
00484 __FILE__,
00485 __LINE__ );
00486 }
00487 }
00488 return 0;
00489 }
00490
00491
00492
00493
00494
00495
00496 int request_handler_REGISTER(struct mc_platform_s* global, void* data)
00497 {
00498
00499 return df_Add(global->df, (struct df_node_s*)data);
00500 }
00501
00502 int request_handler_SEARCH(struct mc_platform_s* global, void* data)
00503 {
00504 df_request_search_p search;
00505 search = (df_request_search_p)data;
00506 df_SearchForService(
00507 global->df,
00508 search->search_string,
00509 &search->search_results->agent_names,
00510 &search->search_results->service_names,
00511 &search->search_results->agent_ids,
00512 &search->search_results->num_results
00513 );
00514 SIGNAL(search->cond,
00515 search->lock,
00516 NULL
00517 );
00518 return MC_SUCCESS;
00519 }
00520
00521 int request_handler_SUBSCRIBE(struct mc_platform_s* global, void* data)
00522 {
00523 return 0;
00524 }
00525
00526 int request_handler_DEREGISTER(struct mc_platform_s* global, void* data)
00527 {
00528 int i, j;
00529 df_deregister_p deregister;
00530 listNode_p node;
00531 df_node_p df_node;
00532 int num_deregistered=0;
00533 df_p df = global->df;
00534 deregister = (df_deregister_p)data;
00535
00536 MUTEX_LOCK(df->lock);
00537 if (df->service_list->listhead== NULL) {
00538 MUTEX_UNLOCK(df->lock);
00539 return 0;
00540 }
00541 node = df->service_list->listhead;
00542 while (node != NULL) {
00543 df_node = (df_node_p)node->node_data;
00544 if (df_node->agent_id == deregister->agent_id) {
00545 for (i = 0; i < df_node->num_services; i++) {
00546 if (!strcmp(
00547 df_node->service_names[i],
00548 deregister->service_name
00549 )
00550 )
00551 {
00552 free(df_node->service_names[i]);
00553 for (j = i; j < df_node->num_services-1; j++) {
00554 df_node->service_names[j] = df_node->service_names[j+1];
00555 }
00556 df_node->num_services--;
00557 num_deregistered++;
00558
00559 if (df_node->num_services == 0) {
00560
00561
00562 }
00563 }
00564 }
00565 }
00566 node = node->next;
00567 }
00568 MUTEX_UNLOCK(df->lock);
00569 return MC_SUCCESS;
00570 }