diff --git a/libp2p/host.nim b/libp2p/host.nim new file mode 100644 index 000000000..16ef07593 --- /dev/null +++ b/libp2p/host.nim @@ -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 + diff --git a/libp2p/identify.nim b/libp2p/identify.nim new file mode 100644 index 000000000..fcaeb50b6 --- /dev/null +++ b/libp2p/identify.nim @@ -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 diff --git a/libp2p/protocol.nim b/libp2p/protocol.nim new file mode 100644 index 000000000..b4f96ccde --- /dev/null +++ b/libp2p/protocol.nim @@ -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 diff --git a/libp2p/switch.nim b/libp2p/switch.nim index a839dcd1d..09887478b 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -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 diff --git a/libp2p/switchtypes.nim b/libp2p/switchtypes.nim new file mode 100644 index 000000000..8456a4b3d --- /dev/null +++ b/libp2p/switchtypes.nim @@ -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