Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8fdbdefc9ef634da79b0a068683c85cbcd288284
[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   //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.
32         
33   TRY{ // contacting the bad guy that will launch the War.
34     temp_sock=gras_socket_client(gras_os_myname(),atoi(argv[1]));
35   }CATCH(e){
36     RETHROW0("Unable to connect known host so as to declare WAR!: %s");
37   }
38   
39   nuke_t nuke_msg;
40   nuke_msg.xId=-1;
41   nuke_msg.yId=-1;
42   nuke_msg.version=atoi(argv[2]); 
43   strcpy(nuke_msg.host,gras_os_myname());
44   nuke_msg.port=atoi(argv[1]);
45
46   TRY{
47     gras_msg_send(temp_sock,"can_nuke",&nuke_msg);
48   }CATCH(e){
49     gras_socket_close(temp_sock);
50     RETHROW0("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
51   }
52   gras_socket_close(temp_sock); // spare.
53   gras_exit(); // spare.
54   return 0;
55 }
56
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();
60
61   if(xId>=globals->x1 && xId<=globals->x2 && yId>=globals->y1 && yId<=globals->y2){
62     INFO0("Nuclear launch missed");
63     return 0;
64   }
65   else{
66     char host[1024];
67     int port=0;
68     
69     if(xId<globals->x1){
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;}
81
82     msg->xId=xId;
83     msg->yId=yId;
84
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);
89     }CATCH(e){
90       RETHROW0("Unable to connect the nuke!: %s");
91     }
92     //INFO4("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
93     TRY{
94       gras_msg_send(temp_sock,"can_nuke",msg);
95     }CATCH(e){
96       RETHROW0("Unable to send the nuke!: %s");
97     }
98     gras_socket_close(temp_sock);
99     INFO4("Nuke launched by %s to %s for (%d;%d)",globals->host,host,msg->xId,msg->yId);
100     return 1;
101   }
102 }
103
104
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();
109
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));
113
114     int x;
115     int y;
116     do{
117       x=(int)(1000.0*rand()/(RAND_MAX+1.0));
118       y=(int)(1000.0*rand()/(RAND_MAX+1.0));
119     }
120     while(send_nuke(incoming,x,y)==0);
121     
122   }
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);
129     }
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));*/
140       
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;
145       
146       int x;
147       int y;
148       do{
149         x=(int)(1000.0*rand()/(RAND_MAX+1.0));
150         y=(int)(1000.0*rand()/(RAND_MAX+1.0));
151       }
152       while(send_nuke(&nuke_msg,x,y)==0); // and sending if it's not on me.
153     }
154   }
155   else{ // the nuke isn't for me, so i forward her.
156     char host[1024];
157     int port=0;
158     
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;}
171     
172     gras_socket_t temp_sock=NULL;
173     xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
174     TRY{
175       temp_sock=gras_socket_client(host,port);
176     }CATCH(e){
177       RETHROW0("Unable to connect the nuke!: %s");
178     }
179     TRY{
180       gras_msg_send(temp_sock,"can_nuke",incoming);
181     }CATCH(e){
182       RETHROW0("Unable to send the nuke!: %s");
183     }
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);
186   }
187   gras_socket_close(expeditor); // spare.
188   xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
189   TRY{
190     gras_msg_handle(10000.0); // wait a bit, in case of..
191   }CATCH(e){
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!");
194     }
195    return 0;
196 }
197
198 // END