-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Single channel handler to manage tasks (#76)
* Move timeout to MQTTTask * Combine all task handlers into one with a list of tasks * remove tasks on eventLoop * Add testMultipleTasks * swift format * Remove task from list, before completing it * Fixed docker-compose file
- Loading branch information
1 parent
483a171
commit 75f32f5
Showing
8 changed files
with
158 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Logging | ||
import NIO | ||
|
||
/// Handler encoding MQTT Messages into ByteBuffers | ||
final class MQTTEncodeHandler: ChannelOutboundHandler { | ||
public typealias OutboundIn = MQTTPacket | ||
public typealias OutboundOut = ByteBuffer | ||
|
||
let client: MQTTClient | ||
|
||
init(client: MQTTClient) { | ||
self.client = client | ||
} | ||
|
||
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { | ||
let message = unwrapOutboundIn(data) | ||
self.client.logger.trace("MQTT Out", metadata: ["mqtt_message": .string("\(message)"), "mqtt_packet_id": .string("\(message.packetId)")]) | ||
var bb = context.channel.allocator.buffer(capacity: 0) | ||
do { | ||
try message.write(version: self.client.configuration.version, to: &bb) | ||
context.write(wrapOutboundOut(bb), promise: promise) | ||
} catch { | ||
promise?.fail(error) | ||
} | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
Sources/MQTTNIO/MQTTChannelHandlers.swift → .../ChannelHandlers/MQTTMessageDecoder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import NIO | ||
|
||
final class MQTTTaskHandler: ChannelInboundHandler, RemovableChannelHandler { | ||
typealias InboundIn = MQTTPacket | ||
|
||
var eventLoop: EventLoop! | ||
|
||
init() { | ||
self.eventLoop = nil | ||
self.tasks = [] | ||
} | ||
|
||
func addTask(_ task: MQTTTask) -> EventLoopFuture<Void> { | ||
return self.eventLoop.submit { | ||
self.tasks.append(task) | ||
} | ||
} | ||
|
||
func _removeTask(_ task: MQTTTask) { | ||
self.tasks.removeAll { $0 === task } | ||
} | ||
|
||
func removeTask(_ task: MQTTTask) { | ||
if self.eventLoop.inEventLoop { | ||
self._removeTask(task) | ||
} else { | ||
self.eventLoop.execute { | ||
self._removeTask(task) | ||
} | ||
} | ||
} | ||
|
||
func handlerAdded(context: ChannelHandlerContext) { | ||
self.eventLoop = context.eventLoop | ||
} | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
let response = self.unwrapInboundIn(data) | ||
for task in self.tasks { | ||
do { | ||
if try task.checkInbound(response) { | ||
self.removeTask(task) | ||
task.succeed(response) | ||
return | ||
} | ||
} catch { | ||
self.removeTask(task) | ||
task.fail(error) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func channelInactive(context: ChannelHandlerContext) { | ||
self.tasks.forEach { $0.fail(MQTTError.serverClosedConnection) } | ||
self.tasks.removeAll() | ||
} | ||
|
||
func errorCaught(context: ChannelHandlerContext, error: Error) { | ||
self.tasks.forEach { $0.fail(error) } | ||
self.tasks.removeAll() | ||
} | ||
|
||
var tasks: [MQTTTask] | ||
} | ||
|
||
/// If packet reaches this handler then it was never dealt with by a task | ||
final class MQTTUnhandledPacketHandler: ChannelInboundHandler { | ||
typealias InboundIn = MQTTPacket | ||
let client: MQTTClient | ||
|
||
init(client: MQTTClient) { | ||
self.client = client | ||
} | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
// we only send response to v5 server | ||
guard self.client.configuration.version == .v5_0 else { return } | ||
guard let connection = client.connection else { return } | ||
let response = self.unwrapInboundIn(data) | ||
switch response.type { | ||
case .PUBREC: | ||
_ = connection.sendMessageNoWait(MQTTPubAckPacket(type: .PUBREL, packetId: response.packetId, reason: .packetIdentifierNotFound)) | ||
case .PUBREL: | ||
_ = connection.sendMessageNoWait(MQTTPubAckPacket(type: .PUBCOMP, packetId: response.packetId, reason: .packetIdentifierNotFound)) | ||
default: | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,34 @@ | ||
|
||
import NIO | ||
|
||
/// Class encapsulating a single task | ||
final class MQTTTask { | ||
let promise: EventLoopPromise<MQTTPacket> | ||
let checkInbound: (MQTTPacket) throws -> Bool | ||
let timeout: TimeAmount? | ||
let timeoutTask: Scheduled<Void>? | ||
|
||
init(on eventLoop: EventLoop, timeout: TimeAmount?, checkInbound: @escaping (MQTTPacket) throws -> Bool) { | ||
self.promise = eventLoop.makePromise(of: MQTTPacket.self) | ||
let promise = eventLoop.makePromise(of: MQTTPacket.self) | ||
self.promise = promise | ||
self.checkInbound = checkInbound | ||
self.timeout = timeout | ||
} | ||
|
||
func succeed(_ response: MQTTPacket) { | ||
self.promise.succeed(response) | ||
} | ||
|
||
func fail(_ error: Error) { | ||
self.promise.fail(error) | ||
} | ||
} | ||
|
||
final class MQTTTaskHandler: ChannelInboundHandler, RemovableChannelHandler { | ||
typealias InboundIn = MQTTPacket | ||
|
||
let task: MQTTTask | ||
let channel: Channel | ||
var timeoutTask: Scheduled<Void>? | ||
|
||
init(task: MQTTTask, channel: Channel) { | ||
self.task = task | ||
self.channel = channel | ||
self.timeoutTask = nil | ||
} | ||
|
||
public func handlerAdded(context: ChannelHandlerContext) { | ||
self.addTimeoutTask() | ||
} | ||
|
||
public func handlerRemoved(context: ChannelHandlerContext) { | ||
self.timeoutTask?.cancel() | ||
} | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
let response = self.unwrapInboundIn(data) | ||
do { | ||
if try self.task.checkInbound(response) { | ||
self.channel.pipeline.removeHandler(self).whenSuccess { _ in | ||
self.timeoutTask?.cancel() | ||
self.task.succeed(response) | ||
} | ||
} else { | ||
context.fireChannelRead(data) | ||
} | ||
} catch { | ||
self.errorCaught(context: context, error: error) | ||
} | ||
} | ||
|
||
func channelInactive(context: ChannelHandlerContext) { | ||
self.task.fail(MQTTError.serverClosedConnection) | ||
} | ||
|
||
func errorCaught(context: ChannelHandlerContext, error: Error) { | ||
self.timeoutTask?.cancel() | ||
self.channel.pipeline.removeHandler(self).whenSuccess { _ in | ||
self.task.fail(error) | ||
} | ||
} | ||
|
||
func addTimeoutTask() { | ||
if let timeout = task.timeout { | ||
self.timeoutTask = self.channel.eventLoop.scheduleTask(in: timeout) { | ||
self.channel.pipeline.removeHandler(self).whenSuccess { _ in | ||
self.task.fail(MQTTError.timeout) | ||
} | ||
if let timeout = timeout { | ||
self.timeoutTask = eventLoop.scheduleTask(in: timeout) { | ||
promise.fail(MQTTError.timeout) | ||
} | ||
} else { | ||
self.timeoutTask = nil | ||
} | ||
} | ||
} | ||
|
||
/// If packet reaches this handler then it was never dealt with by a task | ||
final class MQTTUnhandledPacketHandler: ChannelInboundHandler { | ||
typealias InboundIn = MQTTPacket | ||
let client: MQTTClient | ||
|
||
init(client: MQTTClient) { | ||
self.client = client | ||
func succeed(_ response: MQTTPacket) { | ||
self.timeoutTask?.cancel() | ||
self.promise.succeed(response) | ||
} | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
// we only send response to v5 server | ||
guard self.client.configuration.version == .v5_0 else { return } | ||
guard let connection = client.connection else { return } | ||
let response = self.unwrapInboundIn(data) | ||
switch response.type { | ||
case .PUBREC: | ||
_ = connection.sendMessageNoWait(MQTTPubAckPacket(type: .PUBREL, packetId: response.packetId, reason: .packetIdentifierNotFound)) | ||
case .PUBREL: | ||
_ = connection.sendMessageNoWait(MQTTPubAckPacket(type: .PUBCOMP, packetId: response.packetId, reason: .packetIdentifierNotFound)) | ||
default: | ||
break | ||
} | ||
func fail(_ error: Error) { | ||
self.timeoutTask?.cancel() | ||
self.promise.fail(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.