Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
52c5ad7c9004f941ce72bb2c82cfd39fc1c4bf4d
[simgrid.git] / examples / gras / p2p / can / can_tests.c
1 //////////////////////////////////////////////////////
2 // Peer-To-Peer CAN simulator 050406 by Dytto ESIAL //
3 //////////////////////////////////////////////////////
4
5 #include <time.h>
6 //#include "gras.h"
7 #include "types.h" // header containing the typedef struct of a node
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(can,"Messages specific to this example");
10
11 // struct of a "nuke" message, when a node send a nuke to (xId;yId).
12 GRAS_DEFINE_TYPE(s_nuke,
13         struct s_nuke{
14           int xId;
15           int yId;
16           char host[1024]; // original expeditor..
17           int port; // ..and his port.
18
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         gras_socket_t temp_sock=NULL;
28         nuke_t nuke_msg;
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.
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   }CATCH(e){
38     RETHROW0("Unable to connect known host so as to declare WAR!: %s");
39   }
40   
41
42   nuke_msg.xId=-1;
43   nuke_msg.yId=-1;
44   nuke_msg.version=atoi(argv[2]); 
45   strcpy(nuke_msg.host,gras_os_myname());
46   nuke_msg.port=atoi(argv[1]);
47
48   TRY{
49     gras_msg_send(temp_sock,"can_nuke",&nuke_msg);
50   }CATCH(e){
51     gras_socket_close(temp_sock);
52     RETHROW0("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
53   }
54   gras_socket_close(temp_sock); // spare.
55   gras_exit(); // spare.
56   return 0;
57 }
58
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.
64
65   if(xId>=globals->x1 && xId<=globals->x2 && yId>=globals->y1 && yId<=globals->y2){
66     INFO0("Nuclear launch missed");
67     return 0;
68   }
69   else{
70     char host[1024];
71     int port=0;
72     
73     if(xId<globals->x1){
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;}
85
86     msg->xId=xId;
87     msg->yId=yId;
88
89
90
91     TRY{ // sending the nuke.
92       temp_sock=gras_socket_client(host,port);
93     }CATCH(e){
94       RETHROW0("Unable to connect the nuke!: %s");
95     }
96     //INFO4("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
97     TRY{
98       gras_msg_send(temp_sock,"can_nuke",msg);
99     }CATCH(e){
100       RETHROW0("Unable to send the nuke!: %s");
101     }
102     gras_socket_close(temp_sock);
103     INFO4("Nuke launched by %s to %s for (%d;%d)",globals->host,host,msg->xId,msg->yId);
104     return 1;
105   }
106 }
107
108
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();
113   
114         int x;
115         int y;
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.
119         
120
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));
124
125     do{
126       x=(int)(1000.0*rand()/(RAND_MAX+1.0));
127       y=(int)(1000.0*rand()/(RAND_MAX+1.0));
128     }
129     while(send_nuke(incoming,x,y)==0);
130     
131   }
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);
138     }
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));*/
149       
150
151           nuke_msg.version=incoming->version;
152       strcpy(nuke_msg.host,globals->host);
153       nuke_msg.port=globals->port;
154       
155           
156       do{
157         x=(int)(1000.0*rand()/(RAND_MAX+1.0));
158         y=(int)(1000.0*rand()/(RAND_MAX+1.0));
159       }
160       while(send_nuke(&nuke_msg,x,y)==0); // and sending if it's not on me.
161     }
162   }
163   else{ // the nuke isn't for me, so i forward her.
164     char host[1024];
165     int port=0;
166     
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;}
179     
180
181
182         TRY{
183       temp_sock=gras_socket_client(host,port);
184     }CATCH(e){
185       RETHROW0("Unable to connect the nuke!: %s");
186     }
187     TRY{
188       gras_msg_send(temp_sock,"can_nuke",incoming);
189     }CATCH(e){
190       RETHROW0("Unable to send the nuke!: %s");
191     }
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);
194   }
195   gras_socket_close(expeditor); // spare.
196
197   TRY{
198     gras_msg_handle(10000.0); // wait a bit, in case of..
199   }CATCH(e){
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!");
202     }
203    return 0;
204 }
205
206 // END