aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-service-fs_pe.c
blob: e09a8567bd68b575b9b956bab3b8be8619da64b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
     This file is part of GNUnet.
     (C) 2011 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     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.
*/

/**
 * @file fs/gnunet-service-fs_pe.c
 * @brief API to manage query plan
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet-service-fs.h"
#include "gnunet-service-fs_cp.h"
#include "gnunet-service-fs_pe.h"
#include "gnunet-service-fs_pr.h"


/**
 * Information we keep per request per peer.  This is a doubly-linked
 * list (with head and tail in the 'struct GSF_PendingRequestData')
 * with one entry in each heap of each 'struct PeerPlan'.  Each
 * entry tracks information relevant for this request and this peer.
 */
struct GSF_RequestPlan
{

  /**
   * This is a doubly-linked list.
   */
  struct GSF_RequestPlan *next;

  /**
   * This is a doubly-linked list.
   */
  struct GSF_RequestPlan *prev;

  /**
   * Heap node associated with this request and this peer.
   */
  struct GNUNET_CONTAINER_HeapNode *hn;

  /**
   * Associated pending request.
   */
  struct GSF_PendingRequest *pr;

  /**
   * Earliest time we'd be happy to (re)transmit this request.
   */
  struct GNUNET_TIME_Absolute earliest_transmission;

  /**
   * When was the last time we transmitted this request to this peer? 0 for never.
   */
  struct GNUNET_TIME_Absolute last_transmission;

  /**
   * Current priority for this request for this target.
   */
  uint64_t priority;

  /**
   * How often did we transmit this request to this peer?
   */
  unsigned int transmission_counter;

};


/**
 * Transmission plan for a peer.
 */
struct PeerPlan
{
  /**
   * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
   */
  struct GNUNET_CONTAINER_Heap *priority_heap;

  /**
   * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
   */
  struct GNUNET_CONTAINER_Heap *delay_heap;

  /**
   * Current transmission request handle.
   */
  struct GSF_PeerTransmitHandle *pth;

  /**
   * Peer for which this is the plan.
   */
  struct GSF_ConnectedPeer *cp;

  /**
   * Current task for executing the plan.
   */
  GNUNET_SCHEDULER_TaskIdentifier task;
};


/**
 * Hash map from peer identities to PeerPlans.
 */
static struct GNUNET_CONTAINER_MultiHashMap *plans;


/**
 * Insert the given request plan into the heap with the appropriate weight.
 *
 * @param pp associated peer's plan
 * @param rp request to plan
 */
static void
plan (struct PeerPlan *pp,
      struct GSF_RequestPlan *rp)
{
  struct GSF_PendingRequestData *prd;

  prd = GSF_pending_request_get_data_ (rp->pr);
  // FIXME: calculate 'rp->earliest_transmission'!
  // fIXME: claculate 'rp->priority'! 

  if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value == 0)
    rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
					   rp,
					   rp->priority);
  else
    rp->hn = GNUNET_CONTAINER_heap_insert (pp->delay_heap,
					   rp,
					   rp->earliest_transmission.abs_value);
}


/**
 * Figure out when and how to transmit to the given peer.
 *
 * @param cls the 'struct GSF_ConnectedPeer' for transmission
 * @param tc scheduler context
 */
static void
schedule_peer_transmission (void *cls,
			    const struct GNUNET_SCHEDULER_TaskContext *tc);


/**
 * Function called to get a message for transmission.
 *
 * @param cls closure
 * @param buf_size number of bytes available in buf
 * @param buf where to copy the message, NULL on error (peer disconnect)
 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
 */
static size_t 
transmit_message_callback (void *cls,
			   size_t buf_size,
			   void *buf)
{
  struct PeerPlan *pp = cls;
  struct GSF_RequestPlan *rp;
  size_t msize;

  pp->pth = NULL;
  if (NULL == buf)
    {
      /* failed, try again... */
      pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
      return 0;
    }
  rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
  if (NULL == rp)
    {
      pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
      return 0;
    }
  msize = GSF_pending_request_get_message_ (rp->pr, buf_size, buf);
  if (msize > buf_size)
    {
      /* buffer to small (message changed), try again */
      pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
      return 0;
    }
  /* remove from root, add again elsewhere... */
  GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
  rp->hn = NULL;
  rp->last_transmission = GNUNET_TIME_absolute_get ();
  rp->transmission_counter++;
  plan (pp, rp);
  return msize;
}


/**
 * Figure out when and how to transmit to the given peer.
 *
 * @param cls the 'struct PeerPlan'
 * @param tc scheduler context
 */
static void
schedule_peer_transmission (void *cls,
			    const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct PeerPlan *pp = cls;
  struct GSF_RequestPlan *rp;
  size_t msize;

  pp->task = GNUNET_SCHEDULER_NO_TASK;
  GNUNET_assert (NULL == pp->pth);
  /* move ready requests to priority queue */
  while ( (NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
	  (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value == 0) )
    {
      GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
      rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
					     rp, 
					     rp->priority);					
    }   
  if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
    {
      /* priority heap (still) empty, check for delay... */
      rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
      if (NULL == rp)
	return; /* both queues empty */
      pp->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission),
					       &schedule_peer_transmission,
					       pp);
      return;
    }
  /* process from priority heap */
  rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
  GNUNET_assert (NULL != rp);
  msize = GSF_pending_request_get_message_ (rp->pr, 0, NULL);					   
  pp->pth = GSF_peer_transmit_ (pp->cp,
				GNUNET_YES,
				rp->priority,
				GNUNET_TIME_UNIT_FOREVER_REL,
				msize,
				&transmit_message_callback,
				pp);
  GNUNET_assert (NULL != pp->pth);
}


/**
 * Create a new query plan entry.
 *
 * @param cp peer with the entry
 * @param pr request with the entry
 */
void
GSF_plan_add_ (struct GSF_ConnectedPeer *cp,
	       struct GSF_PendingRequest *pr)
{
  struct GNUNET_PeerIdentity id;
  struct PeerPlan *pp;
  struct GSF_PendingRequestData *prd;
  struct GSF_RequestPlan *rp;
  
  GSF_connected_peer_get_identity_ (cp, &id);
  pp = GNUNET_CONTAINER_multihashmap_get (plans,
					  &id.hashPubKey);
  if (NULL == pp)
    {
      pp = GNUNET_malloc (sizeof (struct PeerPlan));
      pp->priority_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
      pp->delay_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
      pp->cp = cp;
      GNUNET_CONTAINER_multihashmap_put (plans,
					 &id.hashPubKey,
					 pp,
					 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
    }
  prd = GSF_pending_request_get_data_ (pr);
  rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
  rp->pr = pr;
  GNUNET_CONTAINER_DLL_insert (prd->rp_head,
			       prd->rp_tail,
			       rp);
  plan (pp, rp);
  if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
    {
      /* no request that should be done immediately, figure out delay */
      if (rp != GNUNET_CONTAINER_heap_peek (pp->delay_heap))
	return; /* did not change delay heap top, no need to do anything */
      GNUNET_assert (NULL == pp->pth);
      if (GNUNET_SCHEDULER_NO_TASK != pp->task)
	GNUNET_SCHEDULER_cancel (pp->task);
      pp->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission),
					       &schedule_peer_transmission,
					       pp);
      return;
    }

  if (pp->pth != NULL)
    {
      if (rp != GNUNET_CONTAINER_heap_peek (pp->priority_heap))
	return; /* did not change priority heap top, no need to do anyhing */
      GSF_peer_transmit_cancel_ (pp->pth);
      pp->pth = NULL;
    }
  if (GNUNET_SCHEDULER_NO_TASK != pp->task)
    GNUNET_SCHEDULER_cancel (pp->task);
  pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission,
				       pp);
}


/**
 * Notify the plan about a peer being no longer available;
 * destroy all entries associated with this peer.
 *
 * @param cp connected peer 
 */
void
GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
{
  struct GNUNET_PeerIdentity id;
  struct PeerPlan *pp;
  struct GSF_RequestPlan *rp;
  struct GSF_PendingRequestData *prd;

  GSF_connected_peer_get_identity_ (cp, &id);
  pp = GNUNET_CONTAINER_multihashmap_get (plans,
					  &id.hashPubKey);
  if (NULL == pp)
    return; /* nothing was ever planned for this peer */
  GNUNET_CONTAINER_multihashmap_remove (plans,
					&id.hashPubKey,
					pp);
  if (NULL != pp->pth)
    GSF_peer_transmit_cancel_ (pp->pth);
  if (GNUNET_SCHEDULER_NO_TASK != pp->task)
    GNUNET_SCHEDULER_cancel (pp->task);
  while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
    {
      prd = GSF_pending_request_get_data_ (rp->pr);
      GNUNET_CONTAINER_DLL_remove (prd->rp_head,
				   prd->rp_tail,
				   rp);
      GNUNET_free (rp);
    }
  GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
  while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
    {
      prd = GSF_pending_request_get_data_ (rp->pr);
      GNUNET_CONTAINER_DLL_remove (prd->rp_head,
				   prd->rp_tail,
				   rp);
      GNUNET_free (rp);
    }
  GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
  GNUNET_free (pp);
}


/**
 * Notify the plan about a request being done; destroy all entries
 * associated with this request.
 *
 * @param pr request that is done
 */
void
GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
{
  struct GSF_RequestPlan *rp;
  struct GSF_PendingRequestData *prd;

  prd = GSF_pending_request_get_data_ (pr);
  while (NULL != (rp = prd->rp_head))
    {
      GNUNET_CONTAINER_heap_remove_node (rp->hn);
      GNUNET_CONTAINER_DLL_remove (prd->rp_head,
				   prd->rp_tail,
				   rp);
      GNUNET_free (rp);
    }
}


/**
 * Initialize plan subsystem.
 */
void
GSF_plan_init ()
{
  plans = GNUNET_CONTAINER_multihashmap_create (256);
}


/**
 * Shutdown plan subsystem.
 */
void
GSF_plan_done ()
{
  GNUNET_assert (0 == 
		 GNUNET_CONTAINER_multihashmap_size (plans));
  GNUNET_CONTAINER_multihashmap_destroy (plans);
}



/* end of gnunet-service-fs_pe.h */