Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
efb814b51160d375b3cce9b8894a779a23f6d009
[simgrid.git] / examples / gras / p2p / can / can_tests.c
1 //////////////////////////////////////////////////////
2 // Peer-To-Peer CAN simulator 050406 by Dytto ESIAL //
3 //////////////////////////////////////////////////////
4
5 #include "types.h" // header containing the typedef struct of a node
6
7 XBT_LOG_NEW_DEFAULT_CATEGORY(can,"Messages specific to this example");
8
9 // struct of a "nuke" message, when a node send a nuke to (xId;yId).
10 GRAS_DEFINE_TYPE(s_nuke,
11         struct s_nuke{
12           int xId;
13           int yId;
14           char host[1024]; // original expeditor..
15           int port; // ..and his port.
16
17           int version; // fun.
18         };
19 );
20 typedef struct s_nuke nuke_t;
21
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.
29         
30   TRY{ // contacting the bad guy that will launch the War.
31     temp_sock=gras_socket_client(gras_os_myname(),atoi(argv[1]));
32   }CATCH(e){
33     RETHROW0("Unable to connect known host so as to declare WAR!: %s");
34   }
35   
36   nuke_t nuke_msg;
37   nuke_msg.xId=-1;
38   nuke_msg.yId=-1;
39   nuke_msg.version=atoi(argv[2]); 
40   strcpy(nuke_msg.host,gras_os_myname());
41   nuke_msg.port=atoi(argv[1]);
42
43   TRY{
44     gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),&nuke_msg);
45   }CATCH(e){
46     gras_socket_close(temp_sock);
47     RETHROW0("Unable to contact known host so as to declare WAR!!!!!!!!!!!!!!!!!!!!!: %s");
48   }
49   gras_socket_close(temp_sock); // spare.
50   gras_exit(); // spare.
51 }
52
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();
56
57   if(xId>=globals->x1 && xId<=globals->x2 && yId>=globals->y1 && yId<=globals->y2){
58     INFO0("Nuclear launch missed");
59     return 0;
60   }
61   else{
62     char host[1024];
63     int port;
64     
65     if(xId<globals->x1){
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;}
77
78     msg->xId=xId;
79     msg->yId=yId;
80
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);
85     }CATCH(e){
86       RETHROW0("Unable to connect the nuke!: %s");
87     }
88     //INFO4("%s ON %s %d %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",globals->host,host,xId,yId);
89     TRY{
90       gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),msg);
91     }CATCH(e){
92       RETHROW0("Unable to send the nuke!: %s");
93     }
94     gras_socket_close(temp_sock);
95     INFO4("Nuke launched by %s to %s for (%d;%d)",globals->host,host,msg->xId,msg->yId);
96     return 1;
97   }
98 }
99
100
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();
104
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));
108
109     int x;
110     int y;
111     do{
112       x=(int)(1000.0*rand()/(RAND_MAX+1.0));
113       y=(int)(1000.0*rand()/(RAND_MAX+1.0));
114     }
115     while(send_nuke(incoming,x,y)==0);
116     
117   }
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);
124     }
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));*/
135       
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;
140       
141       int x;
142       int y;
143       do{
144         x=(int)(1000.0*rand()/(RAND_MAX+1.0));
145         y=(int)(1000.0*rand()/(RAND_MAX+1.0));
146       }
147       while(send_nuke(&nuke_msg,x,y)==0); // and sending if it's not on me.
148     }
149   }
150   else{ // the nuke isn't for me, so i forward her.
151     char host[1024];
152     int port;
153     
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;}
166     
167     gras_socket_t temp_sock;
168     xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
169     TRY{
170       temp_sock=gras_socket_client(host,port);
171     }CATCH(e){
172       RETHROW0("Unable to connect the nuke!: %s");
173     }
174     TRY{
175       gras_msg_send(temp_sock,gras_msgtype_by_name("can_nuke"),incoming);
176     }CATCH(e){
177       RETHROW0("Unable to send the nuke!: %s");
178     }
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);
181   }
182   gras_socket_close(expeditor); // spare.
183   xbt_ex_t e; // the error variable used in TRY.. CATCH tokens.
184   TRY{
185     gras_msg_handle(10000.0); // wait a bit, in case of..
186   }CATCH(e){
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!");
189     }
190 }
191
192 // END