Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
coverage madness in examples
[simgrid.git] / examples / msg / pastry / pastry.c
index 887ab79..131e8e7 100644 (file)
@@ -1,6 +1,12 @@
+/* Copyright (c) 2013-2015. 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 <stdio.h>
 #include <math.h>
-#include "msg/msg.h"
+#include "simgrid/msg.h"
 #include "xbt/log.h"
 #include "xbt/asserts.h"
 
 #define MAILBOX_NAME_SIZE 10
 
 static int nb_bits = 16;
-static int nb_keys = 0;
 static int timeout = 50;
 static int max_simulation_time = 1000;
 
 extern long int smx_total_comms;
 
 
-
-static int domain_mask = pow(2, DOMAIN_SIZE) - 1;
-
 typedef struct s_node {
   int id;                                 //128bits generated random(2^128 -1)
   int known_id;
@@ -79,6 +81,7 @@ typedef struct s_task_data {
   state_t state;
 } s_task_data_t, *task_data_t;
 
+
 static void print_node(node_t node);
 static void print_node_id(node_t node);
 static void print_node_neighborood_set(node_t node);
@@ -108,7 +111,10 @@ static void get_mailbox(int node_id, char* mailbox)
 /**
  * Get the specific level of a node id
  */
+int domain_mask = 0;
 static int domain(int a, int level) {
+  if (domain_mask == 0)
+    domain_mask = pow(2, DOMAIN_SIZE) - 1;
   int shift = (LEVELS_COUNT-level-1)*DOMAIN_SIZE;
   return (a >> shift) & domain_mask;
 }
@@ -124,12 +130,12 @@ static int shl(int a, int b) {
 }
 
 /*
- * Get the cloest id to the dest in the node namespace_set
+ * Get the closest id to the dest in the node namespace_set
  */
 static int closest_in_namespace_set(node_t node, int dest) {
   int best_dist;
   int res = -1;
-  if (node->namespace_set[NAMESPACE_SIZE-1] <= dest & dest <= node->namespace_set[0]) {
+  if ((node->namespace_set[NAMESPACE_SIZE-1] <= dest) & (dest <= node->namespace_set[0])) {
     best_dist = abs(node->id - dest);
     res = node->id;
     int i, dist;
@@ -207,7 +213,7 @@ static void handle_task(node_t node, msg_task_t task) {
     /*
      * Try to join the ring
      */
-    case TASK_JOIN:;
+    case TASK_JOIN: {
       int next = routing_next(node, task_data->answer_id);
       XBT_DEBUG("Join request from %08x forwarding to %08x", task_data->answer_id, next);      
       type = TASK_JOIN_LAST_REPLY;
@@ -234,7 +240,7 @@ static void handle_task(node_t node, msg_task_t task) {
       task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
       MSG_task_send_with_timeout(task_sent, task_data->answer_to, timeout);
       break;
-
+    }
     /*
      * Join reply from all the node touched by the join
      */
@@ -327,7 +333,8 @@ static void handle_task(node_t node, msg_task_t task) {
       max = -1;
       for (i=0; i<=NAMESPACE_SIZE; i++) {
        j = task_namespace_set[i];
-       printf("%08x %08x | ", j, curr_namespace_set[i]);
+        if (i<NAMESPACE_SIZE)
+         printf("%08x %08x | ", j, curr_namespace_set[i]);
        if (j != -1 && j < node->id) min = i;
        if (j != -1 && max == -1 && j > node->id) max = i;
       }
@@ -416,7 +423,7 @@ static int join(node_t node){
 }
 
 /*
- * Print the node infomations
+ * Print the node infomation
  */
 static void print_node(node_t node) {
   printf("Node:\n");
@@ -502,7 +509,7 @@ static state_t node_get_state(node_t node) {
  * - the time to sleep before I join (except for the first node)
  * - the deadline time
  */
-int node(int argc, char *argv[])
+static int node(int argc, char *argv[])
 {
   double init_time = MSG_get_clock();
   msg_task_t task_received = NULL;  
@@ -583,6 +590,7 @@ int node(int argc, char *argv[])
     }
     print_node(&node);
   }
+  return 1;
 }
 
 /*
@@ -605,11 +613,10 @@ int node(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
   MSG_init(&argc, argv);
-  if (argc < 3) {
-    printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
-    printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
-    exit(1);
-  }
+  xbt_assert(argc > 2, 
+            "Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n"
+            "\tExample: %s ../msg_platform.xml pastry10.xml\n", 
+            argv[0], argv[0]);
   
   char **options = &argv[1];
   while (!strncmp(options[0], "-", 1)) {
@@ -645,9 +652,5 @@ int main(int argc, char *argv[])
   XBT_CRITICAL("Messages created: %ld", smx_total_comms);
   XBT_INFO("Simulated time: %g", MSG_get_clock());
 
-  if (res == MSG_OK)
-    return 0;
-  else
-    return 1;
-
+  return res != MSG_OK;
 }