Skip to content

Commit

Permalink
Merge branch 'master' into refactor/node-send
Browse files Browse the repository at this point in the history
  • Loading branch information
decanus authored Sep 24, 2019
2 parents 5ffded3 + f79ef6f commit 98f3974
Show file tree
Hide file tree
Showing 45 changed files with 251 additions and 479 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

gem 'xcpretty'
gem 'jazzy', '0.11.1'
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ autocorrect:
swiftlint autocorrect

docs:
jazzy --author "Ultralight Beam" --author_url http://ultralightbeam.io --github_url https://github.com/ultralight-beam/UB.swift
jazzy --module UB --author "Ultralight Beam" --author_url http://ultralightbeam.io --github_url https://github.com/ultralight-beam/UB.swift
rm -rf build/

xcode:
Expand Down
4 changes: 2 additions & 2 deletions Sources/UB/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public struct Message: Equatable {
self.message = message
}

/// Initializes a Message from a protocol buffer `msg` and a passed `from`.
/// Initializes a Message with a packet and a from addr.
///
/// - Parameters
/// - protobuf: The protocol buffer.
/// - from: The protocol buffer.
/// - from: The from address.
init(protobuf: Packet, from: Addr) {
proto = UBID(protobuf.protocol)
recipient = Addr(protobuf.recipient)
Expand Down
5 changes: 3 additions & 2 deletions Sources/UB/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ public class Node {
/// The nodes delegate.
public weak var delegate: NodeDelegate?

/// Initializes a node.
public init() {}

/// Adds a new transport to the list of known transports.
///
/// - Parameters:
/// - transport: The new *Transport* to add.
/// - transport: The transport to be added.
public func add(transport: Transport) {
let id = String(describing: transport)

Expand All @@ -38,7 +39,7 @@ public class Node {
/// Removes a transport from the list of known transports.
///
/// - Parameters:
/// - transport: The identifier of the *Transport* to remove.
/// - transport: The identifier of the transport to remove.
public func remove(transport: String) {
guard transports[transport] != nil else {
return
Expand Down
4 changes: 2 additions & 2 deletions Sources/UB/NodeDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation

/// NodeDelegate is used to handle the receiving of messages.
/// An interface used to handle events on the Node.
public protocol NodeDelegate: AnyObject {
/// Called when a node receives a message.
/// This method is called when a node receives a message.
///
/// - Parameters:
/// - node: The node that received the message.
Expand Down
8 changes: 4 additions & 4 deletions Sources/UB/Peer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import Foundation

// @todo clean this up properly, currently very rough for testing purposes.

/// Peer represents the nodes a transport can communicate with.
/// Represents the nodes a transport can communicate with.
public class Peer {
/// The peers `id`.
/// The peers id.
public let id: Addr

/// The services a peer knows.
public let services: [UBID]

/// Initializes a Peer with a specified id and list of known services.
/// Initializes a peer with a specified id and list of known services.
///
/// - Parameters:
/// - id: The peers `id`
/// - id: The peer id.
/// - services: The services a peer can knows.
init(id: Addr, services: [UBID]) {
self.id = id
Expand Down
51 changes: 24 additions & 27 deletions Sources/UB/Transports/CoreBluetoothTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import CoreBluetooth
import Foundation

/// CoreBluetoothTransport is used to send and receive message over Bluetooth
public class CoreBluetoothTransport: NSObject, Transport {
/// The transports delegate.
public class CoreBluetoothTransport: NSObject {
/// :nodoc:
public weak var delegate: TransportDelegate?

/// The peers a specific transport can send messages to.
/// :nodoc:
public fileprivate(set) var peers = [Peer]()

private let centralManager: CBCentralManager
Expand Down Expand Up @@ -38,8 +38,8 @@ public class CoreBluetoothTransport: NSObject, Transport {
/// Initializes a CoreBluetoothTransport.
///
/// - Parameters:
/// - centralManager: The CoreBluetooth Central Manager to use.
/// - peripheralManager: The CoreBluetooth Peripheral Manager to use.
/// - centralManager: The CBCentralManager to use.
/// - peripheralManager: The CBPeripheralManager to use.
public init(centralManager: CBCentralManager, peripheralManager: CBPeripheralManager) {
self.centralManager = centralManager
self.peripheralManager = peripheralManager
Expand All @@ -48,11 +48,25 @@ public class CoreBluetoothTransport: NSObject, Transport {
self.peripheralManager.delegate = self
}

/// Send implements a function to send messages between nodes using Bluetooth
///
/// - Parameters:
/// - message: The message to send.
/// - to: The recipient address of the message.
private func remove(peer: Addr) {
peripherals.removeValue(forKey: peer)
peers.removeAll(where: { $0.id == peer })
}

private func add(central: CBCentral) {
let id = Addr(central.identifier.bytes)

if centrals[id] != nil {
return
}

centrals[id] = central
peers.append(Peer(id: id, services: [UBID]()))
}
}

/// :nodoc:
extension CoreBluetoothTransport: Transport {
public func send(message: Data, to: Addr) {
if let peer = peripherals[to] {
return peer.peripheral.writeValue(
Expand All @@ -71,26 +85,9 @@ public class CoreBluetoothTransport: NSObject, Transport {
}
}

/// Listen implements a function to receive messages being sent to a node.
public func listen() {
// @todo mark as listening, only turn on peripheral characteristic at this point, etc.
}

fileprivate func remove(peer: Addr) {
peripherals.removeValue(forKey: peer)
peers.removeAll(where: { $0.id == peer })
}

fileprivate func add(central: CBCentral) {
let id = Addr(central.identifier.bytes)

if centrals[id] != nil {
return
}

centrals[id] = central
peers.append(Peer(id: id, services: [UBID]()))
}
}

/// :nodoc:
Expand Down
4 changes: 2 additions & 2 deletions Sources/UB/Transports/TransportDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation

/// TransportDelegate is used to handle the receiving data.
/// An interface used to handle events on the Transport.
public protocol TransportDelegate: AnyObject {
/// Called when a transport receives new data.
/// This method is called when a transport receives new data.
///
/// - Parameters:
/// - transport: The transport that received a data.
Expand Down
10 changes: 5 additions & 5 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">UB Docs</a> (97% documented)</p>
<p><a href="index.html">UB Docs</a> (100% 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 Down Expand Up @@ -132,7 +132,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>Peer represents the nodes a transport can communicate with.</p>
<p>Represents the nodes a transport can communicate with.</p>

<a href="Classes/Peer.html" class="slightly-smaller">See more</a>
</div>
Expand Down Expand Up @@ -172,7 +172,7 @@ <h4>Declaration</h4>
<h4>Declaration</h4>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">class</span> <span class="kt">CoreBluetoothTransport</span> <span class="p">:</span> <span class="kt">NSObject</span><span class="p">,</span> <span class="kt"><a href="Protocols/Transport.html">Transport</a></span></code></pre>
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">class</span> <span class="kt">CoreBluetoothTransport</span> <span class="p">:</span> <span class="kt">NSObject</span></code></pre>

</div>
</div>
Expand All @@ -184,8 +184,8 @@ <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-09-10)</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>
<p>&copy; 2019 <a class="link" href="http://ultralightbeam.io" target="_blank" rel="external">Ultralight Beam</a>. All rights reserved. (Last updated: 2019-09-22)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.1</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
</div>
Expand Down
Loading

0 comments on commit 98f3974

Please sign in to comment.