1 //////////////////////////////////////////////////////
2 // Peer-To-Peer CAN simulator 050406 by Dytto ESIAL //
3 //////////////////////////////////////////////////////
7 #include "types.h" // header containing the typedef struct of a node
9 XBT_LOG_NEW_DEFAULT_CATEGORY(can,"Messages specific to this example");
11 // struct of a "nuke" message, when a node send a nuke to (xId;yId).
12 GRAS_DEFINE_TYPE(s_nuke,
16 char host[1024]; // original expeditor..
17 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){
27 gras_socket_t temp_sock=NULL;
29 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
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]));
38 RETHROW0("Unable to connect known host so as to declare WAR!: %s");
44 nuke_msg.version=atoi(argv[2]);
45 strcpy(nuke_msg.host,gras_os_myname());
46 nuke_msg.port=atoi(argv[1]);
49 gras_msg_send(temp_sock,"can_nuke",&nuke_msg);
51 gras_socket_close(temp_sock);
52 RETHROW0("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
54 gras_socket_close(temp_sock); // spare.
55 gras_exit(); // spare.
59 // the function thaht send the nuke "msg" on (xId;yId), if it's not on me :p.
60 static int send_nuke(nuke_t *msg, int xId, int yId){
61 node_data_t *globals=(node_data_t*)gras_userdata_get();
62 gras_socket_t temp_sock=NULL;
63 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
65 if(xId>=globals->x1 && xId<=globals->x2 && yId>=globals->y1 && yId<=globals->y2){
66 INFO0("Nuclear launch missed");
74 strcpy(host,globals->west_host);
75 port=globals->west_port;}
76 else if(xId>globals->x2){
77 strcpy(host,globals->east_host);
78 port=globals->east_port;}
79 else if(yId<globals->y1){
80 strcpy(host,globals->south_host);
81 port=globals->south_port;}
82 else if(yId>globals->y2){
83 strcpy(host,globals->north_host);
84 port=globals->north_port;}
91 TRY{ // sending the nuke.
92 temp_sock=gras_socket_client(host,port);
94 RETHROW0("Unable to connect the nuke!: %s");
96 //INFO4("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
98 gras_msg_send(temp_sock,"can_nuke",msg);
100 RETHROW0("Unable to send the nuke!: %s");
102 gras_socket_close(temp_sock);
103 INFO4("Nuke launched by %s to %s for (%d;%d)",globals->host,host,msg->xId,msg->yId);
109 static int node_nuke_handler(gras_msg_cb_ctx_t ctx,void *payload_data){
110 gras_socket_t expeditor=gras_msg_cb_ctx_from(ctx);
111 nuke_t *incoming=(nuke_t*)payload_data;
112 node_data_t *globals=(node_data_t*)gras_userdata_get();
116 nuke_t nuke_msg; // writing my name one the nuke.
117 gras_socket_t temp_sock=NULL;
118 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
121 if(incoming->xId==-1){ // i must start the War
122 INFO2("%s:%d declare the WAR!!!!!!!!!!!!!!!!!",globals->host,globals->port);
123 srand((unsigned int)time((time_t *)NULL));
126 x=(int)(1000.0*rand()/(RAND_MAX+1.0));
127 y=(int)(1000.0*rand()/(RAND_MAX+1.0));
129 while(send_nuke(incoming,x,y)==0);
132 else if(incoming->xId>=globals->x1 && incoming->xId<=globals->x2 && incoming->yId>=globals->y1 && incoming->yId<=globals->y2){ // the nuke crash on my area..
133 if(globals->version==incoming->version) // ..but i'm dead.
134 INFO0("I'm already dead :p");
135 else if((incoming->xId-globals->xId)/60==0 && (incoming->yId-globals->yId)/60==0){ // ..and it's on me, so i die :X.
136 globals->version=incoming->version;
137 INFO2("Euuuaarrrgghhhh... %s killed %s !!!!!!!!!!!!!!!!!",incoming->host,globals->host);
139 else{ // and it miss me, i angry and i send my own nuke!
140 INFO1("%s was missed, and counteract!",globals->host);
141 /*int x1=(int)(1000.0*rand()/(RAND_MAX+1.0));
142 int y1=(int)(1000.0*rand()/(RAND_MAX+1.0));
143 int x2=(int)(1000.0*rand()/(RAND_MAX+1.0));
144 int y2=(int)(1000.0*rand()/(RAND_MAX+1.0));
145 int x3=(int)(1000.0*rand()/(RAND_MAX+1.0));
146 int y3=(int)(1000.0*rand()/(RAND_MAX+1.0));
147 int x4=(int)(1000.0*rand()/(RAND_MAX+1.0));
148 int y4=(int)(1000.0*rand()/(RAND_MAX+1.0));*/
151 nuke_msg.version=incoming->version;
152 strcpy(nuke_msg.host,globals->host);
153 nuke_msg.port=globals->port;
157 x=(int)(1000.0*rand()/(RAND_MAX+1.0));
158 y=(int)(1000.0*rand()/(RAND_MAX+1.0));
160 while(send_nuke(&nuke_msg,x,y)==0); // and sending if it's not on me.
163 else{ // the nuke isn't for me, so i forward her.
167 if(incoming->xId<globals->x1){
168 strcpy(host,globals->west_host);
169 port=globals->west_port;}
170 else if(incoming->xId>globals->x2){
171 strcpy(host,globals->east_host);
172 port=globals->east_port;}
173 else if(incoming->yId<globals->y1){
174 strcpy(host,globals->south_host);
175 port=globals->south_port;}
176 else if(incoming->yId>globals->y2){
177 strcpy(host,globals->north_host);
178 port=globals->north_port;}
183 temp_sock=gras_socket_client(host,port);
185 RETHROW0("Unable to connect the nuke!: %s");
188 gras_msg_send(temp_sock,"can_nuke",incoming);
190 RETHROW0("Unable to send the nuke!: %s");
192 INFO4("Nuke re-aimed by %s to %s for (%d;%d)",globals->host,host,incoming->xId,incoming->yId);
193 gras_socket_close(temp_sock);
195 gras_socket_close(expeditor); // spare.
198 gras_msg_handle(10000.0); // wait a bit, in case of..
200 INFO4("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2);
201 //INFO0("Closing node, all has been done!");