Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/node-send #64

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions Sources/UB/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import SwiftProtobuf

/// An ultralight beam node, handles the interaction with transports and services.
public class Node {
/// Represents errors that can be encountered when interacting with a node.
public enum NodeError: Error {
/// This error is thrown when send is called without a recipient and proto.
case noTarget
}

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

Expand Down Expand Up @@ -42,11 +48,40 @@ public class Node {
transports.removeValue(forKey: transport)
}

// @todo maybe we need convinience functions where you send to a proto, a to or one for both.

/// Sends a message through the current transports.
///
/// - Parameters:
/// - message: The message to send.
public func send(_ message: Message) {
/// - data: The data to send.
/// - to: The address to send to.
/// - proto: The protocol required.
///
/// - Throws: Error if both the to address and proto are empty.
public func send(
data: Data,
to: Addr = Addr(repeating: 0, count: 0),
proto: UBID = UBID(repeating: 0, count: 0)
) throws {
if to.count == 0, proto.count == 0 {
throw NodeError.noTarget
}

// @todo think about message, makes no sense to have the entire from part.
// we only need that when we receive a message to determine where we retransmit it to.

send(
Message(
proto: proto,
recipient: to,
from: Addr(repeating: 0, count: 0),
origin: Addr(repeating: 0, count: 0), // @todo, figure out how best to set this. Nodes need an identity
message: data
)
)
}

private func send(_ message: Message) {
if message.recipient.count == 0, message.proto.count == 0 {
return
}
Expand Down