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: %% @private
  init([TorrentID]) ->
      true = register_server(TorrentID),
      Table = ets:new(none, [private, set]),
      InitState = #state{
          receiver=self(),
          table=Table},
  
negative.code: handle_call({register, Peerpid}, _, State) ->
      #state{table=Table} = State,
      Ref = erlang:monitor(process, Peerpid),
      Reply = case insert_peer(Table, Peerpid, Ref) of
          true  -> ok;
          false ->
              erlang:demonitor(Ref),
              error
      end,
      {reply, ok, State};
  
  handle_call({receiver, Newrecvpid}, _, State) ->
      #state{table=Table} = State,
      Requests = list_requests(Table),
      Assigned = fun({Piece, Offset, Length, Peerpid}) ->
          etorrent_chunkstate:assigned(Piece, Offset, Length, Peerpid, Newrecvpid)
      end,
      erlang:display(Requests),
      [Assigned(Request) || Request <- Requests],
      NewState = State#state{receiver=Newrecvpid},
      {reply, ok, NewState}.
  