1 /* stoken - simple/static token ring */
3 /* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
12 /*#define NBLOOPS 30000*/
14 XBT_LOG_NEW_DEFAULT_CATEGORY(SimpleToken,
15 "Messages specific to this example");
17 /* Function prototypes */
18 int node(int argc, char *argv[]);
21 /* **********************************************************************
23 * **********************************************************************/
25 /* Global private data */
27 gras_socket_t sock; /* server socket on which I hear */
28 int remaining_loop; /* number of loops to do until done */
29 int create; /* whether I have to create the token */
30 gras_socket_t tosuccessor; /* how to connect to the successor on ring */
31 double start_time; /* to measure the elapsed time. Only used by the
32 node that creates the token */
36 /* Callback function */
37 static int node_cb_stoken_handler(gras_msg_cb_ctx_t ctx, void *payload)
39 /* 1. Get the payload into the msg variable, and retrieve my caller */
40 int msg = *(int *) payload;
41 gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
44 /* 2. Retrieve the node's state (globals) */
45 node_data_t *globals = (node_data_t *) gras_userdata_get();
47 /* 3. Log which predecessor connected */
48 int supersteps = 1; /* only log every superstep loop */
49 if (NBLOOPS >= 1000) {
51 } else if (NBLOOPS >= 100) {
53 } else if (NBLOOPS <= 10) {
56 if (globals->create && (!(globals->remaining_loop % supersteps))) {
57 XBT_INFO("Begin a new loop. Still to do: %d", globals->remaining_loop);
58 } else if (!(globals->remaining_loop % supersteps)) {
59 XBT_VERB("Got token(%d) from %s remaining_loop=%d",
60 msg, gras_socket_peer_name(expeditor), globals->remaining_loop);
63 /* 4. If the right shouldn't be stopped yet */
64 if (globals->remaining_loop > 0) {
67 XBT_DEBUG("Send token(%d) to %s:%d", msg,
68 gras_socket_peer_name(globals->tosuccessor),
69 gras_socket_peer_port(globals->tosuccessor));
71 /* 5. Send the token as payload of a stoken message to the successor */
73 gras_msg_send(globals->tosuccessor, "stoken", &msg);
75 /* 6. Deal with errors */
78 gras_socket_close(globals->sock);
79 RETHROWF("Unable to forward token: %s");
84 /* DO NOT CLOSE THE expeditor SOCKET since the client socket is
85 reused by our predecessor.
86 Closing this side would thus create troubles */
88 /* 7. Decrease the remaining_loop integer. */
89 globals->remaining_loop -= 1;
91 /* 8. Repport the hop number to the user at the end */
92 if (globals->remaining_loop == -1 && globals->create) {
93 double elapsed = gras_os_time() - globals->start_time;
94 XBT_INFO("Shut down the token-ring. There was %d hops.", msg);
95 XBT_VERB("Elapsed time: %g", elapsed);
98 /* 9. Tell GRAS that we consummed this message */
100 } /* end_of_node_cb_stoken_handler */
102 int node(int argc, char *argv[])
104 node_data_t *globals;
110 /* 1. Init the GRAS infrastructure and declare my globals */
111 gras_init(&argc, argv);
112 globals = gras_userdata_new(node_data_t);
115 /* 2. Get the successor's address. The command line overrides
116 defaults when specified */
121 myport = atoi(argv[1]);
123 peerport = atoi(argv[3]);
126 /* 3. Save successor's address in global var */
127 globals->remaining_loop = NBLOOPS;
129 globals->tosuccessor = NULL;
131 if (!gras_os_getpid() % 100 || gras_if_RL())
132 XBT_INFO("Launch node listening on %d (successor on %s:%d)",
133 myport, host, peerport);
135 /* 4. Register the known messages. */
136 gras_msgtype_declare("stoken", gras_datadesc_by_name("int"));
138 /* 5. Create my master socket for listening */
139 globals->sock = gras_socket_server(myport);
140 gras_os_sleep(1.0); /* Make sure all server sockets are created */
142 /* 6. Create socket to the successor on the ring */
143 XBT_DEBUG("Connect to my successor on %s:%d", host, peerport);
145 globals->tosuccessor = gras_socket_client(host, peerport);
147 /* 7. Register my callback */
148 gras_cb_register("stoken", &node_cb_stoken_handler);
150 /* 8. One node has to create the token at startup.
151 It's specified by a command line argument */
153 && !strncmp("--create-token", argv[4], strlen("--create-token")))
156 if (globals->create) {
159 globals->start_time = gras_os_time();
161 globals->remaining_loop = NBLOOPS - 1;
163 XBT_INFO("Create the token (with value %d) and send it to %s:%d",
164 token, host, peerport);
167 gras_msg_send(globals->tosuccessor, "stoken", &token);
170 RETHROWF("Unable to send the freshly created token: %s");
174 /* 8. Wait up to 10 seconds for an incomming message to handle */
175 while (globals->remaining_loop > (globals->create ? -1 : 0)) {
178 XBT_DEBUG("looping (remaining_loop=%d)", globals->remaining_loop);
181 gras_os_sleep(1.0); /* FIXME: if the sender quited, receive fail */
183 /* 9. Free the allocated resources, and shut GRAS down */
184 gras_socket_close(globals->sock);
185 gras_socket_close(globals->tosuccessor);