1 /* bandwidth - bandwidth test demo of GRAS features */
3 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
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. */
10 #include "amok/bandwidth.h"
11 #include "amok/peermanagement.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(Bandwidth, "Messages specific to this example");
15 /* **********************************************************************
17 * **********************************************************************/
19 /* Function prototypes */
20 int sensor(int argc, char *argv[]);
22 int sensor(int argc, char *argv[])
25 gras_socket_t master = NULL;
26 int connection_try = 10;
29 gras_init(&argc, argv);
33 mysock = gras_socket_server_range(3000, 9999, 0, 0);
34 INFO1("Sensor starting (on port %d)", gras_os_myport());
35 while (connection_try > 0 && master == NULL) {
38 master = gras_socket_client_from_string(argv[1]);
45 gras_os_sleep(0.5); /* let the master get ready */
49 amok_pm_group_join(master, "bandwidth");
52 gras_socket_close(mysock);
53 gras_socket_close(master);
58 /* **********************************************************************
60 * **********************************************************************/
62 /* Function prototypes */
63 int maestro(int argc, char *argv[]);
65 int maestro(int argc, char *argv[])
68 int buf_size = 32 * 1024;
69 int msg_size = 512 * 1024;
71 double min_duration = 1;
75 xbt_peer_t h1, h2, h_temp;
78 gras_init(&argc, argv);
82 INFO0("Maestro starting");
84 ERROR0("Usage: maestro port\n");
87 mysock = gras_socket_server(atoi(argv[1]));
88 group = amok_pm_group_new("bandwidth");
89 INFO0("Wait for peers for 5 sec");
90 gras_msg_handleall(5); /* friends, we're ready. Come and play */
92 if (xbt_dynar_length(group) < 2) {
94 asprintf(&msg, "Not enough peers arrived. Expected 2 got %ld",
95 xbt_dynar_length(group));
96 amok_pm_group_shutdown("bandwidth");
99 h1 = *(xbt_peer_t *) xbt_dynar_get_ptr(group, 0);
100 h2 = *(xbt_peer_t *) xbt_dynar_get_ptr(group, 1);
101 /* sort peers in right order to keep output right */
102 if (strcmp(h1->name, h2->name) < 0 || h1->port > h2->port) {
108 INFO2("Contact %s:%d", h1->name, h1->port);
109 peer = gras_socket_client(h1->name, h1->port);
111 INFO0("Test the BW between me and one of the sensors");
112 amok_bw_test(peer, buf_size, msg_size, msg_amount, min_duration, &sec, &bw);
114 ("Experience between me and %s:%d (initially %d msgs of %d bytes, maybe modified to fill the pipe at least %.1fs) took %f sec, achieving %f kb/s",
115 h1->name, h1->port, msg_amount, msg_size, min_duration, sec,
116 ((double) bw) / 1024.0);
118 INFO4("Test the BW between %s:%d and %s:%d", h1->name, h1->port, h2->name,
120 amok_bw_request(h1->name, h1->port, h2->name, h2->port, buf_size, msg_size,
121 msg_amount, min_duration, &sec, &bw);
123 ("Experience between %s:%d and %s:%d took took %f sec, achieving %f kb/s",
124 h1->name, h1->port, h2->name, h2->port, sec, ((double) bw) / 1024.0);
126 /* Game is over, friends */
127 amok_pm_group_shutdown("bandwidth");
129 gras_socket_close(mysock);