Logo AND Algorithmique Numérique Distribuée

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