1 /* Broken Peer-To-Peer CAN simulator */
3 /* Copyright (c) 2006, 2007, 2010. 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. */
11 #include "types.h" // header containing the typedef struct of a node
13 XBT_LOG_NEW_DEFAULT_CATEGORY(can, "Messages specific to this example");
15 // struct of a "nuke" message, when a node send a nuke to (xId;yId).
16 XBT_DEFINE_TYPE(s_nuke, struct s_nuke {
17 int xId; int yId; char host[1024]; // original expeditor..
18 int port; // ..and his port.
22 typedef struct s_nuke nuke_t;
24 // the function that start the **** War of the Nodes ****
25 int start_war(int argc, char **argv);
26 int start_war(int argc, char **argv)
28 xbt_socket_t temp_sock = NULL;
30 //return 0; // in order to inhibit the War of the Nodes
31 gras_init(&argc, argv);
32 gras_os_sleep((15 - gras_os_getpid()) * 20 + 200); // wait a bit.
35 TRY { // contacting the bad guy that will launch the War.
36 temp_sock = gras_socket_client(gras_os_myname(), atoi(argv[1]));
39 RETHROWF("Unable to connect known host so as to declare WAR!: %s");
45 nuke_msg.version = atoi(argv[2]);
46 strcpy(nuke_msg.host, gras_os_myname());
47 nuke_msg.port = atoi(argv[1]);
50 gras_msg_send(temp_sock, "can_nuke", &nuke_msg);
53 gras_socket_close(temp_sock);
55 ("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
57 gras_socket_close(temp_sock); // spare.
58 gras_exit(); // spare.
62 // the function thaht send the nuke "msg" on (xId;yId), if it's not on me :p.
63 static int send_nuke(nuke_t * msg, int xId, int yId)
65 node_data_t *globals = (node_data_t *) gras_userdata_get();
66 xbt_socket_t temp_sock = NULL;
68 if (xId >= globals->x1 && xId <= globals->x2 && yId >= globals->y1
69 && yId <= globals->y2) {
70 XBT_INFO("Nuclear launch missed");
76 if (xId < globals->x1) {
77 strcpy(host, globals->west_host);
78 port = globals->west_port;
79 } else if (xId > globals->x2) {
80 strcpy(host, globals->east_host);
81 port = globals->east_port;
82 } else if (yId < globals->y1) {
83 strcpy(host, globals->south_host);
84 port = globals->south_port;
85 } else if (yId > globals->y2) {
86 strcpy(host, globals->north_host);
87 port = globals->north_port;
95 TRY { // sending the nuke.
96 temp_sock = gras_socket_client(host, port);
99 RETHROWF("Unable to connect the nuke!: %s");
101 //XBT_INFO("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
103 gras_msg_send(temp_sock, "can_nuke", msg);
106 RETHROWF("Unable to send the nuke!: %s");
108 gras_socket_close(temp_sock);
109 XBT_INFO("Nuke launched by %s to %s for (%d;%d)", globals->host, host,
116 static int node_nuke_handler(gras_msg_cb_ctx_t ctx, void *payload_data)
118 xbt_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
119 nuke_t *incoming = (nuke_t *) payload_data;
120 node_data_t *globals = (node_data_t *) gras_userdata_get();
124 nuke_t nuke_msg; // writing my name one the nuke.
125 xbt_socket_t temp_sock = NULL;
126 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
129 if (incoming->xId == -1) { // i must start the War
130 XBT_INFO("%s:%d declare the WAR!!!!!!!!!!!!!!!!!", globals->host,
132 srand((unsigned int) time((time_t *) NULL));
135 x = (int) (1000.0 * rand() / (RAND_MAX + 1.0));
136 y = (int) (1000.0 * rand() / (RAND_MAX + 1.0));
138 while (send_nuke(incoming, x, y) == 0);
140 } else if (incoming->xId >= globals->x1 && incoming->xId <= globals->x2 && incoming->yId >= globals->y1 && incoming->yId <= globals->y2) { // the nuke crash on my area..
141 if (globals->version == incoming->version) // ..but i'm dead.
142 XBT_INFO("I'm already dead :p");
143 else if ((incoming->xId - globals->xId) / 60 == 0 && (incoming->yId - globals->yId) / 60 == 0) { // ..and it's on me, so i die :X.
144 globals->version = incoming->version;
145 XBT_INFO("Euuuaarrrgghhhh... %s killed %s !!!!!!!!!!!!!!!!!",
146 incoming->host, globals->host);
147 } else { // and it miss me, i angry and i send my own nuke!
148 XBT_INFO("%s was missed, and counteract!", globals->host);
149 /*int x1=(int)(1000.0*rand()/(RAND_MAX+1.0));
150 int y1=(int)(1000.0*rand()/(RAND_MAX+1.0));
151 int x2=(int)(1000.0*rand()/(RAND_MAX+1.0));
152 int y2=(int)(1000.0*rand()/(RAND_MAX+1.0));
153 int x3=(int)(1000.0*rand()/(RAND_MAX+1.0));
154 int y3=(int)(1000.0*rand()/(RAND_MAX+1.0));
155 int x4=(int)(1000.0*rand()/(RAND_MAX+1.0));
156 int y4=(int)(1000.0*rand()/(RAND_MAX+1.0)); */
159 nuke_msg.version = incoming->version;
160 strcpy(nuke_msg.host, globals->host);
161 nuke_msg.port = globals->port;
165 x = (int) (1000.0 * rand() / (RAND_MAX + 1.0));
166 y = (int) (1000.0 * rand() / (RAND_MAX + 1.0));
168 while (send_nuke(&nuke_msg, x, y) == 0); // and sending if it's not on me.
170 } else { // the nuke isn't for me, so i forward her.
174 if (incoming->xId < globals->x1) {
175 strcpy(host, globals->west_host);
176 port = globals->west_port;
177 } else if (incoming->xId > globals->x2) {
178 strcpy(host, globals->east_host);
179 port = globals->east_port;
180 } else if (incoming->yId < globals->y1) {
181 strcpy(host, globals->south_host);
182 port = globals->south_port;
183 } else if (incoming->yId > globals->y2) {
184 strcpy(host, globals->north_host);
185 port = globals->north_port;
191 temp_sock = gras_socket_client(host, port);
194 RETHROWF("Unable to connect the nuke!: %s");
197 gras_msg_send(temp_sock, "can_nuke", incoming);
200 RETHROWF("Unable to send the nuke!: %s");
202 XBT_INFO("Nuke re-aimed by %s to %s for (%d;%d)", globals->host, host,
203 incoming->xId, incoming->yId);
204 gras_socket_close(temp_sock);
206 gras_socket_close(expeditor); // spare.
209 gras_msg_handle(10000.0); // wait a bit, in case of..
212 XBT_INFO("My area is [%d;%d;%d;%d]", globals->x1, globals->x2,
213 globals->y1, globals->y2);
214 //XBT_INFO("Closing node, all has been done!");