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

feature/services #71

Closed
wants to merge 13 commits into from
40 changes: 33 additions & 7 deletions Sources/UB/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class Node {
/// The known transports for the node.
public private(set) var transports = [String: Transport]()

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

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

Expand Down Expand Up @@ -35,11 +38,20 @@ public class Node {
/// - Parameters:
/// - transport: The identifier of the transport to remove.
public func remove(transport: String) {
guard transports[transport] != nil else {
transports.removeValue(forKey: transport)
}

public func add(service: Service) {
let id = service.identifier
if services[id] != nil {
return
}

transports.removeValue(forKey: transport)
services[id] = service
}

public func remove(service: UBID) {
services.removeValue(forKey: service)
}

/// Sends a message through the current transports.
Expand Down Expand Up @@ -100,17 +112,31 @@ public class Node {
/// :nodoc:
extension Node: TransportDelegate {
public func transport(_: Transport, didReceiveData data: Data, from: Addr) {
// @todo message should probably be created here

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

// @todo if node delegate doesn't return anything success, send out the message?

guard let packet = try? Packet(serializedData: data) else {
// @todo
return
}

delegate?.node(self, didReceiveMessage: Message(protobuf: packet, from: from))
let message = Message(protobuf: packet, from: from)

if message.service.count > 0 {
if let service = services[message.service] {
return service.node(self, didReceiveMessage: message) // @todo maybe status check?
}
}

// @todo check if we are the recipient else retransmit
// if node.identity == message.recipient {
// return delegate?.node(self, didReceiveMessage: message) // @todo maybe status check
// }


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limit vertical whitespace to a single empty line. Currently 2.

// @todo retransmit if we don't have the service


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limit vertical whitespace to a single empty line. Currently 2.

return send(message)

}
}
7 changes: 7 additions & 0 deletions Sources/UB/Service.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

public protocol Service: NodeDelegate {
/// The unique service identifier.
var identifier: UBID { get }

}