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 //return 0; // in order to inhibit the War of the Nodes
28 gras_init(&argc,argv);
29 gras_os_sleep((15-gras_os_getpid())*20+200); // wait a bit.
30 gras_socket_t temp_sock=NULL;
31 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
33 TRY{ // contacting the bad guy that will launch the War.
34 temp_sock=gras_socket_client(gras_os_myname(),atoi(argv[1]));
36 RETHROW0("Unable to connect known host so as to declare WAR!: %s");
42 nuke_msg.version=atoi(argv[2]);
43 strcpy(nuke_msg.host,gras_os_myname());
44 nuke_msg.port=atoi(argv[1]);
47 gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),&nuke_msg);
49 gras_socket_close(temp_sock);
50 RETHROW0("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
52 gras_socket_close(temp_sock); // spare.
53 gras_exit(); // spare.
57 // the function thaht send the nuke "msg" on (xId;yId), if it's not on me :p.
58 static int send_nuke(nuke_t *msg, int xId, int yId){
59 node_data_t *globals=(node_data_t*)gras_userdata_get();
61 if(xId>=globals->x1 && xId<=globals->x2 && yId>=globals->y1 && yId<=globals->y2){
62 INFO0("Nuclear launch missed");
70 strcpy(host,globals->west_host);
71 port=globals->west_port;}
72 else if(xId>globals->x2){
73 strcpy(host,globals->east_host);
74 port=globals->east_port;}
75 else if(yId<globals->y1){
76 strcpy(host,globals->south_host);
77 port=globals->south_port;}
78 else if(yId>globals->y2){
79 strcpy(host,globals->north_host);
80 port=globals->north_port;}
85 gras_socket_t temp_sock=NULL;
86 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
87 TRY{ // sending the nuke.
88 temp_sock=gras_socket_client(host,port);
90 RETHROW0("Unable to connect the nuke!: %s");
92 //INFO4("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
94 gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),msg);
96 RETHROW0("Unable to send the nuke!: %s");
98 gras_socket_close(temp_sock);
99 INFO4("Nuke launched by %s to %s for (%d;%d)",globals->host,host,msg->xId,msg->yId);
105 static int node_nuke_handler(gras_msg_cb_ctx_t ctx,void *payload_data){
106 gras_socket_t expeditor=gras_msg_cb_ctx_from(ctx);
107 nuke_t *incoming=(nuke_t*)payload_data;
108 node_data_t *globals=(node_data_t*)gras_userdata_get();
110 if(incoming->xId==-1){ // i must start the War
111 INFO2("%s:%d declare the WAR!!!!!!!!!!!!!!!!!",globals->host,globals->port);
112 srand((unsigned int)time((time_t *)NULL));
117 x=(int)(1000.0*rand()/(RAND_MAX+1.0));
118 y=(int)(1000.0*rand()/(RAND_MAX+1.0));
120 while(send_nuke(incoming,x,y)==0);
123 else if(incoming->xId>=globals->x1 && incoming->xId<=globals->x2 && incoming->yId>=globals->y1 && incoming->yId<=globals->y2){ // the nuke crash on my area..
124 if(globals->version==incoming->version) // ..but i'm dead.
125 INFO0("I'm already dead :p");
126 else if((incoming->xId-globals->xId)/60==0 && (incoming->yId-globals->yId)/60==0){ // ..and it's on me, so i die :X.
127 globals->version=incoming->version;
128 INFO2("Euuuaarrrgghhhh... %s killed %s !!!!!!!!!!!!!!!!!",incoming->host,globals->host);
130 else{ // and it miss me, i angry and i send my own nuke!
131 INFO1("%s was missed, and counteract!",globals->host);
132 /*int x1=(int)(1000.0*rand()/(RAND_MAX+1.0));
133 int y1=(int)(1000.0*rand()/(RAND_MAX+1.0));
134 int x2=(int)(1000.0*rand()/(RAND_MAX+1.0));
135 int y2=(int)(1000.0*rand()/(RAND_MAX+1.0));
136 int x3=(int)(1000.0*rand()/(RAND_MAX+1.0));
137 int y3=(int)(1000.0*rand()/(RAND_MAX+1.0));
138 int x4=(int)(1000.0*rand()/(RAND_MAX+1.0));
139 int y4=(int)(1000.0*rand()/(RAND_MAX+1.0));*/
141 nuke_t nuke_msg; // writing my name one the nuke.
142 nuke_msg.version=incoming->version;
143 strcpy(nuke_msg.host,globals->host);
144 nuke_msg.port=globals->port;
149 x=(int)(1000.0*rand()/(RAND_MAX+1.0));
150 y=(int)(1000.0*rand()/(RAND_MAX+1.0));
152 while(send_nuke(&nuke_msg,x,y)==0); // and sending if it's not on me.
155 else{ // the nuke isn't for me, so i forward her.
159 if(incoming->xId<globals->x1){
160 strcpy(host,globals->west_host);
161 port=globals->west_port;}
162 else if(incoming->xId>globals->x2){
163 strcpy(host,globals->east_host);
164 port=globals->east_port;}
165 else if(incoming->yId<globals->y1){
166 strcpy(host,globals->south_host);
167 port=globals->south_port;}
168 else if(incoming->yId>globals->y2){
169 strcpy(host,globals->north_host);
170 port=globals->north_port;}
172 gras_socket_t temp_sock=NULL;
173 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
175 temp_sock=gras_socket_client(host,port);
177 RETHROW0("Unable to connect the nuke!: %s");
180 gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),incoming);
182 RETHROW0("Unable to send the nuke!: %s");
184 INFO4("Nuke re-aimed by %s to %s for (%d;%d)",globals->host,host,incoming->xId,incoming->yId);
185 gras_socket_close(temp_sock);
187 gras_socket_close(expeditor); // spare.
188 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
190 gras_msg_handle(10000.0); // wait a bit, in case of..
192 INFO4("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2);
193 //INFO0("Closing node, all has been done!");