3f305d2a0168100b07fc8623b6e9984c553ebf5a
[oweals/gnunet.git] / src / testbed / testbed_api_hosts.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file testbed/testbed_api_hosts.c
23  * @brief API for manipulating 'hosts' controlled by the GNUnet testing service;
24  *        allows parsing hosts files, starting, stopping and communicating (via
25  *        SSH/stdin/stdout) with the remote (or local) processes
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_transport_service.h"
33
34 #include "testbed_api.h"
35 #include "testbed_api_hosts.h"
36 #include "testbed_api_operations.h"
37 #include "testbed_api_sd.h"
38
39 /**
40  * Generic logging shorthand
41  */
42 #define LOG(kind, ...)                          \
43   GNUNET_log_from (kind, "testbed-api-hosts", __VA_ARGS__);
44
45 /**
46  * Number of extra elements we create space for when we grow host list
47  */
48 #define HOST_LIST_GROW_STEP 10
49
50
51 /**
52  * A list entry for registered controllers list
53  */
54 struct RegisteredController
55 {
56   /**
57    * The controller at which this host is registered
58    */
59   const struct GNUNET_TESTBED_Controller *controller;
60
61   /**
62    * The next ptr for DLL
63    */
64   struct RegisteredController *next;
65
66   /**
67    * The prev ptr for DLL
68    */
69   struct RegisteredController *prev;
70 };
71
72
73 /**
74  * A slot to record time taken by an overlay connect operation
75  */
76 struct TimeSlot
77 {
78   /**
79    * A key to identify this timeslot
80    */
81   void *key;
82
83   /**
84    * Time
85    */
86   struct GNUNET_TIME_Relative time;
87
88   /**
89    * Number of timing values accumulated
90    */
91   unsigned int nvals;
92 };
93
94
95 /**
96  * Opaque handle to a host running experiments managed by the testing framework.
97  * The master process must be able to SSH to this host without password (via
98  * ssh-agent).
99  */
100 struct GNUNET_TESTBED_Host
101 {
102
103   /**
104    * The next pointer for DLL
105    */
106   struct GNUNET_TESTBED_Host *next;
107
108   /**
109    * The prev pointer for DLL
110    */
111   struct GNUNET_TESTBED_Host *prev;
112
113   /**
114    * The hostname of the host; NULL for localhost
115    */
116   const char *hostname;
117
118   /**
119    * The username to be used for SSH login
120    */
121   const char *username;
122
123   /**
124    * The head for the list of controllers where this host is registered
125    */
126   struct RegisteredController *rc_head;
127
128   /**
129    * The tail for the list of controllers where this host is registered
130    */
131   struct RegisteredController *rc_tail;
132
133   /**
134    * Operation queue for simultaneous overlay connect operations target at this
135    * host
136    */
137   struct OperationQueue *opq_parallel_overlay_connect_operations;
138
139   /**
140    * An array of timing slots; size should be equal to the current number of parallel
141    * overlay connects
142    */
143   struct TimeSlot *tslots;
144
145   /**
146    * Handle for SD calculations amount parallel overlay connect operation finish
147    * times
148    */
149   struct SDHandle *poc_sd;
150
151   /**
152    * The number of parallel overlay connects we do currently
153    */
154   unsigned int num_parallel_connects;
155
156   /**
157    * Counter to indicate when all the available time slots are filled
158    */
159   unsigned int tslots_filled;
160
161   /**
162    * Global ID we use to refer to a host on the network
163    */
164   uint32_t id;
165
166   /**
167    * The port which is to be used for SSH
168    */
169   uint16_t port;
170
171 };
172
173
174 /**
175  * Array of available hosts
176  */
177 static struct GNUNET_TESTBED_Host **host_list;
178
179 /**
180  * The size of the available hosts list
181  */
182 static unsigned int host_list_size;
183
184
185 /**
186  * Lookup a host by ID.
187  *
188  * @param id global host ID assigned to the host; 0 is
189  *        reserved to always mean 'localhost'
190  * @return handle to the host, NULL if host not found
191  */
192 struct GNUNET_TESTBED_Host *
193 GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id)
194 {
195   if (host_list_size <= id)
196     return NULL;
197   return host_list[id];
198 }
199
200
201 /**
202  * Create a host by ID; given this host handle, we could not
203  * run peers at the host, but we can talk about the host
204  * internally.
205  *
206  * @param id global host ID assigned to the host; 0 is
207  *        reserved to always mean 'localhost'
208  * @return handle to the host, NULL on error
209  */
210 struct GNUNET_TESTBED_Host *
211 GNUNET_TESTBED_host_create_by_id_ (uint32_t id)
212 {
213   return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, 0);
214 }
215
216
217 /**
218  * Obtain the host's unique global ID.
219  *
220  * @param host handle to the host, NULL means 'localhost'
221  * @return id global host ID assigned to the host (0 is
222  *         'localhost', but then obviously not globally unique)
223  */
224 uint32_t
225 GNUNET_TESTBED_host_get_id_ (const struct GNUNET_TESTBED_Host * host)
226 {
227   return host->id;
228 }
229
230
231 /**
232  * Obtain the host's hostname.
233  *
234  * @param host handle to the host, NULL means 'localhost'
235  * @return hostname of the host
236  */
237 const char *
238 GNUNET_TESTBED_host_get_hostname (const struct GNUNET_TESTBED_Host *host)
239 {
240   return host->hostname;
241 }
242
243
244 /**
245  * Obtain the host's username
246  *
247  * @param host handle to the host, NULL means 'localhost'
248  * @return username to login to the host
249  */
250 const char *
251 GNUNET_TESTBED_host_get_username_ (const struct GNUNET_TESTBED_Host *host)
252 {
253   return host->username;
254 }
255
256
257 /**
258  * Obtain the host's ssh port
259  *
260  * @param host handle to the host, NULL means 'localhost'
261  * @return username to login to the host
262  */
263 uint16_t
264 GNUNET_TESTBED_host_get_ssh_port_ (const struct GNUNET_TESTBED_Host * host)
265 {
266   return host->port;
267 }
268
269
270 /**
271  * Create a host to run peers and controllers on.
272  *
273  * @param id global host ID assigned to the host; 0 is
274  *        reserved to always mean 'localhost'
275  * @param hostname name of the host, use "NULL" for localhost
276  * @param username username to use for the login; may be NULL
277  * @param port port number to use for ssh; use 0 to let ssh decide
278  * @return handle to the host, NULL on error
279  */
280 struct GNUNET_TESTBED_Host *
281 GNUNET_TESTBED_host_create_with_id (uint32_t id, const char *hostname,
282                                     const char *username, uint16_t port)
283 {
284   struct GNUNET_TESTBED_Host *host;
285   unsigned int new_size;
286
287   if ((id < host_list_size) && (NULL != host_list[id]))
288   {
289     LOG (GNUNET_ERROR_TYPE_WARNING, "Host with id: %u already created\n", id);
290     return NULL;
291   }
292   host = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host));
293   host->hostname = (NULL != hostname) ? GNUNET_strdup (hostname) : NULL;
294   host->username = (NULL != username) ? GNUNET_strdup (username) : NULL;
295   host->id = id;
296   host->port = (0 == port) ? 22 : port;  
297   host->opq_parallel_overlay_connect_operations =
298       GNUNET_TESTBED_operation_queue_create_ (0);
299   GNUNET_TESTBED_set_num_parallel_overlay_connects_ (host, 1);
300   host->poc_sd = GNUNET_TESTBED_SD_init_ (10);
301   new_size = host_list_size;
302   while (id >= new_size)
303     new_size += HOST_LIST_GROW_STEP;
304   if (new_size != host_list_size)
305     GNUNET_array_grow (host_list, host_list_size, new_size);
306   GNUNET_assert (id < host_list_size);
307   LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding host with id: %u\n", host->id);
308   host_list[id] = host;
309   return host;
310 }
311
312
313 /**
314  * Create a host to run peers and controllers on.
315  *
316  * @param hostname name of the host, use "NULL" for localhost
317  * @param username username to use for the login; may be NULL
318  * @param port port number to use for ssh; use 0 to let ssh decide
319  * @return handle to the host, NULL on error
320  */
321 struct GNUNET_TESTBED_Host *
322 GNUNET_TESTBED_host_create (const char *hostname, const char *username,
323                             uint16_t port)
324 {
325   static uint32_t uid_generator;
326
327   if (NULL == hostname)
328     return GNUNET_TESTBED_host_create_with_id (0, hostname, username, port);
329   return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname,
330                                              username, port);
331 }
332
333
334 /**
335  * Load a set of hosts from a configuration file.
336  *
337  * @param filename file with the host specification
338  * @param hosts set to the hosts found in the file; caller must free this if
339  *          number of hosts returned is greater than 0
340  * @return number of hosts returned in 'hosts', 0 on error
341  */
342 unsigned int
343 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
344                                      struct GNUNET_TESTBED_Host ***hosts)
345 {
346   //struct GNUNET_TESTBED_Host **host_array;
347   struct GNUNET_TESTBED_Host *starting_host;
348   char *data;
349   char *buf;
350   char username[256];
351   char hostname[256];
352   uint64_t fs;
353   short int port;
354   int ret;
355   unsigned int offset;
356   unsigned int count;
357
358
359   GNUNET_assert (NULL != filename);
360   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
361   {
362     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename);
363     return 0;
364   }
365   if (GNUNET_OK !=
366       GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
367     fs = 0;
368   if (0 == fs)
369   {
370     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename);
371     return 0;
372   }
373   data = GNUNET_malloc (fs);
374   if (fs != GNUNET_DISK_fn_read (filename, data, fs))
375   {
376     GNUNET_free (data);
377     LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"),
378          filename);
379     return 0;
380   }
381   buf = data;
382   offset = 0;
383   starting_host = NULL;
384   count = 0;
385   while (offset < (fs - 1))
386   {
387     offset++;
388     if (((data[offset] == '\n')) && (buf != &data[offset]))
389     {
390       data[offset] = '\0';
391       ret =
392           SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
393                   hostname, &port);
394       if (3 == ret)
395       {
396         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397                     "Successfully read host %s, port %d and user %s from file\n",
398                     hostname, port, username);
399         /* We store hosts in a static list; hence we only require the starting
400          * host pointer in that list to access the newly created list of hosts */
401         if (NULL == starting_host)
402           starting_host = GNUNET_TESTBED_host_create (hostname, username, port);
403         else
404           (void) GNUNET_TESTBED_host_create (hostname, username, port);
405         count++;
406       }
407       else
408         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
409                     "Error reading line `%s' in hostfile\n", buf);
410       buf = &data[offset + 1];
411     }
412     else if ((data[offset] == '\n') || (data[offset] == '\0'))
413       buf = &data[offset + 1];
414   }
415   GNUNET_free (data);
416   if (NULL == starting_host)
417     return 0;
418   *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count);
419   memcpy (*hosts, &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)],
420           sizeof (struct GNUNET_TESTBED_Host *) * count);
421   return count;
422 }
423
424
425 /**
426  * Destroy a host handle.  Must only be called once everything
427  * running on that host has been stopped.
428  *
429  * @param host handle to destroy
430  */
431 void
432 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host)
433 {
434   struct RegisteredController *rc;
435   uint32_t id;
436
437   GNUNET_assert (host->id < host_list_size);
438   GNUNET_assert (host_list[host->id] == host);
439   host_list[host->id] = NULL;
440   /* clear registered controllers list */
441   for (rc = host->rc_head; NULL != rc; rc = host->rc_head)
442   {
443     GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
444     GNUNET_free (rc);
445   }
446   GNUNET_free_non_null ((char *) host->username);
447   GNUNET_free_non_null ((char *) host->hostname);
448   GNUNET_TESTBED_operation_queue_destroy_
449       (host->opq_parallel_overlay_connect_operations);
450   GNUNET_TESTBED_SD_destroy_ (host->poc_sd);
451   GNUNET_free_non_null (host->tslots);
452   GNUNET_free (host);
453   while (host_list_size >= HOST_LIST_GROW_STEP)
454   {
455     for (id = host_list_size - 1; id > host_list_size - HOST_LIST_GROW_STEP;
456          id--)
457       if (NULL != host_list[id])
458         break;
459     if (id != host_list_size - HOST_LIST_GROW_STEP)
460       break;
461     if (NULL != host_list[id])
462       break;
463     host_list_size -= HOST_LIST_GROW_STEP;
464   }
465   host_list =
466       GNUNET_realloc (host_list,
467                       sizeof (struct GNUNET_TESTBED_Host *) * host_list_size);
468 }
469
470
471 /**
472  * Marks a host as registered with a controller
473  *
474  * @param host the host to mark
475  * @param controller the controller at which this host is registered
476  */
477 void
478 GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
479                                          const struct GNUNET_TESTBED_Controller
480                                          *const controller)
481 {
482   struct RegisteredController *rc;
483
484   for (rc = host->rc_head; NULL != rc; rc = rc->next)
485   {
486     if (controller == rc->controller)   /* already registered at controller */
487     {
488       GNUNET_break (0);
489       return;
490     }
491   }
492   rc = GNUNET_malloc (sizeof (struct RegisteredController));
493   rc->controller = controller;
494   GNUNET_CONTAINER_DLL_insert_tail (host->rc_head, host->rc_tail, rc);
495 }
496
497
498 /**
499  * Checks whether a host has been registered
500  *
501  * @param host the host to check
502  * @param controller the controller at which host's registration is checked
503  * @return GNUNET_YES if registered; GNUNET_NO if not
504  */
505 int
506 GNUNET_TESTBED_is_host_registered_ (const struct GNUNET_TESTBED_Host *host,
507                                     const struct GNUNET_TESTBED_Controller
508                                     *const controller)
509 {
510   struct RegisteredController *rc;
511
512   for (rc = host->rc_head; NULL != rc; rc = rc->next)
513   {
514     if (controller == rc->controller)   /* already registered at controller */
515     {
516       return GNUNET_YES;
517     }
518   }
519   return GNUNET_NO;
520 }
521
522
523 /**
524  * The handle for whether a host is habitable or not
525  */
526 struct GNUNET_TESTBED_HostHabitableCheckHandle
527 {
528   /**
529    * The host to check
530    */
531   const struct GNUNET_TESTBED_Host *host;
532
533   /* /\** */
534   /*  * the configuration handle to lookup the path of the testbed helper */
535   /*  *\/ */
536   /* const struct GNUNET_CONFIGURATION_Handle *cfg; */
537
538   /**
539    * The callback to call once we have the status
540    */
541   GNUNET_TESTBED_HostHabitableCallback cb;
542
543   /**
544    * The callback closure
545    */
546   void *cb_cls;
547
548   /**
549    * The process handle for the SSH process
550    */
551   struct GNUNET_OS_Process *auxp;
552
553   /**
554    * The SSH destination address string
555    */
556   char *ssh_addr;
557
558   /**
559    * The destination port string
560    */
561   char *portstr;
562
563   /**
564    * The path for hte testbed helper binary
565    */
566   char *helper_binary_path;
567
568   /**
569    * Task id for the habitability check task
570    */
571   GNUNET_SCHEDULER_TaskIdentifier habitability_check_task;
572
573   /**
574    * How long we wait before checking the process status. Should grow
575    * exponentially
576    */
577   struct GNUNET_TIME_Relative wait_time;
578
579 };
580
581
582 /**
583  * Task for checking whether a host is habitable or not
584  *
585  * @param cls GNUNET_TESTBED_HostHabitableCheckHandle
586  * @param tc the scheduler task context
587  */
588 static void
589 habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
590 {
591   struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls;
592   void *cb_cls;
593   GNUNET_TESTBED_HostHabitableCallback cb;
594   const struct GNUNET_TESTBED_Host *host;
595   unsigned long code;
596   enum GNUNET_OS_ProcessStatusType type;
597   int ret;
598
599   h->habitability_check_task = GNUNET_SCHEDULER_NO_TASK;
600   ret = GNUNET_OS_process_status (h->auxp, &type, &code);
601   if (GNUNET_SYSERR == ret)
602   {
603     GNUNET_break (0);
604     ret = GNUNET_NO;
605     goto call_cb;
606   }
607   if (GNUNET_NO == ret)
608   {
609     h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
610     h->habitability_check_task =
611         GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
612     return;
613   }
614   GNUNET_OS_process_destroy (h->auxp);
615   h->auxp = NULL;
616   ret = (0 != code) ? GNUNET_NO : GNUNET_YES;
617
618 call_cb:
619   GNUNET_free (h->ssh_addr);
620   GNUNET_free (h->portstr);
621   GNUNET_free (h->helper_binary_path);
622   if (NULL != h->auxp)
623     GNUNET_OS_process_destroy (h->auxp);
624   cb = h->cb;
625   cb_cls = h->cb_cls;
626   host = h->host;
627   GNUNET_free (h);
628   if (NULL != cb)
629     cb (cb_cls, host, ret);
630 }
631
632
633 /**
634  * Checks whether a host can be used to start testbed service
635  *
636  * @param host the host to check
637  * @param config the configuration handle to lookup the path of the testbed
638  *          helper
639  * @param cb the callback to call to inform about habitability of the given host
640  * @param cb_cls the closure for the callback
641  * @return NULL upon any error or a handle which can be passed to
642  *           GNUNET_TESTBED_is_host_habitable_cancel()
643  */
644 struct GNUNET_TESTBED_HostHabitableCheckHandle *
645 GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
646                                   const struct GNUNET_CONFIGURATION_Handle
647                                   *config,
648                                   GNUNET_TESTBED_HostHabitableCallback cb,
649                                   void *cb_cls)
650 {
651   struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
652   char *remote_args[11];
653   const char *hostname;
654   unsigned int argp;
655
656   h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle));
657   h->cb = cb;
658   h->cb_cls = cb_cls;
659   h->host = host;
660   hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname;
661   if (NULL == host->username)
662     h->ssh_addr = GNUNET_strdup (hostname);
663   else
664     GNUNET_asprintf (&h->ssh_addr, "%s@%s", host->username, hostname);
665   if (GNUNET_OK !=
666       GNUNET_CONFIGURATION_get_value_string (config, "testbed",
667                                              "HELPER_BINARY_PATH",
668                                              &h->helper_binary_path))
669     h->helper_binary_path =
670         GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY);
671   argp = 0;
672   remote_args[argp++] = "ssh";
673   GNUNET_asprintf (&h->portstr, "%u", host->port);
674   remote_args[argp++] = "-p";
675   remote_args[argp++] = h->portstr;
676   remote_args[argp++] = "-o";
677   remote_args[argp++] = "BatchMode=yes";
678   remote_args[argp++] = "-o";
679   remote_args[argp++] = "NoHostAuthenticationForLocalhost=yes";
680   remote_args[argp++] = h->ssh_addr;
681   remote_args[argp++] = "stat";
682   remote_args[argp++] = h->helper_binary_path;
683   remote_args[argp++] = NULL;
684   GNUNET_assert (argp == 11);
685   h->auxp =
686       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
687                                    NULL, "ssh", remote_args);
688   if (NULL == h->auxp)
689   {
690     GNUNET_break (0);           /* Cannot exec SSH? */
691     GNUNET_free (h->ssh_addr);
692     GNUNET_free (h->portstr);
693     GNUNET_free (h->helper_binary_path);
694     GNUNET_free (h);
695     return NULL;
696   }
697   h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time);
698   h->habitability_check_task =
699       GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h);
700   return h;
701 }
702
703
704 /**
705  * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable()
706  *
707  * @param handle the habitability check handle
708  */
709 void
710 GNUNET_TESTBED_is_host_habitable_cancel (struct
711                                          GNUNET_TESTBED_HostHabitableCheckHandle
712                                          *handle)
713 {
714   GNUNET_SCHEDULER_cancel (handle->habitability_check_task);
715   (void) GNUNET_OS_process_kill (handle->auxp, SIGTERM);
716   (void) GNUNET_OS_process_wait (handle->auxp);
717   GNUNET_OS_process_destroy (handle->auxp);
718   GNUNET_free (handle->ssh_addr);
719   GNUNET_free (handle->portstr);
720   GNUNET_free (handle->helper_binary_path);
721   GNUNET_free (handle);
722 }
723
724
725 /**
726  * Initializes the operation queue for parallel overlay connects
727  *
728  * @param h the host handle
729  * @param npoc the number of parallel overlay connects - the queue size
730  */
731 void
732 GNUNET_TESTBED_set_num_parallel_overlay_connects_ (struct
733                                                    GNUNET_TESTBED_Host *h,
734                                                    unsigned int npoc)
735 {
736   fprintf (stderr, "%d", npoc);
737   GNUNET_free_non_null (h->tslots);
738   h->tslots_filled = 0;
739   h->num_parallel_connects = npoc;
740   h->tslots = GNUNET_malloc (npoc * sizeof (struct TimeSlot));
741   GNUNET_TESTBED_operation_queue_reset_max_active_
742       (h->opq_parallel_overlay_connect_operations, npoc);
743 }
744
745
746 /**
747  * Returns a timing slot which will be exclusively locked
748  *
749  * @param h the host handle
750  * @param key a pointer which is associated to the returned slot; should not be
751  *          NULL. It serves as a key to determine the correct owner of the slot
752  * @return the time slot index in the array of time slots in the controller
753  *           handle
754  */
755 unsigned int
756 GNUNET_TESTBED_get_tslot_ (struct GNUNET_TESTBED_Host *h, void *key)
757 {
758   unsigned int slot;
759
760   GNUNET_assert (NULL != h->tslots);
761   GNUNET_assert (NULL != key);
762   for (slot = 0; slot < h->num_parallel_connects; slot++)
763     if (NULL == h->tslots[slot].key)
764     {
765       h->tslots[slot].key = key;
766       return slot;
767     }
768   GNUNET_assert (0);            /* We should always find a free tslot */
769 }
770
771
772 /**
773  * Decides whether any change in the number of parallel overlay connects is
774  * necessary to adapt to the load on the system
775  *
776  * @param h the host handle
777  */
778 static void
779 decide_npoc (struct GNUNET_TESTBED_Host *h)
780 {
781   struct GNUNET_TIME_Relative avg;
782   int sd;
783   unsigned int slot;
784   unsigned int nvals;
785
786   if (h->tslots_filled != h->num_parallel_connects)
787     return;
788   avg = GNUNET_TIME_UNIT_ZERO;
789   nvals = 0;
790   for (slot = 0; slot < h->num_parallel_connects; slot++)
791   {
792     avg = GNUNET_TIME_relative_add (avg, h->tslots[slot].time);
793     nvals += h->tslots[slot].nvals;
794   }
795   GNUNET_assert (nvals >= h->num_parallel_connects);
796   avg = GNUNET_TIME_relative_divide (avg, nvals);
797   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != avg.rel_value);
798   sd = GNUNET_TESTBED_SD_deviation_factor_ (h->poc_sd, (unsigned int) avg.rel_value);
799   if ( (sd <= 5) ||
800        (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
801                                        h->num_parallel_connects)) )
802     GNUNET_TESTBED_SD_add_data_ (h->poc_sd, (unsigned int) avg.rel_value);
803   if (GNUNET_SYSERR == sd)
804   {
805     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
806                                                        h->num_parallel_connects);
807     return;
808   }
809   GNUNET_assert (0 <= sd);
810   if (0 == sd)
811   {
812     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
813                                                        h->num_parallel_connects
814                                                        * 2);
815     return;
816   }
817   if (1 == sd)
818   {
819     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
820                                                        h->num_parallel_connects
821                                                        + 1);
822     return;
823   }
824   if (1 == h->num_parallel_connects)
825   {
826     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
827     return;
828   }
829   if (2 == sd)
830   {
831     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
832                                                        h->num_parallel_connects
833                                                        - 1);
834     return;
835   }
836   GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
837                                                      h->num_parallel_connects /
838                                                      2);
839 }
840
841
842 /**
843  * Releases a time slot thus making it available for be used again
844  *
845  * @param h the host handle
846  * @param index the index of the the time slot
847  * @param key the key to prove ownership of the timeslot
848  * @return GNUNET_YES if the time slot is successfully removed; GNUNET_NO if the
849  *           time slot cannot be removed - this could be because of the index
850  *           greater than existing number of time slots or `key' being different
851  */
852 int
853 GNUNET_TESTBED_release_time_slot_ (struct GNUNET_TESTBED_Host *h,
854                                    unsigned int index, void *key)
855 {
856   struct TimeSlot *slot;
857
858   GNUNET_assert (NULL != key);
859   if (index >= h->num_parallel_connects)
860     return GNUNET_NO;
861   slot = &h->tslots[index];
862   if (key != slot->key)
863     return GNUNET_NO;
864   slot->key = NULL;
865   return GNUNET_YES;
866 }
867
868
869 /**
870  * Function to update a time slot
871  *
872  * @param h the host handle
873  * @param index the index of the time slot to update
874  * @param key the key to identify ownership of the slot
875  * @param time the new time
876  * @param failed should this reading be treated as coming from a fail event
877  */
878 void
879 GNUNET_TESTBED_update_time_slot_ (struct GNUNET_TESTBED_Host *h,
880                                   unsigned int index, void *key,
881                                   struct GNUNET_TIME_Relative time, int failed)
882 {
883   struct TimeSlot *slot;
884
885   if (GNUNET_YES == failed)
886   {
887     if (1 == h->num_parallel_connects)
888     {
889       GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h, 1);
890       return;
891     }
892     GNUNET_TESTBED_set_num_parallel_overlay_connects_ (h,
893                                                        h->num_parallel_connects
894                                                        - 1);
895   }
896   if (GNUNET_NO == GNUNET_TESTBED_release_time_slot_ (h, index, key))
897     return;
898   slot = &h->tslots[index];
899   slot->nvals++;
900   if (GNUNET_TIME_UNIT_ZERO.rel_value == slot->time.rel_value)
901   {
902     slot->time = time;
903     h->tslots_filled++;
904     decide_npoc (h);
905     return;
906   }
907   slot->time = GNUNET_TIME_relative_add (slot->time, time);
908 }
909
910
911 /**
912  * Queues the given operation in the queue for parallel overlay connects of the
913  * given host
914  *
915  * @param h the host handle
916  * @param op the operation to queue in the given host's parally overlay connect
917  *          queue 
918  */
919 void
920 GNUNET_TESTBED_host_queue_oc (struct GNUNET_TESTBED_Host *h, 
921                               struct GNUNET_TESTBED_Operation *op)
922 {  
923   GNUNET_TESTBED_operation_queue_insert_
924       (h->opq_parallel_overlay_connect_operations, op);
925 }
926
927 /* end of testbed_api_hosts.c */