Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2017
3,410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. /*
  2.  * Peer manager
  3.  *
  4.  * The peer manager keeps track of neighbor nodes in a peer-to-peer
  5.  * network.  This module might be used as part of a BitTorrent, BitCoin,
  6.  * or Tor client.
  7.  *
  8.  * Every time there is activity from a peer, the peer manager is told about it.
  9.  * The peer manager remembers the n most recently active unique peers.
  10.  *
  11.  * When we wish to communicate, the peer manager can select a random
  12.  * peer.
  13.  *
  14.  * Example usage:
  15.  *
  16.  *   PeerManager mgr;
  17.  *   peermgr_init(&mgr, 8);
  18.  *
  19.  *   peermgr_saw_peer(&mgr, 1234, time(NULL));
  20.  *   peermgr_saw_peer(&mgr, 5432, time(NULL));
  21.  *
  22.  *   PeerId random_peer;
  23.  *   if (peermgr_pick_random_peer(&mgr, &random_peer)) {
  24.  *       printf("chosen peer = %" PRIu64 "\n", random_peer);
  25.  *   }
  26.  *
  27.  *   peermgr_cleanup(&mgr);
  28.  */
  29.  
  30. #include <stdbool.h>
  31. #include <stdint.h>
  32. #include <stdlib.h>
  33. #include <time.h>
  34.  
  35. /* TODO You may include additional C standard library headers and define
  36.  * additional structs.  You cannot change the function prototypes, the API is
  37.  * fixed.
  38.  */
  39.  
  40. /** A unique identifier for a peer */
  41. typedef uint64_t PeerId;
  42.  
  43. typedef struct {
  44.     /* TODO */
  45. } PeerManager;
  46.  
  47. /**
  48.  * Initialize @mgr so that it can track up to @maxpeers peers.
  49.  */
  50. void peermgr_init(PeerManager *mgr, unsigned int maxpeers)
  51. {
  52.     /* TODO */
  53. }
  54.  
  55. /**
  56.  * Free any resources allocated by @mgr.
  57.  */
  58. void peermgr_cleanup(PeerManager *mgr)
  59. {
  60.     /* TODO */
  61. }
  62.  
  63. /**
  64.  * Update peer information with new activity from @peer at time @timestamp.
  65.  * The peer manager retains a fixed number of unique peers with the most recent
  66.  * timestamps.  The maximum number of peers to remember was set in
  67.  * peermgr_init().
  68.  *
  69.  * If the maximum number of peers to remember has been reached, it may be
  70.  * necessary to forget about the peer with the oldest timestamp so that there
  71.  * is space for the newer peer.
  72.  */
  73. void peermgr_saw_peer(PeerManager *mgr, PeerId peer, time_t timestamp)
  74. {
  75.     /* TODO */
  76. }
  77.  
  78. /**
  79.  * Select a peer at random and store its identifier in @peer.
  80.  *
  81.  * Returns: true on success
  82.  */
  83. bool peermgr_pick_random_peer(PeerManager *mgr, PeerId *peer)
  84. {
  85.     /* TODO use random(3) and assume a seed has already been set */
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement