-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();
-
- 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->xId<globals->x1) // test if the message must be forwarded to a neighbour.
- forward_get_suc(*incoming, globals->west_host, globals->west_port);
- else if(incoming->xId>globals->x2)
- forward_get_suc(*incoming, globals->east_host, globals->east_port);
- else if(incoming->yId<globals->y1)
- forward_get_suc(*incoming, globals->south_host, globals->south_port);
- else if(incoming->yId>globals->y2)
- forward_get_suc(*incoming, globals->north_host, globals->north_port);
- else{ // if the message must not be forwarded, then the area is splitted in half and one half is assignated to the new node.
- rep_suc_t outgoing;
- int validate=0;
- INFO4("Spliting my area between me (%d;%d) and the inserting node (%d;%d)!",
- globals->xId,globals->yId,incoming->xId,incoming->yId);
- if((globals->x2-globals->x1)>(globals->y2-globals->y1)){ // the height of the area is smaller than its width.
- if(incoming->xId<globals->xId){ // the new node is west from the actual node.
- outgoing.x1=globals->x1;
- outgoing.x2=(incoming->xId+globals->xId)/2;
- outgoing.y1=globals->y1;
- outgoing.y2=globals->y2;
- strcpy(outgoing.north_host,globals->north_host);
- outgoing.north_port=globals->north_port;
- strcpy(outgoing.south_host,globals->south_host);
- outgoing.south_port=globals->south_port;
- strcpy(outgoing.east_host,globals->host);
- outgoing.east_port=globals->port;
- strcpy(outgoing.west_host,globals->west_host);
- outgoing.west_port=globals->west_port;
-
- globals->x1=(incoming->xId+globals->xId)/2;
- strcpy(globals->west_host,incoming->host);
- globals->west_port=incoming->port;
- validate=1;
- }
- else if(incoming->xId>globals->xId){ // the new node is east from the actual node.
- outgoing.x1=(incoming->xId+globals->xId)/2;
- outgoing.x2=globals->x2;
- outgoing.y1=globals->y1;
- outgoing.y2=globals->y2;
- strcpy(outgoing.north_host,globals->north_host);
- outgoing.north_port=globals->north_port;
- strcpy(outgoing.south_host,globals->south_host);
- outgoing.south_port=globals->south_port;
- strcpy(outgoing.east_host,globals->east_host);
- outgoing.east_port=globals->east_port;
- strcpy(outgoing.west_host,globals->host);
- outgoing.west_port=globals->port;
-
- globals->x2=(incoming->xId+globals->xId)/2;
- strcpy(globals->east_host,incoming->host);
- globals->east_port=incoming->port;
- validate=1;
- }
- }
- else{
- if(incoming->yId<globals->yId){ // the new node is south from the actual node.
- outgoing.y1=globals->y1;
- outgoing.y2=(incoming->yId+globals->yId)/2;
- outgoing.y1=globals->y1;
- outgoing.x2=globals->x2;
- strcpy(outgoing.east_host,globals->east_host);
- outgoing.east_port=globals->east_port;
- strcpy(outgoing.west_host,globals->west_host);
- outgoing.west_port=globals->west_port;
- strcpy(outgoing.north_host,globals->host);
- outgoing.north_port=globals->port;
- strcpy(outgoing.south_host,globals->south_host);
- outgoing.south_port=globals->south_port;
-
- globals->y1=(incoming->yId+globals->yId)/2;
- strcpy(globals->south_host,incoming->host);
- globals->south_port=incoming->port;
- validate=1;
- }
- else if(incoming->yId>globals->yId){ // the new node is north from the actual node.
- outgoing.y1=(incoming->yId+globals->yId)/2;
- outgoing.y2=globals->y2;
- outgoing.x1=globals->x1;
- outgoing.x2=globals->x2;
- strcpy(outgoing.east_host,globals->east_host);
- outgoing.east_port=globals->east_port;
- strcpy(outgoing.west_host,globals->west_host);
- outgoing.west_port=globals->west_port;
- strcpy(outgoing.north_host,globals->north_host);
- outgoing.north_port=globals->north_port;
- strcpy(outgoing.south_host,globals->host);
- outgoing.south_port=globals->port;
-
- globals->y2=(incoming->yId+globals->yId)/2;
- strcpy(globals->north_host,incoming->host);
- globals->north_port=incoming->port;
- validate=1;
- }
- }
- 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=NULL;
- 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);
- INFO0("Environment informations sent!");
- }CATCH(e){
- RETHROW2("%s:Timeout sending environment informations to %s: %s",globals->host,gras_socket_peer_name(expeditor));
- }
- gras_socket_close(temp_sock);
- }
- else // we have a problem!
- INFO0("An error occurded!!!!!!!!!!!!!");
-
- }
- gras_socket_close(expeditor); // spare
- TRY{
- gras_msg_handle(10000.0); // wait a bit in case of someone want to ask me for something.
- }CATCH(e){
- INFO4("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2);
- //INFO0("Closing node, all has been done!");
- }
- return 1;
+static int node_get_suc_handler(gras_msg_cb_ctx_t ctx, void *payload_data)
+{
+ xbt_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();
+ xbt_socket_t temp_sock = NULL;
+ XBT_INFO("Received a get_successor message from %s for (%d;%d)",
+ xbt_socket_peer_name(expeditor), incoming->xId, incoming->yId);
+ //XBT_INFO("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2);
+ if (incoming->xId < globals->x1) // test if the message must be forwarded to a neighbour.
+ forward_get_suc(*incoming, globals->west_host, globals->west_port);
+ else if (incoming->xId > globals->x2)
+ forward_get_suc(*incoming, globals->east_host, globals->east_port);
+ else if (incoming->yId < globals->y1)
+ forward_get_suc(*incoming, globals->south_host, globals->south_port);
+ else if (incoming->yId > globals->y2)
+ forward_get_suc(*incoming, globals->north_host, globals->north_port);
+ else { // if the message must not be forwarded, then the area is splitted in half and one half is assignated to the new node.
+ rep_suc_t outgoing;
+ int validate = 0;
+ XBT_INFO
+ ("Spliting my area between me (%d;%d) and the inserting node (%d;%d)!",
+ globals->xId, globals->yId, incoming->xId, incoming->yId);
+ if ((globals->x2 - globals->x1) > (globals->y2 - globals->y1)) { // the height of the area is smaller than its width.
+ if (incoming->xId < globals->xId) { // the new node is west from the actual node.
+ outgoing.x1 = globals->x1;
+ outgoing.x2 = (incoming->xId + globals->xId) / 2;
+ outgoing.y1 = globals->y1;
+ outgoing.y2 = globals->y2;
+ strcpy(outgoing.north_host, globals->north_host);
+ outgoing.north_port = globals->north_port;
+ strcpy(outgoing.south_host, globals->south_host);
+ outgoing.south_port = globals->south_port;
+ strcpy(outgoing.east_host, globals->host);
+ outgoing.east_port = globals->port;
+ strcpy(outgoing.west_host, globals->west_host);
+ outgoing.west_port = globals->west_port;
+
+ globals->x1 = (incoming->xId + globals->xId) / 2;
+ strcpy(globals->west_host, incoming->host);
+ globals->west_port = incoming->port;
+ validate = 1;
+ } else if (incoming->xId > globals->xId) { // the new node is east from the actual node.
+ outgoing.x1 = (incoming->xId + globals->xId) / 2;
+ outgoing.x2 = globals->x2;
+ outgoing.y1 = globals->y1;
+ outgoing.y2 = globals->y2;
+ strcpy(outgoing.north_host, globals->north_host);
+ outgoing.north_port = globals->north_port;
+ strcpy(outgoing.south_host, globals->south_host);
+ outgoing.south_port = globals->south_port;
+ strcpy(outgoing.east_host, globals->east_host);
+ outgoing.east_port = globals->east_port;
+ strcpy(outgoing.west_host, globals->host);
+ outgoing.west_port = globals->port;
+
+ globals->x2 = (incoming->xId + globals->xId) / 2;
+ strcpy(globals->east_host, incoming->host);
+ globals->east_port = incoming->port;
+ validate = 1;
+ }
+ } else {
+ if (incoming->yId < globals->yId) { // the new node is south from the actual node.
+ outgoing.y1 = globals->y1;
+ outgoing.y2 = (incoming->yId + globals->yId) / 2;
+ outgoing.y1 = globals->y1;
+ outgoing.x2 = globals->x2;
+ strcpy(outgoing.east_host, globals->east_host);
+ outgoing.east_port = globals->east_port;
+ strcpy(outgoing.west_host, globals->west_host);
+ outgoing.west_port = globals->west_port;
+ strcpy(outgoing.north_host, globals->host);
+ outgoing.north_port = globals->port;
+ strcpy(outgoing.south_host, globals->south_host);
+ outgoing.south_port = globals->south_port;
+
+ globals->y1 = (incoming->yId + globals->yId) / 2;
+ strcpy(globals->south_host, incoming->host);
+ globals->south_port = incoming->port;
+ validate = 1;
+ } else if (incoming->yId > globals->yId) { // the new node is north from the actual node.
+ outgoing.y1 = (incoming->yId + globals->yId) / 2;
+ outgoing.y2 = globals->y2;
+ outgoing.x1 = globals->x1;
+ outgoing.x2 = globals->x2;
+ strcpy(outgoing.east_host, globals->east_host);
+ outgoing.east_port = globals->east_port;
+ strcpy(outgoing.west_host, globals->west_host);
+ outgoing.west_port = globals->west_port;
+ strcpy(outgoing.north_host, globals->north_host);
+ outgoing.north_port = globals->north_port;
+ strcpy(outgoing.south_host, globals->host);
+ outgoing.south_port = globals->port;
+
+ globals->y2 = (incoming->yId + globals->yId) / 2;
+ strcpy(globals->north_host, incoming->host);
+ globals->north_port = incoming->port;
+ validate = 1;
+ }
+ }
+ if (validate == 1) { // the area for the new node has been defined, then send theses informations to the new node.
+ XBT_INFO("Sending environment informations to node %s:%d",
+ incoming->host, incoming->port);
+
+ TRY {
+ temp_sock = gras_socket_client(incoming->host, incoming->port);
+ }
+ CATCH_ANONYMOUS {
+ RETHROWF
+ ("Unable to connect to the node wich has requested for an area!: %s");
+ }
+ TRY {
+ gras_msg_send(temp_sock, "can_rep_suc", &outgoing);
+ XBT_INFO("Environment informations sent!");
+ }
+ CATCH_ANONYMOUS {
+ RETHROWF("%s:Timeout sending environment informations to %s: %s",
+ globals->host, xbt_socket_peer_name(expeditor));
+ }
+ gras_socket_close(temp_sock);
+ } else // we have a problem!
+ XBT_INFO("An error occurded!!!!!!!!!!!!!");
+
+ }
+ gras_socket_close(expeditor); // spare
+ TRY {
+ gras_msg_handle(10000.0); // wait a bit in case of someone want to ask me for something.
+ }
+ CATCH(e) {
+ XBT_INFO("My area is [%d;%d;%d;%d]", globals->x1, globals->x2,
+ globals->y1, globals->y2);
+ //XBT_INFO("Closing node, all has been done!");
+ xbt_ex_free(e);
+ }
+ return 0;