Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use private headers to help debugging
[simgrid.git] / examples / gras / spawn / spawn_father.c
1 /* $Id$ */
2
3 /* spawn - demo of the gras_agent_spawn function                            */
4
5 /* Copyright (c) 2007 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "spawn.h"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Spawn);
13
14 /* Global private data */
15 typedef struct {
16   gras_socket_t sock;
17   int msg_got;
18 } father_data_t;
19
20
21 static int father_cb_ping_handler(gras_msg_cb_ctx_t ctx, void *payload)
22 {
23
24   xbt_ex_t e;
25   /* 1. Get the payload into the msg variable, and retrieve my caller */
26   int msg = *(int *) payload;
27   gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
28
29   /* 2. Retrieve the father's state (globals) */
30   father_data_t *globals = (father_data_t *) gras_userdata_get();
31   globals->msg_got++;
32
33   /* 3. Log which client connected */
34   INFO3("Kid %s:%d pinged me with %d",
35         gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor),
36         msg);
37
38   /* 4. Change the value of the msg variable */
39   msg = 4321;
40   /* 5. Send it back as payload of a pong message to the expeditor */
41   TRY {
42     gras_msg_send(expeditor, "pong", &msg);
43
44     /* 6. Deal with errors: add some details to the exception */
45   } CATCH(e) {
46     gras_socket_close(globals->sock);
47     RETHROW0("Unable to answer to my poor child: %s");
48   }
49   INFO2("Answered to %s:%d's request",
50         gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor));
51
52   /* 7. Tell GRAS that we consummed this message */
53   return 0;
54 }                               /* end_of_father_cb_ping_handler */
55
56 int father(int argc, char *argv[])
57 {
58   father_data_t *globals;
59
60   int port = 4000;
61   int child_amount = 5;
62   char **child_args;
63   int i;
64
65   /* 1. Init the GRAS infrastructure and declare my globals */
66   gras_init(&argc, argv);
67   globals = gras_userdata_new(father_data_t);
68
69   /* 2. Get args from the command line, if specified */
70   if (argc == 2) {
71     port = atoi(argv[1]);
72     child_amount = atoi(argv[2]);
73   }
74
75   /* 3. Initialize my globals (mainly create my master socket) */
76   globals->sock = gras_socket_server(port);
77   globals->msg_got = 0;
78
79   /* 4. Register the known messages. */
80   spawn_register_messages();
81
82   /* 5. Register my callback */
83   gras_cb_register("ping", &father_cb_ping_handler);
84
85   /* 6. Spawn the kids */
86   INFO0("Spawn the kids");
87   for (i = 0; i < child_amount; i++) {
88     child_args = xbt_new0(char *, 3);
89     child_args[0] = xbt_strdup("child");
90     child_args[1] = bprintf("%d", port);
91     child_args[2] = NULL;
92     gras_agent_spawn("child", NULL, child, 2, child_args, NULL);
93   }
94
95   /* 7. Wait to be contacted be the kids */
96   while (globals->msg_got < child_amount)
97     gras_msg_handle(60.0);
98   INFO0("All kids gone. Leave now.");
99
100   /* 8. Free the allocated resources, and shut GRAS down */
101   gras_socket_close(globals->sock);
102   free(globals);
103   gras_exit();
104
105   return 0;
106 }                               /* end_of_father */