Logo AND Algorithmique Numérique Distribuée

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