Skip to content

Commit

Permalink
enhancement/delegation-pattern (#17)
Browse files Browse the repository at this point in the history
* updates

* fixed test

* updated

* updated docs

* more docs
  • Loading branch information
decanus authored Aug 24, 2019
1 parent 3104beb commit 96ea0e6
Show file tree
Hide file tree
Showing 34 changed files with 1,044 additions and 284 deletions.
17 changes: 13 additions & 4 deletions Sources/UB/Message.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import Foundation

/// Message represents the message sent between nodes.
public struct Message {
let proto: UBID
let to: Addr
let from: Addr
let message: Data

/// The message protocol.
public let proto: UBID

/// The recipient of the message.
public let to: Addr

/// The sender of the message.
public let from: Addr

/// The raw message data.
public let message: Data
}

// @todo encoding and decoding
48 changes: 20 additions & 28 deletions Sources/UB/Node.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import Foundation

// @todo figure out architecture to support new forwarding algorithm.

/// An ultralight beam node, handles the interaction with transports and services.
public class Node {

/// The known transports for the node.
private(set) public var transports = [String: Transport]()

/// The known services for a node.
private(set) public var services = [UBID: Service]()

/// The nodes delegate.
public var delegate: NodeDelegate?

public init() { }

/// Adds a new transport to the list of known transports.
///
Expand All @@ -21,11 +25,10 @@ public class Node {
}

transport.listen { msg in
guard let service = services[msg.proto] else {
return
}

service.handle(msg)

// @todo delegate should return something where we handle retransmission.

delegate?.node(self, didReceiveMessage: msg)
}

transports[id] = transport
Expand All @@ -42,28 +45,17 @@ public class Node {

transports.removeValue(forKey: transport)
}

/// Adds a new service to the list of known services.
/// Sends a message through the current transports.
///
/// - Parameters:
/// - service: The new *Service* to add.
public func add(service: Service) {
if services[service.type] != nil {
return // @TODO: Maybe errors?
/// - message: The message to send.
public func send(_ message: Message) {
// @todo this is naive
transports.forEach { (_, transport) in
transport.send(message: message)
}

services[service.type] = service
}

/// Removes a service from the list of known services.
///
/// - Parameters:
/// - service: The *UBID* of the service to remove.
public func remove(service: UBID) {
guard services[service] != nil else {
return
}

services.removeValue(forKey: service)
}

// @todo create a message send loop with retransmissions and shit
}
12 changes: 12 additions & 0 deletions Sources/UB/NodeDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

/// NodeDelegate is used to handle the receiving of messages.
public protocol NodeDelegate {

/// Called when a node receives a message.
///
/// - Parameters:
/// - node: The node that received the message.
/// - message: The received message.
func node(_ node: Node, didReceiveMessage message: Message) // @todo return something?
}
16 changes: 0 additions & 16 deletions Sources/UB/Service/Service.swift

This file was deleted.

File renamed without changes.
5 changes: 4 additions & 1 deletion Sources/UB/Types.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Foundation

typealias Addr = [UInt8] // @todo use yeeth multiaddr
/// A node address represented as a byte array.
public typealias Addr = [UInt8] // @todo use yeeth multiaddr

/// Ultralight Beam specific IDs represented as byte arrays.
public typealias UBID = [UInt8] // @todo might be data?
25 changes: 0 additions & 25 deletions Tests/UBTests/NodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,4 @@ final class NodeTests: XCTestCase {

XCTAssert(node.transports.values.isEmpty)
}

func testAddService() {
let service = Service()
let node = UB.Node()

node.add(service: service)

let data = node.services.first!
XCTAssert(data.key == service.type)
XCTAssert((data.value as? Service) === service)
}

func testRemoveService() {
let service = Service()
let node = UB.Node()

node.add(service: service)

let data = node.services.first!
XCTAssert(data.key == service.type)

node.remove(service: service.type)

XCTAssert(node.services.values.isEmpty)
}
}
13 changes: 0 additions & 13 deletions Tests/UBTests/stubs/Service.swift

This file was deleted.

11 changes: 7 additions & 4 deletions docs/Classes.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<a title="Classes Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="index.html"> Docs</a> (87% documented)</p>
<p><a href="index.html"> Docs</a> (95% documented)</p>
<p class="header-right"><a href="https://github.com/ultralight-beam/UB.swift"><img src="img/gh.png"/>View on GitHub</a></p>
</div>
</header>
Expand All @@ -40,7 +40,7 @@
<a href="Protocols.html">Protocols</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Protocols/Service.html">Service</a>
<a href="Protocols/NodeDelegate.html">NodeDelegate</a>
</li>
<li class="nav-group-task">
<a href="Protocols/Transport.html">Transport</a>
Expand All @@ -51,13 +51,16 @@
<a href="Structs.html">Structures</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Structs.html#/s:2UB7MessageV">Message</a>
<a href="Structs/Message.html">Message</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Typealiases.html">Type Aliases</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Typealiases.html#/s:2UB4Addra">Addr</a>
</li>
<li class="nav-group-task">
<a href="Typealiases.html#/s:2UB7Handlera">Handler</a>
</li>
Expand Down Expand Up @@ -111,7 +114,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2019 <a class="link" href="http://ultralightbeam.io" target="_blank" rel="external">Ultralight Beam</a>. All rights reserved. (Last updated: 2019-06-23)</p>
<p>&copy; 2019 <a class="link" href="http://ultralightbeam.io" target="_blank" rel="external">Ultralight Beam</a>. All rights reserved. (Last updated: 2019-08-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
Loading

0 comments on commit 96ea0e6

Please sign in to comment.