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(Token,"Messages specific to this example");
19 /* register messages which may be sent */
20 static void register_messages(void) {
21 gras_msgtype_declare("stoken", gras_datadesc_by_name("int"));
24 /* Function prototypes */
25 int node (int argc,char *argv[]);
28 /* **********************************************************************
30 * **********************************************************************/
32 /* Global private data */
34 gras_socket_t sock; /* server socket on which I hear */
35 int remaining_loop; /* loop to do until done */
36 int create; /* I have to create the token */
37 gras_socket_t tosuccessor; /* how to connect to next peer on ring */
41 /* Callback function */
42 static int node_cb_stoken_handler(gras_msg_cb_ctx_t ctx, void *payload) {
46 /* 1. Get the payload into the msg variable, and retrieve my caller */
47 int msg=*(int*)payload;
48 gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
51 /* 2. Retrieve the node's state (globals) */
52 node_data_t *globals=(node_data_t*)gras_userdata_get();
54 /* 3. Log which predecessor connected */
56 if (NBLOOPS >= 1000) {
58 } else if (NBLOOPS >= 100) {
60 } else if (NBLOOPS <=10) {
63 if (globals->create && (! (globals->remaining_loop % supersteps))) {
64 INFO1("Begin a new loop. Still to do: %d", globals->remaining_loop);
65 } else if (! (globals->remaining_loop % supersteps)) {
66 VERB3("Got token(%d) from %s remaining_loop=%d",
67 msg, gras_socket_peer_name(expeditor),globals->remaining_loop);
70 if (globals->remaining_loop > 0) {
74 /* 5. I forward it to my successor */
75 DEBUG3("Send token(%d) to %s:%d",
77 gras_socket_peer_name(globals->tosuccessor),
78 gras_socket_peer_port(globals->tosuccessor));
81 /* 6. Send it as payload of a stoken message to the successor */
83 gras_msg_send(globals->tosuccessor,
84 gras_msgtype_by_name("stoken"), &msg);
86 /* 7. Deal with errors */
88 gras_socket_close(globals->sock);
89 RETHROW0("Unable to forward token: %s");
94 /* DO NOT CLOSE THE expeditor SOCKET since the client socket is
95 reused by our predecessor.
96 Closing this side would thus create troubles */
98 /* 8. Decrease the remaining_loop integer. */
99 globals->remaining_loop -= 1;
101 /* 9. Repport the hop number to the user at the end */
102 if (globals->remaining_loop == -1 && globals->create) {
103 INFO1("Shut down the token-ring. There was %d hops.",msg);
106 /* 10. Tell GRAS that we consummed this message */
108 } /* end_of_node_cb_stoken_handler */
111 int node (int argc,char *argv[]) {
112 node_data_t *globals;
120 /* 1. Init the GRAS infrastructure and declare my globals */
121 gras_init(&argc,argv);
122 globals=gras_userdata_new(node_data_t);
125 /* 2. Get the successor's address. The command line overrides
126 defaults when specified */
131 myport=atoi(argv[1]);
133 peerport=atoi(argv[3]);
136 /* 3. Save successor's address in global var */
137 globals->remaining_loop=NBLOOPS;
139 globals->tosuccessor = NULL;
141 if (!gras_os_getpid() % 100)
142 INFO4("Launch node %ld (successor on %s:%d; listening on %d)",
143 gras_os_getpid(), host,peerport, myport);
145 /* 4. Create my master socket for listening */
146 globals->sock = gras_socket_server(myport);
147 gras_os_sleep(1.0); /* Make sure all server sockets are created */
149 /* 5. Create socket to the successor on the ring */
150 DEBUG2("Connect to my successor on %s:%d",host,peerport);
152 globals->tosuccessor = gras_socket_client(host,peerport);
154 /* 6. Register the known messages. This function is called twice here,
155 but it's because this file also acts as regression test.
156 No need to do so yourself of course. */
158 register_messages(); /* just to make sure it works ;) */
160 /* 7. Register my callback */
161 gras_cb_register(gras_msgtype_by_name("stoken"),&node_cb_stoken_handler);
164 /* 8. One node has to create the token at startup.
165 It's specified by a command line argument */
166 if (argc >= 5 && !strncmp("--create-token", argv[4],strlen(argv[4])))
169 if (globals->create) {
172 globals->remaining_loop = NBLOOPS - 1;
174 INFO3("Create the token (with value %d) and send it to %s:%d",
175 token, host, peerport);
178 gras_msg_send(globals->tosuccessor,
179 gras_msgtype_by_name("stoken"), &token);
181 RETHROW0("Unable to send the freshly created token: %s");
185 /* 8. Wait up to 10 seconds for an incomming message to handle */
186 while (globals->remaining_loop > (globals->create ? -1 : 0)) {
187 gras_msg_handle(10.0);
189 DEBUG1("looping (remaining_loop=%d)", globals->remaining_loop);
192 gras_os_sleep(1.0); /* FIXME: if the sender quited, receive fail */
194 /* 9. Free the allocated resources, and shut GRAS down */
195 gras_socket_close(globals->sock);
196 gras_socket_close(globals->tosuccessor);