Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove gras from the main documentation
[simgrid.git] / examples / gras / p2p / can / can_tests.c
1 /* Broken Peer-To-Peer CAN simulator                                        */
2
3 /* Copyright (c) 2006, 2007, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include <time.h>
10 //#include "gras.h"
11 #include "types.h"              // header containing the typedef struct of a node
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(can, "Messages specific to this example");
14
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.
19                  int version;   // fun.
20                  };);
21
22 typedef struct s_nuke nuke_t;
23
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)
27 {
28   xbt_socket_t temp_sock = NULL;
29   nuke_t nuke_msg;
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.
33
34
35   TRY {                         // contacting the bad guy that will launch the War.
36     temp_sock = gras_socket_client(gras_os_myname(), atoi(argv[1]));
37   }
38   CATCH_ANONYMOUS {
39     RETHROWF("Unable to connect known host so as to declare WAR!: %s");
40   }
41
42
43   nuke_msg.xId = -1;
44   nuke_msg.yId = -1;
45   nuke_msg.version = atoi(argv[2]);
46   strcpy(nuke_msg.host, gras_os_myname());
47   nuke_msg.port = atoi(argv[1]);
48
49   TRY {
50     gras_msg_send(temp_sock, "can_nuke", &nuke_msg);
51   }
52   CATCH_ANONYMOUS {
53     gras_socket_close(temp_sock);
54     RETHROWF
55         ("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
56   }
57   gras_socket_close(temp_sock); // spare.
58   gras_exit();                  // spare.
59   return 0;
60 }
61
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)
64 {
65   node_data_t *globals = (node_data_t *) gras_userdata_get();
66   xbt_socket_t temp_sock = NULL;
67
68   if (xId >= globals->x1 && xId <= globals->x2 && yId >= globals->y1
69       && yId <= globals->y2) {
70     XBT_INFO("Nuclear launch missed");
71     return 0;
72   } else {
73     char host[1024];
74     int port = 0;
75
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;
88     }
89
90     msg->xId = xId;
91     msg->yId = yId;
92
93
94
95     TRY {                       // sending the nuke.
96       temp_sock = gras_socket_client(host, port);
97     }
98     CATCH_ANONYMOUS {
99       RETHROWF("Unable to connect the nuke!: %s");
100     }
101     //XBT_INFO("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
102     TRY {
103       gras_msg_send(temp_sock, "can_nuke", msg);
104     }
105     CATCH_ANONYMOUS {
106       RETHROWF("Unable to send the nuke!: %s");
107     }
108     gras_socket_close(temp_sock);
109     XBT_INFO("Nuke launched by %s to %s for (%d;%d)", globals->host, host,
110           msg->xId, msg->yId);
111     return 1;
112   }
113 }
114
115
116 static int node_nuke_handler(gras_msg_cb_ctx_t ctx, void *payload_data)
117 {
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();
121
122   int x;
123   int y;
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.
127
128
129   if (incoming->xId == -1) {    // i must start the War
130     XBT_INFO("%s:%d declare the WAR!!!!!!!!!!!!!!!!!", globals->host,
131           globals->port);
132     srand((unsigned int) time((time_t *) NULL));
133
134     do {
135       x = (int) (1000.0 * rand() / (RAND_MAX + 1.0));
136       y = (int) (1000.0 * rand() / (RAND_MAX + 1.0));
137     }
138     while (send_nuke(incoming, x, y) == 0);
139
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)); */
157
158
159       nuke_msg.version = incoming->version;
160       strcpy(nuke_msg.host, globals->host);
161       nuke_msg.port = globals->port;
162
163
164       do {
165         x = (int) (1000.0 * rand() / (RAND_MAX + 1.0));
166         y = (int) (1000.0 * rand() / (RAND_MAX + 1.0));
167       }
168       while (send_nuke(&nuke_msg, x, y) == 0);  // and sending if it's not on me.
169     }
170   } else {                      // the nuke isn't for me, so i forward her.
171     char host[1024];
172     int port = 0;
173
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;
186     }
187
188
189
190     TRY {
191       temp_sock = gras_socket_client(host, port);
192     }
193     CATCH_ANONYMOUS {
194       RETHROWF("Unable to connect the nuke!: %s");
195     }
196     TRY {
197       gras_msg_send(temp_sock, "can_nuke", incoming);
198     }
199     CATCH_ANONYMOUS {
200       RETHROWF("Unable to send the nuke!: %s");
201     }
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);
205   }
206   gras_socket_close(expeditor); // spare.
207
208   TRY {
209     gras_msg_handle(10000.0);   // wait a bit, in case of..
210   }
211   CATCH(e) {
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!");
215     xbt_ex_free(e);
216   }
217   return 0;
218 }
219
220 // END