opendht-hs-0.1.1.1: Haskell bindings for OpenDHT
Copyright(c) Simon Désaulniers 2025
LicenseGPL-3
Maintainersim.desaulniers@gmail.com
Safe HaskellSafe-Inferred
LanguageHaskell2010

OpenDHT.DhtRunner

Description

This encapsulates functions and data types for manipulating an OpenDHT node. In OpenDHT, a node is used through the C++ class DhtRunner. This module gives all required bindings for using a DHT node.

For reference, the user should also take a look at the C++ API documentation by OpenDHT authors:

Synopsis

The DhtRunnerM monad

data DhtRunnerM m a Source #

This type wraps all function calls to OpenDHT. It is a transformer wrapping ReaderT and some other monad (usually Dht).

This type should be used in conjunction with runDhtRunnerM.

Instances

Instances details
MonadTrans DhtRunnerM Source # 
Instance details

Defined in OpenDHT.DhtRunner

Methods

lift :: Monad m => m a -> DhtRunnerM m a #

MonadIO m => MonadIO (DhtRunnerM m) Source # 
Instance details

Defined in OpenDHT.DhtRunner

Methods

liftIO :: IO a -> DhtRunnerM m a #

Applicative m => Applicative (DhtRunnerM m) Source # 
Instance details

Defined in OpenDHT.DhtRunner

Methods

pure :: a -> DhtRunnerM m a #

(<*>) :: DhtRunnerM m (a -> b) -> DhtRunnerM m a -> DhtRunnerM m b #

liftA2 :: (a -> b -> c) -> DhtRunnerM m a -> DhtRunnerM m b -> DhtRunnerM m c #

(*>) :: DhtRunnerM m a -> DhtRunnerM m b -> DhtRunnerM m b #

(<*) :: DhtRunnerM m a -> DhtRunnerM m b -> DhtRunnerM m a #

Functor m => Functor (DhtRunnerM m) Source # 
Instance details

Defined in OpenDHT.DhtRunner

Methods

fmap :: (a -> b) -> DhtRunnerM m a -> DhtRunnerM m b #

(<$) :: a -> DhtRunnerM m b -> DhtRunnerM m a #

Monad m => Monad (DhtRunnerM m) Source # 
Instance details

Defined in OpenDHT.DhtRunner

Methods

(>>=) :: DhtRunnerM m a -> (a -> DhtRunnerM m b) -> DhtRunnerM m b #

(>>) :: DhtRunnerM m a -> DhtRunnerM m b -> DhtRunnerM m b #

return :: a -> DhtRunnerM m a #

runDhtRunnerM Source #

Arguments

:: ShutdownCallback

A callback to run before shutting down the DHT node.

-> DhtRunnerM Dht ()

The DhtRunnerM action.

-> IO () 

Starts a DhtRunner session. This initializes the underlying OpenDHT node and takes care of freeing it before this function terminates.

DhtRunner's function calls don't block the thread they're running on. Therefore, the user should take care of writing appropriate concurrency code for waiting on the underlying node's callback invocation before letting this function terminate. In general, the programmer should not let this function terminate while DHT operations are still susceptible to occur for the application. However, before this function terminates, the node should have been gracefully shutdown by calling it's shutdown method. This will therefore call the user's shutdown callback.

Configuration

Main options

data DhtRunnerConfig Source #

Constructors

DhtRunnerConfig 

Fields

Instances

Instances details
Default DhtRunnerConfig Source # 
Instance details

Defined in OpenDHT.DhtRunner

Security

data DhtSecureConfig Source #

DHT node and security config.

Constructors

DhtSecureConfig 

Fields

Instances

Instances details
Default DhtSecureConfig Source # 
Instance details

Defined in OpenDHT.DhtRunner

data DhtIdentity Source #

Instances

Instances details
Default DhtIdentity Source # 
Instance details

Defined in OpenDHT.DhtRunner

Methods

def :: DhtIdentity #

DHT options

data DhtNodeConfig Source #

Constructors

DhtNodeConfig 

Fields

Instances

Instances details
Default DhtNodeConfig Source # 
Instance details

Defined in OpenDHT.DhtRunner

Methods

def :: DhtNodeConfig #

Callbacks

type GetCallback Source #

Arguments

 = Value

A value found for the asked hash.

-> IO Bool 

Callback invoked whenever a Value is retrieved on the DHT during a Get (get) request. This callback shall return a boolean indicating whether to stop the Get request or not.

type ValueCallback Source #

Arguments

 = Value

A value found for the asked hash.

-> Bool

Whether the value is expired or not.

-> IO Bool 

Callback invoked whenever a Value is retrieved on the DHT during a Listen (listen) request. This callback shall be called once when a value is found and once when this same value has expired on the network. Finally, it returns a boolean indicating whether to stop the Listen request or not.

type DoneCallback Source #

Arguments

 = Bool

A boolean indicating whether the operation was successful or not.

-> IO () 

The generic callback invoked for all asynchronous operations when those terminate.

type ShutdownCallback = IO () Source #

A callback invoked before the Dhtnode is shutdown.

Accessors

data OpToken Source #

A token used to track Listen requests.

Instances

Instances details
Eq OpToken Source # 
Instance details

Defined in OpenDHT.DhtRunner

Methods

(==) :: OpToken -> OpToken -> Bool #

(/=) :: OpToken -> OpToken -> Bool #

type OpTokenMap = Map InfoHash [OpToken] Source #

Map of OpToken by InfoHash location.

getNodeIdHash :: DhtRunnerM Dht InfoHash Source #

Get the ID of the underlying DHT node.

getPublicKeyID :: DhtRunnerM Dht InfoHash Source #

Get the public key ID of the DhtRunner.

getPermanentMetaValues :: DhtRunnerM Dht [Value] Source #

Access to the current list of values permanently put.

getListenTokens :: DhtRunnerM Dht OpTokenMap Source #

Access to the current listen OpToken map (see listen and cancelListen for more info).

Initialization

run Source #

Arguments

:: Word16

The port on which to run the DHT node. Use 0 to let the network layer decide.

-> DhtRunnerM Dht () 

Run the OpenDHT node on a given port.

runConfig Source #

Arguments

:: Word16

The port on which to run the DHT node. Use 0 to let the network layer decide.

-> DhtRunnerConfig

The DhtRunner configuration.

-> DhtRunnerM Dht () 

Run the OpenDHT node on a given port according to the specified configuration.

This module exposes lenses (see Lens) for configuring easily every field of the config data type. Therefore, it's convinient to use & and .~ lens operators in order to set the config. This paired with the Default instance of DhtRunnerConfig make it so that you can start from the default config and build up a specific config in the following manner:

myDhtConfig :: DhtRunnerConfig
myDhtConfig = def
  & logging                          .~ True
  & proxyServer                      .~ "dhtproxy.jami.net:80"
  & dhtConfig.nodeConfig.persistPath .~ "~/.opendht/dht.data"

For the full list of config lenses, see fields of DhtRunnerConfig, DhtSecureConfig, DhtNodeConfig and DhtIdentity. The lists for each data type should follow the respective data type.

isRunning :: DhtRunnerM Dht Bool Source #

Indicates whether the underlying node is running. This should yield True after calling run or runConfig.

bootstrap Source #

Arguments

:: String

The hostname (or IP address) used to bootstrap the connection to the network.

-> String

The remote bootstrapping node port to use to connect.

-> DhtRunnerM Dht () 

Connect to the OpenDHT network before doing any operation.

Note that when _proxyServer is enabled, this has no effect.

DHT operations

get Source #

Arguments

:: InfoHash

The hash for which to get data at.

-> GetCallback

The callback invoked for all values retrieved on the DHT for the given hash.

-> DoneCallback

The callback invoked when OpenDHT has completed the get request.

-> DhtRunnerM Dht () 

Get a Value pointed at by a given hash on the DHT.

put Source #

Arguments

:: InfoHash

The hash under which to store the value.

-> Value

The value to put on the DHT.

-> DoneCallback

The callback to invoke when the request is completed (or has failed).

-> Bool

Whether the value should be "permanent". A permanent value is reannounced automatically after it has expired (after 10 minutes). NOTE: This requires node to keep running.

-> DhtRunnerM Dht Word64

The ID of the value that was put on the network. This serves to cancel a permanent put later on (see cancelPut).

Put a Value on the DHT for a given hash.

cancelPut Source #

Arguments

:: InfoHash

The hash for which the value was first put.

-> Word64

The value ID.

-> DhtRunnerM Dht () 

Cancel a Put request.

The DhtRunnerM monad automatically tracks the permanently put values. In order to cancel one, the user can call getPermanentMetaValues to get the ID of the value he wants to cancel.

listen Source #

Arguments

:: InfoHash

The hash indicating where to listen to.

-> ValueCallback

The callback to invoke when a value is found or has expired.

-> ShutdownCallback

The callback to invoke before the OpenDHT node shuts down.

-> DhtRunnerM Dht OpToken

The token identifying the Listen request.

Initiate a Listen operation for a given hash.

  • The ValueCallback will be invoked once for every value found and once also when each of these same values expire on the DHT.
  • While the Listen operation is not cancelled (or the node shutdown), it goes on. Threfore, values subsequently published will be received.
  • When listen terminates, an OpToken is added to the map of tokens (listenTokens) for the given hash in the state of DhtRunnerM.

cancelListen Source #

Arguments

:: InfoHash

The hash for which the Listen request was previously issued.

-> OpToken

The token associated identifying the exact Listen operation.

-> MaybeT (DhtRunnerM Dht) () 

Cancel an on-going Listen operation.

If no Listen request is found for the given arguments, the function returns and does nothing.