macula_download behaviour (macula v9.8.2)

View Source

Behaviour for supervised content downloads (the get/fetch side).

get_content/2 is a plain blocking call — no addressable pid to cancel it from outside. This is the consumer-side counterpart to macula_feeder: start_link/4,5 returns immediately with a pid, runs macula:get_content/2 in a linked worker, delivers the outcome to Module:handle_downloaded/2, and publishes sharing.get_started_v1 / sharing.get_completed_v1 mesh facts around the transfer — including outcome => cancelled if the download is cancelled before the get resolves.

This is content sharing, not general-purpose RPC streaming — see macula_streamer / macula_stream_sink for that (streaming.* facts belong to that pair).

Direct-dial

start_link/4,5 fetches through the pool's own connected link (whichever pick_connected_link/1 picks), reaching a copy via that station's 1-hop peer relay. start_link_direct/4,5 is the direct-dial counterpart: it resolves Mcid's provider from its signed content_announcement (published automatically by the provider's station on receipt — nothing to advertise explicitly, no direct-dial counterpart needed on the macula_feeder side) and dials that station directly, in one hop, instead of depending on the caller's own station being able to reach it via relay. Only chunked content is discoverable this way — see macula:find_content_providers/2. See macula_direct_dial's module doc, "Content" section, for the trust model (deliberately lighter than RPC's — content is self-verifying by hash).

Example

   -module(doc_download).
   -behaviour(macula_download).
   -export([init/1, handle_downloaded/2]).
  
   init(Parent) -> {ok, Parent}.
  
   handle_downloaded(Result, Parent) ->
       Parent ! {downloaded, Result},
       {stop, normal, Parent}.
   {ok, Pid} = macula_download:start_link(doc_download, Pool, Realm,
       Mcid, self()).

Summary

Functions

Cancel an in-flight download. Publishes sharing.get_completed_v1 with outcome => cancelled if the get had not resolved yet.

Start a download. Fetches Mcid via Pool.

As start_link/4, with Args passed to Module:init/1.

As start_link/4, but resolves and dials the MCID's provider directly instead of fetching through the pool's existing links. See the "Direct-dial" section above.

Callbacks

handle_downloaded/2

-callback handle_downloaded(Result :: {ok, binary()} | {error, term()}, State :: term()) ->
                               {noreply, NewState :: term()} | {stop, Reason :: term(), NewState :: term()}.

init/1

-callback init(Args :: term()) -> {ok, State :: term()} | {stop, Reason :: term()}.

Functions

cancel(Pid)

-spec cancel(pid()) -> ok.

Cancel an in-flight download. Publishes sharing.get_completed_v1 with outcome => cancelled if the get had not resolved yet.

start_link(Module, Pool, Realm, Mcid)

-spec start_link(module(), macula:pool(), macula:realm(), macula:mcid()) ->
                    {ok, pid()} | {error, term()}.

Start a download. Fetches Mcid via Pool.

start_link(Module, Pool, Realm, Mcid, Args)

-spec start_link(module(), macula:pool(), macula:realm(), macula:mcid(), term()) ->
                    {ok, pid()} | {error, term()}.

As start_link/4, with Args passed to Module:init/1.