3 /* ALLTOALL - alltoall of GRAS features */
5 /* Copyright (c) 2006 Ahmed Harbaoui. All rights reserved. */
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. */
13 XBT_LOG_NEW_DEFAULT_CATEGORY(alltoall,"Messages specific to this example");
15 /* register data which may be sent (common to client and server) */
16 static void register_messages(void) {
17 gras_msgtype_declare("data", gras_datadesc_by_name("int"));
20 /* Function prototypes */
21 int node (int argc,char *argv[]);
24 /* **********************************************************************
26 * **********************************************************************/
28 /* Global private data */
34 static void free_host(void *d){
35 xbt_host_t h=*(xbt_host_t*)d;
40 static void kill_buddy(char *name,int port){
41 gras_socket_t sock=gras_socket_client(name,port);
42 gras_msg_send(sock,gras_msgtype_by_name("kill"),NULL);
43 gras_socket_close(sock);
46 static void kill_buddy_dynar(void *b) {
47 xbt_host_t buddy=*(xbt_host_t*)b;
48 kill_buddy(buddy->name,buddy->port);
51 static int node_cb_data_handler(gras_msg_cb_ctx_t ctx,
54 /* Get the payload into the msg variable */
55 int data=*(int*)payload_data;
57 gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
59 /* Retrieve the server's state (globals) */
61 node_data_t *globals=(node_data_t*)gras_userdata_get();
64 /* Log which client connected */
65 INFO3(">>>>>>>> Got Data(%d) from %s:%d <<<<<<<<",
67 gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor));
69 /* Set the done boolean to true (and make sure the server stops after receiving it). */
72 /* Make sure we don't leak sockets */
73 //gras_socket_close(expeditor);
75 /* Tell GRAS that we consummed this message */
77 } /* end_of_server_cb_ping_handler */
79 int node (int argc,char *argv[]) {
83 int port,nb_hosts,data,
88 gras_socket_t peer; /* socket to node */
92 /* xbt_dynar for hosts */
93 xbt_dynar_t hosts = xbt_dynar_new(sizeof(xbt_host_t),&free_host);
95 /* Init the GRAS infrastructure and declare my globals */
96 gras_init(&argc,argv);
98 globals=gras_userdata_new(node_data_t *);
100 /* Get the port I should listen on from the command line, if specified */
105 /* Get the node location from argc/argv */
106 for (i=2; i<argc; i++){
107 xbt_host_t host=xbt_new(s_xbt_host_t,1);
108 host->name=strdup(argv[i]);
109 host->port=atoi(argv[1]);
110 INFO2("New node : %s:%d",host->name,host->port);
111 xbt_dynar_push(hosts,&host);
113 nb_hosts = xbt_dynar_length(hosts);
115 INFO1("Launch current node (port=%d)", port);
117 /* Create my master socket */
118 globals->sock = gras_socket_server(port);
120 /* Register the known messages */
124 /* 3. Wait for others nodesthe startup */
128 /* Register my callback */
129 gras_cb_register(gras_msgtype_by_name("data"),&node_cb_data_handler);
131 INFO1(">>>>>>>> Listening on port %d <<<<<<<<", gras_socket_my_port(globals->sock));
135 xbt_dynar_foreach(hosts,i,h1) {
136 peer = gras_socket_client(h1->name,h1->port);
142 if (e.category != timeout_error)
149 gras_msg_send(peer,gras_msgtype_by_name("data"),&data);
150 INFO3(">>>>>>>> Send Data (%d) from %s to %s <<<<<<<<",
151 data,argv[0],h1->name);
155 WARN0("An error occured, the done was not set by the callback");
157 /* Free the allocated resources, and shut GRAS down */
158 gras_socket_close(globals->sock);
162 //xbt_dynar_map(hosts,kill_buddy_dynar);
163 //xbt_dynar_free(&hosts);