Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
acc35238e8def4607f5cd470f1f43c458b28f4de
[simgrid.git] / examples / gras / spawn / spawn_child.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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Spawn);
11
12 int child(int argc, char *argv[])
13 {
14   xbt_ex_t e;
15   gras_socket_t dady = NULL;    /* peer */
16
17   gras_socket_t from;
18   int ping, pong;
19
20   const char *host = gras_os_myname();
21   int port = 4000;
22
23   /* 1. Init the GRAS's infrastructure */
24   gras_init(&argc, argv);
25
26   /* 2. Get the server's address. The command line override defaults when specified */
27   if (argc == 2) {
28     port = atoi(argv[1]);
29   }
30
31   gras_socket_server_range(4000, 5000, 0, 0);
32
33   /* 3. Connect back to my father */
34   TRY {
35     dady = gras_socket_client(host, port);
36   }
37   CATCH(e) {
38     RETHROW0("Unable to connect to my dady: %s");
39   }
40   INFO4("I (%s:%d) have found my dady on %s:%d.",
41         gras_os_myname(), gras_os_myport(), host, port);
42
43
44   /* 4. Register the messages. */
45   spawn_register_messages();
46
47   /* 5. Ping my dady */
48   ping = 1234;
49   TRY {
50     gras_msg_send(dady, "ping", &ping);
51   }
52   CATCH(e) {
53     gras_socket_close(dady);
54     RETHROW0("Failed to ping my dady: %s");
55   }
56
57   /* 6. Wait for the answer from the server, and deal with issues */
58   TRY {
59     gras_msg_wait(6000, "pong", &from, &pong);
60   }
61   CATCH(e) {
62     gras_socket_close(dady);
63     RETHROW0("Dad don't want to speak with me! : %s");
64   }
65   INFO2("Pinged dad with %d, he answered with %d; leaving now.", ping,
66         pong);
67
68   /* 7. Free the allocated resources, and shut GRAS down */
69   gras_socket_close(dady);
70   gras_exit();
71   return 0;
72 }                               /* end_of_child */