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 int broadcaster_build_chain(broadcaster_t bc)
23 msg_task_t task = NULL;
24 char **cur = (char**)xbt_dynar_iterator_next(bc->it);
25 const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
26 const char *current_host = NULL;
27 const char *prev = NULL;
28 const char *next = NULL;
29 const char *last = NULL;
31 /* Build the chain if there's at least one peer */
33 /* init: prev=NULL, host=current cur, next=next cur */
37 /* This iterator iterates one step ahead: cur is current iterated element,
38 but it's actually the next one in the chain */
40 /* following steps: prev=last, host=next, next=cur */
41 cur = (char**)xbt_dynar_iterator_next(bc->it);
48 XBT_DEBUG("Building chain -- broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
50 /* Send message to current peer */
51 task = task_message_chain_new(me, current_host, prev, next, bc->piece_count);
52 MSG_task_send(task, current_host);
55 } while (cur != NULL);
61 int broadcaster_send_file(broadcaster_t bc)
63 const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
64 //msg_comm_t comm = NULL;
65 msg_task_t task = NULL;
67 bc->current_piece = 0;
69 while (bc->current_piece < bc->piece_count) {
70 task = task_message_data_new(me, bc->first, NULL, PIECE_SIZE);
71 XBT_DEBUG("Sending (send) piece %d from %s into mailbox %s", bc->current_piece, me, bc->first);
72 MSG_task_send(task, bc->first);
79 broadcaster_t broadcaster_init(xbt_dynar_t host_list, unsigned int piece_count)
82 broadcaster_t bc = xbt_new(s_broadcaster_t, 1);
84 bc->piece_count = piece_count;
85 bc->current_piece = 0;
86 bc->host_list = host_list;
87 bc->it = xbt_dynar_iterator_new(bc->host_list, forward_indices_list);
88 bc->max_pending_sends = MAX_PENDING_SENDS;
89 bc->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
91 status = broadcaster_build_chain(bc);
92 xbt_assert(status == MSG_OK, "Chain initialization failed");
97 static void broadcaster_destroy(broadcaster_t bc)
99 /* Destroy iterator and hostlist */
100 xbt_dynar_iterator_delete(bc->it);
101 xbt_dynar_free(&bc->pending_sends);
102 xbt_dynar_free(&bc->host_list);
105 /** Emitter function */
106 int broadcaster(int argc, char *argv[])
108 broadcaster_t bc = NULL;
109 xbt_dynar_t host_list = NULL;
111 unsigned int piece_count = PIECE_COUNT;
113 XBT_DEBUG("broadcaster");
115 /* Add every mailbox given by the hostcount in argv[1] to a dynamic array */
116 host_list = build_hostlist_from_hostcount(atoi(argv[1]));
118 /* argv[2] is the number of pieces */
120 piece_count = atoi(argv[2]);
121 XBT_DEBUG("piece_count set to %d", piece_count);
123 XBT_DEBUG("No piece_count specified, defaulting to %d", piece_count);
125 bc = broadcaster_init(host_list, piece_count);
127 /* TODO: Error checking */
128 status = broadcaster_send_file(bc);
130 broadcaster_destroy(bc);