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) {
19 /* Function prototypes */
20 int receiver (int argc,char *argv[]);
21 int sender (int argc,char *argv[]);
24 /* **********************************************************************
26 * **********************************************************************/
27 int receiver (int argc,char *argv[]) {
29 int myport; /* port on which I receive stuff */
30 int todo; /* amount of messages I should get */
31 char *data; /* message content */
33 gras_socket_t mysock; /* socket on which other people contact me */
34 gras_socket_t expeditor; /* to notice who wrote me */
36 /* Init the GRAS infrastructure and declare my globals */
37 gras_init(&argc,argv);
39 /* Get my settings from the command line */
43 /* Create my master socket */
44 mysock = gras_socket_server(myport);
46 /* Register the known messages */
47 gras_msgtype_declare("data", gras_datadesc_by_name("string"));
51 INFO2("Listening on port %d (expecting %d messages)",
52 gras_socket_my_port(mysock),
55 gras_msg_wait(60 /* wait up to one minute */,
56 gras_msgtype_by_name("data"),
61 INFO3("Got Data from %s:%d (still %d to go)",
62 gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor),
67 /* Free the allocated resources, and shut GRAS down */
68 gras_socket_close(mysock);
72 } /* end_of_receiver */
74 /* **********************************************************************
76 * **********************************************************************/
78 int sender (int argc,char *argv[]) {
81 char *data; /* data exchanged */
82 int datasize; /* size of message */
83 xbt_peer_t h; /* iterator */
85 gras_socket_t peer; /* socket to node */
88 /* xbt_dynar for peers */
89 xbt_dynar_t peers = xbt_dynar_new(sizeof(xbt_peer_t),&xbt_peer_free_voidp);
91 /* Init the GRAS infrastructure and declare my globals */
92 gras_init(&argc,argv);
94 /* Get the node location from argc/argv */
95 for (i=1; i<argc-1; i++){
96 xbt_peer_t peer = xbt_peer_from_string(argv[i]);
97 xbt_dynar_push(peers,&peer);
100 datasize=atoi(argv[argc-1]);
102 data=(char *) malloc(datasize+1); // allocation of datasize octets
103 memset(data, 32, datasize);
104 data[datasize] = '\0';
106 INFO0("Launch current node");
108 /* Register the known messages */
109 gras_msgtype_declare("data", gras_datadesc_by_name("string"));
112 /* Wait for receivers to startup */
116 xbt_dynar_foreach(peers,i,h) {
118 peer = gras_socket_client(h->name,h->port);
119 gras_msg_send(peer,gras_msgtype_by_name("data"),&data);
120 INFO2(" Sent Data from %s to %s",
121 gras_os_myname(),h->name);
122 gras_socket_close(peer);
125 /* Free the allocated resources, and shut GRAS down */
126 xbt_dynar_free(&peers);
130 } /* end_of_sender */