diff --git a/.gitignore b/.gitignore index 6771b32..caa4b93 100644 --- a/.gitignore +++ b/.gitignore @@ -51,8 +51,7 @@ playground.xcworkspace # Carthage # # Add this line if you want to avoid checking in source code from Carthage dependencies. -# Carthage/Checkouts - +Carthage/Checkouts Carthage/Build # fastlane diff --git a/Sources/libp2p/Enums/State.swift b/Sources/libp2p/Enums/State.swift new file mode 100644 index 0000000..607f3cd --- /dev/null +++ b/Sources/libp2p/Enums/State.swift @@ -0,0 +1,8 @@ +import Foundation + +enum State { + case stopped + case starting + case started + case stopping +} diff --git a/Sources/libp2p/Node/Node.swift b/Sources/libp2p/Node/Node.swift new file mode 100644 index 0000000..38e570f --- /dev/null +++ b/Sources/libp2p/Node/Node.swift @@ -0,0 +1,57 @@ +import Foundation + +public class Node { + + var delegate: NodeDelegate? + + private(set) var state: State { + didSet { + switch (state) { + case .started: + delegate?.nodeDidStart(self) + case .stopped: + delegate?.nodeDidStop(self) + case .starting: + onStarting() + case .stopping: + onStopping() + } + } + } + + private(set) var peerBook: PeerBook + + init() { + state = .stopped + peerBook = PeerBook() + } + + func isStarted() -> Bool { + return state == .started + } + + // @todo on the any + func dial(peer: Any, protocol: String) { + + } + + func dialFSM(peer: Any, protocol: String) { + + } + + func hangUp(peer: Any) { + + } + + func ping(peer: Any) { + + } + + private func onStarting() { + + } + + private func onStopping() { + + } +} diff --git a/Sources/libp2p/Node/NodeDelegate.swift b/Sources/libp2p/Node/NodeDelegate.swift new file mode 100644 index 0000000..d26d731 --- /dev/null +++ b/Sources/libp2p/Node/NodeDelegate.swift @@ -0,0 +1,12 @@ +import Foundation + +public protocol NodeDelegate { + + func node(_ node: Node, didEmitError: Error); + func node(_ node: Node, didConnectPeer: PeerInfo); + func node(_ node: Node, didDisconnectPeer: PeerInfo); + func node(_ node: Node, didDiscoverPeer: PeerInfo); + func nodeDidStart(_ node: Node); + func nodeDidStop(_ node: Node); + +} diff --git a/Sources/libp2p/Peer/PeerInfo.swift b/Sources/libp2p/Peer/PeerInfo.swift new file mode 100644 index 0000000..0b0802e --- /dev/null +++ b/Sources/libp2p/Peer/PeerInfo.swift @@ -0,0 +1,8 @@ +import Foundation +import SwiftMultiaddr + +public struct PeerInfo { + let id: Data // @todo + let multiaddrs: [Multiaddr] + let protocols: [String] // @todo +} diff --git a/Sources/libp2p/PeerBook.swift b/Sources/libp2p/PeerBook.swift new file mode 100644 index 0000000..f34a3fe --- /dev/null +++ b/Sources/libp2p/PeerBook.swift @@ -0,0 +1,33 @@ +import Foundation +import SwiftMultiaddr + +class PeerBook { + + private(set) var peers = [String:PeerInfo]() + + func has(_ peer: Any) -> Bool { + + } + + func put(_ peer: PeerInfo) { + + } + + func get(_ peer: Any) -> PeerInfo? { + return peers[b58String(peer)] + } + + func remove(_ peer: Any) { + peers.removeValue(forKey: b58String(peer)) + } + + // @todo MultiAddr type + func getMultiAddrs(_ peer: Any) -> [MultiAddr]? { + let info = get(peer) + return info?.multiaddrs + } + + func b58String(_ peer: Any) -> String { + + } +} diff --git a/Sources/libp2p/TCP/TCP.swift b/Sources/libp2p/TCP/TCP.swift new file mode 100644 index 0000000..82e85a9 --- /dev/null +++ b/Sources/libp2p/TCP/TCP.swift @@ -0,0 +1,20 @@ +import Foundation + +class TCP { + + // @todo add option + func dial(multiAddress: String) { + + } + + // @todo + func createListener() { + + } + + // @todo + func filter(multiAddresses: [String]) { + + } + +} diff --git a/Sources/libp2p/libp2p.swift b/Sources/libp2p/libp2p.swift deleted file mode 100644 index c15936d..0000000 --- a/Sources/libp2p/libp2p.swift +++ /dev/null @@ -1,3 +0,0 @@ -class libp2p { - -}