View Source YaBTT (YaBTT v0.1.5)
Yet another BitTorrent Tracker
This is the main entry point for the YaBTT
as a library.
All the functions will be contained in this module.
Specifically, the insert_or_update/1
function is used to insert or update a
torrent and a peer, and thier status and relationship. The query/1
function
is used to query the peers who hold the target torrent.
Link to this section Summary
Functions
A transaction that inserts or updates a torrent and a peer.
A query function optimized specifically for insert_or_update/1
.
Re-export the YaBTT.Query.State.query/1
function.
Link to this section Types
@type errors() :: {:error, Ecto.Multi.name(), Ecto.Changeset.t(), Ecto.Multi.t()} | {:error, Bento.Encoder.bencodable()} | {:error, Ecto.Changeset.t()}
@type t(res) :: {:ok, res} | errors()
Link to this section Functions
@spec insert_or_update(Plug.Conn.t()) :: t()
A transaction that inserts or updates a torrent and a peer.
The main process of the transaction:
- The transaction begins.
- Read and disinfect the HTTP parameters.
- Get the
torrent
from database, or create a new one if it doesn't exist. - Get the
peer
from database, or create a new one if it doesn't exist. - Create a
connection
between thetorrent
and thepeer
, and record the status of theconnection
. - Commit the transaction.
parameters
Parameters
conn
: thePlug.Conn
struct
examples
Examples
iex> params = %{
...> "info_hash" => "f0a15e27fafbffc1c2f18f69fcac2dfa461ff4e7",
...> "peer_id" => "-TR14276775888084597",
...> "key" => "ecsc1ggh0h",
...> "port" => "6881",
...> "uploaded" => "121",
...> "downloaded" => "41421",
...> "left" => "0",
...> "event" => "started"
...> }
iex> conn = %Plug.Conn{params: params, remote_ip: {127, 0, 0, 1}}
iex> YaBTT.insert_or_update(conn)
A query function optimized specifically for insert_or_update/1
.
Its essence is still query_peers/2
, but we extract the YaBTT.Query.Peers.id/0
and YaBTT.Query.Peers.opts/0
from the transaction
result. At the same time,
we wrap the result in a {:ok, _}
tuple, and propagating the error from
the transaction
result.
parameters
Parameters
transaction
: the result of transactions
examples
Examples
iex> deco = %YaBTT.Deco{
...> ids: %{info_hash: "f0a15e27fafbffc1c2f18f69fcac2dfa461ff4e7"},
...> config: %{mode: :compact, query_limit: 50}
...> }
iex> YaBTT.query_peers({:ok, %{deco: deco}})
iex> YaBTT.query_peers({:error, :multi_name, %{}, %{}})
iex> YaBTT.query_peers({:error, %{}})
iex> YaBTT.query_peers(:internal_errors)
@spec query_state(t() | [YaBTT.Query.State.info_hash()]) :: t() | YaBTT.Query.State.t()
Re-export the YaBTT.Query.State.query/1
function.
examples
Examples
iex> YaBTT.query_state(["info_hash_1", "info_hash_2"])
iex> YaBTT.query_state({:ok, %{info_hash: ["info_hash_1", "info_hash_2"]}})
iex> YaBTT.query_state({:error, %{}})