Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Oops, forgot to actually add the new tesh
[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   msg_host_t h = NULL;
11   int i = 1;
12   
13   for (; i < hostcount+1; i++) {
14     hostname = xbt_new(char, HOSTNAME_LENGTH);
15     snprintf(hostname, HOSTNAME_LENGTH, "host%d", i);
16     XBT_DEBUG("%s", hostname);
17     xbt_dynar_push(host_list, &hostname);
18   }
19   return host_list;
20 }
21
22 void delete_hostlist(xbt_dynar_t h)
23 {
24   xbt_dynar_free(&h);
25 }
26
27 int broadcaster_build_chain(const char **first, xbt_dynar_t host_list, xbt_dynar_iterator_t it)
28 {
29   msg_task_t task = NULL;
30   char **cur = (char**)xbt_dynar_iterator_next(it);
31   const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
32   const char *current_host = NULL;
33   const char *prev = NULL;
34   const char *next = NULL;
35   const char *last = NULL;
36
37   /* Build the chain if there's at least one peer */
38   if (cur != NULL) {
39     /* init: prev=NULL, host=current cur, next=next cur */
40     next = *cur;
41     *first = next;
42
43     /* This iterator iterates one step ahead: cur is current iterated element, 
44        but it's actually the next one in the chain */
45     do {
46       /* following steps: prev=last, host=next, next=cur */
47       cur = (char**)xbt_dynar_iterator_next(it);
48       prev = last;
49       current_host = next;
50       if (cur != NULL)
51         next = *cur;
52       else
53         next = NULL;
54       XBT_DEBUG("Building chain -- broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
55     
56       /* Send message to current peer */
57       task = task_message_chain_new(me, current_host, prev, next);
58       //MSG_task_set_category(task, current_host);
59       MSG_task_send(task, current_host);
60
61       last = current_host;
62     } while (cur != NULL);
63   }
64
65   return MSG_OK;
66 }
67
68 int broadcaster_send_file(const char *first)
69 {
70   const char *me = MSG_host_get_name(MSG_host_self());
71   msg_task_t task = NULL;
72   msg_comm_t comm = NULL;
73   int status;
74
75   int piece_count = PIECE_COUNT;
76   int cur = 0;
77
78   for (; cur < piece_count; cur++) {
79     task = task_message_data_new(me, first, NULL, 0);
80     XBT_DEBUG("Sending (send) from %s into mailbox %s", me, first);
81     status = MSG_task_send(task, first);
82    
83     xbt_assert(status == MSG_OK, "broadcaster_send_file() failed");
84   }
85
86   return MSG_OK;
87 }
88
89 int broadcaster_finish(xbt_dynar_iterator_t it)
90 {
91   msg_task_t task = NULL;
92   const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
93   const char *current_host = NULL;
94   char **cur = NULL;
95
96   xbt_dynar_iterator_seek(it, 0);
97
98   /* Send goodbye message to every peer in the order generated by iterator it */
99   for (cur = (char**)xbt_dynar_iterator_next(it); cur != NULL; cur = (char**)xbt_dynar_iterator_next(it)) {
100     /* Send message to current peer */
101     current_host = *cur;
102     task = task_message_end_data_new(me, current_host);
103     //MSG_task_set_category(task, current_host);
104     MSG_task_send(task, current_host);
105   }
106
107   return MSG_OK;
108 }
109
110
111 /** Emitter function  */
112 int broadcaster(int argc, char *argv[])
113 {
114   xbt_dynar_t host_list = NULL;
115   const char *first = NULL;
116   int status = !MSG_OK;
117
118   XBT_INFO("broadcaster");
119
120   /* Add every mailbox given by the hostcount in argv[1] to a dynamic array */
121   host_list = build_hostlist_from_hostcount(atoi(argv[1]));
122   /*host_list = build_hostlist_from_argv(argc, argv);*/
123   
124   /* Initialize iterator */
125   xbt_dynar_iterator_t it = xbt_dynar_iterator_new(host_list, forward_indices_list);
126
127   /* TODO: Error checking */
128   status = broadcaster_build_chain(&first, host_list, it);
129   status = broadcaster_send_file(first);
130   status = broadcaster_finish(it);
131
132   /* Destroy iterator and hostlist */
133   xbt_dynar_iterator_delete(it);
134   delete_hostlist(host_list);
135
136   return status;
137 }