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.

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() :: t(map())
@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:

  1. The transaction begins.
  2. Read and disinfect the HTTP parameters.
  3. Get the torrent from database, or create a new one if it doesn't exist.
  4. Get the peer from database, or create a new one if it doesn't exist.
  5. Create a connection between the torrent and the peer, and record the status of the connection.
  6. Commit the transaction.

parameters

Parameters

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)
@spec query_peers(t()) :: t()

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, %{}})