Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
59e0a347280b202751da464da2ec483dd6da5c09
[simgrid.git] / examples / gras / all2all / all2all.c
1 /* $Id$ */
2
3 /* ALL2ALL - all2all of GRAS features                                       */
4
5 /* Copyright (c) 2006 Ahmed Harbaoui. All rights reserved.                  */
6
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. */
9
10 #include "gras.h"
11 #include "xbt/ex.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(all2all,"Messages specific to this example");
14
15 /* register data which may be sent (common to client and server) */
16 static void register_messages(void) {
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   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),
52         todo);
53   while (todo>0) {
54      gras_msg_wait(60 /* wait up to one minute */,
55                    "data",
56                    &expeditor,
57                    &data);
58      todo--;
59
60      INFO3("Got Data from %s:%d (still %d to go)",
61            gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor),
62            todo);
63
64   }
65
66   /* Free the allocated resources, and shut GRAS down */
67   gras_socket_close(mysock);
68
69   gras_exit();
70   return 0;
71 } /* end_of_receiver */
72
73 /* **********************************************************************
74  * Sender code
75  * **********************************************************************/
76
77 int sender (int argc,char *argv[]) {
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   gras_socket_t peer=NULL;  /* socket to node */
86
87
88   /* xbt_dynar for peers */
89   xbt_dynar_t peers = xbt_dynar_new(sizeof(xbt_peer_t),&xbt_peer_free_voidp);
90
91   /* Init the GRAS infrastructure and declare my globals */
92   gras_init(&argc,argv);
93
94   /* Get the node location from argc/argv */
95   for (iter=1; iter<argc-1; iter++){
96     xbt_peer_t peer = xbt_peer_from_string(argv[iter]);
97     xbt_dynar_push(peers,&peer);
98   }
99
100   datasize=atoi(argv[argc-1]);
101
102   data=(char *) malloc(datasize+1);  // allocation of datasize octets
103   memset(data, 32, datasize);
104   data[datasize] = '\0';
105
106   INFO0("Launch current node");
107
108   /* Register the known messages */
109   gras_msgtype_declare("data", gras_datadesc_by_name("string"));
110
111
112   /* write to the receivers */
113   xbt_dynar_foreach(peers,iter,h) {
114      connected = 0;
115      while (!connected) {
116         xbt_ex_t e;
117         TRY {
118            peer = gras_socket_client(h->name,h->port);
119            connected=1;
120         } CATCH(e) {
121            if (e.category != system_error /*in RL*/&& e.category != mismatch_error/*in SG*/)
122              RETHROW;
123            xbt_ex_free(e);
124            gras_os_sleep(0.01);
125         }
126      }
127      gras_msg_send(peer,"data",&data);
128      if (gras_if_SG()) {
129         INFO2("  Sent Data from %s to %s", gras_os_myname(),h->name);
130      } else {
131         INFO0("  Sent Data");
132      }
133
134      gras_socket_close(peer);
135   }
136
137   /* Free the allocated resources, and shut GRAS down */
138   free(data);
139   xbt_dynar_free(&peers);
140
141   gras_exit();
142   return 0;
143 } /* end_of_sender */