3 /* ALL2ALL - all2all 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(all2all, "Messages specific to this example");
15 /* register data which may be sent (common to client and server) */
16 static void register_messages(void)
20 /* Function prototypes */
21 int receiver(int argc, char *argv[]);
22 int sender(int argc, char *argv[]);
25 /* **********************************************************************
27 * **********************************************************************/
28 int receiver(int argc, char *argv[])
31 int myport; /* port on which I receive stuff */
32 int todo; /* amount of messages I should get */
33 char *data; /* message content */
35 gras_socket_t mysock; /* socket on which other people contact me */
36 gras_socket_t expeditor; /* to notice who wrote me */
38 /* Init the GRAS infrastructure and declare my globals */
39 gras_init(&argc, argv);
41 /* Get my settings from the command line */
42 myport = atoi(argv[1]);
45 /* Register the known messages */
46 gras_msgtype_declare("data", gras_datadesc_by_name("string"));
48 /* Create my master socket */
49 mysock = gras_socket_server(myport);
52 INFO2("Listening on port %d (expecting %d messages)",
53 gras_socket_my_port(mysock), todo);
55 gras_msg_wait(60 /* wait up to one minute */ ,
56 "data", &expeditor, &data);
60 INFO3("Got Data from %s:%d (still %d to go)",
61 gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor),
66 /* Free the allocated resources, and shut GRAS down */
67 gras_socket_close(mysock);
71 } /* end_of_receiver */
73 /* **********************************************************************
75 * **********************************************************************/
77 int sender(int argc, char *argv[])
80 unsigned int iter; /* iterator */
81 char *data; /* data exchanged */
82 int datasize; /* size of message */
83 xbt_peer_t h; /* iterator */
86 gras_socket_t peer = NULL; /* socket to node */
89 /* xbt_dynar for peers */
90 xbt_dynar_t peers = xbt_dynar_new(sizeof(xbt_peer_t), &xbt_peer_free_voidp);
92 /* Init the GRAS infrastructure and declare my globals */
93 gras_init(&argc, argv);
95 /* Get the node location from argc/argv */
96 for (iter = 1; iter < argc - 1; iter++) {
97 xbt_peer_t peer = xbt_peer_from_string(argv[iter]);
98 xbt_dynar_push(peers, &peer);
101 datasize = atoi(argv[argc - 1]);
103 data = (char *) malloc(datasize + 1); // allocation of datasize octets
104 memset(data, 32, datasize);
105 data[datasize] = '\0';
107 INFO0("Launch current node");
109 /* Register the known messages */
110 gras_msgtype_declare("data", gras_datadesc_by_name("string"));
113 /* write to the receivers */
114 xbt_dynar_foreach(peers, iter, h) {
119 peer = gras_socket_client(h->name, h->port);
123 if (e.category != system_error /*in RL */
124 && e.category != mismatch_error /*in SG */ )
130 gras_msg_send(peer, "data", &data);
132 INFO2(" Sent Data from %s to %s", gras_os_myname(), h->name);
137 gras_socket_close(peer);
140 /* Free the allocated resources, and shut GRAS down */
142 xbt_dynar_free(&peers);
146 } /* end_of_sender */