Skip to content

Commit

Permalink
changed queue
Browse files Browse the repository at this point in the history
  • Loading branch information
NickKibish committed Nov 30, 2023
1 parent 01f34b1 commit ede0935
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "iOS-BLE-LibraryTests"
BuildableName = "iOS-BLE-LibraryTests"
BlueprintName = "iOS-BLE-LibraryTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
32 changes: 14 additions & 18 deletions Sources/iOS-BLE-Library/Peripheral/Peripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,22 @@ extension Peripheral {
.first()

return allServices.bluetooth {
self.peripheralDelegate.discoveredServicesQueue.enqueue(ReactivePeripheralDelegate.TaskID(
id: id,
task: {
self.peripheral.discoverServices(serviceUUIDs)
self.l.d("\(#function). operation ID: \(id)")
print("----| Request discover. Operation ID: \(id)")
if let serviceUUIDs {
for sid in serviceUUIDs {
self.l.d("Services: \(sid)")
print("----| Service: \(sid)")
}
} else {
self.l.d("All services")
print("----| All Services")
let operation = IdentifiableOperation(id: id) {
self.peripheral.discoverServices(serviceUUIDs)
self.l.d("\(#function). operation ID: \(id)")
print("----| Request discover. Operation ID: \(id)")
if let serviceUUIDs {
for sid in serviceUUIDs {
self.l.d("Services: \(sid)")
print("----| Service: \(sid)")
}
}))

if self.peripheralDelegate.discoveredServicesQueue.count == 1 {
self.peripheralDelegate.discoveredServicesQueue.head?.task()
} else {
self.l.d("All services")
print("----| All Services")
}
}

self.peripheralDelegate.discoveredServicesQueue.addOperation(operation)
}
.autoconnect()
.eraseToAnyPublisher()
Expand Down
46 changes: 29 additions & 17 deletions Sources/iOS-BLE-Library/Peripheral/ReactivePeripheralDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ struct BluetoothOperationResult<T> {
let id: UUID
}

struct IdentifiableOperation {
let id: UUID
let block: () -> Void
}

class SingleTaskQueue {
private var queue = Queue<IdentifiableOperation>()

func addOperation(_ task: IdentifiableOperation) {
if queue.isEmpty {
queue.enqueue(task)
task.block()
} else {
queue.enqueue(task)
}
}

func dequeue() -> IdentifiableOperation? {
queue.dequeue()
}

func runNext() {
queue.peek()?.block()
}
}

open class ReactivePeripheralDelegate: NSObject, CBPeripheralDelegate {
let l = L(category: #file)

Expand All @@ -31,7 +57,7 @@ open class ReactivePeripheralDelegate: NSObject, CBPeripheralDelegate {
let task: () -> ()
}

var discoveredServicesQueue = Queue<TaskID>()
var discoveredServicesQueue = SingleTaskQueue()
var discoveredCharacteristicsQueue = Queue<UUID>()
var discoveredDescriptorsQueue = Queue<UUID>()

Expand Down Expand Up @@ -86,27 +112,13 @@ open class ReactivePeripheralDelegate: NSObject, CBPeripheralDelegate {
// MARK: Discovering Services

open func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
let operation = discoveredServicesQueue.dequeue()!
l.d("\(#function). Operation ID: \(operation.id)")
print("--| Operation ID: \(operation.id)")

if let services = peripheral.services {
for service in services {
l.d("Service: \(service.uuid)")
print("--| Service: \(service.uuid)")
}
if services.isEmpty {
l.d("No Services Discovered")
print("--| No Services Discovered")
}
}
print("\n")
guard let operation = discoveredServicesQueue.dequeue() else { return }

let result = BluetoothOperationResult<[CBService]?>(value: peripheral.services, error: error, id: operation.id)

discoveredServicesSubject.send(result)

discoveredServicesQueue.head?.task()
discoveredServicesQueue.runNext()
}

/*
Expand Down
58 changes: 57 additions & 1 deletion Sources/iOS-BLE-Library/Utilities/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,64 @@

import Foundation

class Node<T> {
var value: T
var next: Node?

init(value: T) {
self.value = value
}
}

class Queue<T> {
private var front: Node<T>?
private var rear: Node<T>?
private let accessQueue = DispatchQueue(label: "com.ble-library.threadSafeQueue")

var isEmpty: Bool {
return front == nil
}

// Enqueue operation to add an element to the rear of the queue
func enqueue(_ value: T) {
accessQueue.sync {
let newNode = Node(value: value)
if isEmpty {
front = newNode
rear = newNode
} else {
rear?.next = newNode
rear = newNode
}
}
}

// Dequeue operation to remove and return the element from the front of the queue
func dequeue() -> T? {
var element: T?
accessQueue.sync {
if let currentFront = front {
front = currentFront.next
if front == nil {
rear = nil
}
element = currentFront.value
} else {
element = nil
}
}
return element
}

// Peek operation to get the value at the front of the queue without removing it
func peek() -> T? {
return front?.value
}
}
/*
struct Queue<T> {
private var queue = [T]()
private let accessQueue = DispatchQueue(label: "com.example.threadSafeQueue")
private let accessQueue = DispatchQueue(label: "com.ble-library.threadSafeQueue")

mutating func enqueue(_ element: T) {
accessQueue.sync {
Expand Down Expand Up @@ -51,3 +106,4 @@ struct Queue<T> {
return queueCount
}
}
*/

0 comments on commit ede0935

Please sign in to comment.