funcmike / rabbitmq-nio

A Swift implementation of AMQP 0.9.1 protocol: decoder + encoder and non-blocking client
Apache License 2.0
44 stars 8 forks source link

Refactor Channel Handling #18

Closed funcmike closed 1 year ago

funcmike commented 1 year ago

New handler proposal

protocol AMPQChannelHandlerParent {
  func write(payload: Payload, promise: EventLoopPromise<Void>?)
}

final class AMPQFrameHandler {
  var channels: [ChannelID: AMPQChannelHandler<Self>]
  var context: ChannelHandlerContext!

  func handlerAdded(context: ChannelHandlerContext) {
    self.context = context
  }

  func handlerRemoved(context: ChannelHandlerContext) {
    self.context = nil
  }

  func openChannel() -> EventLoopFuture<AMPQChannelHandler<Self>> {
    let nextChannelID = ...
    // sendMessage to create channel
    // when done create AMPQChannelHandler with ChannelID
  }
}

extension AMPQFrameHandler: AMPQChannelHandlerParent {
  func write(frame: Frame, promise: EventLoopPromise<Void>?) {
    // write into the channel pipeline using self.context
  }
}

final class AMPQChannelHandler<Parent: AMPQChannelHandlerParent> {
  let parent: Parent
  let eventLoop: EventLoop

  init(parent: Parent, channelID: ChannelID, eventLoop: EventLoop) {
    self.parent = parent
  }

  func send(payload: Payload) -> EventLoopFuture<Response> {
    let frame = Frame(channelID: self.channelID, payload: Payload)
    self.parent.write(frame: frame)
  }

  func receive(payload: Payload) {

  }
}