1 //////////////////////////////////////////////////////
2 // Peer-To-Peer CAN simulator 050406 by Dytto ESIAL //
3 //////////////////////////////////////////////////////
5 #include "types.h" // header containing the typedef struct of a node
7 XBT_LOG_NEW_DEFAULT_CATEGORY(can,"Messages specific to this example");
9 // struct of a "nuke" message, when a node send a nuke to (xId;yId).
10 GRAS_DEFINE_TYPE(s_nuke,
14 char host[1024]; // original expeditor..
15 int port; // ..and his port.
20 typedef struct s_nuke nuke_t;
22 // the function that start the **** War of the Nodes ****
23 int start_war(int argc,char **argv){
24 //return 0; // in order to inhibit the War of the Nodes
25 gras_init(&argc,argv);
26 gras_os_sleep((15-gras_os_getpid())*20+200); // wait a bit.
27 gras_socket_t temp_sock;
28 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
30 TRY{ // contacting the bad guy that will launch the War.
31 temp_sock=gras_socket_client(gras_os_myname(),atoi(argv[1]));
33 RETHROW0("Unable to connect known host so as to declare WAR!: %s");
39 nuke_msg.version=atoi(argv[2]);
40 strcpy(nuke_msg.host,gras_os_myname());
41 nuke_msg.port=atoi(argv[1]);
44 gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),&nuke_msg);
46 gras_socket_close(temp_sock);
47 RETHROW0("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
49 gras_socket_close(temp_sock); // spare.
50 gras_exit(); // spare.
53 // the function thaht send the nuke "msg" on (xId;yId), if it's not on me :p.
54 static int send_nuke(nuke_t *msg, int xId, int yId){
55 node_data_t *globals=(node_data_t*)gras_userdata_get();
57 if(xId>=globals->x1 && xId<=globals->x2 && yId>=globals->y1 && yId<=globals->y2){
58 INFO0("Nuclear launch missed");
66 strcpy(host,globals->west_host);
67 port=globals->west_port;}
68 else if(xId>globals->x2){
69 strcpy(host,globals->east_host);
70 port=globals->east_port;}
71 else if(yId<globals->y1){
72 strcpy(host,globals->south_host);
73 port=globals->south_port;}
74 else if(yId>globals->y2){
75 strcpy(host,globals->north_host);
76 port=globals->north_port;}
81 gras_socket_t temp_sock;
82 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
83 TRY{ // sending the nuke.
84 temp_sock=gras_socket_client(host,port);
86 RETHROW0("Unable to connect the nuke!: %s");
88 //INFO4("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
90 gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),msg);
92 RETHROW0("Unable to send the nuke!: %s");
94 gras_socket_close(temp_sock);
95 INFO4("Nuke launched by %s to %s for (%d;%d)",globals->host,host,msg->xId,msg->yId);
101 static int node_nuke_handler(gras_socket_t expeditor,void *payload_data){
102 nuke_t *incoming=(nuke_t*)payload_data;
103 node_data_t *globals=(node_data_t*)gras_userdata_get();
105 if(incoming->xId==-1){ // i must start the War
106 INFO2("%s:%d declare the WAR!!!!!!!!!!!!!!!!!",globals->host,globals->port);
107 srand((unsigned int)time((time_t *)NULL));
112 x=(int)(1000.0*rand()/(RAND_MAX+1.0));
113 y=(int)(1000.0*rand()/(RAND_MAX+1.0));
115 while(send_nuke(incoming,x,y)==0);
118 else if(incoming->xId>=globals->x1 && incoming->xId<=globals->x2 && incoming->yId>=globals->y1 && incoming->yId<=globals->y2){ // the nuke crash on my area..
119 if(globals->version==incoming->version) // ..but i'm dead.
120 INFO0("I'm already dead :p");
121 else if((incoming->xId-globals->xId)/60==0 && (incoming->yId-globals->yId)/60==0){ // ..and it's on me, so i die :X.
122 globals->version=incoming->version;
123 INFO2("Euuuaarrrgghhhh... %s killed %s !!!!!!!!!!!!!!!!!",incoming->host,globals->host);
125 else{ // and it miss me, i angry and i send my own nuke!
126 INFO1("%s was missed, and counteract!",globals->host);
127 /*int x1=(int)(1000.0*rand()/(RAND_MAX+1.0));
128 int y1=(int)(1000.0*rand()/(RAND_MAX+1.0));
129 int x2=(int)(1000.0*rand()/(RAND_MAX+1.0));
130 int y2=(int)(1000.0*rand()/(RAND_MAX+1.0));
131 int x3=(int)(1000.0*rand()/(RAND_MAX+1.0));
132 int y3=(int)(1000.0*rand()/(RAND_MAX+1.0));
133 int x4=(int)(1000.0*rand()/(RAND_MAX+1.0));
134 int y4=(int)(1000.0*rand()/(RAND_MAX+1.0));*/
136 nuke_t nuke_msg; // writing my name one the nuke.
137 nuke_msg.version=incoming->version;
138 strcpy(nuke_msg.host,globals->host);
139 nuke_msg.port=globals->port;
144 x=(int)(1000.0*rand()/(RAND_MAX+1.0));
145 y=(int)(1000.0*rand()/(RAND_MAX+1.0));
147 while(send_nuke(&nuke_msg,x,y)==0); // and sending if it's not on me.
150 else{ // the nuke isn't for me, so i forward her.
154 if(incoming->xId<globals->x1){
155 strcpy(host,globals->west_host);
156 port=globals->west_port;}
157 else if(incoming->xId>globals->x2){
158 strcpy(host,globals->east_host);
159 port=globals->east_port;}
160 else if(incoming->yId<globals->y1){
161 strcpy(host,globals->south_host);
162 port=globals->south_port;}
163 else if(incoming->yId>globals->y2){
164 strcpy(host,globals->north_host);
165 port=globals->north_port;}
167 gras_socket_t temp_sock;
168 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
170 temp_sock=gras_socket_client(host,port);
172 RETHROW0("Unable to connect the nuke!: %s");
175 gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),incoming);
177 RETHROW0("Unable to send the nuke!: %s");
179 INFO4("Nuke re-aimed by %s to %s for (%d;%d)",globals->host,host,incoming->xId,incoming->yId);
180 gras_socket_close(temp_sock);
182 gras_socket_close(expeditor); // spare.
183 xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
185 gras_msg_handle(10000.0); // wait a bit, in case of..
187 INFO4("My area is [%d;%d;%d;%d]",globals->x1,globals->x2,globals->y1,globals->y2);
188 //INFO0("Closing node, all has been done!");