1 #include "broadcaster.h"
3 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_broadcaster,
4 "Messages specific for kadeploy");
6 xbt_dynar_t build_hostlist_from_hostcount(int hostcount)
8 xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
13 for (; i < hostcount+1; i++) {
14 hostname = xbt_new(char, HOSTNAME_LENGTH);
15 snprintf(hostname, HOSTNAME_LENGTH, "host%d", i);
16 //XBT_INFO("%s", hostname);
17 /*h = MSG_get_host_by_name(hostname);
19 XBT_INFO("Unknown host %s. Stopping Now! ", hostname);
22 xbt_dynar_push(host_list, &hostname);
28 /*xbt_dynar_t build_hostlist_from_argv(int argc, char *argv[])
30 xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
34 for (; i < argc; i++) {
35 XBT_INFO("host%d = %s", i, argv[i]);
36 h = MSG_get_host_by_name(argv[i]);
38 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
41 xbt_dynar_push(host_list, &(argv[i]));
47 void delete_hostlist(xbt_dynar_t h)
52 int broadcaster_build_chain(const char **first, xbt_dynar_t host_list, xbt_dynar_iterator_t it)
54 msg_task_t task = NULL;
55 char **cur = (char**)xbt_dynar_iterator_next(it);
56 const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
57 const char *current_host = NULL;
58 const char *prev = NULL;
59 const char *next = NULL;
60 const char *last = NULL;
62 /* Build the chain if there's at least one peer */
64 /* init: prev=NULL, host=current cur, next=next cur */
68 /* This iterator iterates one step ahead: cur is current iterated element,
69 but it's actually the next one in the chain */
71 /* following steps: prev=last, host=next, next=cur */
72 cur = (char**)xbt_dynar_iterator_next(it);
79 //XBT_INFO("Building chain -- broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
81 /* Send message to current peer */
82 task = task_message_chain_new(me, current_host, prev, next);
83 //MSG_task_set_category(task, current_host);
84 MSG_task_send(task, current_host);
87 } while (cur != NULL);
93 int broadcaster_send_file(const char *first)
95 const char *me = MSG_host_get_name(MSG_host_self());
96 msg_task_t task = NULL;
97 msg_comm_t comm = NULL;
100 int piece_count = PIECE_COUNT;
103 for (; cur < piece_count; cur++) {
104 task = task_message_data_new(me, first, NULL, 0);
105 XBT_INFO("Sending (send) from %s into mailbox %s", me, first);
106 status = MSG_task_send(task, first);
108 xbt_assert(status == MSG_OK, "broadcaster_send_file() failed");
114 int broadcaster_finish(xbt_dynar_iterator_t it)
116 msg_task_t task = NULL;
117 const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
118 const char *current_host = NULL;
121 xbt_dynar_iterator_seek(it, 0);
123 /* Send goodbye message to every peer in the order generated by iterator it */
124 for (cur = (char**)xbt_dynar_iterator_next(it); cur != NULL; cur = (char**)xbt_dynar_iterator_next(it)) {
125 /* Send message to current peer */
127 task = task_message_end_data_new(me, current_host);
128 //MSG_task_set_category(task, current_host);
129 MSG_task_send(task, current_host);
136 /** Emitter function */
137 int broadcaster(int argc, char *argv[])
139 xbt_dynar_t host_list = NULL;
140 const char *first = NULL;
141 int status = !MSG_OK;
143 XBT_INFO("broadcaster");
145 /* Add every mailbox given by the hostcount in argv[1] to a dynamic array */
146 host_list = build_hostlist_from_hostcount(atoi(argv[1]));
147 /*host_list = build_hostlist_from_argv(argc, argv);*/
149 /* Initialize iterator */
150 xbt_dynar_iterator_t it = xbt_dynar_iterator_new(host_list, forward_indices_list);
152 /* TODO: Error checking */
153 status = broadcaster_build_chain(&first, host_list, it);
154 status = broadcaster_send_file(first);
155 status = broadcaster_finish(it);
157 /* Destroy iterator and hostlist */
158 xbt_dynar_iterator_delete(it);
159 delete_hostlist(host_list);