Skip to content

Commit

Permalink
rename ctx to context (#842)
Browse files Browse the repository at this point in the history
Motivation:

`ctx` was always an abbreviation was 'context` and in Swift we don't
really use abbreviations, so let's fix it.

Modifications:

- rename all instances of `ctx` to `context`

Result:

- fixes #483
  • Loading branch information
weissi committed Feb 25, 2019
1 parent 862533d commit a412809
Show file tree
Hide file tree
Showing 55 changed files with 1,287 additions and 1,285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ private final class SimpleHTTPServer: ChannelInboundHandler {
return buffer
}

public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
if case .head(let req) = self.unwrapInboundIn(data), req.uri == "/allocation-test-1" {
ctx.write(self.wrapOutboundOut(.head(self.responseHead)), promise: nil)
ctx.write(self.wrapOutboundOut(.body(.byteBuffer(self.responseBody(allocator: ctx.channel.allocator)))), promise: nil)
ctx.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
context.write(self.wrapOutboundOut(.head(self.responseHead)), promise: nil)
context.write(self.wrapOutboundOut(.body(.byteBuffer(self.responseBody(allocator: context.channel.allocator)))), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
}
}
}
Expand Down Expand Up @@ -75,25 +75,25 @@ private final class PingHandler: ChannelInboundHandler {
self.allDone = eventLoop.makePromise()
}

public func channelActive(ctx: ChannelHandlerContext) {
self.pingBuffer = ctx.channel.allocator.buffer(capacity: 1)
public func channelActive(context: ChannelHandlerContext) {
self.pingBuffer = context.channel.allocator.buffer(capacity: 1)
self.pingBuffer.writeInteger(PingHandler.pingCode)

ctx.writeAndFlush(self.wrapOutboundOut(self.pingBuffer), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(self.pingBuffer), promise: nil)
}

public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
var buf = self.unwrapInboundIn(data)
if buf.readableBytes == 1 &&
buf.readInteger(as: UInt8.self) == PongHandler.pongCode {
if self.remainingNumberOfRequests > 0 {
self.remainingNumberOfRequests -= 1
ctx.writeAndFlush(self.wrapOutboundOut(self.pingBuffer), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(self.pingBuffer), promise: nil)
} else {
ctx.close(promise: self.allDone)
context.close(promise: self.allDone)
}
} else {
ctx.close(promise: nil)
context.close(promise: nil)
self.allDone.fail(PingPongFailure(problem: "wrong buffer received: \(buf.debugDescription)"))
}
}
Expand All @@ -110,18 +110,18 @@ private final class PongHandler: ChannelInboundHandler {
private var pongBuffer: ByteBuffer!
public static let pongCode: UInt8 = 0xef

public func channelActive(ctx: ChannelHandlerContext) {
self.pongBuffer = ctx.channel.allocator.buffer(capacity: 1)
public func channelActive(context: ChannelHandlerContext) {
self.pongBuffer = context.channel.allocator.buffer(capacity: 1)
self.pongBuffer.writeInteger(PongHandler.pongCode)
}

public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
var buf = self.unwrapInboundIn(data)
if buf.readableBytes == 1 &&
buf.readInteger(as: UInt8.self) == PingHandler.pingCode {
ctx.writeAndFlush(self.wrapOutboundOut(self.pongBuffer), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(self.pongBuffer), promise: nil)
} else {
ctx.close(promise: nil)
context.close(promise: nil)
}
}
}
Expand Down Expand Up @@ -163,20 +163,20 @@ public func swiftMain() -> Int {
return reqs
}

func errorCaught(ctx: ChannelHandlerContext, error: Error) {
ctx.channel.close(promise: nil)
func errorCaught(context: ChannelHandlerContext, error: Error) {
context.channel.close(promise: nil)
self.isDonePromise.fail(error)
}

func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let respPart = self.unwrapInboundIn(data)
if case .end(nil) = respPart {
if self.remainingNumberOfRequests <= 0 {
ctx.channel.close().map { self.numberOfRequests - self.remainingNumberOfRequests }.cascade(to: self.isDonePromise)
context.channel.close().map { self.numberOfRequests - self.remainingNumberOfRequests }.cascade(to: self.isDonePromise)
} else {
self.remainingNumberOfRequests -= 1
ctx.write(self.wrapOutboundOut(.head(RepeatedRequests.requestHead)), promise: nil)
ctx.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
context.write(self.wrapOutboundOut(.head(RepeatedRequests.requestHead)), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions Sources/NIO/Bootstrap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,16 @@ public final class ServerBootstrap {
self.childChannelOptions = childChannelOptions
}

func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
if event is ChannelShouldQuiesceEvent {
ctx.channel.close(promise: nil)
context.channel.close(promise: nil)
}
ctx.fireUserInboundEventTriggered(event)
context.fireUserInboundEventTriggered(event)
}

func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let accepted = self.unwrapInboundIn(data)
let ctxEventLoop = ctx.eventLoop
let ctxEventLoop = context.eventLoop
let childEventLoop = accepted.eventLoop
let childChannelInit = self.childChannelInit ?? { (_: Channel) in childEventLoop.makeSucceededFuture(()) }

Expand All @@ -273,14 +273,14 @@ public final class ServerBootstrap {
ctxEventLoop.assertInEventLoop()
future.flatMap { (_) -> EventLoopFuture<Void> in
ctxEventLoop.assertInEventLoop()
guard !ctx.pipeline.destroyed else {
return ctx.eventLoop.makeFailedFuture(ChannelError.ioOnClosedChannel)
guard !context.pipeline.destroyed else {
return context.eventLoop.makeFailedFuture(ChannelError.ioOnClosedChannel)
}
ctx.fireChannelRead(data)
return ctx.eventLoop.makeSucceededFuture(())
context.fireChannelRead(data)
return context.eventLoop.makeSucceededFuture(())
}.whenFailure { error in
ctxEventLoop.assertInEventLoop()
self.closeAndFire(ctx: ctx, accepted: accepted, err: error)
self.closeAndFire(context: context, accepted: accepted, err: error)
}
}

Expand All @@ -293,13 +293,13 @@ public final class ServerBootstrap {
}
}

private func closeAndFire(ctx: ChannelHandlerContext, accepted: SocketChannel, err: Error) {
private func closeAndFire(context: ChannelHandlerContext, accepted: SocketChannel, err: Error) {
accepted.close(promise: nil)
if ctx.eventLoop.inEventLoop {
ctx.fireErrorCaught(err)
if context.eventLoop.inEventLoop {
context.fireErrorCaught(err)
} else {
ctx.eventLoop.execute {
ctx.fireErrorCaught(err)
context.eventLoop.execute {
context.fireErrorCaught(err)
}
}
}
Expand Down
Loading

0 comments on commit a412809

Please sign in to comment.