Handle HAVE_ALL and HAVE_NONE. Cleanup the BITFIELD message.
[etorrent.git] / lib / etorrent-1.0 / src / etorrent_peer.erl
blobcf0defed875c5f9a53a5681953fccaefeaf4602a
1 %%%-------------------------------------------------------------------
2 %%% File : etorrent_peer.erl
3 %%% Author : Jesper Louis Andersen <>
4 %%% Description : Manipulations of the peer mnesia table.
5 %%%
6 %%% Created : 16 Jun 2008 by Jesper Louis Andersen <>
7 %%%-------------------------------------------------------------------
8 -module(etorrent_peer).
10 -include_lib("stdlib/include/qlc.hrl").
11 -include("etorrent_mnesia_table.hrl").
13 %% API
14 -export([new/5, all/1, delete/1, connected/3, ip_port/1, select/1,
16 statechange/2]).
18 %%====================================================================
19 %% API
20 %%====================================================================
21 %%--------------------------------------------------------------------
22 %% Function: new(IP, Port, InfoHash, Pid) -> transaction
23 %% Description: Insert a row for the peer
24 %%--------------------------------------------------------------------
25 new(IP, Port, TorrentId, Pid, State) ->
26 mnesia:dirty_write(#peer { pid = Pid,
27 ip = IP,
28 port = Port,
29 torrent_id = TorrentId,
30 state = State}).
32 %%--------------------------------------------------------------------
33 %% Function: statechange(Pid, seeder) -> transaction
34 %% Description: Change the peer to a seeder
35 %%--------------------------------------------------------------------
36 statechange(Pid, seeder) ->
37 {atomic, _} = mnesia:transaction(
38 fun () ->
39 [Row] = mnesia:read(peer, Pid, write),
40 mnesia:write(Row#peer { state = seeding })
41 end),
42 ok.
44 %%--------------------------------------------------------------------
45 %% Function: ip_port(Pid) -> {IP, Port}
46 %% Description: Select the IP and Port pair of a Pid
47 %%--------------------------------------------------------------------
48 ip_port(Pid) ->
49 [R] = mnesia:dirty_read(peer, Pid),
50 {R#peer.ip, R#peer.port}.
52 %%--------------------------------------------------------------------
53 %% Function: delete(Pid) -> ok | {aborted, Reason}
54 %% Description: Delete all references to the peer owned by Pid
55 %%--------------------------------------------------------------------
56 delete(Id) when is_integer(Id) ->
57 [mnesia:dirty_delete_object(Peer) ||
58 Peer <- mnesia:dirty_index_read(peer, Id, #peer.torrent_id)];
59 delete(Pid) when is_pid(Pid) ->
60 mnesia:dirty_delete(peer, Pid).
62 %%--------------------------------------------------------------------
63 %% Function: connected(IP, Port, Id) -> bool()
64 %% Description: Returns true if we are already connected to this peer.
65 %%--------------------------------------------------------------------
66 connected(IP, Port, Id) when is_integer(Id) ->
67 F = fun () ->
68 Q = qlc:q([P || P <- mnesia:table(peer),
69 P#peer.ip =:= IP,
70 P#peer.port =:= Port,
71 P#peer.torrent_id =:= Id]),
72 length(qlc:e(Q)) > 0
73 end,
74 {atomic, B} = mnesia:transaction(F),
77 %%--------------------------------------------------------------------
78 %% Function: all(Id) -> [#peer]
79 %% Description: Return all peers with a given Id
80 %%--------------------------------------------------------------------
81 all(Id) ->
82 mnesia:dirty_index_read(peer, Id, #peer.torrent_id).
84 %%--------------------------------------------------------------------
85 %% Function: select(P)
86 %% P ::= pid()
87 %% Description: Select the peer matching pid P.
88 %%--------------------------------------------------------------------
89 select(Pid) when is_pid(Pid) ->
90 mnesia:dirty_read(peer, Pid).
92 %%====================================================================
93 %% Internal functions
94 %%====================================================================