anchor.diff:      receiver :: pid(),
       table :: ets:tid()}).
   
  --opaque peerhandle() :: {peer, pid()}.
  --opaque originhandle() :: {origin, pid()}.
  --export_type([peerhandle/0, originhandle/0]).
  -
   -type torrent_id()  :: etorrent_types:torrent_id().
   -type pieceindex()  :: etorrent_types:pieceindex().
   -type chunkoffset() :: etorrent_types:chunkoffset().
   -type chunklength() :: etorrent_types:chunklength().
  -
   
   register_server(TorrentID) ->
       etorrent_utils:register(server_name(TorrentID)).
  
positive.code:     %% received the request and sent us this message. If so, bounce back a
      %% dropped-notification immidiately.
      #state{receiver=Receiver, table=Table} = State,
      case insert_request(Table, Peerpid, Index, Offset, Length) of
          true  -> ok;
          false ->
              etorrent_chunkstate:dropped(Index, Offset, Length, Peerpid, Receiver)
  
negative.code:     end.
  
  flush_requests(Table, Pid) ->
      Match = ets:fun2ms(fun({{I, O, L, P}}) -> {I, O, L} end),
      Requests  = ets:select(Table, Match),
      ets:select_delete(Table, Match),
      Requests.
  
  list_requests(Table) ->
      Match = ets:fun2ms(fun({{I, O, L, P}}) -> {I, O, L, P} end),
      ets:select(Table, Match).
  
  
  -ifdef(TEST).
  -include_lib("eunit/include/eunit.hrl").
  -define(pending, ?MODULE).
  -define(chunks, etorrent_chunkstate).
  -define(expect, etorrent_utils:expect).
  -define(wait, etorrent_utils:wait).
  -define(ping, etorrent_utils:ping).
  -define(first, etorrent_utils:first).
  
  
  testid() -> 0.
  testpid() -> ?pending:await_server(testid()).
  
  setup_env() ->
      {ok, Pid} = ?pending:start_link(testid()),
      ok = ?pending:receiver(self(), Pid),
      put({pending, testid()}, Pid),
      Pid.
  
  