Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
refactored smpi into multiple source files.
[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   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   } 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   } CATCH(e) {
51     gras_socket_close(dady);
52     RETHROW0("Failed to ping my dady: %s");
53   }
54
55   /* 6. Wait for the answer from the server, and deal with issues */
56   TRY {
57     gras_msg_wait(6000,"pong", &from,&pong);
58   } CATCH(e) {
59     gras_socket_close(dady);
60     RETHROW0("Dad don't want to speak with me! : %s");
61   }
62   INFO2("Pinged dad with %d, he answered with %d; leaving now.", ping, pong);
63    
64   /* 7. Free the allocated resources, and shut GRAS down */
65   gras_socket_close(dady);
66   gras_exit();
67   return 0;
68 } /* end_of_child */