Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / examples / msg / chainsend / broadcaster.c
index 85e5ca1..112bb25 100644 (file)
@@ -1,3 +1,9 @@
+/* Copyright (c) 2012-2014. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
 #include "broadcaster.h"
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_broadcaster,
@@ -5,13 +11,11 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_broadcaster,
 
 xbt_dynar_t build_hostlist_from_hostcount(int hostcount)
 {
-  xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
-  char *hostname = NULL;
-  int i = 1;
+  xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), xbt_free_ref);
+  int i;
   
-  for (; i < hostcount+1; i++) {
-    hostname = xbt_new(char, HOSTNAME_LENGTH);
-    snprintf(hostname, HOSTNAME_LENGTH, "host%d", i);
+  for (i = 1; i <= hostcount; i++) {
+    char *hostname = bprintf("host%d", i);
     XBT_DEBUG("%s", hostname);
     xbt_dynar_push(host_list, &hostname);
   }
@@ -48,8 +52,7 @@ int broadcaster_build_chain(broadcaster_t bc)
       XBT_DEBUG("Building chain -- broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
     
       /* Send message to current peer */
-      task = task_message_chain_new(me, current_host, prev, next);
-      //MSG_task_set_category(task, current_host);
+      task = task_message_chain_new(prev, next, bc->piece_count);
       MSG_task_send(task, current_host);
 
       last = current_host;
@@ -62,43 +65,16 @@ int broadcaster_build_chain(broadcaster_t bc)
 int broadcaster_send_file(broadcaster_t bc)
 {
   const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
-  msg_comm_t comm = NULL;
+  //msg_comm_t comm = NULL;
   msg_task_t task = NULL;
 
   bc->current_piece = 0;
 
   while (bc->current_piece < bc->piece_count) {
-    if (xbt_dynar_length(bc->pending_sends) < bc->max_pending_sends) {
-      task = task_message_data_new(me, bc->first, NULL, PIECE_SIZE);
-      XBT_DEBUG("Sending (isend) piece %d from %s into mailbox %s (current pending %lu)", bc->current_piece, me, bc->first, xbt_dynar_length(bc->pending_sends));
-      comm = MSG_task_isend(task, bc->first);
-      queue_pending_connection(comm, bc->pending_sends);
-      bc->current_piece++;
-    } else {
-      MSG_process_sleep(0.01);
-    }
-    process_pending_connections(bc->pending_sends);
-  }
-
-  return MSG_OK;
-}
-
-int broadcaster_finish(broadcaster_t bc)
-{
-  msg_task_t task = NULL;
-  const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
-  const char *current_host = NULL;
-  char **cur = NULL;
-
-  xbt_dynar_iterator_seek(bc->it, 0);
-
-  /* Send goodbye message to every peer in the order generated by iterator it */
-  for (cur = (char**)xbt_dynar_iterator_next(bc->it); cur != NULL; cur = (char**)xbt_dynar_iterator_next(bc->it)) {
-    /* Send message to current peer */
-    current_host = *cur;
-    task = task_message_end_data_new(me, current_host);
-    //MSG_task_set_category(task, current_host);
-    MSG_task_send(task, current_host);
+    task = task_message_data_new(NULL, PIECE_SIZE);
+    XBT_DEBUG("Sending (send) piece %d from %s into mailbox %s", bc->current_piece, me, bc->first);
+    MSG_task_send(task, bc->first);
+    bc->current_piece++;
   }
 
   return MSG_OK;
@@ -127,7 +103,8 @@ static void broadcaster_destroy(broadcaster_t bc)
   /* Destroy iterator and hostlist */
   xbt_dynar_iterator_delete(bc->it);
   xbt_dynar_free(&bc->pending_sends);
-  xbt_dynar_free(&bc->host_list);
+  xbt_dynar_free(&bc->host_list); /* FIXME: host names are not free'd */
+  xbt_free(bc);
 }
 
 /** Emitter function  */
@@ -138,14 +115,14 @@ int broadcaster(int argc, char *argv[])
   int status;
   unsigned int piece_count = PIECE_COUNT;
 
-  XBT_INFO("broadcaster");
+  XBT_DEBUG("broadcaster");
 
   /* Add every mailbox given by the hostcount in argv[1] to a dynamic array */
-  host_list = build_hostlist_from_hostcount(atoi(argv[1]));
+  host_list = build_hostlist_from_hostcount(xbt_str_parse_int(argv[1], "Invalid number of peers: %s"));
 
   /* argv[2] is the number of pieces */
   if (argc > 2) {
-    piece_count = atoi(argv[2]);
+    piece_count = xbt_str_parse_int(argv[2], "Invalid number of pieces: %s");
     XBT_DEBUG("piece_count set to %d", piece_count);
   } else {
     XBT_DEBUG("No piece_count specified, defaulting to %d", piece_count);
@@ -154,7 +131,6 @@ int broadcaster(int argc, char *argv[])
 
   /* TODO: Error checking */
   status = broadcaster_send_file(bc);
-  status = broadcaster_finish(bc);
 
   broadcaster_destroy(bc);