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 <mxml.h>
00042 #include <time.h>
00043 #include "include/fipa_acl_envelope.h"
00044 #include "include/mc_error.h"
00045 #include "include/mc_platform.h"
00046
00047
00048 fipa_acl_envelope_Received_t* fipa_acl_envelope_Received_New(void)
00049 {
00050 fipa_acl_envelope_Received_t* received;
00051 received = (fipa_acl_envelope_Received_t*)malloc(
00052 sizeof(fipa_acl_envelope_Received_t) );
00053 memset(received, 0, sizeof(fipa_acl_envelope_Received_t));
00054 return received;
00055 }
00056
00057 int fipa_acl_envelope_Received_Destroy(fipa_acl_envelope_Received_t* received)
00058 {
00059 if (received == NULL) return 0;
00060 fipa_url_Destroy(received->received_by);
00061 fipa_url_Destroy(received->received_from);
00062 fipa_DateTime_Destroy(received->received_date);
00063 if (received->received_id != NULL) free(received->received_id);
00064 fipa_url_Destroy(received->received_via);
00065 free(received);
00066 return 0;
00067 }
00068
00069 fipa_acl_envelope_Received_t* fipa_acl_envelope_Received_Copy(
00070 fipa_acl_envelope_Received_t* received)
00071 {
00072 fipa_acl_envelope_Received_t* copy;
00073 if (received == NULL) return NULL;
00074 copy = fipa_acl_envelope_Received_New();
00075 copy->received_by = fipa_url_Copy(received->received_by);
00076 copy->received_from = fipa_url_Copy(received->received_from);
00077 copy->received_date = fipa_DateTime_Copy(received->received_date);
00078 copy->received_id = strdup(received->received_id);
00079 copy->received_via = fipa_url_Copy(received->received_via);
00080
00081 return copy;
00082 }
00083
00084
00085 fipa_acl_Param_t* fipa_acl_Param_New(void)
00086 {
00087 fipa_acl_Param_t* param;
00088 param = (fipa_acl_Param_t*)malloc(sizeof(fipa_acl_Param_t));
00089 memset(param, 0, sizeof(fipa_acl_Param_t));
00090 return param;
00091 }
00092
00093 int fipa_acl_Param_Destroy(fipa_acl_Param_t* param)
00094 {
00095 if (param == NULL) return 0;
00096 fipa_agent_identifier_set_Destroy(param->to);
00097 fipa_agent_identifier_Destroy(param->from);
00098 if (param->comments != NULL) free(param->comments);
00099 if (param->acl_representation != NULL) free(param->acl_representation);
00100 if (param->payload_length != NULL) free(param->payload_length);
00101 if (param->payload_encoding != NULL) free (param->payload_encoding);
00102 fipa_DateTime_Destroy(param->date);
00103 fipa_agent_identifier_set_Destroy(param->intended_receiver);
00104 fipa_acl_envelope_Received_Destroy(param->received);
00105
00106 free(param);
00107 return 0;
00108 }
00109
00110 fipa_acl_Param_t* fipa_acl_Param_Copy(fipa_acl_Param_t* param)
00111 {
00112 fipa_acl_Param_t* copy;
00113 if (param == NULL) return NULL;
00114 copy = fipa_acl_Param_New();
00115 copy->to = fipa_agent_identifier_set_Copy(param->to);
00116 copy->from = fipa_agent_identifier_Copy(param->from);
00117 copy->comments = strdup(param->comments);
00118 copy->acl_representation = strdup(param->acl_representation);
00119 copy->payload_length = strdup(param->payload_length);
00120 copy->payload_encoding = strdup(param->payload_encoding);
00121 copy->date = fipa_DateTime_Copy(param->date);
00122 copy->intended_receiver = fipa_agent_identifier_set_Copy(param->intended_receiver);
00123 copy->received = fipa_acl_envelope_Received_Copy(param->received);
00124
00125 return copy;
00126 }
00127
00128
00129 fipa_acl_envelope_t* fipa_acl_envelope_New(void)
00130 {
00131 fipa_acl_envelope_t* envelope;
00132 envelope = (fipa_acl_envelope_t*)malloc(sizeof(fipa_acl_envelope_t));
00133 memset(envelope, 0, sizeof(fipa_acl_envelope_t));
00134 return envelope;
00135 }
00136
00137 int fipa_acl_envelope_Destroy(fipa_acl_envelope_t* envelope)
00138 {
00139 int i;
00140 if (envelope == NULL) return 0;
00141 if (envelope->params == NULL) return 0;
00142 for(i = 0; i < envelope->num_params; i++) {
00143 fipa_acl_Param_Destroy(envelope->params[i]);
00144 }
00145 free(envelope->params);
00146 free(envelope);
00147 return 0;
00148 }
00149
00150 fipa_acl_envelope_t* fipa_acl_envelope_Copy(fipa_acl_envelope_t* envelope)
00151 {
00152 int i;
00153 fipa_acl_envelope_t* copy;
00154 if (envelope == NULL) return NULL;
00155 copy = fipa_acl_envelope_New();
00156 copy->num_params = envelope->num_params;
00157 copy->params = (fipa_acl_Param_t**)malloc(sizeof(fipa_acl_Param_t*)*copy->num_params);
00158 for (i = 0; i < copy->num_params; i++) {
00159 copy->params[i] = fipa_acl_Param_Copy(envelope->params[i]);
00160 }
00161 return copy;
00162 }
00163
00164
00165 int fipa_envelope_Parse(
00166 struct fipa_acl_envelope_s* envelope,
00167 const char* message)
00168 {
00169
00170 int retval;
00171 mxml_node_t* root_node;
00172 root_node = mxmlLoadString(NULL, message, MXML_NO_CALLBACK);
00173 retval = fipa_envelope_HandleEnvelope(envelope, root_node);
00174 mxmlDelete(root_node);
00175 return retval;
00176 }
00177
00178 int fipa_envelope_HandleEnvelope(
00179 struct fipa_acl_envelope_s* envelope,
00180 mxml_node_t* node)
00181 {
00182
00183 mxml_node_t *envelope_node;
00184 envelope_node = mxmlFindElement(
00185 node,
00186 node,
00187 "envelope",
00188 NULL,
00189 NULL,
00190 MXML_DESCEND_FIRST
00191 );
00192 if (envelope_node == NULL) {
00193 return MC_ERR_PARSE;
00194 }
00195 return fipa_envelope_HandleParams(envelope, envelope_node);
00196 }
00197
00198 int fipa_envelope_HandleParams(
00199 struct fipa_acl_envelope_s* envelope,
00200 mxml_node_t* node)
00201 {
00202 mxml_node_t* param;
00203 int i;
00204 char buf[10];
00205
00206
00207 for (i = 1; ; i++) {
00208 sprintf(buf, "%d", i);
00209 param = mxmlFindElement(
00210 node,
00211 node,
00212 "params",
00213 "index",
00214 buf,
00215 MXML_DESCEND_FIRST);
00216 if (param == NULL) { break; }
00217 }
00218 i--;
00219 if (i == 0) {
00220
00221 return MC_ERR_PARSE;
00222 }
00223 envelope->num_params = i;
00224 envelope->params = (struct fipa_acl_Param_s**)malloc(
00225 sizeof(struct fipa_acl_Param_s*) * i);
00226 memset(envelope->params, 0, i*sizeof(struct fipa_acl_Param_s*) );
00227
00228 for (i = 1; i <= envelope->num_params; i++) {
00229 sprintf(buf, "%d", i);
00230 param = mxmlFindElement(
00231 node,
00232 node,
00233 "params",
00234 "index",
00235 buf,
00236 MXML_DESCEND_FIRST);
00237 fipa_envelope_HandleTo(envelope, param, i-1);
00238 fipa_envelope_HandleFrom(envelope, param, i-1);
00239 fipa_envelope_HandleComments(envelope, param, i-1);
00240 fipa_envelope_HandleAclRepresentation(envelope, param, i-1);
00241 fipa_envelope_HandlePayloadLength(envelope, param, i-1);
00242 fipa_envelope_HandlePayloadEncoding(envelope, param, i-1);
00243 fipa_envelope_HandleDate(envelope, param, i-1);
00244 fipa_envelope_HandleIntendedReceiver(envelope, param, i-1);
00245 fipa_envelope_HandleReceived(envelope, param, i-1);
00246 }
00247 return 0;
00248 }
00249
00250 int fipa_envelope_HandleTo(
00251 struct fipa_acl_envelope_s* envelope,
00252 mxml_node_t* param_node,
00253 int cur_param)
00254 {
00255 int i;
00256 mxml_node_t* to_node;
00257 mxml_node_t* aid_node;
00258
00259 to_node = mxmlFindElement(
00260 param_node,
00261 param_node,
00262 "to",
00263 NULL, NULL,
00264 MXML_DESCEND_FIRST );
00265 if (to_node == NULL) return MC_ERR_PARSE;
00266
00267
00268
00269 aid_node = mxmlFindElement(
00270 to_node,
00271 to_node,
00272 "agent-identifier",
00273 NULL, NULL,
00274 MXML_DESCEND_FIRST );
00275 if (aid_node == NULL) return MC_ERR_PARSE;
00276
00277 for(
00278 i = 1;
00279 (aid_node = mxmlFindElement(
00280 aid_node,
00281 to_node,
00282 "agent-identifier",
00283 NULL,NULL,
00284 MXML_NO_DESCEND )) != NULL;
00285 i++
00286 );
00287
00288 envelope->params[cur_param] = fipa_acl_Param_New();
00289 envelope->params[cur_param]->to = fipa_agent_identifier_set_New();
00290 envelope->params[cur_param]->to->num = i;
00291 envelope->params[cur_param]->to->retain_order = 0;
00292 envelope->params[cur_param]->to->fipa_agent_identifiers =
00293 (struct fipa_agent_identifier_s**) malloc(
00294 sizeof(struct fipa_agent_identifier_s*) *
00295 envelope->params[cur_param]->to->num );
00296
00297
00298 aid_node = mxmlFindElement(
00299 to_node,
00300 to_node,
00301 "agent-identifier",
00302 NULL, NULL,
00303 MXML_DESCEND_FIRST );
00304 if (aid_node == NULL) return MC_ERR_PARSE;
00305
00306 fipa_envelope_ParseAgentIdentifier(
00307 &(envelope->params[cur_param]->to->fipa_agent_identifiers[0]),
00308 aid_node );
00309
00310 for(
00311 i = 1;
00312 (aid_node = mxmlFindElement(
00313 aid_node,
00314 to_node,
00315 "agent-identifier",
00316 NULL,NULL,
00317 MXML_NO_DESCEND ) );
00318 i++
00319 )
00320 {
00321 fipa_envelope_ParseAgentIdentifier(
00322 &(envelope->params[cur_param]->to->fipa_agent_identifiers[i]),
00323 aid_node );
00324 }
00325 return 0;
00326 }
00327
00328 int fipa_envelope_HandleFrom(
00329 struct fipa_acl_envelope_s* envelope,
00330 mxml_node_t* param_node,
00331 int cur_param)
00332 {
00333 mxml_node_t* from_node;
00334 mxml_node_t* aid_node;
00335
00336 from_node = mxmlFindElement(
00337 param_node,
00338 param_node,
00339 "from",
00340 NULL, NULL,
00341 MXML_DESCEND_FIRST);
00342 if (from_node == NULL) return 0;
00343
00344 aid_node = mxmlFindElement(
00345 from_node,
00346 from_node,
00347 "agent-identifier",
00348 NULL, NULL,
00349 MXML_DESCEND_FIRST);
00350 if (aid_node == NULL) return MC_ERR_PARSE;
00351
00352 return fipa_envelope_ParseAgentIdentifier(
00353 &envelope->params[cur_param]->from,
00354 aid_node);
00355 }
00356
00357 int fipa_envelope_HandleComments(
00358 struct fipa_acl_envelope_s* envelope,
00359 mxml_node_t* param_node,
00360 int cur_param)
00361 {
00362 mxml_node_t* comments_node;
00363
00364 comments_node = mxmlFindElement(
00365 param_node,
00366 param_node,
00367 "comments",
00368 NULL, NULL,
00369 MXML_DESCEND_FIRST);
00370 if (comments_node == NULL) return 0;
00371
00372
00373 if (comments_node->child->type != MXML_TEXT) return MC_ERR_PARSE;
00374 envelope->params[cur_param]->comments = (char*)malloc(
00375 sizeof(char) * (strlen(comments_node->child->value.text.string)+1));
00376 strcpy(envelope->params[cur_param]->comments,
00377 comments_node->child->value.text.string);
00378 return 0;
00379 }
00380
00381 int fipa_envelope_HandleAclRepresentation(
00382 struct fipa_acl_envelope_s* envelope,
00383 mxml_node_t* param_node,
00384 int cur_param)
00385 {
00386 mxml_node_t* acl_representation_node;
00387
00388 acl_representation_node = mxmlFindElement(
00389 param_node,
00390 param_node,
00391 "acl-representation",
00392 NULL, NULL,
00393 MXML_DESCEND_FIRST);
00394 if (acl_representation_node == NULL) return 0;
00395
00396
00397 if (acl_representation_node->child->type != MXML_TEXT) return MC_ERR_PARSE;
00398 envelope->params[cur_param]->acl_representation = (char*)malloc(
00399 sizeof(char) * (strlen(acl_representation_node->child->value.text.string)+1));
00400 strcpy(envelope->params[cur_param]->acl_representation,
00401 acl_representation_node->child->value.text.string);
00402 return 0;
00403 }
00404
00405 int fipa_envelope_HandlePayloadLength(
00406 struct fipa_acl_envelope_s* envelope,
00407 mxml_node_t* param_node,
00408 int cur_param)
00409 {
00410 mxml_node_t* payload_length_node;
00411
00412 payload_length_node = mxmlFindElement(
00413 param_node,
00414 param_node,
00415 "payload-length",
00416 NULL, NULL,
00417 MXML_DESCEND_FIRST);
00418 if (payload_length_node == NULL) return 0;
00419
00420 if (payload_length_node->child->type != MXML_TEXT) return MC_ERR_PARSE;
00421 envelope->params[cur_param]->payload_length = (char*)malloc(
00422 sizeof(char) * (strlen(payload_length_node->child->value.text.string)+1) );
00423 strcpy( envelope->params[cur_param]->payload_length,
00424 payload_length_node->child->value.text.string );
00425 return 0;
00426 }
00427
00428 int fipa_envelope_HandlePayloadEncoding(
00429 struct fipa_acl_envelope_s* envelope,
00430 mxml_node_t* param_node,
00431 int cur_param)
00432 {
00433 mxml_node_t* payload_encoding_node;
00434
00435 payload_encoding_node = mxmlFindElement(
00436 param_node,
00437 param_node,
00438 "payload-encoding",
00439 NULL, NULL,
00440 MXML_DESCEND_FIRST);
00441 if (payload_encoding_node == NULL) return 0;
00442
00443 if (payload_encoding_node->child->type != MXML_TEXT) return MC_ERR_PARSE;
00444
00445 envelope->params[cur_param]->payload_encoding = (char*)malloc(
00446 sizeof(char) * (strlen(payload_encoding_node->child->value.text.string)+1) );
00447 strcpy( envelope->params[cur_param]->payload_encoding,
00448 payload_encoding_node->child->value.text.string );
00449 return 0;
00450 }
00451
00452 int fipa_envelope_HandleDate(
00453 struct fipa_acl_envelope_s* envelope,
00454 mxml_node_t* param_node,
00455 int cur_param)
00456 {
00457 char date_string[25];
00458 mxml_node_t* date_node;
00459 fipa_message_string_t message_string;
00460
00461 date_node = mxmlFindElement(
00462 param_node,
00463 param_node,
00464 "date",
00465 NULL, NULL,
00466 MXML_DESCEND_FIRST);
00467 if (date_node == NULL) return 0;
00468
00469 if (date_node->child->type != MXML_TEXT) return MC_ERR_PARSE;
00470 if ( strlen(date_node->child->value.text.string) > 22) return MC_ERR_PARSE;
00471
00472 strcpy(date_string, date_node->child->value.text.string);
00473
00474 message_string.message = date_string;
00475 message_string.parse = date_string;
00476
00477 return fipa_datetime_Parse(
00478 &(envelope->params[cur_param]->date),
00479 &message_string);
00480 }
00481
00482 int fipa_envelope_HandleIntendedReceiver(
00483 struct fipa_acl_envelope_s* envelope,
00484 mxml_node_t* param_node,
00485 int cur_param)
00486 {
00487 int i;
00488 mxml_node_t* intended_receiver_node;
00489 mxml_node_t* aid_node;
00490
00491 intended_receiver_node = mxmlFindElement(
00492 param_node,
00493 param_node,
00494 "intended_receiver",
00495 NULL, NULL,
00496 MXML_DESCEND_FIRST);
00497 if (intended_receiver_node == NULL) {
00498 envelope->params[cur_param]->intended_receiver = NULL;
00499 return 0;
00500 }
00501 envelope->params[cur_param]->intended_receiver = fipa_agent_identifier_set_New();
00502
00503
00504 aid_node = mxmlFindElement(
00505 intended_receiver_node,
00506 intended_receiver_node,
00507 "agent-identifier",
00508 NULL, NULL,
00509 MXML_DESCEND_FIRST);
00510 if (aid_node == NULL) return MC_ERR_PARSE;
00511 for(
00512 i = 1;
00513 ( aid_node = mxmlFindElement(
00514 aid_node,
00515 intended_receiver_node,
00516 "agent-identifier",
00517 NULL,NULL,
00518 MXML_NO_DESCEND)) ;
00519 i++
00520 );
00521
00522 envelope->params[cur_param]->intended_receiver->num = i;
00523 envelope->params[cur_param]->intended_receiver->fipa_agent_identifiers =
00524 (struct fipa_agent_identifier_s**)malloc(
00525 sizeof(struct fipa_agent_identifier_s*) * i);
00526 envelope->params[cur_param]->intended_receiver->retain_order = 0;
00527
00528
00529 aid_node = mxmlFindElement(
00530 intended_receiver_node,
00531 intended_receiver_node,
00532 "agent-identifier",
00533 NULL, NULL,
00534 MXML_DESCEND_FIRST);
00535 if (aid_node == NULL) return MC_ERR_PARSE;
00536 fipa_envelope_ParseAgentIdentifier(
00537 &(envelope->params[cur_param]->intended_receiver->
00538 fipa_agent_identifiers[0]),
00539 aid_node );
00540
00541 for(
00542 i = 1;
00543 (aid_node = mxmlFindElement(
00544 aid_node,
00545 intended_receiver_node,
00546 "agent-identifier",
00547 NULL,NULL,
00548 MXML_NO_DESCEND)) ;
00549 i++
00550 )
00551 {
00552 fipa_envelope_ParseAgentIdentifier(
00553 &(envelope->params[cur_param]->intended_receiver->
00554 fipa_agent_identifiers[i]),
00555 aid_node );
00556 }
00557 return 0;
00558 }
00559
00560 int fipa_envelope_HandleReceived(
00561 struct fipa_acl_envelope_s* envelope,
00562 mxml_node_t* param_node,
00563 int cur_param)
00564 {
00565 int err;
00566 mxml_node_t* received_node;
00567
00568 mxml_node_t* node;
00569 const char* attr;
00570
00571 fipa_message_string_t message_string;
00572
00573 char date_string[25];
00574
00575 received_node = mxmlFindElement(
00576 param_node,
00577 param_node,
00578 "received",
00579 NULL, NULL,
00580 MXML_DESCEND_FIRST);
00581
00582 if (received_node == NULL) return 0;
00583 envelope->params[cur_param]->received = fipa_acl_envelope_Received_New();
00584
00585
00586
00587
00588
00589
00590
00591
00592 node = mxmlFindElement(
00593 received_node,
00594 received_node,
00595 "received-by",
00596 NULL, NULL,
00597 MXML_DESCEND_FIRST);
00598 if (node == NULL) return MC_ERR_PARSE;
00599 attr = mxmlElementGetAttr(node, "value");
00600 envelope->params[cur_param]->received->received_by = fipa_url_New();
00601 envelope->params[cur_param]->received->received_by->str =
00602 (char*)malloc(sizeof(char) * (strlen(attr)+1));
00603 strcpy(envelope->params[cur_param]->received->received_by->str,
00604 attr);
00605
00606
00607 node = mxmlFindElement(
00608 received_node,
00609 received_node,
00610 "received-date",
00611 NULL, NULL,
00612 MXML_DESCEND_FIRST);
00613 if (node == NULL) return MC_ERR_PARSE;
00614 attr = mxmlElementGetAttr(node, "value");
00615 if (attr == NULL) return MC_ERR_PARSE;
00616 if ( strlen(attr) > 22 ) return MC_ERR_PARSE;
00617 strcpy(date_string, attr);
00618 message_string.message = date_string;
00619 message_string.parse = date_string;
00620
00621 err = fipa_datetime_Parse(
00622 &(envelope->params[cur_param]->received->received_date),
00623 &message_string);
00624 if (err) return err;
00625
00626
00627 node = mxmlFindElement(
00628 received_node,
00629 received_node,
00630 "received-from",
00631 NULL, NULL,
00632 MXML_DESCEND_FIRST);
00633 if(node == NULL) {
00634 envelope->params[cur_param]->received->received_from = NULL;
00635 } else {
00636 attr = mxmlElementGetAttr(node, "value");
00637 if (attr == NULL) return MC_ERR_PARSE;
00638 envelope->params[cur_param]->received->received_from =
00639 (struct fipa_url_s*)malloc(sizeof(struct fipa_url_s));
00640 envelope->params[cur_param]->received->received_from->str =
00641 (char*)malloc(sizeof(char)*(strlen(attr)+1));
00642 strcpy(
00643 envelope->params[cur_param]->received->received_from->str,
00644 attr );
00645 }
00646
00647
00648 node = mxmlFindElement(
00649 received_node,
00650 received_node,
00651 "received-id",
00652 NULL, NULL,
00653 MXML_DESCEND_FIRST);
00654 if(node == NULL) {
00655 envelope->params[cur_param]->received->received_id = NULL;
00656 } else {
00657 attr = mxmlElementGetAttr(node, "value");
00658 if (attr == NULL) return MC_ERR_PARSE;
00659 envelope->params[cur_param]->received->received_id =
00660 (char*)malloc(sizeof(char) * (strlen(attr)+1));
00661 strcpy(envelope->params[cur_param]->received->received_id,
00662 attr);
00663 }
00664
00665
00666 node = mxmlFindElement(
00667 received_node,
00668 received_node,
00669 "received-via",
00670 NULL, NULL,
00671 MXML_DESCEND_FIRST);
00672 if(node == NULL) {
00673 envelope->params[cur_param]->received->received_via = NULL;
00674 } else {
00675 attr = mxmlElementGetAttr(node, "value");
00676 if (attr == NULL) return MC_ERR_PARSE;
00677 envelope->params[cur_param]->received->received_via =
00678 (struct fipa_url_s*)malloc(sizeof(struct fipa_url_s));
00679 envelope->params[cur_param]->received->received_via->str =
00680 (char*)malloc(sizeof(char) * (strlen(attr)+1));
00681 strcpy(envelope->params[cur_param]->received->received_via->str,
00682 attr);
00683 }
00684 return 0;
00685 }
00686
00687 int fipa_envelope_ParseAgentIdentifier(
00688 struct fipa_agent_identifier_s** aid,
00689 mxml_node_t* agent_identifier_node
00690 )
00691 {
00692 int err;
00693 mxml_node_t* name_node;
00694 mxml_node_t* addresses_node;
00695 mxml_node_t* resolvers_node;
00696
00697 name_node = mxmlFindElement(
00698 agent_identifier_node,
00699 agent_identifier_node,
00700 "name",
00701 NULL, NULL,
00702 MXML_DESCEND_FIRST);
00703
00704 if(name_node->child->type != MXML_TEXT) {
00705 return MC_ERR_PARSE;
00706 } else {
00707 *aid = fipa_agent_identifier_New();
00708 (*aid)->name = (char*)malloc(sizeof(char) *
00709 (strlen(name_node->child->value.text.string)+1) );
00710 strcpy((*aid)->name,
00711 name_node->child->value.text.string );
00712 }
00713
00714 addresses_node = mxmlFindElement(
00715 agent_identifier_node,
00716 agent_identifier_node,
00717 "addresses",
00718 NULL, NULL,
00719 MXML_DESCEND_FIRST);
00720 if (addresses_node != NULL) {
00721 err = fipa_envelope_ParseAddresses(*aid, addresses_node);
00722 }
00723 if (err) return err;
00724 resolvers_node = mxmlFindElement(
00725 agent_identifier_node,
00726 agent_identifier_node,
00727 "resolvers",
00728 NULL, NULL,
00729 MXML_DESCEND_FIRST);
00730 if (resolvers_node != NULL) {
00731 err = fipa_envelope_ParseResolvers(*aid, resolvers_node);
00732 }
00733 if (err) return err;
00734 return 0;
00735 }
00736
00737 int fipa_envelope_ParseAddresses(
00738 struct fipa_agent_identifier_s* aid,
00739 mxml_node_t* addresses_node)
00740 {
00741 int i;
00742 mxml_node_t* url_node;
00743
00744
00745 url_node = mxmlFindElement(
00746 addresses_node,
00747 addresses_node,
00748 "url",
00749 NULL, NULL,
00750 MXML_DESCEND_FIRST);
00751 if (url_node == NULL) {
00752 return MC_ERR_PARSE;
00753 }
00754 for (
00755 i = 1;
00756 (url_node = mxmlFindElement(
00757 url_node,
00758 addresses_node,
00759 "url",
00760 NULL, NULL,
00761 MXML_NO_DESCEND)) ;
00762 i++
00763 );
00764 aid->addresses = (fipa_url_sequence_t*)malloc(sizeof(fipa_url_sequence_t));
00765 aid->addresses->num = i;
00766 aid->addresses->urls = (struct fipa_url_s**)malloc(
00767 sizeof(struct fipa_url_s*) * aid->addresses->num );
00768
00769 url_node = mxmlFindElement(
00770 addresses_node,
00771 addresses_node,
00772 "url",
00773 NULL, NULL,
00774 MXML_DESCEND_FIRST);
00775 if (url_node->child->type != MXML_TEXT) {
00776 return MC_ERR_PARSE;
00777 }
00778 aid->addresses->urls[0] = fipa_url_New();
00779 aid->addresses->urls[0]->str = (char*)malloc(
00780 sizeof(char) * (strlen(url_node->child->value.text.string)+1));
00781 strcpy(aid->addresses->urls[0]->str, url_node->child->value.text.string);
00782 for (
00783 i = 1;
00784 (url_node = mxmlFindElement(
00785 url_node,
00786 addresses_node,
00787 "url",
00788 NULL, NULL,
00789 MXML_NO_DESCEND)) ;
00790 i++
00791 )
00792 {
00793 if (url_node->child->type != MXML_TEXT) {
00794 return MC_ERR_PARSE;
00795 }
00796 aid->addresses->urls[i] = fipa_url_New();
00797 aid->addresses->urls[i]->str = (char*)malloc(
00798 sizeof(char) * (strlen(url_node->child->value.text.string)+1));
00799 strcpy(aid->addresses->urls[i]->str, url_node->child->value.text.string);
00800 }
00801 return 0;
00802 }
00803
00804 int fipa_envelope_ParseResolvers(
00805 struct fipa_agent_identifier_s* aid,
00806 mxml_node_t* resolvers_node)
00807 {
00808 int i;
00809 mxml_node_t* agent_id_node;
00810
00811
00812
00813 agent_id_node = mxmlFindElement(
00814 resolvers_node,
00815 resolvers_node,
00816 "agent-identifier",
00817 NULL, NULL,
00818 MXML_DESCEND_FIRST);
00819 if (agent_id_node == NULL) {
00820 return MC_ERR_PARSE;
00821 }
00822 for(
00823 i = 1;
00824 (agent_id_node = mxmlFindElement(
00825 agent_id_node,
00826 resolvers_node,
00827 "agent-identifier",
00828 NULL, NULL,
00829 MXML_NO_DESCEND));
00830 i++);
00831
00832
00833 aid->resolvers = (fipa_agent_identifier_set_t*)malloc(sizeof(fipa_agent_identifier_set_t));
00834 aid->resolvers->num = i;
00835 aid->resolvers->retain_order = 0;
00836 aid->resolvers->fipa_agent_identifiers = (struct fipa_agent_identifier_s**)malloc(
00837 sizeof(struct fipa_agent_identifier_s*) * aid->resolvers->num);
00838 agent_id_node = mxmlFindElement(
00839 resolvers_node,
00840 resolvers_node,
00841 "agent-identifier",
00842 NULL, NULL,
00843 MXML_DESCEND_FIRST);
00844 if (agent_id_node == NULL) {
00845 return MC_ERR_PARSE;
00846 }
00847 fipa_envelope_ParseAgentIdentifier(
00848 &(aid->resolvers->fipa_agent_identifiers[0]), agent_id_node);
00849 for(
00850 i = 1;
00851 (agent_id_node = mxmlFindElement(
00852 agent_id_node,
00853 resolvers_node,
00854 "agent-identifier",
00855 NULL, NULL,
00856 MXML_NO_DESCEND));
00857 i++
00858 )
00859 {
00860 fipa_envelope_ParseAgentIdentifier(
00861 &(aid->resolvers->fipa_agent_identifiers[i]), agent_id_node);
00862 }
00863 return 0;
00864 }
00865
00866
00867
00868
00869 char* fipa_envelope_Compose(fipa_acl_message_t* fipa_acl)
00870 {
00871 char* retval;
00872 mxml_node_t* node;
00873 node = mxmlLoadString
00874 (
00875 NULL,
00876 "<?xml version=\"1.0\"?>",
00877 MXML_NO_CALLBACK
00878 );
00879 mxmlAdd
00880 (
00881 node,
00882 MXML_ADD_AFTER,
00883 MXML_ADD_TO_PARENT,
00884 fipa_envelope_Compose__envelope(fipa_acl)
00885 );
00886 retval = mxmlSaveAllocString(
00887 node,
00888 MXML_NO_CALLBACK);
00889 mxmlDelete(node);
00890 return retval;
00891 }
00892
00893 mxml_node_t*
00894 fipa_envelope_Compose__envelope(fipa_acl_message_t* fipa_acl)
00895 {
00896 mxml_node_t* node;
00897
00898 node = mxmlNewElement(
00899 NULL,
00900 "envelope"
00901 );
00902
00903 mxmlAdd
00904 (
00905 node,
00906 MXML_ADD_AFTER,
00907 MXML_ADD_TO_PARENT,
00908 fipa_envelope_Compose__params(fipa_acl)
00909 );
00910 return node;
00911 }
00912
00913 mxml_node_t*
00914 fipa_envelope_Compose__params(fipa_acl_message_t* fipa_acl)
00915 {
00916 mxml_node_t* node;
00917 node = mxmlNewElement(
00918 NULL,
00919 "params"
00920 );
00921 mxmlElementSetAttr(node, "index", "1");
00922 mxmlAdd
00923 (
00924 node,
00925 MXML_ADD_AFTER,
00926 MXML_ADD_TO_PARENT,
00927 fipa_envelope_Compose__to(fipa_acl)
00928 );
00929 mxmlAdd
00930 (
00931 node,
00932 MXML_ADD_AFTER,
00933 MXML_ADD_TO_PARENT,
00934 fipa_envelope_Compose__from(fipa_acl)
00935 );
00936 mxmlAdd
00937 (
00938 node,
00939 MXML_ADD_AFTER,
00940 MXML_ADD_TO_PARENT,
00941 fipa_envelope_Compose__acl_representation(fipa_acl)
00942 );
00943 mxmlAdd
00944 (
00945 node,
00946 MXML_ADD_AFTER,
00947 MXML_ADD_TO_PARENT,
00948 fipa_envelope_Compose__payload_encoding(fipa_acl)
00949 );
00950 mxmlAdd
00951 (
00952 node,
00953 MXML_ADD_AFTER,
00954 MXML_ADD_TO_PARENT,
00955 fipa_envelope_Compose__date(fipa_acl)
00956 );
00957 mxmlAdd
00958 (
00959 node,
00960 MXML_ADD_AFTER,
00961 MXML_ADD_TO_PARENT,
00962 fipa_envelope_Compose__intended_receiver(fipa_acl)
00963 );
00964
00965
00966
00967
00968
00969
00970
00971 return node;
00972 }
00973
00974 mxml_node_t*
00975 fipa_envelope_Compose__to(fipa_acl_message_t* fipa_acl)
00976 {
00977 int i;
00978 int j;
00979 mxml_node_t* node;
00980 mxml_node_t* agent_identifier_node;
00981 mxml_node_t* name_node;
00982 mxml_node_t* addresses_node;
00983 mxml_node_t* url_node;
00984 node = mxmlNewElement(
00985 NULL,
00986 "to");
00987 for(i = 0; i < fipa_acl->receiver->num; i++) {
00988 agent_identifier_node = mxmlNewElement(
00989 node,
00990 "agent-identifier");
00991 name_node = mxmlNewElement(
00992 agent_identifier_node,
00993 "name");
00994 mxmlNewText(
00995 name_node,
00996 0,
00997 fipa_acl->receiver->fipa_agent_identifiers[i]->name);
00998
00999 addresses_node = mxmlNewElement(
01000 agent_identifier_node,
01001 "addresses");
01002 for(
01003 j = 0;
01004 j < fipa_acl->receiver->fipa_agent_identifiers[i]->addresses->num;
01005 j++
01006 )
01007 {
01008 url_node = mxmlNewElement(
01009 addresses_node,
01010 "url" );
01011 mxmlNewText(
01012 url_node,
01013 0,
01014 fipa_acl->receiver->fipa_agent_identifiers[i]->addresses->urls[j]->str );
01015 }
01016 }
01017 return node;
01018 }
01019
01020 mxml_node_t*
01021 fipa_envelope_Compose__from(fipa_acl_message_t* fipa_acl)
01022 {
01023 int i;
01024 extern mc_platform_p g_mc_platform;
01025 mxml_node_t* from_node;
01026 mxml_node_t* agent_identifier_node;
01027 mxml_node_t* name_node;
01028 mxml_node_t* addresses_node;
01029 mxml_node_t* url_node;
01030 dynstring_t* hostname;
01031 char buf[20];
01032
01033 from_node = mxmlNewElement(
01034 NULL,
01035 "from" );
01036 agent_identifier_node = mxmlNewElement(
01037 from_node,
01038 "agent-identifier" );
01039 name_node = mxmlNewElement(
01040 agent_identifier_node,
01041 "name" );
01042 mxmlNewText(
01043 name_node,
01044 0,
01045 fipa_acl->sender->name );
01046
01047
01048 addresses_node = mxmlNewElement(
01049 agent_identifier_node,
01050 "addresses");
01051 if (fipa_acl->sender->addresses != NULL) {
01052 if (fipa_acl->sender->addresses->num != 0) {
01053 for(i = 0; i < fipa_acl->sender->addresses->num; i++) {
01054 url_node = mxmlNewElement(
01055 addresses_node,
01056 "url" );
01057
01058 mxmlNewText(
01059 url_node,
01060 0,
01061 fipa_acl->sender->addresses->urls[i]->str );
01062
01063
01064
01065
01066
01067
01068 }
01069 }
01070 } else {
01071 hostname = dynstring_New();
01072 url_node = mxmlNewElement(
01073 addresses_node,
01074 "url" );
01075 dynstring_Append(hostname, "http://");
01076 dynstring_Append(hostname, g_mc_platform->hostname);
01077 sprintf(buf, ":%d", g_mc_platform->port);
01078 dynstring_Append(hostname, buf);
01079 mxmlNewText(
01080 url_node,
01081 0,
01082 hostname->message );
01083 dynstring_Destroy(hostname);
01084 }
01085 return from_node;
01086 }
01087
01088 mxml_node_t*
01089 fipa_envelope_Compose__acl_representation(fipa_acl_message_t* fipa_acl)
01090 {
01091 mxml_node_t* node;
01092 node = mxmlNewElement(
01093 NULL,
01094 "acl-representation");
01095 mxmlNewText(
01096 node,
01097 0,
01098 "fipa.acl.rep.string.std");
01099 return node;
01100 }
01101
01102 mxml_node_t*
01103 fipa_envelope_Compose__payload_encoding(fipa_acl_message_t* fipa_acl)
01104 {
01105 mxml_node_t* node;
01106 node = mxmlNewElement(
01107 NULL,
01108 "payload-encoding");
01109 mxmlNewText(
01110 node,
01111 0,
01112 "US-ASCII" );
01113 return node;
01114 }
01115
01116 mxml_node_t*
01117 fipa_envelope_Compose__date(fipa_acl_message_t* fipa_acl)
01118 {
01119 time_t t;
01120 struct tm* mytm;
01121 char buf[30];
01122 mxml_node_t* node;
01123 t = time(NULL);
01124 mytm = localtime(&t);
01125 if (mytm == NULL) {
01126 fprintf(stderr, "time failure. %s:%d\n", __FILE__, __LINE__);
01127 return NULL;
01128 }
01129 if (strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S000", mytm) == 0) {
01130 fprintf(stderr, "strftime failure. %s:%d\n", __FILE__, __LINE__);
01131 return NULL;
01132 }
01133
01134 node = mxmlNewElement(
01135 NULL,
01136 "date" );
01137 mxmlNewText(
01138 node,
01139 0,
01140 buf );
01141
01142 return node;
01143 }
01144
01145 mxml_node_t*
01146 fipa_envelope_Compose__intended_receiver(fipa_acl_message_t* fipa_acl)
01147 {
01148 int i;
01149 int j;
01150 mxml_node_t* node;
01151 mxml_node_t* agent_identifier_node;
01152 mxml_node_t* name_node;
01153 mxml_node_t* addresses_node;
01154 mxml_node_t* url_node;
01155 node = mxmlNewElement(
01156 NULL,
01157 "intended-receiver");
01158 i = 0;
01159 agent_identifier_node = mxmlNewElement(
01160 node,
01161 "agent-identifier");
01162 name_node = mxmlNewElement(
01163 agent_identifier_node,
01164 "name");
01165 mxmlNewText(
01166 name_node,
01167 0,
01168 fipa_acl->receiver->fipa_agent_identifiers[i]->name);
01169
01170 addresses_node = mxmlNewElement(
01171 agent_identifier_node,
01172 "addresses");
01173 for(
01174 j = 0;
01175 j < fipa_acl->receiver->fipa_agent_identifiers[i]->addresses->num;
01176 j++
01177 )
01178 {
01179 url_node = mxmlNewElement(
01180 addresses_node,
01181 "url" );
01182 mxmlNewText(
01183 url_node,
01184 0,
01185 fipa_acl->receiver->fipa_agent_identifiers[i]->addresses->urls[j]->str );
01186 }
01187 return node;
01188 }