Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f359e0e6cba2e0de187075f2b1fab2675dd2f4d1
[simgrid.git] / src / amok / HostManagement / hostmanagement.c
1 /* $Id$ */
2
3 /* amok host management - servers main loop and remote host stopping        */
4
5 /* Copyright (c) 2006 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 "xbt/sysdep.h"
11 #include "xbt/host.h"
12 #include "amok/hostmanagement.h"
13 #include "gras/Virtu/virtu_interface.h" /* libdata */
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(amok_hm,amok,"Host management");
16
17
18 /* libdata management */
19 static int amok_hm_libdata_id;
20 typedef struct {
21   int done;
22   xbt_dict_t groups;
23 } s_amok_hm_libdata_t, *amok_hm_libdata_t;
24
25 static void *amok_hm_libdata_new() {
26   amok_hm_libdata_t res=xbt_new(s_amok_hm_libdata_t,1);
27   res->done = 0;
28   res->groups = xbt_dict_new();
29   return res;
30 }
31 static void amok_hm_libdata_free(void *d) {
32   amok_hm_libdata_t data=(amok_hm_libdata_t)d;
33   xbt_dict_free(&data->groups);
34   free (data);
35 }
36
37 /* Message callbacks */
38 static int amok_hm_cb_kill(gras_msg_cb_ctx_t ctx,
39                            void             *payload_data) {
40
41   amok_hm_libdata_t g=gras_libdata_by_id(amok_hm_libdata_id);
42   g->done = 1;
43   return 1;
44 }
45 static int amok_hm_cb_killrpc(gras_msg_cb_ctx_t ctx,
46                               void             *payload_data) {
47
48   amok_hm_libdata_t g=gras_libdata_by_id(amok_hm_libdata_id);
49   g->done = 1;
50   gras_msg_rpcreturn(30,ctx,NULL);
51   return 1;
52 }
53
54 static int amok_hm_cb_get(gras_msg_cb_ctx_t ctx, void *payload) {
55   amok_hm_libdata_t g=gras_libdata_by_id(amok_hm_libdata_id);
56   char *name = *(void**)payload;
57   xbt_dynar_t res = xbt_dict_get(g->groups, name);
58
59   gras_msg_rpcreturn(30, ctx, &res);
60   return 1;
61 }
62 static int amok_hm_cb_join(gras_msg_cb_ctx_t ctx, void *payload) {
63   amok_hm_libdata_t g=gras_libdata_by_id(amok_hm_libdata_id);
64   char *name = *(void**)payload;
65   xbt_dynar_t group = xbt_dict_get(g->groups, name);
66   
67   gras_socket_t exp = gras_msg_cb_ctx_from(ctx);
68   xbt_host_t dude = xbt_host_new(gras_socket_peer_name(exp),
69                                  gras_socket_peer_port(exp));
70
71   xbt_dynar_push(group,&dude);
72
73   gras_msg_rpcreturn(30, ctx, NULL);
74   return 1;
75 }
76 static int amok_hm_cb_leave(gras_msg_cb_ctx_t ctx, void *payload) {
77   amok_hm_libdata_t g=gras_libdata_by_id(amok_hm_libdata_id);
78   char *name = *(void**)payload;
79   xbt_dynar_t group = xbt_dict_get(g->groups, name);
80   
81   gras_socket_t exp = gras_msg_cb_ctx_from(ctx);
82   xbt_host_t dude = xbt_host_new(gras_socket_peer_name(exp),
83                                  gras_socket_peer_port(exp));
84
85   int cpt;
86   xbt_host_t host_it;
87
88   xbt_dynar_foreach(group, cpt, host_it) {
89     if (!strcmp(host_it->name, dude->name) && 
90         host_it->port == dude->port) {
91       xbt_dynar_cursor_rm (group,&cpt);
92       goto end;
93     }
94   }
95   WARN3("Asked to remove %s:%d from group '%s', but not found. Ignoring",
96         dude->name,dude->port, name);
97
98  end:
99   gras_msg_rpcreturn(30, ctx, NULL);
100   return 1;
101 }
102
103 static int amok_hm_cb_shutdown(gras_msg_cb_ctx_t ctx, void *payload) {
104   char *name = *(void**)payload;
105   amok_hm_group_shutdown_local(name);
106
107   gras_msg_rpcreturn(30, ctx, NULL);
108   return 1;
109 }
110
111
112 /* Initialization stuff */
113 static short amok_hm_used = 0;
114 void amok_hm_init() {
115   /* pure INIT part */
116   if (! amok_hm_used) {
117
118     /* dependencies */
119     amok_base_init();
120
121     /* module data on each process */
122     amok_hm_libdata_id = gras_procdata_add("amok_hm",
123                                            amok_hm_libdata_new,
124                                            amok_hm_libdata_free);
125
126     /* Datatype and message declarations */
127     gras_msgtype_declare("amok_hm_kill",NULL);   
128     gras_msgtype_declare_rpc("amok_hm_kill",NULL,NULL);   
129
130     gras_msgtype_declare_rpc("amok_hm_get",
131                              gras_datadesc_by_name("string"),
132                              gras_datadesc_by_name("xbt_dynar_t"));
133     gras_msgtype_declare_rpc("amok_hm_join",
134                              gras_datadesc_by_name("string"),
135                              NULL);
136     gras_msgtype_declare_rpc("amok_hm_leave",
137                              gras_datadesc_by_name("string"),
138                              NULL);
139
140     gras_msgtype_declare_rpc("amok_hm_shutdown",
141                              gras_datadesc_by_name("string"),
142                              NULL);
143   }
144   amok_hm_used++;
145
146   /* JOIN part */
147   gras_cb_register(gras_msgtype_by_name("amok_hm_kill"),
148                    &amok_hm_cb_kill);
149   gras_cb_register(gras_msgtype_by_name("amok_hm_killrpc"),
150                    &amok_hm_cb_killrpc);
151
152   gras_cb_register(gras_msgtype_by_name("amok_hm_get"),
153                    &amok_hm_cb_get);
154   gras_cb_register(gras_msgtype_by_name("amok_hm_join"),
155                    &amok_hm_cb_join);
156   gras_cb_register(gras_msgtype_by_name("amok_hm_leave"),
157                    &amok_hm_cb_leave);
158   gras_cb_register(gras_msgtype_by_name("amok_hm_shutdown"),
159                    &amok_hm_cb_shutdown);
160 }
161
162
163
164 void amok_hm_exit() {
165   /* pure EXIT part */
166   amok_hm_used--;
167
168   /* LEAVE part */
169   gras_cb_unregister(gras_msgtype_by_name("amok_hm_kill"),
170                      &amok_hm_cb_kill);
171   gras_cb_unregister(gras_msgtype_by_name("amok_hm_killrpc"),
172                      &amok_hm_cb_killrpc);
173
174   gras_cb_unregister(gras_msgtype_by_name("amok_hm_get"),
175                      &amok_hm_cb_get);
176   gras_cb_unregister(gras_msgtype_by_name("amok_hm_join"),
177                      &amok_hm_cb_join);
178   gras_cb_unregister(gras_msgtype_by_name("amok_hm_leave"),
179                      &amok_hm_cb_leave);
180
181   gras_cb_unregister(gras_msgtype_by_name("amok_hm_shutdown"),
182                      &amok_hm_cb_shutdown);
183 }
184
185
186 void amok_hm_mainloop(double timeOut) {
187   amok_hm_libdata_t g=gras_libdata_by_id(amok_hm_libdata_id);
188   
189   while (!g->done) {
190     gras_msg_handle(timeOut);
191   }
192 }
193
194 /** \brief kill a buddy identified by its hostname and port */
195 void amok_hm_kill_hp(char *name,int port) {
196   gras_socket_t sock=gras_socket_client(name,port);
197   amok_hm_kill(sock);
198   gras_socket_close(sock);
199 }
200
201 /** \brief kill a buddy to which we have a socket already */
202 void amok_hm_kill(gras_socket_t buddy) {
203   gras_msg_send(buddy,gras_msgtype_by_name("amok_hm_kill"),NULL);
204 }
205
206 /** \brief kill syncronously a buddy (do not return before its death) */
207 void amok_hm_kill_sync(gras_socket_t buddy) {
208   gras_msg_rpccall(buddy,30,gras_msgtype_by_name("amok_hm_killrpc"),NULL,NULL);
209 }
210
211
212 /** \brief create a new hostmanagement group located on local host 
213  *
214  * The dynar elements are of type xbt_host_t
215  */
216 xbt_dynar_t amok_hm_group_new(char *group_name) {
217   amok_hm_libdata_t g=gras_libdata_by_id(amok_hm_libdata_id);
218   xbt_dynar_t res = xbt_dynar_new(sizeof(xbt_host_t),
219                                   &xbt_host_free_voidp);
220
221   xbt_dict_set(g->groups,group_name,res,&xbt_dynar_free_voidp);
222
223   return res;
224 }
225 /** \brief retrieve all members of the given remote group */
226 xbt_dynar_t amok_hm_group_get(gras_socket_t master, char *group_name) {
227   xbt_dynar_t res;
228   
229   gras_msg_rpccall(master,30,gras_msgtype_by_name("amok_hm_get"),
230                    &group_name,&res);
231   return res;
232 }
233
234 /** \brief add current host to the given remote group */
235 void        amok_hm_group_join(gras_socket_t master, char *group_name) {
236   gras_msg_rpccall(master,30,gras_msgtype_by_name("amok_hm_join"),
237                    &group_name,NULL);
238 }
239 /** \brief remove current host from the given remote group if found
240  *
241  * If not found, call is ignored 
242  */
243 void        amok_hm_group_leave(gras_socket_t master, char *group_name) {
244   gras_msg_rpccall(master,30,gras_msgtype_by_name("amok_hm_leave"),
245                    &group_name,NULL);
246 }
247
248 /** \brief stops all members of the given local group */
249 void amok_hm_group_shutdown_local(char *group_name) {
250   amok_hm_libdata_t g=gras_libdata_by_id(amok_hm_libdata_id);
251   xbt_dynar_t group = xbt_dict_get(g->groups, group_name);
252   
253   int cpt;
254   xbt_host_t host_it;
255
256   xbt_dynar_foreach(group, cpt, host_it) {
257     amok_hm_kill_hp(host_it->name, host_it->port);
258   }
259
260   xbt_dict_remove(g->groups,group_name);
261 }
262 /** \brief stops all members of the given remote group */
263 void amok_hm_group_shutdown_remote(gras_socket_t master, char *group_name){
264   gras_msg_rpccall(master,30,gras_msgtype_by_name("amok_hm_shutdown"),
265                    &group_name,NULL);
266 }