X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d753340c54c230f9ab7b6e5c5ed5ac47f7421ad5..0c2ff3268deebf7a4ccc059a3142ca129353f9e2:/examples/gras/p2p/can/can.c diff --git a/examples/gras/p2p/can/can.c b/examples/gras/p2p/can/can.c index 372588dd18..7f5f0b5376 100644 --- a/examples/gras/p2p/can/can.c +++ b/examples/gras/p2p/can/can.c @@ -1,16 +1,18 @@ -////////////////////////////////////////////////////// +/*////////////////////////////////////////////////////*/ // Peer-To-Peer CAN simulator 050406 by Dytto ESIAL // ////////////////////////////////////////////////////// - +#include #include "xbt/sysdep.h" #include "gras.h" -#include "can_tests.c" // test class & header containing the typedef struct of a node // include type.h must be OFF. +#include "can_tests.c" //#include "types.h" // header alone containing the typedef struct of a node // include can_tests.c must be OFF. //XBT_LOG_NEW_DEFAULT_CATEGORY(can,"Messages specific to this example"); // include can_tests.c must be OFF. +//extern char *_gras_this_type_symbol_does_not_exist__s_nuke; +int node_nuke_handler(gras_msg_cb_ctx_t ctx,void *payload_data); // struct of a "get_successor" message, when a node look after the area in which he want to be. GRAS_DEFINE_TYPE(s_get_suc, @@ -43,6 +45,8 @@ GRAS_DEFINE_TYPE(s_rep_suc, ); typedef struct s_rep_suc rep_suc_t; +int node(int argc,char **argv); + // registering messages types static void register_messages(){ gras_msgtype_declare("can_get_suc",gras_datadesc_by_symbol(s_get_suc)); @@ -53,7 +57,7 @@ static void register_messages(){ // a forwarding function for a "get_suc" message. static void forward_get_suc(get_suc_t msg, char host[1024], int port){ - gras_socket_t temp_sock; + gras_socket_t temp_sock=NULL; xbt_ex_t e; // the error variable used in TRY.. CATCH tokens. //INFO2("Transmiting message to %s:%d",host,port); TRY{ @@ -62,7 +66,7 @@ static void forward_get_suc(get_suc_t msg, char host[1024], int port){ RETHROW0("Unable to connect!: %s"); } TRY{ - gras_msg_send(temp_sock,gras_msgtype_by_name("can_get_suc"),&msg); + gras_msg_send(temp_sock,"can_get_suc",&msg); }CATCH(e){ RETHROW0("Unable to send!: %s"); } @@ -71,11 +75,12 @@ static void forward_get_suc(get_suc_t msg, char host[1024], int port){ } // the handling function of a "get_suc" message (what do a node when he receive a "get_suc" message. -static void node_get_suc_handler(gras_socket_t expeditor,void *payload_data){ - get_suc_t *incoming=(get_suc_t*)payload_data; +static int node_get_suc_handler(gras_msg_cb_ctx_t ctx,void *payload_data){ + gras_socket_t expeditor=gras_msg_cb_ctx_from(ctx); + get_suc_t *incoming=(get_suc_t*)payload_data; xbt_ex_t e; // the error variable used in TRY.. CATCH tokens. node_data_t *globals=(node_data_t*)gras_userdata_get(); - + gras_socket_t temp_sock=NULL; INFO3("Received a get_successor message from %s for (%d;%d)",gras_socket_peer_name(expeditor),incoming->xId,incoming->yId); //INFO4("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2); if(incoming->xIdx1) // test if the message must be forwarded to a neighbour. @@ -173,14 +178,14 @@ static void node_get_suc_handler(gras_socket_t expeditor,void *payload_data){ } if(validate==1){ // the area for the new node has been defined, then send theses informations to the new node. INFO2("Sending environment informations to node %s:%d",incoming->host,incoming->port); - gras_socket_t temp_sock; + TRY{ temp_sock=gras_socket_client(incoming->host,incoming->port); }CATCH(e){ RETHROW0("Unable to connect to the node wich has requested for an area!: %s"); } TRY{ - gras_msg_send(temp_sock,gras_msgtype_by_name("can_rep_suc"),&outgoing); + gras_msg_send(temp_sock,"can_rep_suc",&outgoing); INFO0("Environment informations sent!"); }CATCH(e){ RETHROW2("%s:Timeout sending environment informations to %s: %s",globals->host,gras_socket_peer_name(expeditor)); @@ -198,6 +203,7 @@ static void node_get_suc_handler(gras_socket_t expeditor,void *payload_data){ INFO4("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2); //INFO0("Closing node, all has been done!"); } + return 0; } @@ -207,6 +213,12 @@ static void node_get_suc_handler(gras_socket_t expeditor,void *payload_data){ int node(int argc,char **argv){ node_data_t *globals=NULL; xbt_ex_t e; // the error variable used in TRY.. CATCH tokens. + gras_socket_t temp_sock=NULL; + + rep_suc_t rep_suc_msg; + + get_suc_t get_suc_msg; // building the "get_suc" message. + gras_socket_t temp_sock2=NULL; INFO0("Starting"); @@ -232,31 +244,31 @@ int node(int argc,char **argv){ globals->y2=1000; }else{ // asking for an area. INFO1("Contacting %s so as to request for an area",argv[4]); - gras_socket_t temp_sock; + TRY{ temp_sock=gras_socket_client(argv[4],atoi(argv[5])); }CATCH(e){ RETHROW0("Unable to connect known host to request for an area!: %s"); } - get_suc_t get_suc_msg; // building the "get_suc" message. + get_suc_msg.xId=globals->xId; get_suc_msg.yId=globals->yId; strcpy(get_suc_msg.host,globals->host); get_suc_msg.port=globals->port; TRY{ // asking. - gras_msg_send(temp_sock,gras_msgtype_by_name("can_get_suc"),&get_suc_msg); + gras_msg_send(temp_sock,"can_get_suc",&get_suc_msg); }CATCH(e){ gras_socket_close(temp_sock); RETHROW0("Unable to contact known host to get an area!: %s"); } gras_socket_close(temp_sock); - rep_suc_t rep_suc_msg; - gras_socket_t temp_sock2=NULL; + + TRY{ // waiting for a reply. INFO0("Waiting for reply!"); - gras_msg_wait(6000,gras_msgtype_by_name("can_rep_suc"),&temp_sock2,&rep_suc_msg); + gras_msg_wait(6000,"can_rep_suc",&temp_sock2,&rep_suc_msg); }CATCH(e){ RETHROW1("%s: Error waiting for an area:%s",globals->host); } @@ -281,8 +293,8 @@ int node(int argc,char **argv){ // associating messages to handlers. register_messages(); - gras_cb_register(gras_msgtype_by_name("can_get_suc"),&node_get_suc_handler); - gras_cb_register(gras_msgtype_by_name("can_nuke"),&node_nuke_handler);// can_test.c handler // include can_tests.c must be ON. + gras_cb_register("can_get_suc",&node_get_suc_handler); + gras_cb_register("can_nuke",&node_nuke_handler);// can_test.c handler // include can_tests.c must be ON. TRY{ gras_msg_handle(10000.0); // waiting.. in case of someone has something to say.