Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent include and src using this command:
[simgrid.git] / examples / gras / p2p / can / can_tests.c
1 /* Broken Peer-To-Peer CAN simulator                                        */
2  
3 /* Copyright (c) 2006, 2007, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include <time.h>
10 //#include "gras.h"
11 #include "types.h" // header containing the typedef struct of a node
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(can,"Messages specific to this example");
14
15 // struct of a "nuke" message, when a node send a nuke to (xId;yId).
16 GRAS_DEFINE_TYPE(s_nuke,
17         struct s_nuke{
18           int xId;
19           int yId;
20           char host[1024]; // original expeditor..
21           int port; // ..and his port.
22
23           int version; // fun.
24         };
25 );
26 typedef struct s_nuke nuke_t;
27
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;
32         nuke_t nuke_msg;
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.
37
38         
39   TRY{ // contacting the bad guy that will launch the War.
40     temp_sock=gras_socket_client(gras_os_myname(),atoi(argv[1]));
41   }CATCH(e){
42     RETHROW0("Unable to connect known host so as to declare WAR!: %s");
43   }
44   
45
46   nuke_msg.xId=-1;
47   nuke_msg.yId=-1;
48   nuke_msg.version=atoi(argv[2]); 
49   strcpy(nuke_msg.host,gras_os_myname());
50   nuke_msg.port=atoi(argv[1]);
51
52   TRY{
53     gras_msg_send(temp_sock,"can_nuke",&nuke_msg);
54   }CATCH(e){
55     gras_socket_close(temp_sock);
56     RETHROW0("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
57   }
58   gras_socket_close(temp_sock); // spare.
59   gras_exit(); // spare.
60   return 0;
61 }
62
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.
68
69   if(xId>=globals->x1 && xId<=globals->x2 && yId>=globals->y1 && yId<=globals->y2){
70     INFO0("Nuclear launch missed");
71     return 0;
72   }
73   else{
74     char host[1024];
75     int port=0;
76     
77     if(xId<globals->x1){
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;}
89
90     msg->xId=xId;
91     msg->yId=yId;
92
93
94
95     TRY{ // sending the nuke.
96       temp_sock=gras_socket_client(host,port);
97     }CATCH(e){
98       RETHROW0("Unable to connect the nuke!: %s");
99     }
100     //INFO4("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
101     TRY{
102       gras_msg_send(temp_sock,"can_nuke",msg);
103     }CATCH(e){
104       RETHROW0("Unable to send the nuke!: %s");
105     }
106     gras_socket_close(temp_sock);
107     INFO4("Nuke launched by %s to %s for (%d;%d)",globals->host,host,msg->xId,msg->yId);
108     return 1;
109   }
110 }
111
112
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();
117   
118         int x;
119         int y;
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.
123         
124
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));
128
129     do{
130       x=(int)(1000.0*rand()/(RAND_MAX+1.0));
131       y=(int)(1000.0*rand()/(RAND_MAX+1.0));
132     }
133     while(send_nuke(incoming,x,y)==0);
134     
135   }
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);
142     }
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));*/
153       
154
155           nuke_msg.version=incoming->version;
156       strcpy(nuke_msg.host,globals->host);
157       nuke_msg.port=globals->port;
158       
159           
160       do{
161         x=(int)(1000.0*rand()/(RAND_MAX+1.0));
162         y=(int)(1000.0*rand()/(RAND_MAX+1.0));
163       }
164       while(send_nuke(&nuke_msg,x,y)==0); // and sending if it's not on me.
165     }
166   }
167   else{ // the nuke isn't for me, so i forward her.
168     char host[1024];
169     int port=0;
170     
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;}
183     
184
185
186         TRY{
187       temp_sock=gras_socket_client(host,port);
188     }CATCH(e){
189       RETHROW0("Unable to connect the nuke!: %s");
190     }
191     TRY{
192       gras_msg_send(temp_sock,"can_nuke",incoming);
193     }CATCH(e){
194       RETHROW0("Unable to send the nuke!: %s");
195     }
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);
198   }
199   gras_socket_close(expeditor); // spare.
200
201   TRY{
202     gras_msg_handle(10000.0); // wait a bit, in case of..
203   }CATCH(e){
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!");
206     }
207    return 0;
208 }
209
210 // END