Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move those files a second time today: automake don't like me using '-' in names,...
[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, 
76                     gras_msgtype_by_name("stoken"), &msg);
77      
78     /* 6. Deal with errors */
79     } CATCH(e) {
80       gras_socket_close(globals->sock);
81       RETHROW0("Unable to forward token: %s");
82     }
83   
84   }
85               
86   /* DO NOT CLOSE THE expeditor SOCKET since the client socket is
87      reused by our predecessor.
88      Closing this side would thus create troubles */
89   
90   /* 7. Decrease the remaining_loop integer. */
91   globals->remaining_loop -= 1;
92    
93   /* 8. Repport the hop number to the user at the end */
94   if (globals->remaining_loop == -1 && globals->create) {
95     INFO1("Shut down the token-ring. There was %d hops.",msg);
96   }
97
98   /* 9. Tell GRAS that we consummed this message */
99   return 1;
100 } /* end_of_node_cb_stoken_handler */
101
102 int node (int argc,char *argv[]) {
103   node_data_t *globals;
104   
105   const char *host;
106   int   myport;
107   int   peerport;
108     
109   xbt_ex_t e;
110   
111   /* 1. Init the GRAS infrastructure and declare my globals */
112   gras_init(&argc,argv);
113   globals=gras_userdata_new(node_data_t);
114   
115   
116   /* 2. Get the successor's address. The command line overrides
117         defaults when specified */
118   host = "127.0.0.1";
119   myport = 4000;
120   peerport = 4000;
121   if (argc >= 4) {
122     myport=atoi(argv[1]);
123     host=argv[2];
124     peerport=atoi(argv[3]);
125   }
126
127   /* 3. Save successor's address in global var */
128   globals->remaining_loop=NBLOOPS;
129   globals->create = 0;
130   globals->tosuccessor = NULL;
131
132   if (!gras_os_getpid() % 100)
133   INFO4("Launch node %ld (successor on %s:%d; listening on %d)",
134         gras_os_getpid(), host,peerport, myport);
135
136   /* 4. Create my master socket for listening */
137   globals->sock = gras_socket_server(myport);
138   gras_os_sleep(1.0); /* Make sure all server sockets are created */
139
140   /* 5. Create socket to the successor on the ring */
141   DEBUG2("Connect to my successor on %s:%d",host,peerport);
142
143   globals->tosuccessor = gras_socket_client(host,peerport);
144   
145   /* 6. Register the known messages.  */
146   gras_msgtype_declare("stoken", gras_datadesc_by_name("int"));
147    
148   /* 7. Register my callback */
149   gras_cb_register(gras_msgtype_by_name("stoken"),&node_cb_stoken_handler);  
150
151   /* 8. One node has to create the token at startup. 
152         It's specified by a command line argument */
153   if (argc >= 5 && !strncmp("--create-token", argv[4],strlen(argv[4])))
154     globals->create=1;
155
156   if (globals->create) {
157     int token = 0;
158             
159     globals->remaining_loop = NBLOOPS - 1;
160       
161     INFO3("Create the token (with value %d) and send it to %s:%d",
162           token, host, peerport);
163
164     TRY {         
165       gras_msg_send(globals->tosuccessor,
166                   gras_msgtype_by_name("stoken"), &token);
167     } CATCH(e) {
168       RETHROW0("Unable to send the freshly created token: %s");
169     }
170   } 
171   
172   /* 8. Wait up to 10 seconds for an incomming message to handle */
173   while (globals->remaining_loop > (globals->create ? -1 : 0)) {
174     gras_msg_handle(10.0);
175   
176     DEBUG1("looping (remaining_loop=%d)", globals->remaining_loop);
177   }
178
179   gras_os_sleep(1.0); /* FIXME: if the sender quited, receive fail */
180
181   /* 9. Free the allocated resources, and shut GRAS down */ 
182   gras_socket_close(globals->sock);
183   gras_socket_close(globals->tosuccessor);
184   free(globals);
185   gras_exit();
186   
187   return 0;
188 } /* end_of_node */