Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
acb committed Dec 27, 2020
1 parent eea00d6 commit b90eead
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions shared/server/TCP/TCPQueryHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,30 @@ final class TCPQueryHandler: ChannelInboundHandler {

var queryHandler: QueryHandling? = nil

private func response(fromInput line: String) -> ControlServer.TCPQueryResponse {
guard let queryHandler = self.queryHandler else {
return .failure("Internal error: query handler not present")
}
guard let query = parse(query: line) else {
return .failure("Invalid query")
}
return queryHandler.handle(query: query)
}

func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let id = ObjectIdentifier(context.channel)
let inb = unwrapInboundIn(data)
let line = inb.getString(at: 0, length: inb.readableBytes) ?? ""
let sp = line.split(separator: " ", maxSplits: 1)

//let query = ControlServer.TCPQuery.listNodes // FIXME: parse this
let result: ControlServer.TCPQueryResponse
if
let queryHandler = queryHandler,
let query = parse(query: line)
{
result = queryHandler.handle(query: query)
} else {
result = .failure("Invalid query")
}
//ControlServer.TCPQueryResponse.success(.nodes([ControlServer.TCPQueryResponse.ResultValue.NodeInfo(id: 23, name: "test")]))
let result = self.response(fromInput: line)

let encoder = JSONEncoder()
let encoded = try! encoder.encode(result)
var outbuf = context.channel.allocator.buffer(capacity: encoded.count+2)
encoded.withUnsafeBytes { (bptr) in
_ = encoded.withUnsafeBytes { (bptr) in
outbuf.writeBytes(bptr)
}
outbuf.writeString("\n")
context.writeAndFlush(self.wrapOutboundOut(outbuf))
print("Line: \(line)")

}
}

0 comments on commit b90eead

Please sign in to comment.