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 GRAS_DEFINE_TYPE(s_nuke,
20 char host[1024]; // original expeditor..
21 int port; // ..and his port.
26 typedef struct s_nuke nuke_t;
28 // the function that start the **** War of the Nodes ****
29 int start_war(int argc,char **argv);
30 int start_war(int argc,char **argv){
31 gras_socket_t temp_sock=NULL;
33 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
34 //return 0; // in order to inhibit the War of the Nodes
35 gras_init(&argc,argv);
36 gras_os_sleep((15-gras_os_getpid())*20+200); // wait a bit.
39 TRY{ // contacting the bad guy that will launch the War.
40 temp_sock=gras_socket_client(gras_os_myname(),atoi(argv[1]));
42 RETHROW0("Unable to connect known host so as to declare WAR!: %s");
48 nuke_msg.version=atoi(argv[2]);
49 strcpy(nuke_msg.host,gras_os_myname());
50 nuke_msg.port=atoi(argv[1]);
53 gras_msg_send(temp_sock,"can_nuke",&nuke_msg);
55 gras_socket_close(temp_sock);
56 RETHROW0("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
58 gras_socket_close(temp_sock); // spare.
59 gras_exit(); // spare.
63 // the function thaht send the nuke "msg" on (xId;yId), if it's not on me :p.
64 static int send_nuke(nuke_t *msg, int xId, int yId){
65 node_data_t *globals=(node_data_t*)gras_userdata_get();
66 gras_socket_t temp_sock=NULL;
67 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
69 if(xId>=globals->x1 && xId<=globals->x2 && yId>=globals->y1 && yId<=globals->y2){
70 INFO0("Nuclear launch missed");
78 strcpy(host,globals->west_host);
79 port=globals->west_port;}
80 else if(xId>globals->x2){
81 strcpy(host,globals->east_host);
82 port=globals->east_port;}
83 else if(yId<globals->y1){
84 strcpy(host,globals->south_host);
85 port=globals->south_port;}
86 else if(yId>globals->y2){
87 strcpy(host,globals->north_host);
88 port=globals->north_port;}
95 TRY{ // sending the nuke.
96 temp_sock=gras_socket_client(host,port);
98 RETHROW0("Unable to connect the nuke!: %s");
100 //INFO4("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
102 gras_msg_send(temp_sock,"can_nuke",msg);
104 RETHROW0("Unable to send the nuke!: %s");
106 gras_socket_close(temp_sock);
107 INFO4("Nuke launched by %s to %s for (%d;%d)",globals->host,host,msg->xId,msg->yId);
113 static int node_nuke_handler(gras_msg_cb_ctx_t ctx,void *payload_data){
114 gras_socket_t expeditor=gras_msg_cb_ctx_from(ctx);
115 nuke_t *incoming=(nuke_t*)payload_data;
116 node_data_t *globals=(node_data_t*)gras_userdata_get();
120 nuke_t nuke_msg; // writing my name one the nuke.
121 gras_socket_t temp_sock=NULL;
122 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
125 if(incoming->xId==-1){ // i must start the War
126 INFO2("%s:%d declare the WAR!!!!!!!!!!!!!!!!!",globals->host,globals->port);
127 srand((unsigned int)time((time_t *)NULL));
130 x=(int)(1000.0*rand()/(RAND_MAX+1.0));
131 y=(int)(1000.0*rand()/(RAND_MAX+1.0));
133 while(send_nuke(incoming,x,y)==0);
136 else if(incoming->xId>=globals->x1 && incoming->xId<=globals->x2 && incoming->yId>=globals->y1 && incoming->yId<=globals->y2){ // the nuke crash on my area..
137 if(globals->version==incoming->version) // ..but i'm dead.
138 INFO0("I'm already dead :p");
139 else if((incoming->xId-globals->xId)/60==0 && (incoming->yId-globals->yId)/60==0){ // ..and it's on me, so i die :X.
140 globals->version=incoming->version;
141 INFO2("Euuuaarrrgghhhh... %s killed %s !!!!!!!!!!!!!!!!!",incoming->host,globals->host);
143 else{ // and it miss me, i angry and i send my own nuke!
144 INFO1("%s was missed, and counteract!",globals->host);
145 /*int x1=(int)(1000.0*rand()/(RAND_MAX+1.0));
146 int y1=(int)(1000.0*rand()/(RAND_MAX+1.0));
147 int x2=(int)(1000.0*rand()/(RAND_MAX+1.0));
148 int y2=(int)(1000.0*rand()/(RAND_MAX+1.0));
149 int x3=(int)(1000.0*rand()/(RAND_MAX+1.0));
150 int y3=(int)(1000.0*rand()/(RAND_MAX+1.0));
151 int x4=(int)(1000.0*rand()/(RAND_MAX+1.0));
152 int y4=(int)(1000.0*rand()/(RAND_MAX+1.0));*/
155 nuke_msg.version=incoming->version;
156 strcpy(nuke_msg.host,globals->host);
157 nuke_msg.port=globals->port;
161 x=(int)(1000.0*rand()/(RAND_MAX+1.0));
162 y=(int)(1000.0*rand()/(RAND_MAX+1.0));
164 while(send_nuke(&nuke_msg,x,y)==0); // and sending if it's not on me.
167 else{ // the nuke isn't for me, so i forward her.
171 if(incoming->xId<globals->x1){
172 strcpy(host,globals->west_host);
173 port=globals->west_port;}
174 else if(incoming->xId>globals->x2){
175 strcpy(host,globals->east_host);
176 port=globals->east_port;}
177 else if(incoming->yId<globals->y1){
178 strcpy(host,globals->south_host);
179 port=globals->south_port;}
180 else if(incoming->yId>globals->y2){
181 strcpy(host,globals->north_host);
182 port=globals->north_port;}
187 temp_sock=gras_socket_client(host,port);
189 RETHROW0("Unable to connect the nuke!: %s");
192 gras_msg_send(temp_sock,"can_nuke",incoming);
194 RETHROW0("Unable to send the nuke!: %s");
196 INFO4("Nuke re-aimed by %s to %s for (%d;%d)",globals->host,host,incoming->xId,incoming->yId);
197 gras_socket_close(temp_sock);
199 gras_socket_close(expeditor); // spare.
202 gras_msg_handle(10000.0); // wait a bit, in case of..
204 INFO4("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2);
205 //INFO0("Closing node, all has been done!");