3 /* stoken - simple/static token ring */
5 /* Copyright (c) 2005 Alexandre Colucci. */
6 /* Copyright (c) 2005 Martin Quinson. */
7 /* All rights reserved. */
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. */
15 /*#define NBLOOPS 30000*/
17 XBT_LOG_NEW_DEFAULT_CATEGORY(SimpleToken,"Messages specific to this example");
19 /* Function prototypes */
20 int node (int argc,char *argv[]);
23 /* **********************************************************************
25 * **********************************************************************/
27 /* Global private data */
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 double start_time; /* to measure the elapsed time. Only used by the
34 node that creates the token */
38 /* Callback function */
39 static int node_cb_stoken_handler(gras_msg_cb_ctx_t ctx, void *payload) {
43 /* 1. Get the payload into the msg variable, and retrieve my caller */
44 int msg=*(int*)payload;
45 gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
48 /* 2. Retrieve the node's state (globals) */
49 node_data_t *globals=(node_data_t*)gras_userdata_get();
51 /* 3. Log which predecessor connected */
52 int supersteps = 1; /* only log every superstep loop */
53 if (NBLOOPS >= 1000) {
55 } else if (NBLOOPS >= 100) {
57 } else if (NBLOOPS <=10) {
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);
67 /* 4. If the right shouldn't be stopped yet */
68 if (globals->remaining_loop > 0) {
71 DEBUG3("Send token(%d) to %s:%d", msg,
72 gras_socket_peer_name(globals->tosuccessor),
73 gras_socket_peer_port(globals->tosuccessor));
75 /* 5. Send the token as payload of a stoken message to the successor */
77 gras_msg_send(globals->tosuccessor, "stoken", &msg);
79 /* 6. Deal with errors */
81 gras_socket_close(globals->sock);
82 RETHROW0("Unable to forward token: %s");
87 /* DO NOT CLOSE THE expeditor SOCKET since the client socket is
88 reused by our predecessor.
89 Closing this side would thus create troubles */
91 /* 7. Decrease the remaining_loop integer. */
92 globals->remaining_loop -= 1;
94 /* 8. Repport the hop number to the user at the end */
95 if (globals->remaining_loop == -1 && globals->create) {
96 double elapsed = gras_os_time() - globals->start_time;
97 INFO1("Shut down the token-ring. There was %d hops.",msg);
98 VERB1("Elapsed time: %g", elapsed);
101 /* 9. Tell GRAS that we consummed this message */
103 } /* end_of_node_cb_stoken_handler */
105 int node (int argc,char *argv[]) {
106 node_data_t *globals;
114 /* 1. Init the GRAS infrastructure and declare my globals */
115 gras_init(&argc,argv);
116 globals=gras_userdata_new(node_data_t);
119 /* 2. Get the successor's address. The command line overrides
120 defaults when specified */
125 myport=atoi(argv[1]);
127 peerport=atoi(argv[3]);
130 /* 3. Save successor's address in global var */
131 globals->remaining_loop=NBLOOPS;
133 globals->tosuccessor = NULL;
135 if (!gras_os_getpid() % 100)
136 INFO4("Launch node %d (successor on %s:%d; listening on %d)",
137 gras_os_getpid(), host,peerport, myport);
139 /* 4. Register the known messages. */
140 gras_msgtype_declare("stoken", gras_datadesc_by_name("int"));
142 /* 5. Create my master socket for listening */
143 globals->sock = gras_socket_server(myport);
144 gras_os_sleep(1.0); /* Make sure all server sockets are created */
146 /* 6. Create socket to the successor on the ring */
147 DEBUG2("Connect to my successor on %s:%d",host,peerport);
149 globals->tosuccessor = gras_socket_client(host,peerport);
151 /* 7. Register my callback */
152 gras_cb_register("stoken",&node_cb_stoken_handler);
154 /* 8. One node has to create the token at startup.
155 It's specified by a command line argument */
156 if (argc >= 5 && !strncmp("--create-token", argv[4],strlen("--create-token")))
159 if (globals->create) {
162 globals->start_time = gras_os_time();
164 globals->remaining_loop = NBLOOPS - 1;
166 INFO3("Create the token (with value %d) and send it to %s:%d",
167 token, host, peerport);
170 gras_msg_send(globals->tosuccessor, "stoken", &token);
172 RETHROW0("Unable to send the freshly created token: %s");
176 /* 8. Wait up to 10 seconds for an incomming message to handle */
177 while (globals->remaining_loop > (globals->create ? -1 : 0)) {
178 gras_msg_handle(10.0);
180 DEBUG1("looping (remaining_loop=%d)", globals->remaining_loop);
183 gras_os_sleep(1.0); /* FIXME: if the sender quited, receive fail */
185 /* 9. Free the allocated resources, and shut GRAS down */
186 gras_socket_close(globals->sock);
187 gras_socket_close(globals->tosuccessor);