Copyright | (c) Simon Désaulniers 2025 |
---|---|
License | GPL-3 |
Maintainer | sim.desaulniers@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
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
- data DhtRunner
- data DhtRunnerM m a
- runDhtRunnerM :: ShutdownCallback -> DhtRunnerM Dht () -> IO ()
- data DhtRunnerConfig = DhtRunnerConfig {}
- dhtConfig :: Lens' DhtRunnerConfig DhtSecureConfig
- proxyServer :: Lens' DhtRunnerConfig String
- pushNodeId :: Lens' DhtRunnerConfig String
- pushToken :: Lens' DhtRunnerConfig String
- pushTopic :: Lens' DhtRunnerConfig String
- pushPlatform :: Lens' DhtRunnerConfig String
- peerDiscovery :: Lens' DhtRunnerConfig Bool
- peerPublish :: Lens' DhtRunnerConfig Bool
- serverCa :: Lens' DhtRunnerConfig ByteString
- clientIdentity :: Lens' DhtRunnerConfig DhtIdentity
- logging :: Lens' DhtRunnerConfig Bool
- data DhtSecureConfig = DhtSecureConfig {}
- nodeConfig :: Lens' DhtSecureConfig DhtNodeConfig
- nodeId :: Lens' DhtSecureConfig DhtIdentity
- data DhtIdentity = DhtIdentity {}
- privatekey :: Lens' DhtIdentity PrivateKey
- certificate :: Lens' DhtIdentity Certificate
- data DhtNodeConfig = DhtNodeConfig {}
- nodeIdHash :: Lens' DhtNodeConfig (Maybe InfoHash)
- network :: Lens' DhtNodeConfig Word32
- isBootstrap :: Lens' DhtNodeConfig Bool
- maintainStorage :: Lens' DhtNodeConfig Bool
- persistPath :: Lens' DhtNodeConfig String
- type GetCallback = Value -> IO Bool
- type ValueCallback = Value -> Bool -> IO Bool
- type DoneCallback = Bool -> IO ()
- type ShutdownCallback = IO ()
- data OpToken
- type OpTokenMap = Map InfoHash [OpToken]
- getNodeIdHash :: DhtRunnerM Dht InfoHash
- getPublicKeyID :: DhtRunnerM Dht InfoHash
- getPermanentMetaValues :: DhtRunnerM Dht [Value]
- getListenTokens :: DhtRunnerM Dht OpTokenMap
- run :: Word16 -> DhtRunnerM Dht ()
- runConfig :: Word16 -> DhtRunnerConfig -> DhtRunnerM Dht ()
- isRunning :: DhtRunnerM Dht Bool
- bootstrap :: String -> String -> DhtRunnerM Dht ()
- get :: InfoHash -> GetCallback -> DoneCallback -> DhtRunnerM Dht ()
- put :: InfoHash -> Value -> DoneCallback -> Bool -> DhtRunnerM Dht Word64
- cancelPut :: InfoHash -> Word64 -> DhtRunnerM Dht ()
- listen :: InfoHash -> ValueCallback -> ShutdownCallback -> DhtRunnerM Dht OpToken
- cancelListen :: InfoHash -> OpToken -> MaybeT (DhtRunnerM Dht) ()
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
Arguments
:: ShutdownCallback | A callback to run before shutting down the DHT node. |
-> DhtRunnerM Dht () | The |
-> 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
Default DhtRunnerConfig Source # | |
Defined in OpenDHT.DhtRunner Methods def :: DhtRunnerConfig # |
Security
data DhtSecureConfig Source #
DHT node and security config.
Constructors
DhtSecureConfig | |
Fields
|
Instances
Default DhtSecureConfig Source # | |
Defined in OpenDHT.DhtRunner Methods def :: DhtSecureConfig # |
data DhtIdentity Source #
Constructors
DhtIdentity | |
Fields |
Instances
Default DhtIdentity Source # | |
Defined in OpenDHT.DhtRunner Methods def :: DhtIdentity # |
DHT options
data DhtNodeConfig Source #
Constructors
DhtNodeConfig | |
Fields
|
Instances
Default DhtNodeConfig Source # | |
Defined in OpenDHT.DhtRunner Methods def :: DhtNodeConfig # |
Callbacks
type GetCallback Source #
type ValueCallback Source #
type DoneCallback Source #
The generic callback invoked for all asynchronous operations when those terminate.
type ShutdownCallback = IO () Source #
A callback invoked before the Dhtnode is shutdown.
Accessors
A token used to track Listen requests.
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
Arguments
:: Word16 | The port on which to run the DHT node. Use |
-> DhtRunnerM Dht () |
Run the OpenDHT node on a given port.
Arguments
:: Word16 | The port on which to run the DHT node. Use |
-> 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.
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
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.
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 |
Put a Value
on the DHT for a given hash.
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.
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, anOpToken
is added to the map of tokens (listenTokens
) for the given hash in the state ofDhtRunnerM
.
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.