1 #include "broadcaster.h"
3 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_broadcaster,
4 "Messages specific for the broadcaster");
6 xbt_dynar_t build_hostlist_from_hostcount(int hostcount)
8 xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
12 for (; i < hostcount+1; i++) {
13 hostname = xbt_new(char, HOSTNAME_LENGTH);
14 snprintf(hostname, HOSTNAME_LENGTH, "host%d", i);
15 XBT_DEBUG("%s", hostname);
16 xbt_dynar_push(host_list, &hostname);
21 static void delete_hostlist(xbt_dynar_t h)
26 int broadcaster_build_chain(const char **first, xbt_dynar_t host_list, xbt_dynar_iterator_t it)
28 msg_task_t task = NULL;
29 char **cur = (char**)xbt_dynar_iterator_next(it);
30 const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
31 const char *current_host = NULL;
32 const char *prev = NULL;
33 const char *next = NULL;
34 const char *last = NULL;
36 /* Build the chain if there's at least one peer */
38 /* init: prev=NULL, host=current cur, next=next cur */
42 /* This iterator iterates one step ahead: cur is current iterated element,
43 but it's actually the next one in the chain */
45 /* following steps: prev=last, host=next, next=cur */
46 cur = (char**)xbt_dynar_iterator_next(it);
53 XBT_DEBUG("Building chain -- broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
55 /* Send message to current peer */
56 task = task_message_chain_new(me, current_host, prev, next);
57 //MSG_task_set_category(task, current_host);
58 MSG_task_send(task, current_host);
61 } while (cur != NULL);
67 int broadcaster_send_file(const char *first)
69 const char *me = MSG_host_get_name(MSG_host_self());
70 msg_task_t task = NULL;
73 int piece_count = PIECE_COUNT;
76 for (; cur < piece_count; cur++) {
77 task = task_message_data_new(me, first, NULL, 0);
78 XBT_DEBUG("Sending (send) from %s into mailbox %s", me, first);
79 status = MSG_task_send(task, first);
81 xbt_assert(status == MSG_OK, "broadcaster_send_file() failed");
87 int broadcaster_finish(xbt_dynar_iterator_t it)
89 msg_task_t task = NULL;
90 const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
91 const char *current_host = NULL;
94 xbt_dynar_iterator_seek(it, 0);
96 /* Send goodbye message to every peer in the order generated by iterator it */
97 for (cur = (char**)xbt_dynar_iterator_next(it); cur != NULL; cur = (char**)xbt_dynar_iterator_next(it)) {
98 /* Send message to current peer */
100 task = task_message_end_data_new(me, current_host);
101 //MSG_task_set_category(task, current_host);
102 MSG_task_send(task, current_host);
109 /** Emitter function */
110 int broadcaster(int argc, char *argv[])
112 xbt_dynar_t host_list = NULL;
113 const char *first = NULL;
114 int status = !MSG_OK;
116 XBT_INFO("broadcaster");
118 /* Add every mailbox given by the hostcount in argv[1] to a dynamic array */
119 host_list = build_hostlist_from_hostcount(atoi(argv[1]));
120 /*host_list = build_hostlist_from_argv(argc, argv);*/
122 /* Initialize iterator */
123 xbt_dynar_iterator_t it = xbt_dynar_iterator_new(host_list, forward_indices_list);
125 /* TODO: Error checking */
126 status = broadcaster_build_chain(&first, host_list, it);
127 status = broadcaster_send_file(first);
128 status = broadcaster_finish(it);
130 /* Destroy iterator and hostlist */
131 xbt_dynar_iterator_delete(it);
132 delete_hostlist(host_list);