Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c0fe0d85a4c94046023d5484d9fa28b8e617d34e
[simgrid.git] / examples / msg / chainsend / broadcaster.c
1 #include "broadcaster.h"
2
3 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_broadcaster,
4                              "Messages specific for the broadcaster");
5
6 xbt_dynar_t build_hostlist_from_hostcount(int hostcount)
7 {
8   xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
9   char *hostname = NULL;
10   int i = 1;
11   
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);
17   }
18   return host_list;
19 }
20
21 void delete_hostlist(xbt_dynar_t h)
22 {
23   xbt_dynar_free(&h);
24 }
25
26 int broadcaster_build_chain(const char **first, xbt_dynar_t host_list, xbt_dynar_iterator_t it)
27 {
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;
35
36   /* Build the chain if there's at least one peer */
37   if (cur != NULL) {
38     /* init: prev=NULL, host=current cur, next=next cur */
39     next = *cur;
40     *first = next;
41
42     /* This iterator iterates one step ahead: cur is current iterated element, 
43        but it's actually the next one in the chain */
44     do {
45       /* following steps: prev=last, host=next, next=cur */
46       cur = (char**)xbt_dynar_iterator_next(it);
47       prev = last;
48       current_host = next;
49       if (cur != NULL)
50         next = *cur;
51       else
52         next = NULL;
53       XBT_DEBUG("Building chain -- broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
54     
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);
59
60       last = current_host;
61     } while (cur != NULL);
62   }
63
64   return MSG_OK;
65 }
66
67 int broadcaster_send_file(const char *first)
68 {
69   const char *me = MSG_host_get_name(MSG_host_self());
70   msg_task_t task = NULL;
71   int status;
72
73   int piece_count = PIECE_COUNT;
74   int cur = 0;
75
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);
80    
81     xbt_assert(status == MSG_OK, "broadcaster_send_file() failed");
82   }
83
84   return MSG_OK;
85 }
86
87 int broadcaster_finish(xbt_dynar_iterator_t it)
88 {
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;
92   char **cur = NULL;
93
94   xbt_dynar_iterator_seek(it, 0);
95
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 */
99     current_host = *cur;
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);
103   }
104
105   return MSG_OK;
106 }
107
108
109 /** Emitter function  */
110 int broadcaster(int argc, char *argv[])
111 {
112   xbt_dynar_t host_list = NULL;
113   const char *first = NULL;
114   int status = !MSG_OK;
115
116   XBT_INFO("broadcaster");
117
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);*/
121   
122   /* Initialize iterator */
123   xbt_dynar_iterator_t it = xbt_dynar_iterator_new(host_list, forward_indices_list);
124
125   /* TODO: Error checking */
126   status = broadcaster_build_chain(&first, host_list, it);
127   status = broadcaster_send_file(first);
128   status = broadcaster_finish(it);
129
130   /* Destroy iterator and hostlist */
131   xbt_dynar_iterator_delete(it);
132   delete_hostlist(host_list);
133
134   return status;
135 }