1 /* Broken Peer-To-Peer CAN simulator */
3 /* Copyright (c) 2006, 2007. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
13 #include "can_tests.c"
14 //#include "types.h" // header alone containing the typedef struct of a node // include can_tests.c must be OFF.
16 //XBT_LOG_NEW_DEFAULT_CATEGORY(can,"Messages specific to this example"); // include can_tests.c must be OFF.
18 //extern char *_gras_this_type_symbol_does_not_exist__s_nuke;
19 int node_nuke_handler(gras_msg_cb_ctx_t ctx, void *payload_data);
21 // struct of a "get_successor" message, when a node look after the area in which he want to be.
22 GRAS_DEFINE_TYPE(s_get_suc, struct s_get_suc {
23 int xId; int yId; char host[1024]; int port;};);
25 typedef struct s_get_suc get_suc_t;
27 // struct of a "response_successor" message, hen a node receive the information of his new area.
28 GRAS_DEFINE_TYPE(s_rep_suc, struct s_rep_suc {
33 char north_host[1024];
35 char south_host[1024];
38 int east_port; char west_host[1024]; int west_port;};);
39 typedef struct s_rep_suc rep_suc_t;
41 int node(int argc, char **argv);
43 // registering messages types
44 static void register_messages()
46 gras_msgtype_declare("can_get_suc", gras_datadesc_by_symbol(s_get_suc));
47 gras_msgtype_declare("can_rep_suc", gras_datadesc_by_symbol(s_rep_suc));
48 gras_msgtype_declare("can_nuke", gras_datadesc_by_symbol(s_nuke)); // can_test.c message // include can_tests.c must be ON.
52 // a forwarding function for a "get_suc" message.
53 static void forward_get_suc(get_suc_t msg, char host[1024], int port)
55 gras_socket_t temp_sock = NULL;
56 //XBT_INFO("Transmiting message to %s:%d",host,port);
58 temp_sock = gras_socket_client(host, port);
61 RETHROWF("Unable to connect!: %s");
64 gras_msg_send(temp_sock, "can_get_suc", &msg);
67 RETHROWF("Unable to send!: %s");
69 XBT_INFO("Forwarding a get_successor message to %s for (%d;%d)", host,
71 gras_socket_close(temp_sock);
74 // the handling function of a "get_suc" message (what do a node when he receive a "get_suc" message.
75 static int node_get_suc_handler(gras_msg_cb_ctx_t ctx, void *payload_data)
77 gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
78 get_suc_t *incoming = (get_suc_t *) payload_data;
79 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
80 node_data_t *globals = (node_data_t *) gras_userdata_get();
81 gras_socket_t temp_sock = NULL;
82 XBT_INFO("Received a get_successor message from %s for (%d;%d)",
83 gras_socket_peer_name(expeditor), incoming->xId, incoming->yId);
84 //XBT_INFO("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2);
85 if (incoming->xId < globals->x1) // test if the message must be forwarded to a neighbour.
86 forward_get_suc(*incoming, globals->west_host, globals->west_port);
87 else if (incoming->xId > globals->x2)
88 forward_get_suc(*incoming, globals->east_host, globals->east_port);
89 else if (incoming->yId < globals->y1)
90 forward_get_suc(*incoming, globals->south_host, globals->south_port);
91 else if (incoming->yId > globals->y2)
92 forward_get_suc(*incoming, globals->north_host, globals->north_port);
93 else { // if the message must not be forwarded, then the area is splitted in half and one half is assignated to the new node.
97 ("Spliting my area between me (%d;%d) and the inserting node (%d;%d)!",
98 globals->xId, globals->yId, incoming->xId, incoming->yId);
99 if ((globals->x2 - globals->x1) > (globals->y2 - globals->y1)) { // the height of the area is smaller than its width.
100 if (incoming->xId < globals->xId) { // the new node is west from the actual node.
101 outgoing.x1 = globals->x1;
102 outgoing.x2 = (incoming->xId + globals->xId) / 2;
103 outgoing.y1 = globals->y1;
104 outgoing.y2 = globals->y2;
105 strcpy(outgoing.north_host, globals->north_host);
106 outgoing.north_port = globals->north_port;
107 strcpy(outgoing.south_host, globals->south_host);
108 outgoing.south_port = globals->south_port;
109 strcpy(outgoing.east_host, globals->host);
110 outgoing.east_port = globals->port;
111 strcpy(outgoing.west_host, globals->west_host);
112 outgoing.west_port = globals->west_port;
114 globals->x1 = (incoming->xId + globals->xId) / 2;
115 strcpy(globals->west_host, incoming->host);
116 globals->west_port = incoming->port;
118 } else if (incoming->xId > globals->xId) { // the new node is east from the actual node.
119 outgoing.x1 = (incoming->xId + globals->xId) / 2;
120 outgoing.x2 = globals->x2;
121 outgoing.y1 = globals->y1;
122 outgoing.y2 = globals->y2;
123 strcpy(outgoing.north_host, globals->north_host);
124 outgoing.north_port = globals->north_port;
125 strcpy(outgoing.south_host, globals->south_host);
126 outgoing.south_port = globals->south_port;
127 strcpy(outgoing.east_host, globals->east_host);
128 outgoing.east_port = globals->east_port;
129 strcpy(outgoing.west_host, globals->host);
130 outgoing.west_port = globals->port;
132 globals->x2 = (incoming->xId + globals->xId) / 2;
133 strcpy(globals->east_host, incoming->host);
134 globals->east_port = incoming->port;
138 if (incoming->yId < globals->yId) { // the new node is south from the actual node.
139 outgoing.y1 = globals->y1;
140 outgoing.y2 = (incoming->yId + globals->yId) / 2;
141 outgoing.y1 = globals->y1;
142 outgoing.x2 = globals->x2;
143 strcpy(outgoing.east_host, globals->east_host);
144 outgoing.east_port = globals->east_port;
145 strcpy(outgoing.west_host, globals->west_host);
146 outgoing.west_port = globals->west_port;
147 strcpy(outgoing.north_host, globals->host);
148 outgoing.north_port = globals->port;
149 strcpy(outgoing.south_host, globals->south_host);
150 outgoing.south_port = globals->south_port;
152 globals->y1 = (incoming->yId + globals->yId) / 2;
153 strcpy(globals->south_host, incoming->host);
154 globals->south_port = incoming->port;
156 } else if (incoming->yId > globals->yId) { // the new node is north from the actual node.
157 outgoing.y1 = (incoming->yId + globals->yId) / 2;
158 outgoing.y2 = globals->y2;
159 outgoing.x1 = globals->x1;
160 outgoing.x2 = globals->x2;
161 strcpy(outgoing.east_host, globals->east_host);
162 outgoing.east_port = globals->east_port;
163 strcpy(outgoing.west_host, globals->west_host);
164 outgoing.west_port = globals->west_port;
165 strcpy(outgoing.north_host, globals->north_host);
166 outgoing.north_port = globals->north_port;
167 strcpy(outgoing.south_host, globals->host);
168 outgoing.south_port = globals->port;
170 globals->y2 = (incoming->yId + globals->yId) / 2;
171 strcpy(globals->north_host, incoming->host);
172 globals->north_port = incoming->port;
176 if (validate == 1) { // the area for the new node has been defined, then send theses informations to the new node.
177 XBT_INFO("Sending environment informations to node %s:%d",
178 incoming->host, incoming->port);
181 temp_sock = gras_socket_client(incoming->host, incoming->port);
185 ("Unable to connect to the node wich has requested for an area!: %s");
188 gras_msg_send(temp_sock, "can_rep_suc", &outgoing);
189 XBT_INFO("Environment informations sent!");
192 RETHROWF("%s:Timeout sending environment informations to %s: %s",
193 globals->host, gras_socket_peer_name(expeditor));
195 gras_socket_close(temp_sock);
196 } else // we have a problem!
197 XBT_INFO("An error occurded!!!!!!!!!!!!!");
200 gras_socket_close(expeditor); // spare
202 gras_msg_handle(10000.0); // wait a bit in case of someone want to ask me for something.
205 XBT_INFO("My area is [%d;%d;%d;%d]", globals->x1, globals->x2,
206 globals->y1, globals->y2);
207 //XBT_INFO("Closing node, all has been done!");
217 int node(int argc, char **argv)
219 node_data_t *globals = NULL;
220 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
221 gras_socket_t temp_sock = NULL;
223 rep_suc_t rep_suc_msg;
225 get_suc_t get_suc_msg; // building the "get_suc" message.
226 gras_socket_t temp_sock2 = NULL;
228 XBT_INFO("Starting");
230 /* 1. Init the GRAS infrastructure and declare my globals */
231 gras_init(&argc, argv);
232 gras_os_sleep((15 - gras_os_getpid()) * 20); // wait a bit.
234 globals = gras_userdata_new(node_data_t);
236 globals->xId = atoi(argv[1]); // x coordinate of the node.
237 globals->yId = atoi(argv[2]); // y coordinate of the node.
238 globals->port = atoi(argv[3]); // node port
239 globals->sock = gras_socket_server(globals->port); // node socket.
240 snprintf(globals->host, 1024, gras_os_myname()); // node name.
241 globals->version = 0; // node version (used for fun)
243 /* 2. Inserting the Node */
244 XBT_INFO("Inserting node %s:%d", globals->host, globals->port);
245 if (argc == 4) { // the node is a server, then he has the whole area.
250 } else { // asking for an area.
251 XBT_INFO("Contacting %s so as to request for an area", argv[4]);
254 temp_sock = gras_socket_client(argv[4], atoi(argv[5]));
257 RETHROWF("Unable to connect known host to request for an area!: %s");
261 get_suc_msg.xId = globals->xId;
262 get_suc_msg.yId = globals->yId;
263 strcpy(get_suc_msg.host, globals->host);
264 get_suc_msg.port = globals->port;
266 gras_msg_send(temp_sock, "can_get_suc", &get_suc_msg);
269 gras_socket_close(temp_sock);
270 RETHROWF("Unable to contact known host to get an area!: %s");
272 gras_socket_close(temp_sock);
276 TRY { // waiting for a reply.
277 XBT_INFO("Waiting for reply!");
278 gras_msg_wait(6000, "can_rep_suc", &temp_sock2, &rep_suc_msg);
281 RETHROWF("%s: Error waiting for an area:%s", globals->host);
284 // retreiving the data of the response.
285 globals->x1 = rep_suc_msg.x1;
286 globals->x2 = rep_suc_msg.x2;
287 globals->y1 = rep_suc_msg.y1;
288 globals->y2 = rep_suc_msg.y2;
289 strcpy(globals->north_host, rep_suc_msg.north_host);
290 globals->north_port = rep_suc_msg.north_port;
291 strcpy(globals->south_host, rep_suc_msg.south_host);
292 globals->south_port = rep_suc_msg.south_port;
293 strcpy(globals->east_host, rep_suc_msg.east_host);
294 globals->east_port = rep_suc_msg.east_port;
295 strcpy(globals->west_host, rep_suc_msg.west_host);
296 globals->west_port = rep_suc_msg.west_port;
298 gras_socket_close(temp_sock); // spare
300 XBT_INFO("Node %s:%d inserted", globals->host, globals->port);
302 // associating messages to handlers.
304 gras_cb_register("can_get_suc", &node_get_suc_handler);
305 gras_cb_register("can_nuke", &node_nuke_handler); // can_test.c handler // include can_tests.c must be ON.
308 gras_msg_handle(10000.0); // waiting.. in case of someone has something to say.
311 XBT_INFO("My area is [%d;%d;%d;%d]", globals->x1, globals->x2,
312 globals->y1, globals->y2);
313 //XBT_INFO("Closing node, all has been done!");
317 gras_socket_close(globals->sock); // spare.
318 free(globals); // spare.