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 #ifndef _WIN32
00035 #include "config.h"
00036 #else
00037 #include "winconfig.h"
00038 #endif
00039
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #ifndef _WIN32
00044 #include <strings.h>
00045 #else
00046 #define strcasecmp(a, b) \
00047 _stricmp(a, b)
00048 #endif
00049 #include "include/fipa_acl.h"
00050 #include "include/mc_error.h"
00051 #include "include/macros.h"
00052
00053 #define FREEMEM(x) \
00054 if (x != NULL) free(x)
00055
00056
00057 fipa_acl_message_t* fipa_acl_message_New(void)
00058 {
00059 fipa_acl_message_t* acl;
00060 acl = (fipa_acl_message_t*)malloc(sizeof(fipa_acl_message_t));
00061 memset(acl, 0, sizeof(fipa_acl_message_t));
00062 return acl;
00063 }
00064
00065 int fipa_acl_message_Destroy(fipa_acl_message_t* message)
00066 {
00067
00068 if (message == NULL) return 0;
00069 fipa_agent_identifier_Destroy(message->sender);
00070 fipa_agent_identifier_set_Destroy(message->receiver);
00071 fipa_agent_identifier_set_Destroy(message->reply_to);
00072 fipa_string_Destroy(message->content);
00073 fipa_expression_Destroy(message->language);
00074 fipa_expression_Destroy(message->encoding);
00075 fipa_expression_Destroy(message->ontology);
00076
00077 fipa_expression_Destroy(message->conversation_id);
00078 fipa_expression_Destroy(message->reply_with);
00079 fipa_expression_Destroy(message->in_reply_to);
00080 fipa_DateTime_Destroy(message->reply_by);
00081
00082 free(message);
00083 return 0;
00084 }
00085
00086 fipa_acl_message_t* fipa_acl_message_Copy(fipa_acl_message_t* src)
00087 {
00088 fipa_acl_message_t* copy;
00089 if (src == NULL) return NULL;
00090 copy = fipa_acl_message_New();
00091 copy->performative = src->performative;
00092 copy->sender = fipa_agent_identifier_Copy(src->sender);
00093 copy->receiver = fipa_agent_identifier_set_Copy(src->receiver);
00094 copy->reply_to = fipa_agent_identifier_set_Copy(src->reply_to);
00095 copy->content = fipa_string_Copy(src->content);
00096 copy->language = fipa_expression_Copy(src->language);
00097 copy->encoding = fipa_expression_Copy(src->encoding);
00098 copy->ontology = fipa_expression_Copy(src->ontology);
00099
00100 copy->protocol = src->protocol;
00101 copy->conversation_id = fipa_expression_Copy(src->conversation_id);
00102 copy->reply_with = fipa_expression_Copy(src->reply_with);
00103 copy->in_reply_to = fipa_expression_Copy(src->in_reply_to);
00104 copy->reply_by = fipa_DateTime_Copy(src->reply_by);
00105
00106 return copy;
00107 }
00108
00109
00110 fipa_message_string_t* fipa_message_string_New(void)
00111 {
00112 fipa_message_string_t* str;
00113 str = (fipa_message_string_t*)malloc(sizeof(fipa_message_string_t));
00114 memset(str, 0, sizeof(fipa_message_string_t));
00115 return str;
00116 }
00117
00118 int fipa_message_string_Destroy(fipa_message_string_t* message)
00119 {
00120 if (message == NULL) return 0;
00121 if (message->message != NULL) {
00122 free(message->message);
00123 }
00124 free(message);
00125 return 0;
00126 }
00127
00128 fipa_message_string_t* fipa_message_string_Copy(fipa_message_string_t* src)
00129 {
00130 fipa_message_string_t* copy;
00131 if (src == NULL) return NULL;
00132 copy = (fipa_message_string_t*)malloc(sizeof(fipa_message_string_t));
00133 copy->message = strdup(src->message);
00134 copy->parse = copy->message + (src->parse - src->message);
00135 return copy;
00136 }
00137
00138
00139 fipa_url_sequence_t* fipa_url_sequence_New(void)
00140 {
00141 fipa_url_sequence_t* sequence;
00142 sequence = (fipa_url_sequence_t*)malloc(sizeof(fipa_url_sequence_t));
00143 memset(sequence, 0, sizeof(fipa_url_sequence_t));
00144 return sequence;
00145 }
00146
00147 int fipa_url_sequence_Destroy(fipa_url_sequence_t* sequence)
00148 {
00149 int i;
00150 if (sequence == NULL) return 0;
00151 for (i = 0; i < sequence->num; i++) {
00152 fipa_url_Destroy(sequence->urls[i]);
00153 }
00154 free(sequence->urls);
00155 free(sequence);
00156 return 0;
00157 }
00158
00159 fipa_url_sequence_t* fipa_url_sequence_Copy(fipa_url_sequence_t* src)
00160 {
00161 int i;
00162 fipa_url_sequence_t* copy;
00163 if (src == NULL) return NULL;
00164 copy = fipa_url_sequence_New();
00165 copy->num = src->num;
00166 copy->urls = (fipa_url_t**)malloc(
00167 sizeof(fipa_url_t*) * src->num);
00168 for( i = 0; i < src->num; i++) {
00169 copy->urls[i] = fipa_url_Copy(src->urls[i]);
00170 }
00171 return copy;
00172 }
00173
00174
00175 fipa_agent_identifier_set_t* fipa_agent_identifier_set_New(void)
00176 {
00177 fipa_agent_identifier_set_t* set;
00178 set = (fipa_agent_identifier_set_t*)malloc(sizeof(fipa_agent_identifier_set_t));
00179 memset(set, 0, sizeof(fipa_agent_identifier_set_t));
00180 return set;
00181 }
00182
00183 int fipa_agent_identifier_set_Destroy(fipa_agent_identifier_set_t* idset)
00184 {
00185 int i;
00186 if (idset == NULL) return 0;
00187 for(i = 0; i < idset->num; i++) {
00188 fipa_agent_identifier_Destroy(idset->fipa_agent_identifiers[i]);
00189 }
00190 free(idset->fipa_agent_identifiers);
00191 free(idset);
00192 return 0;
00193 }
00194
00195 fipa_agent_identifier_set_t* fipa_agent_identifier_set_Copy(
00196 fipa_agent_identifier_set_t* src)
00197 {
00198 int i;
00199 fipa_agent_identifier_set_t* copy;
00200
00201 if (src == NULL) { return NULL; }
00202 copy = fipa_agent_identifier_set_New();
00203 copy->num = src->num;
00204 copy->retain_order = src->retain_order;
00205 copy->fipa_agent_identifiers = (fipa_agent_identifier_t**)malloc(
00206 sizeof(fipa_agent_identifier_t*) * src->num);
00207 for(i = 0; i < src->num; i++) {
00208 copy->fipa_agent_identifiers[i] = fipa_agent_identifier_Copy(
00209 src->fipa_agent_identifiers[i]);
00210 }
00211
00212 return copy;
00213 }
00214
00215
00216 fipa_agent_identifier_t* fipa_agent_identifier_New(void)
00217 {
00218 fipa_agent_identifier_t* id;
00219 id = (fipa_agent_identifier_t*)malloc(sizeof(fipa_agent_identifier_t));
00220 memset(id, 0, sizeof(fipa_agent_identifier_t));
00221 return id;
00222 }
00223
00224 int fipa_agent_identifier_Destroy(fipa_agent_identifier_t* id)
00225 {
00226 if (id == NULL) return 0;
00227 if (id->name != NULL) {
00228 free(id->name);
00229 }
00230 fipa_url_sequence_Destroy(id->addresses);
00231 fipa_agent_identifier_set_Destroy(id->resolvers);
00232 free(id);
00233 return 0;
00234 }
00235
00236 fipa_agent_identifier_t* fipa_agent_identifier_Copy(fipa_agent_identifier_t* src)
00237 {
00238 fipa_agent_identifier_t* copy;
00239 if (src == NULL) return NULL;
00240 copy = fipa_agent_identifier_New();
00241 copy->name = strdup(src->name);
00242 copy->addresses = fipa_url_sequence_Copy(src->addresses);
00243 copy->resolvers = fipa_agent_identifier_set_Copy(src->resolvers);
00244 return copy;
00245 }
00246
00247
00248 fipa_expression_t* fipa_expression_New(void)
00249 {
00250 fipa_expression_t* expr;
00251 expr = (fipa_expression_t*)malloc(sizeof(fipa_expression_t));
00252 memset(expr, 0, sizeof(fipa_expression_t));
00253 return expr;
00254 }
00255
00256 int fipa_expression_Destroy(fipa_expression_t* expr)
00257 {
00258 int i;
00259 if (expr == NULL) return 0;
00260 switch (expr->type) {
00261 case FIPA_EXPR_WORD:
00262 fipa_word_Destroy(expr->content.word);
00263 break;
00264 case FIPA_EXPR_STRING:
00265 fipa_string_Destroy(expr->content.string);
00266 break;
00267 case FIPA_EXPR_NUMBER:
00268 fipa_number_Destroy(expr->content.number);
00269 break;
00270 case FIPA_EXPR_DATETIME:
00271 fipa_DateTime_Destroy(expr->content.datetime);
00272 break;
00273 case FIPA_EXPR_EXPRESSION:
00274 if (expr->content.expression == NULL) break;
00275 for (i = 0; expr->content.expression[i] != NULL; i++) {
00276 fipa_expression_Destroy(expr->content.expression[i]);
00277 }
00278 FREEMEM(expr->content.expression);
00279 break;
00280 default:
00281 break;
00282 }
00283 free(expr);
00284 return 0;
00285 }
00286
00287 fipa_expression_t* fipa_expression_Copy(fipa_expression_t* src)
00288 {
00289 int i, num;
00290 fipa_expression_t* copy;
00291 if (src == NULL) return NULL;
00292 copy = fipa_expression_New();
00293 copy->type = src->type;
00294 switch(src->type) {
00295 case FIPA_EXPR_WORD:
00296 copy->content.word = fipa_word_Copy(src->content.word);
00297 break;
00298 case FIPA_EXPR_STRING:
00299 copy->content.string = fipa_string_Copy(src->content.string);
00300 break;
00301 case FIPA_EXPR_NUMBER:
00302 copy->content.number = fipa_number_Copy(src->content.number);
00303 break;
00304 case FIPA_EXPR_DATETIME:
00305 copy->content.datetime = fipa_DateTime_Copy(src->content.datetime);
00306 break;
00307 case FIPA_EXPR_EXPRESSION:
00308
00309 for(i = 0; src->content.expression[i] != NULL; i++);
00310
00311 num = i;
00312 copy->content.expression = (fipa_expression_t**)malloc(
00313 sizeof(fipa_expression_t*) * (num + 1));
00314 for(i = 0; i < num; i++) {
00315 copy->content.expression[i] = fipa_expression_Copy(
00316 src->content.expression[i] );
00317 }
00318 copy->content.expression[num] = NULL;
00319 break;
00320 default:
00321 fipa_expression_Destroy(copy);
00322 return NULL;
00323 }
00324 return copy;
00325 }
00326
00327
00328 fipa_word_t* fipa_word_New(void)
00329 {
00330 fipa_word_t* word;
00331 word = (fipa_word_t*)malloc(sizeof(fipa_word_t));
00332 memset(word, 0, sizeof(fipa_word_t));
00333 return word;
00334 }
00335
00336 int fipa_word_Destroy(fipa_word_t* word)
00337 {
00338 if (word == NULL) return 0;
00339 if (word->content != NULL) {
00340 free( word->content );
00341 }
00342 free(word);
00343 return 0;
00344 }
00345
00346 fipa_word_t* fipa_word_Copy(fipa_word_t* src)
00347 {
00348 fipa_word_t* copy;
00349 if (src == NULL) return NULL;
00350 copy = fipa_word_New();
00351 copy->content = strdup(src->content);
00352 return copy;
00353 }
00354
00355
00356 fipa_string_t* fipa_string_New(void)
00357 {
00358 fipa_string_t* str;
00359 str = (fipa_string_t*)malloc(sizeof(fipa_string_t));
00360 memset(str, 0, sizeof(fipa_string_t));
00361 return str;
00362 }
00363
00364 int fipa_string_Destroy(fipa_string_t* str)
00365 {
00366 if (str == NULL) return 0;
00367 if (str->content != NULL) {
00368 free(str->content);
00369 }
00370 free(str);
00371 return 0;
00372 }
00373
00374 fipa_string_t* fipa_string_Copy(fipa_string_t* src)
00375 {
00376 fipa_string_t* copy;
00377 if (src == NULL) return NULL;
00378 copy = fipa_string_New();
00379 copy->content = strdup(src->content);
00380 return copy;
00381 }
00382
00383
00384 fipa_DateTime_t* fipa_DateTime_New(void)
00385 {
00386 fipa_DateTime_t* dt;
00387 dt = (fipa_DateTime_t*)malloc(sizeof(fipa_DateTime_t));
00388 memset(dt, 0, sizeof(fipa_DateTime_t));
00389 return dt;
00390 }
00391
00392 int fipa_DateTime_Destroy(fipa_DateTime_t* dt)
00393 {
00394 if(dt == NULL) return 0;
00395 free(dt);
00396 return 0;
00397 }
00398
00399 fipa_DateTime_t* fipa_DateTime_Copy(fipa_DateTime_t* src)
00400 {
00401 fipa_DateTime_t* copy;
00402 if (src == NULL) return NULL;
00403 copy = fipa_DateTime_New();
00404 *copy = *src;
00405 return copy;
00406 }
00407
00408
00409 fipa_url_t* fipa_url_New(void)
00410 {
00411 fipa_url_t* url;
00412 url = (fipa_url_t*)malloc(sizeof(fipa_url_t));
00413 memset(url, 0, sizeof(fipa_url_t));
00414 return url;
00415 }
00416
00417 int fipa_url_Destroy(fipa_url_t* url)
00418 {
00419 if (url == NULL) return 0;
00420 if (url->str != NULL) {
00421 free(url->str);
00422 }
00423 free(url);
00424 return 0;
00425 }
00426
00427 fipa_url_t* fipa_url_Copy(fipa_url_t* src)
00428 {
00429 fipa_url_t* copy;
00430 if (src == NULL) return NULL;
00431 copy = fipa_url_New();
00432 copy->str = strdup(src->str);
00433 return copy;
00434 }
00435
00436
00437 fipa_number_t* fipa_number_New(void)
00438 {
00439 fipa_number_t* num;
00440 num = (fipa_number_t*)malloc(sizeof(fipa_number_t));
00441 memset(num, 0, sizeof(fipa_number_t));
00442 return num;
00443 }
00444
00445 int fipa_number_Destroy(fipa_number_t* number)
00446 {
00447 if (number == NULL) return 0;
00448 if (number->str != NULL){
00449 free(number->str);
00450 }
00451 free(number);
00452 return 0;
00453 }
00454
00455 fipa_number_t* fipa_number_Copy(fipa_number_t* src)
00456 {
00457 fipa_number_t* copy;
00458 if (src == NULL) return NULL;
00459 copy = fipa_number_New();
00460 copy->str = strdup(src->str);
00461 return copy;
00462 }
00463
00464
00465 int fipa_acl_Parse(fipa_acl_message_p acl, fipa_message_string_p message)
00466 {
00467 int err = 0;
00468 if (fipa_GetAtom(message,'(')) {
00469 err = MC_ERR_PARSE;
00470 goto exit;
00471 }
00472 if (fipa_message_type_Parse(&(acl->performative), message)) {
00473 err = MC_ERR_PARSE;
00474 goto exit;
00475 }
00476 while(fipa_GetAtom(message, ')')){
00477 err = fipa_message_parameter_Parse(acl, message);
00478 if (err) return err;
00479 }
00480
00481 exit:
00482 return err;
00483 }
00484
00485 int fipa_message_parameter_Parse(fipa_acl_message_p acl, fipa_message_string_p message)
00486 {
00487 int err;
00488 fipa_word_t* word = NULL;
00489 char* parameter;
00490 if((err = fipa_GetAtom(message, ':'))) return err;
00491 if((err = fipa_word_Parse(&word, message))) return err;
00492 parameter = word->content;
00493 if (!strcmp(parameter, "sender")) {
00494 err = fipa_agent_identifier_Parse(&(acl->sender), message);
00495 } else if (!strcmp(parameter, "receiver")) {
00496 err = fipa_agent_identifier_set_Parse(&(acl->receiver), message);
00497 } else if (!strcmp(parameter, "content")) {
00498 err = fipa_string_Parse(&(acl->content), message);
00499 } else if (!strcmp(parameter, "reply-with")) {
00500 err = fipa_expression_Parse(&(acl->reply_with), message);
00501 } else if (!strcmp(parameter, "reply-by")) {
00502 err = fipa_datetime_Parse(&(acl->reply_by), message);
00503 } else if (!strcmp(parameter, "in-reply-to")) {
00504 err = fipa_expression_Parse(&(acl->in_reply_to), message);
00505 } else if (!strcmp(parameter, "reply-to")) {
00506 err = fipa_agent_identifier_set_Parse(&(acl->reply_to), message);
00507 } else if (!strcmp(parameter, "language")) {
00508 err = fipa_expression_Parse(&(acl->language), message);
00509 } else if (!strcmp(parameter, "encoding")) {
00510 err = fipa_expression_Parse(&(acl->encoding), message);
00511 } else if (!strcmp(parameter, "ontology")) {
00512 err = fipa_expression_Parse(&(acl->ontology), message);
00513 } else if (!strcmp(parameter, "protocol")) {
00514
00515 err = fipa_protocol_type_Parse(&(acl->protocol), message);
00516 } else if (!strcmp(parameter, "conversation-id")) {
00517 err = fipa_expression_Parse(&(acl->conversation_id), message);
00518 } else {
00519
00520 fprintf(stderr, "FIXME: No handling of user defined parameters. %s:%d\n",
00521 __FILE__, __LINE__);
00522 err = MC_ERR_PARSE;
00523 }
00524 fipa_word_Destroy(word);
00525 return err;
00526 }
00527
00528 int fipa_protocol_type_Parse(
00529 enum fipa_protocol_e* protocol,
00530 fipa_message_string_p message
00531 )
00532 {
00533 int err = 0;
00534 fipa_word_p word;
00535 err = fipa_word_Parse(&word, message);
00536 if (err) return err;
00537 if(!strcasecmp(word->content, "fipa-request")) {
00538 *protocol = FIPA_PROTOCOL_REQUEST;
00539 } else if (!strcasecmp(word->content, "fipa-query")) {
00540 *protocol = FIPA_PROTOCOL_QUERY;
00541 } else if (!strcasecmp(word->content, "fipa-request-when")) {
00542 *protocol = FIPA_PROTOCOL_REQUEST_WHEN;
00543 } else if (!strcasecmp(word->content, "fipa-contract-net")) {
00544 *protocol = FIPA_PROTOCOL_CONTRACT_NET;
00545 } else if (!strcasecmp(word->content, "fipa-iterated-contract-net")) {
00546 *protocol = FIPA_PROTOCOL_ITERATED_CONTRACT_NET;
00547 } else if (!strcasecmp(word->content, "fipa-auction-english")) {
00548 *protocol = FIPA_PROTOCOL_ENGLISH_AUCTION;
00549 } else if (!strcasecmp(word->content, "fipa-auction-dutch")) {
00550 *protocol = FIPA_PROTOCOL_DUTCH_AUCTION;
00551 } else if (!strcasecmp(word->content, "fipa-brokering")) {
00552 *protocol = FIPA_PROTOCOL_BROKERING;
00553 } else if (!strcasecmp(word->content, "fipa-recruiting")) {
00554 *protocol = FIPA_PROTOCOL_RECRUITING;
00555 } else if (!strcasecmp(word->content, "fipa-subscribe")) {
00556 *protocol = FIPA_PROTOCOL_SUBSCRIBE;
00557 } else if (!strcasecmp(word->content, "fipa-propose")) {
00558 *protocol = FIPA_PROTOCOL_PROPOSE;
00559 } else {
00560 fprintf(stderr, "Unknown protocol: '%s'. %s:%d\n",
00561 word->content, __FILE__, __LINE__);
00562 err = MC_ERR_PARSE;
00563 }
00564 fipa_word_Destroy(word);
00565 return err;
00566 }
00567
00568 int fipa_message_type_Parse(
00569 enum fipa_performative_e* performative,
00570 fipa_message_string_p message
00571 )
00572 {
00573 int err = 0;
00574 fipa_word_p word;
00575 err = fipa_word_Parse(&word, message);
00576 if (err) return err;
00577 if(!strcasecmp(word->content, "accept-proposal")) {
00578 *performative = FIPA_ACCEPT_PROPOSAL;
00579 } else if (!strcasecmp(word->content, "agree")) {
00580 *performative = FIPA_AGREE;
00581 } else if (!strcasecmp(word->content, "cancel")) {
00582 *performative = FIPA_CANCEL;
00583 } else if (!strcasecmp(word->content, "call-for-proposal")) {
00584 *performative = FIPA_CALL_FOR_PROPOSAL;
00585 } else if (!strcasecmp(word->content, "confirm")) {
00586 *performative = FIPA_CONFIRM;
00587 } else if (!strcasecmp(word->content, "disconfirm")) {
00588 *performative = FIPA_DISCONFIRM;
00589 } else if (!strcasecmp(word->content, "failure")) {
00590 *performative = FIPA_FAILURE;
00591 } else if (!strcasecmp(word->content, "inform")) {
00592 *performative = FIPA_INFORM;
00593 } else if (!strcasecmp(word->content, "inform-if")) {
00594 *performative = FIPA_INFORM_IF;
00595 } else if (!strcasecmp(word->content, "inform-ref")) {
00596 *performative = FIPA_INFORM_REF;
00597 } else if (!strcasecmp(word->content, "not-understood")) {
00598 *performative = FIPA_NOT_UNDERSTOOD;
00599 } else if (!strcasecmp(word->content, "propogate")) {
00600 *performative = FIPA_PROPOGATE;
00601 } else if (!strcasecmp(word->content, "propose")) {
00602 *performative = FIPA_PROPOSE;
00603 } else if (!strcasecmp(word->content, "proxy")) {
00604 *performative = FIPA_PROXY;
00605 } else if (!strcasecmp(word->content, "query-if")) {
00606 *performative = FIPA_QUERY_IF;
00607 } else if (!strcasecmp(word->content, "query-ref")) {
00608 *performative = FIPA_QUERY_REF;
00609 } else if (!strcasecmp(word->content, "refuse")) {
00610 *performative = FIPA_REFUSE;
00611 } else if (!strcasecmp(word->content, "reject-proposal")) {
00612 *performative = FIPA_REJECT_PROPOSAL;
00613 } else if (!strcasecmp(word->content, "request")) {
00614 *performative = FIPA_REQUEST;
00615 } else if (!strcasecmp(word->content, "request-when")) {
00616 *performative = FIPA_REQUEST_WHEN;
00617 } else if (!strcasecmp(word->content, "request-whenever")) {
00618 *performative = FIPA_REQUEST_WHENEVER;
00619 } else if (!strcasecmp(word->content, "subscribe")) {
00620 *performative = FIPA_SUBSCRIBE;
00621 } else {
00622 fprintf(stderr, "Unknown performative: '%s'. %s:%d\n",
00623 word->content, __FILE__, __LINE__);
00624 err = MC_ERR_PARSE;
00625 }
00626 fipa_word_Destroy(word);
00627 return err;
00628 }
00629
00630 int fipa_GetAtom(
00631 fipa_message_string_p message,
00632 char expected_atom
00633 )
00634 {
00635 while
00636 (
00637 (*(message->parse) >= 0x00) &&
00638 (*(message->parse) <= 0x20)
00639 )
00640 {
00641 if (*(message->parse) == 0x00)
00642 return MC_ERR_PARSE;
00643 message->parse++;
00644 }
00645 if( *(message->parse) == expected_atom) {
00646 message->parse++;
00647 return MC_SUCCESS;
00648 } else {
00649 return MC_ERR_PARSE;
00650 }
00651 }
00652
00653 int fipa_word_Parse(fipa_word_t** word, fipa_message_string_p message)
00654 {
00655 char* tmp;
00656 int i = 0;
00657
00658 while
00659 (
00660 (*(message->parse)>=0x00) &&
00661 (*(message->parse)<=0x20)
00662 )
00663 {
00664
00665 if (*(message->parse) == '\0') {
00666 return MC_ERR_PARSE;
00667 }
00668 message->parse++;
00669 }
00670
00671 tmp = message->parse;
00672 while (*tmp > 0x20) {
00673 tmp++;
00674 i++;
00675 }
00676 *word = (fipa_word_t*)malloc(sizeof(fipa_word_t));
00677 CHECK_NULL(*word, exit(0););
00678 (*word)->content = (char*)malloc
00679 (
00680 sizeof(char) * (i+1)
00681 );
00682 CHECK_NULL((*word)->content, exit(0););
00683
00684
00685 i = 0;
00686 while( *(message->parse) > 0x20 ) {
00687 ((*word)->content)[i] = *(message->parse);
00688 message->parse++;
00689 i++;
00690 }
00691 ((*word)->content)[i] = '\0';
00692 return MC_SUCCESS;
00693 }
00694
00695 int fipa_CheckNextToken(const fipa_message_string_p message, const char* token)
00696 {
00697 char* tmp = message->parse;
00698 while
00699 (
00700 (*tmp >= 0x00) &&
00701 (*tmp <= 0x20)
00702 )
00703 tmp++;
00704 while (*token != '\0') {
00705 if (*token != *tmp) {
00706 return 0;
00707 }
00708 token++;
00709 tmp++;
00710 }
00711 return 1;
00712 }
00713
00714 int fipa_expression_Parse(fipa_expression_t** expression, fipa_message_string_p message)
00715 {
00716 int i=0;
00717 *expression = (fipa_expression_t*)malloc(sizeof(fipa_expression_t));
00718
00719
00720
00721 if (fipa_CheckNextToken(message, "(")) {
00722 (*expression)->type = FIPA_EXPR_EXPRESSION;
00723 if(fipa_GetAtom(message, '(')) {
00724
00725 fprintf(stderr, "Fatal error. %s:%d\n", __FILE__, __LINE__);
00726 exit(0);
00727 }
00728 for
00729 (
00730 i = 0;
00731 !fipa_expression_Parse( &(((*expression)->content.expression)[i]), message);
00732 i++
00733 );
00734 if(fipa_GetAtom(message, ')')) {
00735 fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
00736 return MC_ERR_PARSE;
00737 }
00738 } else if (
00739
00740 !fipa_datetime_Parse(&((*expression)->content.datetime), message)
00741 )
00742 {
00743 (*expression)->type = FIPA_EXPR_DATETIME;
00744 } else if (
00745
00746 !fipa_string_Parse(&((*expression)->content.string), message)
00747 )
00748 {
00749 (*expression)->type = FIPA_EXPR_STRING;
00750 } else if (
00751
00752 !fipa_word_Parse(&((*expression)->content.word), message)
00753 )
00754 {
00755 (*expression)->type=FIPA_EXPR_WORD;
00756 }
00757 else
00758 {
00759
00760 return MC_ERR_PARSE;
00761 }
00762 return MC_SUCCESS;
00763 }
00764
00765 int fipa_GetNextWord(char** word, const fipa_message_string_p message)
00766 {
00767 char *tmp;
00768 int i = 0;
00769 int j;
00770
00771 tmp = message->parse;
00772 while
00773 (
00774 (*tmp >= 0x00) &&
00775 (*tmp <= 0x20)
00776 )
00777 tmp++;
00778
00779
00780 if
00781 (
00782 ((*tmp >= 0x00) && (*tmp <= 0x20)) ||
00783 (*tmp == '(') ||
00784 (*tmp == ')') ||
00785 (*tmp == '#') ||
00786 ((*tmp >= 0x30) && (*tmp <= 0x39)) ||
00787 (*tmp == '-') ||
00788 (*tmp == '@')
00789 )
00790 return ERR;
00791 i++;
00792 tmp++;
00793
00794 while
00795 (
00796 ((*tmp < 0x00) || (*tmp > 0x20)) &&
00797 (*tmp != '(') &&
00798 (*tmp != ')')
00799 ) {
00800 i++;
00801 tmp++;
00802 }
00803
00804 *word = (char*)malloc(sizeof(char) * (i+1));
00805
00806 for (j = 0; j < i; j++) {
00807 *((*word) + j) = *(message->parse+j);
00808 }
00809 *((*word)+j) = '\0';
00810 return MC_SUCCESS;
00811 }
00812
00813 int fipa_GetWholeToken(char** word, fipa_message_string_p message)
00814 {
00815 char *tmp;
00816 int i = 0;
00817 int j;
00818
00819 tmp = message->parse;
00820 while
00821 (
00822 (*tmp >= 0x00) &&
00823 (*tmp <= 0x20)
00824 )
00825 {
00826 tmp++;
00827 message->parse++;
00828 }
00829
00830 i++;
00831 tmp++;
00832
00833 while
00834 (
00835 ((*tmp < 0x00) || (*tmp > 0x20))
00836 ) {
00837 i++;
00838 tmp++;
00839 }
00840
00841 *word = (char*)malloc(sizeof(char) * (i+1));
00842
00843 for (j = 0; j < i; j++) {
00844 *((*word) + j) = *(message->parse+j);
00845 }
00846 *((*word)+j) = '\0';
00847 return MC_SUCCESS;
00848 }
00849
00850 int fipa_datetime_Parse(fipa_DateTime_p* datetime, fipa_message_string_p message)
00851 {
00852 char *word;
00853 char *tmp;
00854 int i;
00855 char buf[5];
00856
00857 fipa_GetWholeToken(&word, message);
00858 tmp = word;
00859 if (
00860 (*tmp == '+') ||
00861 (*tmp == '-')
00862 )
00863 tmp++;
00864
00865 for(i = 0; i < 8; i++) {
00866 if (*tmp < 0x30 || *tmp > 0x39) {
00867 free(word);
00868 return MC_ERR_PARSE;
00869 }
00870 tmp++;
00871 }
00872
00873 if (*tmp == 'T') {
00874 tmp++;
00875 } else {
00876 free(word);
00877 return MC_ERR_PARSE;
00878 }
00879
00880 for(i = 0; i < 9; i++) {
00881 if(*tmp < 0x30 || *tmp > 0x39) {
00882 free(word);
00883 return MC_ERR_PARSE;
00884 }
00885 tmp++;
00886 }
00887
00888
00889 *datetime = (fipa_DateTime_p)malloc(sizeof(fipa_DateTime_t));
00890 tmp = word;
00891 switch(*tmp) {
00892 case '+':
00893 (*datetime)->sign = '+';
00894 tmp++;
00895 message->parse++;
00896 break;
00897 case '-':
00898 (*datetime)->sign = '-';
00899 tmp++;
00900 message->parse++;
00901 break;
00902 default:
00903 break;
00904 }
00905
00906
00907 for(i = 0; i < 4; i++) {
00908 buf[i] = *tmp;
00909 tmp++;
00910 message->parse++;
00911 }
00912 buf[i] = '\0';
00913 (*datetime)->year = atoi(buf);
00914
00915
00916 for(i = 0; i < 2; i++) {
00917 buf[i] = *tmp;
00918 tmp++;
00919 message->parse++;
00920 }
00921 buf[i] = '\0';
00922 (*datetime)->month = atoi(buf);
00923
00924
00925 for(i = 0; i < 2; i++) {
00926 buf[i] = *tmp;
00927 tmp++;
00928 message->parse++;
00929 }
00930 buf[i] = '\0';
00931 (*datetime)->month = atoi(buf);
00932
00933
00934 if (*tmp != 'T') {
00935
00936 fprintf(stderr, "Fatal Error. %s:%d\n", __FILE__, __LINE__);
00937 exit(0);
00938 }
00939 tmp++;
00940 message->parse++;
00941
00942
00943 for(i = 0; i < 2; i++) {
00944 buf[i] = *tmp;
00945 tmp++;
00946 message->parse++;
00947 }
00948 buf[i] = '\0';
00949 (*datetime)->hour = atoi(buf);
00950
00951
00952 for(i = 0; i < 2; i++) {
00953 buf[i] = *tmp;
00954 tmp++;
00955 message->parse++;
00956 }
00957 buf[i] = '\0';
00958 (*datetime)->minute = atoi(buf);
00959
00960
00961 for(i = 0; i < 2; i++) {
00962 buf[i] = *tmp;
00963 tmp++;
00964 message->parse++;
00965 }
00966 buf[i] = '\0';
00967 (*datetime)->second = atoi(buf);
00968
00969
00970 for(i = 0; i < 3; i++) {
00971 buf[i] = *tmp;
00972 tmp++;
00973 message->parse++;
00974 }
00975 buf[i] = '\0';
00976 (*datetime)->millisecond = atoi(buf);
00977
00978 if (*tmp == 'Z') {
00979 (*datetime)->is_utc = 1;
00980 message->parse++;
00981 }
00982 else
00983 (*datetime)->is_utc = 0;
00984 free(word);
00985 return MC_SUCCESS;
00986 }
00987
00988 int fipa_string_Parse( fipa_string_p* fipa_string, fipa_message_string_p message)
00989 {
00990 int len;
00991 char *tmp;
00992
00993 if(fipa_GetAtom(message, '\"')) {
00994 return MC_ERR_PARSE;
00995 }
00996
00997 tmp = message->parse;
00998 len = 0;
00999 while
01000 (
01001 (*tmp != '\0')
01002 )
01003 {
01004 if (*tmp == '\"') {
01005 break;
01006 }
01007 if (*tmp == '\\') {
01008 tmp++;
01009 len++;
01010 }
01011 tmp++;
01012 len++;
01013 }
01014 *fipa_string = (fipa_string_p)malloc(sizeof(fipa_string_t));
01015 (*fipa_string)->content = (char*)malloc
01016 (
01017 sizeof(char) * (len + 1)
01018 );
01019 len = 0;
01020 while (message->parse < tmp) {
01021 ((*fipa_string)->content)[len] = *(message->parse);
01022 len++;
01023 message->parse++;
01024 }
01025 ((*fipa_string)->content)[len] = '\0';
01026
01027 if(fipa_GetAtom(message, '\"')) {
01028 return MC_ERR_PARSE;
01029 }
01030 return MC_SUCCESS;
01031 }
01032
01033 int fipa_agent_identifier_Parse(fipa_agent_identifier_p* aid, fipa_message_string_p message)
01034 {
01035 int err = 0;
01036 fipa_word_t* word = NULL;
01037 char *rewind;
01038 if
01039 (
01040 (err = fipa_GetAtom(message, '(') )
01041 ) return err;
01042 if
01043 (
01044 (err = fipa_word_Parse(&word, message) )
01045 )
01046 {
01047 fipa_word_Destroy(word);
01048 return err;
01049 }
01050 if (strcmp(word->content, "agent-identifier")) {
01051 free(word->content);
01052 fipa_word_Destroy(word);
01053 return MC_ERR_PARSE;
01054 }
01055 fipa_word_Destroy(word);
01056 word = NULL;
01057 if
01058 (
01059 (err = fipa_GetAtom(message, ':') )
01060 ) return err;
01061 if
01062 (
01063 (err = fipa_word_Parse(&word, message))
01064 )
01065 {
01066 fipa_word_Destroy(word);
01067 return err;
01068 }
01069 if (strcmp(word->content, "name")) {
01070 return MC_ERR_PARSE;
01071 }
01072 fipa_word_Destroy(word);
01073 word = NULL;
01074
01075 *aid = (fipa_agent_identifier_p)malloc(sizeof(fipa_agent_identifier_t));
01076 memset(*aid, 0, sizeof(fipa_agent_identifier_t));
01077 if
01078 (
01079 (err = fipa_word_Parse(&word, message))
01080 )
01081 {
01082 fipa_word_Destroy(word);
01083 return err;
01084 }
01085 (*aid)->name = (char*)malloc
01086 (
01087 sizeof(char) *
01088 (strlen(word->content)+1)
01089 );
01090 CHECK_NULL((*aid)->name, exit(0););
01091 strcpy((*aid)->name, word->content);
01092
01093 fipa_word_Destroy(word);
01094
01095
01096
01097 rewind = message->parse;
01098 if
01099 (
01100 (!(err = fipa_GetAtom(message, ':')))
01101 )
01102 {
01103 if
01104 (
01105 (err = fipa_word_Parse(&word, message))
01106 ) {
01107 message->parse = rewind;
01108 fipa_word_Destroy(word);
01109 return MC_ERR_PARSE;
01110 }
01111
01112 if (!strcmp(word->content, "addresses"))
01113 {
01114 err = fipa_url_sequence_Parse(&((*aid)->addresses), message);
01115 if(err) {
01116 message->parse = rewind;
01117 fipa_word_Destroy(word);
01118 return err;
01119 }
01120 } else if (!strcmp(word->content, "resolvers"))
01121 {
01122 err = fipa_agent_identifier_set_Parse(&((*aid)->resolvers), message);
01123 if (err) {
01124 message->parse = rewind;
01125 fipa_word_Destroy(word);
01126 return err;
01127 }
01128 } else {
01129 message->parse = rewind;
01130 }
01131 }
01132
01133 err = fipa_GetAtom(message, ')');
01134 fipa_word_Destroy(word);
01135 if (err) {return MC_ERR_PARSE;}
01136 return MC_SUCCESS;
01137
01138 }
01139
01140 int fipa_url_sequence_Parse(fipa_url_sequence_p* urls, fipa_message_string_p message)
01141 {
01142 int err;
01143 fipa_word_p word;
01144 int i;
01145 if
01146 (
01147 (err = fipa_GetAtom(message, '(') )
01148 ) return err;
01149 if
01150 (
01151 (err = fipa_word_Parse(&word, message) )
01152 ) return err;
01153 if ( strcmp(word->content, "sequence")) {
01154 fipa_word_Destroy(word);
01155 return MC_ERR_PARSE;
01156 }
01157 fipa_word_Destroy(word);
01158 *urls = fipa_url_sequence_New();
01159
01160
01161 (*urls)->urls = (fipa_url_t**)malloc(sizeof(fipa_url_t*)*20);
01162 i = 0;
01163 (*urls)->num = 0;
01164 while( fipa_GetAtom(message, ')') ) {
01165 fipa_url_Parse(&(*urls)->urls[i], message);
01166 i++;
01167 (*urls)->num++;
01168 }
01169 return 0;
01170 }
01171
01172 int fipa_url_Parse(fipa_url_p* url, fipa_message_string_p message)
01173 {
01174 fipa_word_p word = NULL;
01175 int err;
01176 *url = (fipa_url_t*)malloc(sizeof(fipa_url_t));
01177 err = fipa_word_Parse(&word, message);
01178 if (err) {
01179 free(*url);
01180 if(word == NULL) fipa_word_Destroy(word);
01181 fprintf(stderr, "Error parsing. %s:%d\n", __FILE__, __LINE__);
01182 return err;
01183 }
01184 (*url)->str = strdup(word->content);
01185 fipa_word_Destroy(word);
01186 return 0;
01187 }
01188
01189
01190
01191
01192 int fipa_agent_identifier_set_Parse(fipa_agent_identifier_set_p* agent_ids, fipa_message_string_p message)
01193 {
01194 int err;
01195 fipa_word_p word;
01196 int i=0;
01197
01198 if
01199 (
01200 (err = fipa_GetAtom(message, '(') )
01201 ) return err;
01202 if
01203 (
01204 (err = fipa_word_Parse(&word, message) )
01205 ) return err;
01206 if (!strcmp(word->content, "set")) {
01207 *agent_ids = (fipa_agent_identifier_set_p)malloc(sizeof(struct fipa_agent_identifier_set_s));
01208 (*agent_ids)->retain_order = 0;
01209 } else if (!strcmp(word->content, "sequence")) {
01210 *agent_ids = (fipa_agent_identifier_set_p)malloc(sizeof(struct fipa_agent_identifier_set_s));
01211 (*agent_ids)->retain_order = 1;
01212 } else {
01213 free(word->content);
01214 free(word);
01215 return MC_ERR_PARSE;
01216 }
01217 free(word->content);
01218 free(word);
01219 (*agent_ids)->fipa_agent_identifiers =
01220 (fipa_agent_identifier_p*)malloc(20 * sizeof(fipa_agent_identifier_t*));
01221 while( fipa_GetAtom(message, ')') ) {
01222 err = fipa_agent_identifier_Parse
01223 (&(((*agent_ids)->fipa_agent_identifiers)[i]), message);
01224 if(err) return err;
01225 i++;
01226 }
01227 (*agent_ids)->num = i;
01228 return MC_SUCCESS;
01229 }
01230
01231
01232
01233 int fipa_acl_Compose(dynstring_t** msg, fipa_acl_message_t* acl)
01234 {
01235 *msg = dynstring_New();
01236 dynstring_Append(*msg, "(");
01237 fipa_performative_Compose(*msg, acl->performative);
01238 if (acl->sender != NULL) {
01239 dynstring_Append(*msg, ":sender ");
01240 fipa_agent_identifier_Compose(*msg, acl->sender);
01241 dynstring_Append(*msg, "\n");
01242 }
01243 if (acl->receiver != NULL) {
01244 dynstring_Append(*msg, ":receiver ");
01245 fipa_agent_identifier_set_Compose(*msg, acl->receiver);
01246 dynstring_Append(*msg, "\n");
01247 }
01248 if (acl->reply_to) {
01249 dynstring_Append(*msg, ":reply-to ");
01250 fipa_agent_identifier_set_Compose(*msg, acl->reply_to);
01251 dynstring_Append(*msg, "\n");
01252 }
01253 if (acl->content) {
01254 dynstring_Append(*msg, ":content ");
01255 fipa_string_Compose(*msg, acl->content);
01256 dynstring_Append(*msg, "\n");
01257 }
01258 if (acl->language) {
01259 dynstring_Append(*msg, ":language ");
01260 fipa_expression_Compose(*msg, acl->language);
01261 dynstring_Append(*msg, "\n");
01262 }
01263 if (acl->encoding) {
01264 dynstring_Append(*msg, ":encoding ");
01265 fipa_expression_Compose(*msg, acl->encoding);
01266 dynstring_Append(*msg, "\n");
01267 }
01268 if (acl->ontology) {
01269 dynstring_Append(*msg, ":ontology ");
01270 fipa_expression_Compose(*msg, acl->ontology);
01271 dynstring_Append(*msg, "\n");
01272 }
01273 if (acl->protocol > FIPA_PROTOCOL_NONE) {
01274 dynstring_Append(*msg, ":protocol ");
01275
01276 fipa_protocol_Compose(*msg, acl->protocol);
01277 dynstring_Append(*msg, "\n");
01278 }
01279 if (acl->conversation_id) {
01280 dynstring_Append(*msg, ":conversation-id ");
01281 fipa_expression_Compose(*msg, acl->conversation_id);
01282 dynstring_Append(*msg, "\n");
01283 }
01284 if (acl->reply_with) {
01285 dynstring_Append(*msg, ":reply-with ");
01286 fipa_expression_Compose(*msg, acl->reply_with);
01287 dynstring_Append(*msg, "\n");
01288 }
01289 if (acl->in_reply_to) {
01290 dynstring_Append(*msg, ":in-reply-to ");
01291 fipa_expression_Compose(*msg, acl->in_reply_to);
01292 dynstring_Append(*msg, "\n");
01293 }
01294 if (acl->reply_by) {
01295 dynstring_Append(*msg, ":reply-by ");
01296 fipa_DateTime_Compose(*msg, acl->reply_by);
01297 dynstring_Append(*msg, "\n");
01298 }
01299 dynstring_Append(*msg, ")");
01300 return 0;
01301 }
01302
01303 int fipa_protocol_Compose(dynstring_t* msg, enum fipa_protocol_e protocol)
01304 {
01305 switch(protocol)
01306 {
01307 case FIPA_PROTOCOL_REQUEST:
01308 dynstring_Append(msg, "fipa-request");
01309 break;
01310 case FIPA_PROTOCOL_QUERY:
01311 dynstring_Append(msg, "fipa-query");
01312 break;
01313 case FIPA_PROTOCOL_REQUEST_WHEN:
01314 dynstring_Append(msg, "fipa-request-when");
01315 break;
01316 case FIPA_PROTOCOL_CONTRACT_NET:
01317 dynstring_Append(msg, "fipa-contract-net");
01318 break;
01319 case FIPA_PROTOCOL_ITERATED_CONTRACT_NET:
01320 dynstring_Append(msg, "fipa-iterated-contract-net");
01321 break;
01322 case FIPA_PROTOCOL_ENGLISH_AUCTION:
01323 dynstring_Append(msg, "fipa-auction-english");
01324 break;
01325 case FIPA_PROTOCOL_DUTCH_AUCTION:
01326 dynstring_Append(msg, "fipa-auction-dutch");
01327 break;
01328 case FIPA_PROTOCOL_BROKERING:
01329 dynstring_Append(msg, "fipa-brokering");
01330 break;
01331 case FIPA_PROTOCOL_RECRUITING:
01332 dynstring_Append(msg, "fipa-recruiting");
01333 break;
01334 case FIPA_PROTOCOL_SUBSCRIBE:
01335 dynstring_Append(msg, "fipa-subscribe");
01336 break;
01337 case FIPA_PROTOCOL_PROPOSE:
01338 dynstring_Append(msg, "fipa-propose");
01339 break;
01340 default:
01341 return MC_ERR_PARSE;
01342 }
01343 return 0;
01344 }
01345
01346 int fipa_performative_Compose(dynstring_t* msg, enum fipa_performative_e performative)
01347 {
01348 switch(performative) {
01349 case FIPA_ACCEPT_PROPOSAL:
01350 dynstring_Append(msg, "accept-proposal ");
01351 break;
01352 case FIPA_AGREE:
01353 dynstring_Append(msg, "agree ");
01354 break;
01355 case FIPA_CANCEL:
01356 dynstring_Append(msg, "cancel ");
01357 break;
01358 case FIPA_CALL_FOR_PROPOSAL:
01359 dynstring_Append(msg, "call-for-proposal ");
01360 break;
01361 case FIPA_CONFIRM:
01362 dynstring_Append(msg, "confirm ");
01363 break;
01364 case FIPA_DISCONFIRM:
01365 dynstring_Append(msg, "disconfirm ");
01366 break;
01367 case FIPA_FAILURE:
01368 dynstring_Append(msg, "failure ");
01369 break;
01370 case FIPA_INFORM:
01371 dynstring_Append(msg, "inform ");
01372 break;
01373 case FIPA_INFORM_IF:
01374 dynstring_Append(msg, "inform-if ");
01375 break;
01376 case FIPA_INFORM_REF:
01377 dynstring_Append(msg, "inform-ref ");
01378 break;
01379 case FIPA_NOT_UNDERSTOOD:
01380 dynstring_Append(msg, "not-understood ");
01381 break;
01382 case FIPA_PROPOGATE:
01383 dynstring_Append(msg, "propogate ");
01384 break;
01385 case FIPA_PROPOSE:
01386 dynstring_Append(msg, "propose ");
01387 break;
01388 case FIPA_PROXY:
01389 dynstring_Append(msg, "proxy ");
01390 break;
01391 case FIPA_QUERY_IF:
01392 dynstring_Append(msg, "query-if ");
01393 break;
01394 case FIPA_QUERY_REF:
01395 dynstring_Append(msg, "query-ref ");
01396 break;
01397 case FIPA_REFUSE:
01398 dynstring_Append(msg, "refuse ");
01399 break;
01400 case FIPA_REJECT_PROPOSAL:
01401 dynstring_Append(msg, "reject-proposal ");
01402 break;
01403 case FIPA_REQUEST:
01404 dynstring_Append(msg, "request ");
01405 break;
01406 case FIPA_REQUEST_WHEN:
01407 dynstring_Append(msg, "request-when ");
01408 break;
01409 case FIPA_REQUEST_WHENEVER:
01410 dynstring_Append(msg, "request-whenever ");
01411 break;
01412 case FIPA_SUBSCRIBE:
01413 dynstring_Append(msg, "subscribe ");
01414 break;
01415 default:
01416 return MC_ERR_PARSE;
01417 }
01418 return 0;
01419 }
01420
01421 int fipa_url_sequence_Compose(dynstring_t* msg, fipa_url_sequence_t* urls)
01422 {
01423 int i;
01424 if(urls == NULL) return 0;
01425 if(urls->num == 0) return 0;
01426 dynstring_Append(msg, "(sequence ");
01427 for(i = 0; i < urls->num; i++) {
01428 fipa_url_Compose(msg, urls->urls[i]);
01429 }
01430 dynstring_Append(msg, ") ");
01431 return 0;
01432 }
01433
01434 int fipa_agent_identifier_set_Compose(dynstring_t* msg, fipa_agent_identifier_set_t* ids)
01435 {
01436 int i;
01437 if(ids == NULL) return 0;
01438 if(ids->num == 0) return 0;
01439 dynstring_Append(msg, "(set ");
01440 for(i = 0; i < ids->num; i++) {
01441 fipa_agent_identifier_Compose(msg, ids->fipa_agent_identifiers[i]);
01442 }
01443 dynstring_Append(msg, ") ");
01444 return 0;
01445 }
01446
01447 int fipa_agent_identifier_Compose(dynstring_t* msg, fipa_agent_identifier_t* id)
01448 {
01449 if(id == NULL) return 0;
01450 dynstring_Append(msg, "(agent-identifier ");
01451 dynstring_Append(msg, ":name ");
01452 dynstring_Append(msg, id->name);
01453 dynstring_Append(msg, " ");
01454
01455 if (id->addresses != NULL) {
01456 if (id->addresses->num != 0) {
01457 dynstring_Append(msg, ":addresses ");
01458 fipa_url_sequence_Compose(msg, id->addresses);
01459 }
01460 }
01461
01462 if (id->resolvers != NULL) {
01463 if (id->resolvers->num != 0) {
01464 dynstring_Append(msg, ":resolvers ");
01465 fipa_agent_identifier_set_Compose(msg, id->resolvers);
01466 }
01467 }
01468
01469 dynstring_Append(msg, ") ");
01470 return 0;
01471 }
01472
01473 int fipa_expression_Compose(dynstring_t* msg, fipa_expression_t* expr)
01474 {
01475 fipa_expression_t* tmp_expr;
01476 if (expr == NULL) return 0;
01477 switch(expr->type) {
01478 case FIPA_EXPR_WORD:
01479 fipa_word_Compose(msg, expr->content.word);
01480 break;
01481 case FIPA_EXPR_STRING:
01482 fipa_string_Compose(msg, expr->content.string);
01483 break;
01484 case FIPA_EXPR_NUMBER:
01485 fipa_number_Compose(msg, expr->content.number);
01486 break;
01487 case FIPA_EXPR_DATETIME:
01488 fipa_DateTime_Compose(msg, expr->content.datetime);
01489 break;
01490 case FIPA_EXPR_EXPRESSION:
01491 tmp_expr = expr->content.expression[0];
01492 while(tmp_expr != NULL) {
01493 fipa_expression_Compose(msg, tmp_expr);
01494 tmp_expr++;
01495 }
01496 break;
01497 default:
01498 return MC_ERR_PARSE;
01499 }
01500 return 0;
01501 }
01502
01503 int fipa_word_Compose(dynstring_t* msg, fipa_word_t* word)
01504 {
01505 if (word == NULL) return 0;
01506 dynstring_Append(msg, word->content);
01507 dynstring_Append(msg, " ");
01508 return 0;
01509 }
01510
01511 int fipa_string_Compose(dynstring_t* msg, fipa_string_t* string)
01512 {
01513 if (string == NULL) return 0;
01514 dynstring_Append(msg, "\"");
01515 dynstring_Append(msg, string->content);
01516 dynstring_Append(msg, "\" ");
01517 return 0;
01518 }
01519
01520 int fipa_DateTime_Compose(dynstring_t* msg, fipa_DateTime_t* date)
01521 {
01522 char buf[40];
01523
01524 if(date == NULL) return 0;
01525 dynstring_Append(msg, &date->sign);
01526 sprintf(buf, "%04d%02d%02dT%02d%02d%02d%03d",
01527 date->year,
01528 date->month,
01529 date->day,
01530 date->hour,
01531 date->minute,
01532 date->second,
01533 date->millisecond
01534 );
01535 dynstring_Append(msg, buf);
01536 return 0;
01537 }
01538
01539
01540 int fipa_url_Compose(dynstring_t* msg, fipa_url_t* url)
01541 {
01542 if(url == NULL) return 0;
01543 dynstring_Append(msg, url->str);
01544 dynstring_Append(msg, " ");
01545 return 0;
01546 }
01547
01548 int fipa_number_Compose(dynstring_t* msg, fipa_number_t* number)
01549 {
01550 if (number == NULL) return 0;
01551 dynstring_Append(msg, number->str);
01552 dynstring_Append(msg, " ");
01553 return 0;
01554 }
01555
01556 struct fipa_acl_message_s* fipa_Reply(
01557 struct fipa_acl_message_s* acl)
01558 {
01559
01560 struct fipa_acl_message_s* acl_reply;
01561
01562 acl_reply = fipa_acl_message_New();
01563
01564
01565
01566 if (acl->reply_to != NULL && acl->reply_to->num != 0) {
01567 acl_reply->receiver = fipa_agent_identifier_set_Copy(acl->reply_to);
01568 } else {
01569 acl_reply->receiver = fipa_agent_identifier_set_New();
01570 acl_reply->receiver->num = 1;
01571 acl_reply->receiver->retain_order = 0;
01572 acl_reply->receiver->fipa_agent_identifiers =
01573 (fipa_agent_identifier_t**)malloc(sizeof(fipa_agent_identifier_t*));
01574 acl_reply->receiver->fipa_agent_identifiers[0] =
01575 fipa_agent_identifier_Copy(acl->sender );
01576 acl_reply->protocol = acl->protocol;
01577 if(acl->conversation_id)
01578 acl_reply->conversation_id = fipa_expression_Copy(acl->conversation_id);
01579 }
01580
01581 return acl_reply;
01582 }
01583
01584 #undef FREEMEM