-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgeMessage.swift
More file actions
68 lines (60 loc) · 2.1 KB
/
BridgeMessage.swift
File metadata and controls
68 lines (60 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// BridgeMessage.swift
// Bridge
//
// Created by Alexey Pichukov on 27/09/2019.
//
import Foundation
/**
* Message object in the Bridge system
*
* NOTE: The message is a reference type for keeping low memory usage during the message delivery process
*
* - id: the unique UUID String of the message
* - sourse: the Sourse Node address
* - destination: the Destination Node address
* - value: the main data that should be delivered by the message
* - callback: an optional callback for the message, can be called from the destination Node
*
*/
public class BridgeMessage<Type: BridgeNodeTypable> {
public let value: Any
public var callback: ((_ result: Result<Any, Error>) -> Void)?
let id: String
let sourse: BridgeNodeAddress<Type>
let destination: BridgeNodeAddress<Type>
private var visitedNodes: [String] = []
/// The queue for make thread safty access to the visitedNodes array
private let nodesAccessQueue = DispatchQueue(label: "com.bridge.synchronize.visited.nodes.array", attributes: .concurrent)
public init(fromSourse sourse: BridgeNodeAddress<Type>,
toDestination destination: BridgeNodeAddress<Type>,
withValue value: Any,
callback: ((_ result: Result<Any, Error>) -> Void)? = nil) {
self.id = UUID().uuidString
self.sourse = sourse
self.destination = destination
self.value = value
self.callback = callback
self.visitedNodes.append(self.id)
}
/**
Internal method for adding Node to the visitedNodes array
*/
func addVisited(nodeWithId id: String?) {
guard id != nil else { return }
nodesAccessQueue.async(flags: .barrier) {
self.visitedNodes.append(id!)
}
}
/**
Internal method for checking the Node for been visited
*/
func visited(nodeWithId id: String?) -> Bool {
guard id != nil else { return false }
var contains = false
nodesAccessQueue.sync {
contains = self.visitedNodes.contains(id!)
}
return contains
}
}