Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / examples / gras / spawn / spawn_child.c
1 /* spawn - demo of the gras_agent_spawn function                            */
2
3 /* Copyright (c) 2007 Martin Quinson. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "spawn.h"
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Spawn);
10
11 int child(int argc, char *argv[])
12 {
13   xbt_ex_t e;
14   gras_socket_t dady = NULL;    /* peer */
15
16   gras_socket_t from;
17   int ping, pong;
18
19   const char *host = gras_os_myname();
20   int port = 4000;
21
22   /* 1. Init the GRAS's infrastructure */
23   gras_init(&argc, argv);
24
25   /* 2. Get the server's address. The command line override defaults when specified */
26   if (argc == 2) {
27     port = atoi(argv[1]);
28   }
29
30   gras_socket_server_range(4000, 5000, 0, 0);
31
32   /* 3. Connect back to my father */
33   TRY {
34     dady = gras_socket_client(host, port);
35   }
36   CATCH(e) {
37     RETHROW0("Unable to connect to my dady: %s");
38   }
39   INFO4("I (%s:%d) have found my dady on %s:%d.",
40         gras_os_myname(), gras_os_myport(), host, port);
41
42
43   /* 4. Register the messages. */
44   spawn_register_messages();
45
46   /* 5. Ping my dady */
47   ping = 1234;
48   TRY {
49     gras_msg_send(dady, "ping", &ping);
50   }
51   CATCH(e) {
52     gras_socket_close(dady);
53     RETHROW0("Failed to ping my dady: %s");
54   }
55
56   /* 6. Wait for the answer from the server, and deal with issues */
57   TRY {
58     gras_msg_wait(6000, "pong", &from, &pong);
59   }
60   CATCH(e) {
61     gras_socket_close(dady);
62     RETHROW0("Dad don't want to speak with me! : %s");
63   }
64   INFO2("Pinged dad with %d, he answered with %d; leaving now.", ping, pong);
65
66   /* 7. Free the allocated resources, and shut GRAS down */
67   gras_socket_close(dady);
68   gras_exit();
69   return 0;
70 }                               /* end_of_child */