Logo AND Algorithmique Numérique Distribuée

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