1 %%%-------------------------------------------------------------------
2 %%% File : etorrent_peer.erl
3 %%% Author : Jesper Louis Andersen <>
4 %%% Description : Manipulations of the peer mnesia table.
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").
14 -export([new
/5, all
/1, delete
/1, connected
/3, ip_port
/1, select
/1,
18 %%====================================================================
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
,
29 torrent_id
= TorrentId
,
32 %%--------------------------------------------------------------------
33 %% Function: statechange(Pid, seeder) -> transaction
34 %% Description: Change the peer to a seeder
35 %%--------------------------------------------------------------------
36 statechange(Pid
, seeder
) ->
37 {atomic
, _
} = mnesia:transaction(
39 [Row
] = mnesia:read(peer
, Pid
, write
),
40 mnesia:write(Row#peer
{ state
= seeding
})
44 %%--------------------------------------------------------------------
45 %% Function: ip_port(Pid) -> {IP, Port}
46 %% Description: Select the IP and Port pair of a Pid
47 %%--------------------------------------------------------------------
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
) ->
68 Q
= qlc:q([P
|| P
<- mnesia:table(peer
),
71 P#peer
.torrent_id
=:= Id
]),
74 {atomic
, B
} = mnesia:transaction(F
),
77 %%--------------------------------------------------------------------
78 %% Function: all(Id) -> [#peer]
79 %% Description: Return all peers with a given Id
80 %%--------------------------------------------------------------------
82 mnesia:dirty_index_read(peer
, Id
, #peer
.torrent_id
).
84 %%--------------------------------------------------------------------
85 %% Function: select(P)
87 %% Description: Select the peer matching pid P.
88 %%--------------------------------------------------------------------
89 select(Pid
) when is_pid(Pid
) ->
90 mnesia:dirty_read(peer
, Pid
).
92 %%====================================================================
94 %%====================================================================