Logo AND Algorithmique Numérique Distribuée

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