diff options
author | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-07-25 12:13:51 +0100 |
---|---|---|
committer | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-07-25 12:13:51 +0100 |
commit | 901397ffe83cbf1aed01e6a77e00db57e9440dac (patch) | |
tree | 03742808a3f1d414615940446868ad30d25aa4d3 /Net1090 | |
parent | 6ab97d2dfe563c2c43c2519b2e6f22494572045c (diff) | |
download | ADSBDecoder-901397ffe83cbf1aed01e6a77e00db57e9440dac.tar.gz ADSBDecoder-901397ffe83cbf1aed01e6a77e00db57e9440dac.zip |
Receiving ADSB stream from dump1090 works in LearnMapKit
Diffstat (limited to 'Net1090')
-rw-r--r-- | Net1090/NetADSBDecoder.swift | 69 | ||||
-rw-r--r-- | Net1090/main.swift | 59 |
2 files changed, 70 insertions, 58 deletions
diff --git a/Net1090/NetADSBDecoder.swift b/Net1090/NetADSBDecoder.swift new file mode 100644 index 0000000..065bf91 --- /dev/null +++ b/Net1090/NetADSBDecoder.swift @@ -0,0 +1,69 @@ +// +// NetADSBDecoder.swift +// Net1090 +// +// Created by Jacky Jack on 19/07/2024. +// + +import Foundation +import Network +import NIO + +class ADSBHandlder: ChannelInboundHandler { + typealias InboundIn = ByteBuffer + typealias OutboundOut = ByteBuffer + + 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: "") + } + } + + func errorCaught(context: ChannelHandlerContext, error: any Error) { + print("error: \(error.localizedDescription)") + context.close(promise: nil) + } +} + +class NetADSBDecoder { + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) + var host: String + var port: Int + + init(host: String, port: Int) { + self.host = host + self.port = port + } + + 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([ADSBHandlder()]) + }.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") + } +} diff --git a/Net1090/main.swift b/Net1090/main.swift index 04d0384..d142613 100644 --- a/Net1090/main.swift +++ b/Net1090/main.swift @@ -10,69 +10,12 @@ import Network import NIO -class ADSBHandlder: ChannelInboundHandler { - typealias InboundIn = ByteBuffer - typealias OutboundOut = ByteBuffer - - 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: "") - } - } - - func errorCaught(context: ChannelHandlerContext, error: any Error) { - print("error: \(error.localizedDescription)") - context.close(promise: nil) - } -} -class TCPClient { - let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) - var host: String - var port: Int - - init(host: String, port: Int) { - self.host = host - self.port = port - } - - 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([ADSBHandlder()]) - }.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") - } -} print("Hello, World!") print("Start listening client") -let ADSBClient = TCPClient(host: "192.168.4.201", port: 30002) +let ADSBClient = NetADSBDecoder(host: "192.168.4.201", port: 30002) do { try ADSBClient.start() } catch let error { |