Logo AND Algorithmique Numérique Distribuée

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