diff --git a/README.md b/README.md index 9ebc40f..70fb868 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,15 @@ Identifies the daemon. Ensures there is a connection open to `peer`. +#### libp2p-disconnect +``` +(libp2p-disconnect c peer) + c := client + peer := ID +``` + +Closes any open connections to `peer`. + #### libp2p-stream ``` (libp2p-stream c peer protos) diff --git a/example/libp2p-chat.ss b/example/libp2p-chat.ss index deb3fd1..c3184e0 100644 --- a/example/libp2p-chat.ss +++ b/example/libp2p-chat.ss @@ -3,6 +3,7 @@ ;;; libp2p chat example (import :gerbil/gambit/threads + :gerbil/gambit/ports :std/getopt :std/sugar :std/iter @@ -48,7 +49,11 @@ ((string-empty? line) (lp)) (else - (displayln "\x1b[32m" line "\x1b[0m") + (write-u8 #x1b) + (display "[32m") + (display line) + (write-u8 #x1b) + (displayln "[0m") (display "> ") (lp)))))) diff --git a/libp2p.ss b/libp2p.ss index 687a05b..dfd9776 100644 --- a/libp2p.ss +++ b/libp2p.ss @@ -28,6 +28,7 @@ open-libp2p-client libp2p-identify libp2p-connect + libp2p-disconnect libp2p-stream libp2p-listen libp2p-close diff --git a/libp2p/client.ss b/libp2p/client.ss index 19c9975..1cbeaea 100644 --- a/libp2p/client.ss +++ b/libp2p/client.ss @@ -98,6 +98,14 @@ addrs: (map multiaddr-bytes (peer-info-addrs pinfo))))) (control-request c req void))) +(def (libp2p-disconnect c p) + (let (req + (Request + type: 'DISCONNECT + disconnect: (DisconnectRequest + peer: (ID-bytes p)))) + (control-request c req void))) + (def (libp2p-stream c peer protos (bufsz 4096)) (let* ((id (if (ID? peer) peer (peer-info-id peer))) (s (open-stream c bufsz)) diff --git a/libp2p/pb/p2pd.ss b/libp2p/pb/p2pd.ss index dfb6a5b..4d63402 100644 --- a/libp2p/pb/p2pd.ss +++ b/libp2p/pb/p2pd.ss @@ -13,6 +13,7 @@ message Request { DHT = 4; LIST_PEERS = 5; CONNMANAGER = 6; + DISCONNECT = 7; } required Type type = 1; @@ -22,6 +23,7 @@ message Request { optional StreamHandlerRequest streamHandler = 4; optional DHTRequest dht = 5; optional ConnManagerRequest connManager = 6; + optional DisconnectRequest disconnect = 7; } message Response { @@ -109,9 +111,9 @@ message PeerInfo { message ConnManagerRequest { enum Type { - TAG_PEER = 0; - UNTAG_PEER = 1; - TRIM = 2; + TAG_PEER = 0; + UNTAG_PEER = 1; + TRIM = 2; } required Type type = 1; @@ -120,3 +122,7 @@ message ConnManagerRequest { optional string tag = 3; optional int64 weight = 4; } + +message DisconnectRequest { + required bytes peer = 1; +}