wip: identify and switch

This commit is contained in:
Dmitriy Ryajov 2019-08-27 20:30:53 -06:00
parent ec86afebe0
commit 90d9dcbce9
5 changed files with 129 additions and 18 deletions

16
libp2p/host.nim Normal file
View File

@ -0,0 +1,16 @@
## Nim-LibP2P
## Copyright (c) 2018 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import chronos
import switch
type
Host* = ref object of RootObj
switch: Switch

34
libp2p/identify.nim Normal file
View File

@ -0,0 +1,34 @@
## Nim-LibP2P
## Copyright (c) 2018 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import chronos
import protobuf/minprotobuf, peerinfo,
switch, protocol as pr, connection
const IdentifyCodec = "/ipfs/id/1.0.0"
const IdentifyPushCodec = "/ipfs/id/push/1.0.0"
type
# TODO: we're doing manual protobuf, this is only temporary
ProtoField[T] = object
index: int
field: T
IdentifyMsg = object
protocolVersion: ProtoField[string]
agentVersion: ProtoField[string]
publicKey: ProtoField[seq[byte]]
listenAddrs: ProtoField[seq[byte]]
observedAddr: ProtoField[seq[byte]]
protocols: ProtoField[seq[ProtoField[seq[string]]]]
Identify = ref object of pr.Protocol
method dial*(p: Identify, peerInfo: PeerInfo): Future[Connection] {.async.} = discard
method handle*(p: Identify, peerInfo: PeerInfo, handler: pr.ProtoHandler) {.async.} = discard

29
libp2p/protocol.nim Normal file
View File

@ -0,0 +1,29 @@
## Nim-LibP2P
## Copyright (c) 2018 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import chronos
import connection, transport, stream,
peerinfo, multiaddress, multistreamselect,
switchtypes
proc newProtocol*(p: typedesc[Protocol],
peerInfo: PeerInfo,
switch: Switch): p =
new result
result.peerInfo = peerInfo
result.switch = switch
result.init()
method init*(p: Protocol) {.base.} = discard
method dial*(p: Protocol, peerInfo: PeerInfo): Future[Connection]
{.base, async, error: "not implemented!".} = discard
method handle*(p: Protocol, peerInfo: PeerInfo, handler: ProtoHandler)
{.base, async, error: "not implemented!".} = discard

View File

@ -8,25 +8,31 @@
## those terms.
import chronos
import connection, transport, stream, peerinfo, multiaddress
import connection, transport, stream,
peerinfo, multiaddress, multistreamselect,
switchtypes
type
ProtoHandler* = proc (conn: Connection, proto: string): Future[void]
ProtoHolder* = object of RootObj
proto: string
handler: ProtoHandler
Switch* = ref object of RootObj
connections*: seq[Connection]
transport*: seq[Transport]
peerInfo*: PeerInfo
protos: seq[ProtoHolder]
proc newSwitch*(): Switch =
proc newSwitch*(peerInfo: PeerInfo, transports: seq[Transport]): Switch =
new result
result.peerInfo = peerInfo
result.ms = newMultistream()
result.transports = transports
result.protocols = newSeq[Protocol]()
result.connections = newSeq[Connection]()
proc dial*(peer: PeerInfo, proto: string = "") {.async.} = discard
proc mount*(proto: string, handler: ProtoHandler) {.async.} = discard
proc dial*(s: Switch, peer: PeerInfo, proto: string = ""): Future[Connection] {.async.} = discard
proc start*() {.async.} = discard
proc stop*() {.async.} = discard
proc mount*(s: Switch, protocol: switchtypes.Protocol) = discard
proc start*(s: Switch) {.async.} =
proc handle(conn: Connection): Future[void] =
discard
for t in s.transports: # for each transport
for a in s.peerInfo.addrs:
if t.handles(a): # check if it handles the multiaddr
await t.listen(a, handle) # listen for incoming connections
break
proc stop*(s: Switch) {.async.} =
discard

26
libp2p/switchtypes.nim Normal file
View File

@ -0,0 +1,26 @@
## Nim-LibP2P
## Copyright (c) 2018 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import chronos
import connection, transport, stream,
peerinfo, multiaddress, multistreamselect
type
ProtoHandler* = proc (conn: Connection, proto: string): Future[void]
Protocol* = ref object of RootObj
peerInfo*: PeerInfo
switch*: Switch
codec*: string
Switch* = ref object of RootObj
peerInfo*: PeerInfo
connections*: seq[Connection]
transports*: seq[Transport]
protocols*: seq[Protocol]
ms*: MultisteamSelect