Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4e99a3b1d72fd45aa904cee3785f76c631708efa
[simgrid.git] / examples / gras / tokenS / tokenS.c
1 /* $Id$ */
2
3 /* stoken - simple/static token ring                                        */
4
5 /* Copyright (c) 2005 Alexandre Colucci.                                    */
6 /* Copyright (c) 2005 Martin Quinson.                                       */
7 /* All rights reserved.                                                     */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11  
12 #include "gras.h"
13  
14 #define NBLOOPS 100
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(Token,"Messages specific to this example");
17
18 /* register messages which may be sent */
19 static void register_messages(void) {
20   gras_msgtype_declare("stoken", gras_datadesc_by_name("int"));
21 }
22
23 /* Function prototypes */
24 int node (int argc,char *argv[]);
25
26
27 /* **********************************************************************
28  * Node code
29  * **********************************************************************/
30
31 /* Global private data */
32 typedef struct {
33   gras_socket_t sock; /* server socket on which I hear */
34   int remaining_loop; /* loop to do until done */
35   int create;         /* I have to create the token */
36   gras_socket_t tosuccessor; /* how to connect to next peer on ring */
37 } node_data_t;
38
39
40 /* Callback function */
41 static int node_cb_stoken_handler(gras_socket_t  expeditor,
42                                   void          *payload_data) {
43   
44   xbt_ex_t e;
45   
46   /* 1. Get the payload into the msg variable */
47   int msg=*(int*)payload_data;
48  
49
50   /* 2. Retrieve the node's state (globals) */
51   node_data_t *globals=(node_data_t*)gras_userdata_get();
52  
53   /* 3. Log which predecessor connected */
54   int supersteps = 1;
55   if (NBLOOPS >= 1000) {
56     supersteps = 100;
57   } else if (NBLOOPS >= 100) {
58     supersteps = 10;
59   } 
60   if (globals->create && (! (globals->remaining_loop % supersteps))) {
61     INFO1("Begin a new loop. Still to do: %d", globals->remaining_loop);
62   } else if (! (globals->remaining_loop % supersteps)) {
63     VERB3("Got token(%d) from %s remaining_loop=%d", 
64           msg, gras_socket_peer_name(expeditor),globals->remaining_loop);
65   }
66   
67   if (globals->remaining_loop > 0) {
68      
69     msg += 1;
70      
71     /* 5. I forward it to my successor */
72     DEBUG3("Send token(%d) to %s:%d",
73            msg, 
74            gras_socket_peer_name(globals->tosuccessor),
75            gras_socket_peer_port(globals->tosuccessor));
76      
77      
78     /* 6. Send it as payload of a stoken message to the successor */
79     TRY {
80       gras_msg_send(globals->tosuccessor, 
81                     gras_msgtype_by_name("stoken"), &msg);
82      
83     /* 7. Deal with errors */
84     } CATCH(e) {
85       gras_socket_close(globals->sock);
86       RETHROW0("Unable to forward token: %s");
87     }
88   
89   }
90               
91   /* DO NOT CLOSE THE expeditor SOCKET since the client socket is
92      reused by our predecessor.
93      Closing this side would thus create troubles */
94   
95   /* 8. Decrease the remaining_loop integer. */
96   globals->remaining_loop -= 1;
97    
98   /* 9. Repport the hop number to the user at the end */
99   if (globals->remaining_loop == -1 && globals->create) {
100     INFO1("Shut down the token-ring. There was %d hops.",msg);
101   }
102
103   /* 10. Tell GRAS that we consummed this message */
104   return 1;
105 } /* end_of_node_cb_stoken_handler */
106
107
108 int node (int argc,char *argv[]) {
109   node_data_t *globals;
110   
111   const char *host;
112   int   myport;
113   int   peerport;
114     
115   xbt_ex_t e;
116   
117   /* 1. Init the GRAS infrastructure and declare my globals */
118   gras_init(&argc,argv);
119   globals=gras_userdata_new(node_data_t);
120   
121   
122   /* 2. Get the successor's address. The command line overrides
123         defaults when specified */
124   host = "127.0.0.1";
125   myport = 4000;
126   peerport = 4000;
127   if (argc >= 4) {
128     myport=atoi(argv[1]);
129     host=argv[2];
130     peerport=atoi(argv[3]);
131   }
132
133   /* 3. Save successor's address in global var */
134   globals->remaining_loop=NBLOOPS;
135   globals->create = 0;
136   globals->tosuccessor = NULL;
137         
138   INFO4("Launch node %d (successor on %s:%d; listening on %d)",
139         gras_os_getpid(), host,peerport, myport);
140
141   /* 4. Create my master socket for listening */
142   globals->sock = gras_socket_server(myport);
143   gras_os_sleep(1.0); /* Make sure all server sockets are created */
144
145
146   /* 5. Create socket to the successor on the ring */
147   DEBUG2("Connect to my successor on %s:%d",host,peerport);
148
149   globals->tosuccessor = gras_socket_client(host,peerport);
150   
151   /* 6. Register the known messages. This function is called twice here,
152         but it's because this file also acts as regression test.
153         No need to do so yourself of course. */
154   register_messages();
155   register_messages(); /* just to make sure it works ;) */
156    
157   /* 7. Register my callback */
158   gras_cb_register(gras_msgtype_by_name("stoken"),&node_cb_stoken_handler);
159   
160
161   /* 8. One node has to create the token at startup. 
162         It's specified by a command line argument */
163   if (argc >= 5 && !strncmp("--create-token", argv[4],strlen(argv[4])))
164     globals->create=1;
165
166   if (globals->create) {
167     int token = 0;
168             
169     globals->remaining_loop = NBLOOPS - 1;
170       
171     INFO3("Create the token (with value %d) and send it to %s:%d",
172           token, host, peerport);
173
174     TRY {         
175       gras_msg_send(globals->tosuccessor,
176                   gras_msgtype_by_name("stoken"), &token);
177     } CATCH(e) {
178       RETHROW0("Unable to send the freshly created token: %s");
179     }
180   } 
181   
182   /* 8. Wait up to 10 seconds for an incomming message to handle */
183   while (globals->remaining_loop > (globals->create ? -1 : 0)) {
184     gras_msg_handle(10.0);
185   
186     DEBUG1("looping (remaining_loop=%d)", globals->remaining_loop);
187   }
188
189   gras_os_sleep(1.0); /* FIXME: if the sender quited, receive fail */
190
191   /* 9. Free the allocated resources, and shut GRAS down */ 
192   gras_socket_close(globals->sock);
193   gras_socket_close(globals->tosuccessor);
194   free(globals);
195   gras_exit();
196   
197   return 0;
198 } /* end_of_node */