macula_content_transfer (macula v0.20.5)

View Source

Content transfer module for Macula content-addressed storage.

Implements the want/have/block exchange protocol for transferring content blocks between mesh peers. Handles request tracking, block verification, and message creation/parsing.

Transfer Flow

1. Requester sends WANT message with list of MCIDs 2. Provider responds with BLOCK messages for each available block 3. Requester verifies each block hash matches MCID 4. Requester stores verified blocks

Example Usage

   %% Request blocks from a provider
   {ok, RequestId} = macula_content_transfer:request_blocks(MCIDs, ProviderNode),
  
   %% Handle incoming want (as provider)
   {ok, Response} = macula_content_transfer:handle_want(WantMsg),
  
   %% Handle incoming block (as requester)
   ok = macula_content_transfer:handle_block(BlockMsg).

Summary

Functions

Cancel a pending request.

Mark a request as complete.

Create a CANCEL message.

Create a HAVE message.

Create a HAVE message with options.

Create a MANIFEST_REQ message.

Create a WANT message. MCIDs can be binary MCIDs or {MCID, Priority} tuples.

Generate a unique request ID.

Handle an incoming BLOCK message. Verifies hash and stores the block.

Handle an incoming MANIFEST_REQ message.

Handle an incoming WANT message. Returns the block data if we have it.

Parse a BLOCK message.

Parse a CANCEL message.

Parse a HAVE message.

Parse a MANIFEST_REQ message.

Parse a MANIFEST_RES message.

Parse a WANT message.

Get list of pending request IDs.

Request blocks from a target node. Returns a request ID for tracking.

Get info about a request.

Start the content transfer server.

Functions

cancel_request(RequestId)

-spec cancel_request(binary()) -> ok.

Cancel a pending request.

complete_request(RequestId)

-spec complete_request(binary()) -> ok.

Mark a request as complete.

create_block_msg(RequestId, MCID, Data, FromNode)

-spec create_block_msg(binary(), binary(), binary(), binary()) -> map().

Create a BLOCK message.

create_cancel_msg(RequestId, FromNode)

-spec create_cancel_msg(binary(), binary()) -> map().

Create a CANCEL message.

create_have_msg(MCIDs, FromNode)

-spec create_have_msg([binary()], binary()) -> map().

Create a HAVE message.

create_have_msg(MCIDs, FromNode, Opts)

-spec create_have_msg([binary()], binary(), map()) -> map().

Create a HAVE message with options.

create_manifest_req_msg(MCID, FromNode)

-spec create_manifest_req_msg(binary(), binary()) -> map().

Create a MANIFEST_REQ message.

create_manifest_res_msg(RequestId, MCID, ManifestBin, FromNode)

-spec create_manifest_res_msg(binary(), binary(), binary(), binary()) -> map().

Create a MANIFEST_RES message.

create_want_msg(MCIDs, FromNode)

-spec create_want_msg([binary() | {binary(), 1..255}], binary()) -> map().

Create a WANT message. MCIDs can be binary MCIDs or {MCID, Priority} tuples.

generate_request_id()

-spec generate_request_id() -> binary().

Generate a unique request ID.

handle_block(Msg)

-spec handle_block(map()) -> ok | {error, term()}.

Handle an incoming BLOCK message. Verifies hash and stores the block.

handle_call(_, From, State)

handle_cast(Msg, State)

handle_info(Info, State)

handle_manifest_req(Msg)

-spec handle_manifest_req(map()) -> {ok, map()} | {error, not_found | term()}.

Handle an incoming MANIFEST_REQ message.

handle_want(Msg)

-spec handle_want(map()) -> {ok, map()} | {error, not_found | term()}.

Handle an incoming WANT message. Returns the block data if we have it.

init(Opts)

parse_block_msg(Msg)

-spec parse_block_msg(map()) -> {ok, map()} | {error, invalid_msg}.

Parse a BLOCK message.

parse_cancel_msg(Msg)

-spec parse_cancel_msg(map()) -> {ok, map()} | {error, invalid_msg}.

Parse a CANCEL message.

parse_have_msg(Msg)

-spec parse_have_msg(map()) -> {ok, map()} | {error, invalid_msg}.

Parse a HAVE message.

parse_manifest_req_msg(Msg)

-spec parse_manifest_req_msg(map()) -> {ok, map()} | {error, invalid_msg}.

Parse a MANIFEST_REQ message.

parse_manifest_res_msg(Msg)

-spec parse_manifest_res_msg(map()) -> {ok, map()} | {error, invalid_msg}.

Parse a MANIFEST_RES message.

parse_want_msg(Msg)

-spec parse_want_msg(map()) -> {ok, map()} | {error, invalid_msg}.

Parse a WANT message.

pending_requests()

-spec pending_requests() -> [binary()].

Get list of pending request IDs.

request_blocks(MCIDs, TargetNode)

-spec request_blocks([binary()], binary()) -> {ok, binary()}.

Request blocks from a target node. Returns a request ID for tracking.

request_info(RequestId)

-spec request_info(binary()) -> {ok, map()} | {error, not_found}.

Get info about a request.

start_link(Opts)

-spec start_link(map()) -> {ok, pid()} | {error, term()}.

Start the content transfer server.

terminate(Reason, State)