blob: 36277eee6a8cf49e8d73418414a70c0156720a59 (
plain) (
blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
//
// NetADSBDecoder.swift
// Net1090
//
// Created by Jacky Jack on 19/07/2024.
//
import Foundation
import Network
import NIO
protocol ADSBQueueDelegate: AnyObject {
//var message_array:Array<String> {get set}
func push(_ message: String)
func pop() -> String?
}
class NetADSBHandlder: ChannelInboundHandler {
typealias InboundIn = ByteBuffer
typealias OutboundOut = ByteBuffer
var messageDelegate: ADSBQueueDelegate?
func channelActive(context: ChannelHandlerContext) {
print("Channel is active")
}
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
var buffer = unwrapInboundIn(data)
let readableBytes = buffer.readableBytes
if let received = buffer.readString(length: readableBytes) {
//print(received,terminator: "")
/*
if let message_queue = message_queue {
message_queue.append(String(received))
print("Add new messsge")
}*/
//if self.message_array != nil {
// message_array?.append(String(received))
// print("\(message_array?.count)")
//}
if self.messageDelegate != nil {
if received.count == 17 {
let trimmed = received.trimmingCharacters(in: .newlines).lowercased()
//messageDelegate?.message_array.append(String(trimmed))
messageDelegate?.push(String(trimmed))
} else if received.count == 31 {
let trimmed = received.trimmingCharacters(in: .newlines).lowercased()
messageDelegate?.push(String(trimmed))
} else {
for line in received.components(separatedBy: .newlines) {
let trimmed = line.trimmingCharacters(in: .newlines).lowercased()
messageDelegate?.push(String(trimmed))
}
}
}
}
}
func errorCaught(context: ChannelHandlerContext, error: any Error) {
print("error: \(error.localizedDescription)")
context.close(promise: nil)
}
}
final class ADSBQueue: ADSBQueueDelegate {
private var message_array:Array<String> = []
private let lock = NSLock()
func push(_ message: String) {
lock.lock()
message_array.append(message)
lock.unlock()
}
func pop() -> String? {
lock.lock()
defer { lock.unlock() }
guard !message_array.isEmpty else { return nil }
return message_array.removeFirst()
}
var count: Int {
lock.lock()
defer { lock.unlock() }
return message_array.count
}
}
class NetADSBDecoder {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
var host: String
var port: Int
var msgarray = ADSBQueue()
var handler = NetADSBHandlder()
//var msg_array:Array<String> = []
init(host: String, port: Int) {
self.host = host
self.port = port
//regiter delegate
handler.messageDelegate = msgarray
}
func start() throws {
do {
let channel = try ClientBootstrap(group: group)
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelInitializer{channel in
//channel.pipeline.add(handler: ADSBHandlder())
channel.pipeline.addHandlers([self.handler])
}.connect(host: self.host, port: self.port)
.wait()
try channel.closeFuture.wait()
} catch let error {
print(error)
throw error
}
}
func stop() {
do {
try group.syncShutdownGracefully()
} catch let error {
print("Error shutting down \(error.localizedDescription)")
exit(0)
}
print("Connection closed")
}
}
|