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. */
16 XBT_LOG_NEW_DEFAULT_CATEGORY(Token,"Messages specific to this example");
18 /* register messages which may be sent */
19 static void register_messages(void) {
20 gras_msgtype_declare("stoken", gras_datadesc_by_name("int"));
23 /* Function prototypes */
24 int node (int argc,char *argv[]);
27 /* **********************************************************************
29 * **********************************************************************/
31 /* Global private data */
33 gras_socket_t sock; /* server socket on which I hear */
34 int remaining_loop; /* loop to do until done */
35 int create; /* I have to create the token */
36 gras_socket_t tosuccessor; /* how to connect to next peer on ring */
40 /* Callback function */
41 static int node_cb_stoken_handler(gras_socket_t expeditor,
46 /* 1. Get the payload into the msg variable */
47 int msg=*(int*)payload_data;
50 /* 2. Retrieve the node's state (globals) */
51 node_data_t *globals=(node_data_t*)gras_userdata_get();
53 /* 3. Log which predecessor connected */
55 if (NBLOOPS >= 1000) {
57 } else if (NBLOOPS >= 100) {
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 if (globals->remaining_loop > 0) {
71 /* 5. I forward it to my successor */
72 DEBUG3("Send token(%d) to %s:%d",
74 gras_socket_peer_name(globals->tosuccessor),
75 gras_socket_peer_port(globals->tosuccessor));
78 /* 6. Send it as payload of a stoken message to the successor */
80 gras_msg_send(globals->tosuccessor,
81 gras_msgtype_by_name("stoken"), &msg);
83 /* 7. Deal with errors */
85 gras_socket_close(globals->sock);
86 RETHROW0("Unable to forward token: %s");
91 /* DO NOT CLOSE THE expeditor SOCKET since the client socket is
92 reused by our predecessor.
93 Closing this side would thus create troubles */
95 /* 9. Decrease the remaining_loop integer. */
96 globals->remaining_loop -= 1;
98 /* 10. Repport the hop number to the user at the end */
99 if (globals->remaining_loop == -1 && globals->create) {
100 INFO1("Shut down the token-ring. There was %d hops.",msg);
103 /* 11. Tell GRAS that we consummed this message */
105 } /* end_of_node_cb_stoken_handler */
108 int node (int argc,char *argv[]) {
109 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 INFO4("Launch node %d (successor on %s:%d; listening on %d)",
137 gras_os_getpid(), host,peerport, myport);
139 /* 4. Create my master socket for listening */
140 globals->sock = gras_socket_server(myport);
141 gras_os_sleep(1.0); /* Make sure all server sockets are created */
144 /* 5. Create socket to the successor on the ring */
145 DEBUG2("Connect to my successor on %s:%d",host,peerport);
147 globals->tosuccessor = gras_socket_client(host,peerport);
149 /* 6. Register the known messages. This function is called twice here,
150 but it's because this file also acts as regression test.
151 No need to do so yourself of course. */
153 register_messages(); /* just to make sure it works ;) */
155 /* 7. Register my callback */
156 gras_cb_register(gras_msgtype_by_name("stoken"),&node_cb_stoken_handler);
159 /* 8. One node has to create the token at startup.
160 It's specified by a command line argument */
161 if (argc >= 5 && !strncmp("--create-token", argv[4],strlen(argv[4])))
164 if (globals->create) {
167 globals->remaining_loop = NBLOOPS - 1;
169 INFO3("Create the token (with value %d) and send it to %s:%d",
170 token, host, peerport);
172 gras_msg_send(globals->tosuccessor,
173 gras_msgtype_by_name("stoken"), &token);
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);