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)
42 /* 1. Get the payload into the msg variable, and retrieve my caller */
43 int msg = *(int *) payload;
44 gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
47 /* 2. Retrieve the node's state (globals) */
48 node_data_t *globals = (node_data_t *) gras_userdata_get();
50 /* 3. Log which predecessor connected */
51 int supersteps = 1; /* only log every superstep loop */
52 if (NBLOOPS >= 1000) {
54 } else if (NBLOOPS >= 100) {
56 } else if (NBLOOPS <= 10) {
59 if (globals->create && (!(globals->remaining_loop % supersteps))) {
60 XBT_INFO("Begin a new loop. Still to do: %d", globals->remaining_loop);
61 } else if (!(globals->remaining_loop % supersteps)) {
62 XBT_VERB("Got token(%d) from %s remaining_loop=%d",
63 msg, gras_socket_peer_name(expeditor), globals->remaining_loop);
66 /* 4. If the right shouldn't be stopped yet */
67 if (globals->remaining_loop > 0) {
70 XBT_DEBUG("Send token(%d) to %s:%d", msg,
71 gras_socket_peer_name(globals->tosuccessor),
72 gras_socket_peer_port(globals->tosuccessor));
74 /* 5. Send the token as payload of a stoken message to the successor */
76 gras_msg_send(globals->tosuccessor, "stoken", &msg);
78 /* 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 XBT_INFO("Shut down the token-ring. There was %d hops.", msg);
98 XBT_VERB("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[])
107 node_data_t *globals;
115 /* 1. Init the GRAS infrastructure and declare my globals */
116 gras_init(&argc, argv);
117 globals = gras_userdata_new(node_data_t);
120 /* 2. Get the successor's address. The command line overrides
121 defaults when specified */
126 myport = atoi(argv[1]);
128 peerport = atoi(argv[3]);
131 /* 3. Save successor's address in global var */
132 globals->remaining_loop = NBLOOPS;
134 globals->tosuccessor = NULL;
136 if (!gras_os_getpid() % 100 || gras_if_RL())
137 XBT_INFO("Launch node listening on %d (successor on %s:%d)",
138 myport, host, peerport);
140 /* 4. Register the known messages. */
141 gras_msgtype_declare("stoken", gras_datadesc_by_name("int"));
143 /* 5. Create my master socket for listening */
144 globals->sock = gras_socket_server(myport);
145 gras_os_sleep(1.0); /* Make sure all server sockets are created */
147 /* 6. Create socket to the successor on the ring */
148 XBT_DEBUG("Connect to my successor on %s:%d", host, peerport);
150 globals->tosuccessor = gras_socket_client(host, peerport);
152 /* 7. Register my callback */
153 gras_cb_register("stoken", &node_cb_stoken_handler);
155 /* 8. One node has to create the token at startup.
156 It's specified by a command line argument */
158 && !strncmp("--create-token", argv[4], strlen("--create-token")))
161 if (globals->create) {
164 globals->start_time = gras_os_time();
166 globals->remaining_loop = NBLOOPS - 1;
168 XBT_INFO("Create the token (with value %d) and send it to %s:%d",
169 token, host, peerport);
172 gras_msg_send(globals->tosuccessor, "stoken", &token);
174 RETHROW0("Unable to send the freshly created token: %s");
178 /* 8. Wait up to 10 seconds for an incomming message to handle */
179 while (globals->remaining_loop > (globals->create ? -1 : 0)) {
182 XBT_DEBUG("looping (remaining_loop=%d)", globals->remaining_loop);
185 gras_os_sleep(1.0); /* FIXME: if the sender quited, receive fail */
187 /* 9. Free the allocated resources, and shut GRAS down */
188 gras_socket_close(globals->sock);
189 gras_socket_close(globals->tosuccessor);