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