Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
823cfe567cb2cd611700b98f9bd4d2b5c529edc6
[simgrid.git] / examples / gras / all2all / all2all.c
1 /* ALL2ALL - all2all of GRAS features                                       */
2
3 /* Copyright (c) 2006 Ahmed Harbaoui. All rights reserved.                  */
4
5  /* This program is free software; you can redistribute it and/or modify it
6   * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "gras.h"
9 #include "xbt/ex.h"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(all2all, "Messages specific to this example");
12
13 /* register data which may be sent (common to client and server) */
14 static void register_messages(void)
15 {
16 }
17
18 /* Function prototypes */
19 int receiver(int argc, char *argv[]);
20 int sender(int argc, char *argv[]);
21
22
23 /* **********************************************************************
24  * Receiver code
25  * **********************************************************************/
26 int receiver(int argc, char *argv[])
27 {
28
29   int myport;                   /* port on which I receive stuff */
30   int todo;                     /* amount of messages I should get */
31   char *data;                   /* message content */
32
33   gras_socket_t mysock;         /* socket on which other people contact me */
34   gras_socket_t expeditor;      /* to notice who wrote me */
35
36   /* Init the GRAS infrastructure and declare my globals */
37   gras_init(&argc, argv);
38
39   /* Get my settings from the command line */
40   myport = atoi(argv[1]);
41   todo = atoi(argv[2]);
42
43   /* Register the known messages */
44   gras_msgtype_declare("data", gras_datadesc_by_name("string"));
45
46   /* Create my master socket */
47   mysock = gras_socket_server(myport);
48
49   /* Get the data */
50   INFO2("Listening on port %d (expecting %d messages)",
51         gras_socket_my_port(mysock), todo);
52   while (todo > 0) {
53     gras_msg_wait(60 /* wait up to one minute */ ,
54                   "data", &expeditor, &data);
55     todo--;
56     free(data);
57
58     INFO3("Got Data from %s:%d (still %d to go)",
59           gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor),
60           todo);
61
62   }
63
64   /* Free the allocated resources, and shut GRAS down */
65   gras_socket_close(mysock);
66
67   gras_exit();
68   return 0;
69 }                               /* end_of_receiver */
70
71 /* **********************************************************************
72  * Sender code
73  * **********************************************************************/
74
75 int sender(int argc, char *argv[])
76 {
77
78   unsigned int iter;            /* iterator */
79   char *data;                   /* data exchanged */
80   int datasize;                 /* size of message */
81   xbt_peer_t h;                 /* iterator */
82   int connected = 0;
83
84   gras_socket_t peer = NULL;    /* socket to node */
85
86
87   /* xbt_dynar for peers */
88   xbt_dynar_t peers = xbt_dynar_new(sizeof(xbt_peer_t), &xbt_peer_free_voidp);
89
90   /* Init the GRAS infrastructure and declare my globals */
91   gras_init(&argc, argv);
92
93   /* Get the node location from argc/argv */
94   for (iter = 1; iter < argc - 1; iter++) {
95     xbt_peer_t peer = xbt_peer_from_string(argv[iter]);
96     xbt_dynar_push(peers, &peer);
97   }
98
99   datasize = atoi(argv[argc - 1]);
100
101   data = (char *) malloc(datasize + 1); // allocation of datasize octets
102   memset(data, 32, datasize);
103   data[datasize] = '\0';
104
105   INFO0("Launch current node");
106
107   /* Register the known messages */
108   gras_msgtype_declare("data", gras_datadesc_by_name("string"));
109
110
111   /* write to the receivers */
112   xbt_dynar_foreach(peers, iter, h) {
113     connected = 0;
114     while (!connected) {
115       xbt_ex_t e;
116       TRY {
117         peer = gras_socket_client(h->name, h->port);
118         connected = 1;
119       }
120       CATCH(e) {
121         if (e.category != system_error  /*in RL */
122             && e.category != mismatch_error /*in SG */ )
123           RETHROW;
124         xbt_ex_free(e);
125         gras_os_sleep(0.01);
126       }
127     }
128     gras_msg_send(peer, "data", &data);
129     if (gras_if_SG()) {
130       INFO2("  Sent Data from %s to %s", gras_os_myname(), h->name);
131     } else {
132       INFO0("  Sent Data");
133     }
134
135     gras_socket_close(peer);
136   }
137
138   /* Free the allocated resources, and shut GRAS down */
139   free(data);
140   xbt_dynar_free(&peers);
141
142   gras_exit();
143   return 0;
144 }                               /* end_of_sender */