-fixed inline docu
[oweals/gnunet.git] / src / rps / gnunet-service-rps_sampler.c
index f382786a4ba8b6111025acd92bf1a9c8886bb6a8..b51867d28b942e32b8edd392e08ec419451ef767 100644 (file)
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -209,10 +209,29 @@ struct RPS_Sampler
   //size_t size;
 
   /**
-   * All Samplers in one array.
+   * All sampler elements in one array.
    */
   struct RPS_SamplerElement **sampler_elements;
 
+  /**
+   * Number of sampler elements trash can holds.
+   */
+  unsigned int trash_can_size;
+
+  /**
+   * Trash can for old sampler elements.
+   * We need this to evaluate the sampler.
+   * TODO remove after evaluation
+   *      and undo changes in
+   *      sampler_resize
+   *      sampler_empty
+   *      sampler_init
+   *      sampler_remove?
+   *      sampler_reinitialise_by_value
+   *      sampler_update
+   */
+  struct RPS_SamplerElement **trash_can;
+
   /**
    * Maximum time a round takes
    *
@@ -394,7 +413,7 @@ RPS_sampler_elem_create (void)
  */
 static void
 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
-                       struct RPS_Sampler *sampler,
+                       struct RPS_Sampler *sampler, /* TODO remove? */
                        const struct GNUNET_PeerIdentity *other)
 {
   struct GNUNET_HashCode other_hash;
@@ -487,7 +506,6 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
 
   if (old_size > new_size)
   { /* Shrinking */
-    /* Temporary store those to properly call the removeCB on those later */
 
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Shrinking sampler %d -> %d\n",
@@ -499,20 +517,27 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
          old_size,
          new_size);
 
+    /* TODO Temporary store those to properly call the removeCB on those later? */
+    GNUNET_array_grow (sampler->trash_can,
+                       sampler->trash_can_size,
+                       old_size - new_size);
     for (i = new_size ; i < old_size ; i++)
     {
       to_file (sampler->file_name,
                "-%" PRIu32 ": %s",
                i,
                sampler->sampler_elements[i]->file_name);
+      to_file (sampler->sampler_elements[i]->file_name,
+               "--- non-active");
+      sampler->trash_can[i - new_size] = sampler->sampler_elements[i];
     }
 
     GNUNET_array_grow (sampler->sampler_elements,
-        sampler->sampler_size,
-        new_size);
+                       sampler->sampler_size,
+                       new_size);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "sampler->sampler_elements now points to %p\n",
-        sampler->sampler_elements);
+         "sampler->sampler_elements now points to %p\n",
+         sampler->sampler_elements);
 
   }
   else if (old_size < new_size)
@@ -607,6 +632,8 @@ RPS_sampler_init (size_t init_size,
 
   sampler->sampler_size = 0;
   sampler->sampler_elements = NULL;
+  sampler->trash_can_size = 0;
+  sampler->trash_can = NULL;
   sampler->max_round_interval = max_round_interval;
   sampler->get_peers = sampler_get_rand_peer;
   sampler->gpc_head = NULL;
@@ -669,6 +696,13 @@ RPS_sampler_update (struct RPS_Sampler *sampler,
                            sampler,
                            id);
   }
+
+  for (i = 0 ; i < sampler->trash_can_size ; i++)
+  {
+    RPS_sampler_elem_next (sampler->trash_can[i],
+                           sampler,
+                           id);
+  }
 }
 
 
@@ -685,12 +719,20 @@ RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
                                    const struct GNUNET_PeerIdentity *id)
 {
   uint32_t i;
+  struct RPS_SamplerElement *trash_entry;
 
   for ( i = 0 ; i < sampler->sampler_size ; i++ )
   {
     if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
+      trash_entry = GNUNET_new (struct RPS_SamplerElement);
+      *trash_entry = *(sampler->sampler_elements[i]);
+      GNUNET_array_append (sampler->trash_can,
+                           sampler->trash_can_size,
+                           trash_entry);
+      to_file (trash_entry->file_name,
+               "--- non-active");
       RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
     }
   }
@@ -813,8 +855,8 @@ sampler_mod_get_rand_peer (void *cls,
     else
       client_get_index++;
 
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "incremented index to %" PRIX32 ".\n",
-         client_get_index);
+    /* LOG (GNUNET_ERROR_TYPE_DEBUG, "incremented index to %" PRIX32 ".\n",
+         client_get_index); */
   } while (EMPTY == gpc->sampler->sampler_elements[client_get_index]->is_empty);
 
   s_elem = gpc->sampler->sampler_elements[client_get_index];
@@ -878,6 +920,8 @@ RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
                               void *cls, uint32_t num_peers)
 {
   GNUNET_assert (0 != sampler->sampler_size);
+  if (0 == num_peers)
+    return;
 
   // TODO check if we have too much (distinct) sampled peers
   uint32_t i;