Skip to content

Commit

Permalink
fix various formatting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnzhou committed Jan 28, 2024
1 parent 7a99b71 commit 61f1220
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 356 deletions.
47 changes: 23 additions & 24 deletions Sources/LCLPing/ICMP/ICMPChannelHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,27 @@ internal final class ICMPDuplexer: ChannelDuplexHandler {

self.timerScheduler.schedule(delay: self.configuration.timeout, key: sequenceNum) { [weak self, context] in
if let self = self, !self.seqToResponse.keys.contains(sequenceNum) {
print("[ICMPDuplexer][\(#function)]: packet #\(sequenceNum) timed out")
logger.debug("[ICMPDuplexer][\(#function)]: packet #\(sequenceNum) timed out")
self.responseSeqNumSet.insert(sequenceNum)
context.eventLoop.execute {
context.fireChannelRead(self.wrapInboundOut(.timeout(sequenceNum)))
self.closeWhenComplete(context: context)
}
}
}
print("[ICMPDuplexer][\(#function)]: schedule timer for # \(sequenceNum) for \(self.configuration.timeout) second")
logger.debug("[ICMPDuplexer][\(#function)]: schedule timer for # \(sequenceNum) for \(self.configuration.timeout) second")
}

private func closeWhenComplete(context: ChannelHandlerContext) {
if self.seqToRequest.count == self.configuration.count && self.responseSeqNumSet.count == self.configuration.count {
print("[ICMPDuplexer]: Ping finished. Closing all channels")
logger.debug("[ICMPDuplexer]: Ping finished. Closing all channels")
context.close(mode: .all, promise: nil)
}
}

func channelRead(context: ChannelHandlerContext, data: NIOAny) {
guard self.state.isOperational else {
print("[ICMPDuplexer][\(#function)]: drop data: \(data) because channel is not in operational state")
logger.debug("[ICMPDuplexer][\(#function)]: drop data: \(data) because channel is not in operational state")
return
}

Expand All @@ -117,11 +117,11 @@ internal final class ICMPDuplexer: ChannelDuplexHandler {
let code = icmpResponse.code
let sequenceNum = icmpResponse.sequenceNum
let identifier = icmpResponse.idenifier
print("[ICMPDuplexer][\(#function)]: received icmp response with type: \(type), code: \(code), sequence number: \(sequenceNum), identifier: \(identifier)")
logger.debug("[ICMPDuplexer][\(#function)]: received icmp response with type: \(type), code: \(code), sequence number: \(sequenceNum), identifier: \(identifier)")

if self.responseSeqNumSet.contains(sequenceNum) {
let pingResponse: PingResponse = self.seqToResponse[sequenceNum] == nil ? .timeout(sequenceNum) : .duplicated(sequenceNum)
print("[ICMPDuplexer][\(#function)]: response for #\(sequenceNum) is \(self.seqToResponse[sequenceNum] == nil ? "timeout" : "duplicate")")
logger.debug("[ICMPDuplexer][\(#function)]: response for #\(sequenceNum) is \(self.seqToResponse[sequenceNum] == nil ? "timeout" : "duplicate")")
context.fireChannelRead(self.wrapInboundOut(pingResponse))
closeWhenComplete(context: context)
return
Expand Down Expand Up @@ -281,13 +281,13 @@ internal final class ICMPDuplexer: ChannelDuplexHandler {
func channelActive(context: ChannelHandlerContext) {
switch self.state {
case .operational:
print("[ICMPDuplexer][\(#function)]: Channel already active")
logger.debug("[ICMPDuplexer][\(#function)]: Channel already active")
break
case .error:
logger.error("[ICMPDuplexer][\(#function)]: in an incorrect state: \(state)")
assertionFailure("[\(#function)]: in an incorrect state: \(state)")
case .inactive:
print("[ICMPDuplexer][\(#function)]: Channel active")
logger.debug("[ICMPDuplexer][\(#function)]: Channel active")
context.fireChannelActive()
self.state = .operational
}
Expand All @@ -301,7 +301,7 @@ internal final class ICMPDuplexer: ChannelDuplexHandler {
self.seqToRequest.removeAll()
self.seqToResponse.removeAll()
self.timerScheduler.reset()
print("[ICMPDuplexer][\(#function)]: Channel inactive")
logger.debug("[ICMPDuplexer][\(#function)]: Channel inactive")
case .error:
break
case .inactive:
Expand All @@ -312,7 +312,7 @@ internal final class ICMPDuplexer: ChannelDuplexHandler {

func errorCaught(context: ChannelHandlerContext, error: Error) {
guard self.state.isOperational else {
print("[ICMPDuplexer]: already in error state. ignore error \(error)")
logger.debug("[ICMPDuplexer]: already in error state. ignore error \(error)")
return
}
self.state = .error
Expand Down Expand Up @@ -347,13 +347,13 @@ internal final class IPDecoder: ChannelInboundHandler {
func channelActive(context: ChannelHandlerContext) {
switch self.state {
case .operational:
print("[IPDecoder][\(#function)]: Channel already active")
logger.debug("[IPDecoder][\(#function)]: Channel already active")
break
case .error:
logger.error("[IPDecoder][\(#function)] in an incorrect state: \(state)")
assertionFailure("[IPDecoder][\(#function)] in an incorrect state: \(state)")
case .inactive:
print("[IPDecoder][\(#function)]: Channel active")
logger.debug("[IPDecoder][\(#function)]: Channel active")
context.fireChannelActive()
self.state = .operational
}
Expand All @@ -362,26 +362,25 @@ internal final class IPDecoder: ChannelInboundHandler {
func channelInactive(context: ChannelHandlerContext) {
switch self.state {
case .operational:
print("[IPDecoder][\(#function)]: Channel inactive")
logger.debug("[IPDecoder][\(#function)]: Channel inactive")
context.fireChannelInactive()
self.state = .inactive
case .error:
break
case .inactive:
print("[IPDecoder][\(#function)]: received inactive signal when channel is already in inactive state.")
logger.debug("[IPDecoder][\(#function)]: received inactive signal when channel is already in inactive state.")
assertionFailure("[IPDecoder][\(#function)]: received inactive signal when channel is already in inactive state.")
}
}

func channelRead(context: ChannelHandlerContext, data: NIOAny) {
guard self.state.isOperational else {
print("[IPDecoder][\(#function)]: drop data: \(data) because channel is not in operational state")
logger.debug("[IPDecoder][\(#function)]: drop data: \(data) because channel is not in operational state")
return
}

let addressedBuffer = self.unwrapInboundIn(data)
var buffer = addressedBuffer.data
print(buffer.readableBytesView)
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
let ipv4Header: IPv4Header
do {
Expand All @@ -392,7 +391,7 @@ internal final class IPDecoder: ChannelInboundHandler {
}
let version = ipv4Header.versionAndHeaderLength & 0xF0
if version != 0x40 {
print("received version: \(version)")
logger.debug("received version: \(version)")
context.fireErrorCaught(PingError.invalidIPVersion)
return
}
Expand All @@ -410,7 +409,7 @@ internal final class IPDecoder: ChannelInboundHandler {

func errorCaught(context: ChannelHandlerContext, error: Error) {
guard self.state.isOperational else {
print("[IPDecoder]: already in error state. ignore error \(error)")
logger.debug("[IPDecoder]: already in error state. ignore error \(error)")
return
}

Expand Down Expand Up @@ -443,13 +442,13 @@ internal final class ICMPDecoder: ChannelInboundHandler {
func channelActive(context: ChannelHandlerContext) {
switch self.state {
case .operational:
print("[ICMPDecoder][\(#function)]: Channel already active")
logger.debug("[ICMPDecoder][\(#function)]: Channel already active")
break
case .error:
logger.error("[ICMPDecoder][\(#function)] in an incorrect state: \(state)")
assertionFailure("[\(#function)] in an incorrect state: \(state)")
case .inactive:
print("[ICMPDecoder][\(#function)]: Channel active")
logger.debug("[ICMPDecoder][\(#function)]: Channel active")
context.fireChannelActive()
self.state = .operational
}
Expand All @@ -458,7 +457,7 @@ internal final class ICMPDecoder: ChannelInboundHandler {
func channelInactive(context: ChannelHandlerContext) {
switch self.state {
case .operational:
print("[ICMPDecoder][\(#function)]: Channel inactive")
logger.debug("[ICMPDecoder][\(#function)]: Channel inactive")
context.fireChannelInactive()
self.state = .inactive
case .error:
Expand All @@ -471,7 +470,7 @@ internal final class ICMPDecoder: ChannelInboundHandler {

func channelRead(context: ChannelHandlerContext, data: NIOAny) {
guard self.state.isOperational else {
print("[ICMPDecoder]: drop data: \(data) because channel is not in operational state")
logger.debug("[ICMPDecoder]: drop data: \(data) because channel is not in operational state")
return
}

Expand All @@ -484,12 +483,12 @@ internal final class ICMPDecoder: ChannelInboundHandler {
return
}
context.fireChannelRead(self.wrapInboundOut(icmpResponseHeader))
print("[ICMPDecoder][\(#function)] finish decoding icmp header: \(icmpResponseHeader)")
logger.debug("[ICMPDecoder][\(#function)] finish decoding icmp header: \(icmpResponseHeader)")
}

func errorCaught(context: ChannelHandlerContext, error: Error) {
guard self.state.isOperational else {
print("[ICMPDecoder]: already in error state. ignore error \(error)")
logger.debug("[ICMPDecoder]: already in error state. ignore error \(error)")
return
}

Expand Down
7 changes: 3 additions & 4 deletions Sources/LCLPing/TestUtils/Rewritable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,24 @@ extension AddressedEnvelope: Rewritable where DataType == ByteBuffer {
func rewrite(newValues: [PartialKeyPath<NIOCore.AddressedEnvelope<DataType>> : AnyObject]) -> NIOCore.AddressedEnvelope<DataType> {
return AddressedEnvelope(
remoteAddress: newValues[\.remoteAddress] as? SocketAddress ?? self.remoteAddress,
data: data.rewrite(newValues: newValues[\AddressedEnvelope.data] as? [RewriteData] ?? [RewriteData(index: 0, byte: 0x55)])
data: data.rewrite(newValues: newValues[\AddressedEnvelope.data] as? [RewriteData] ?? [])
)
}
}


extension ByteBuffer {
func rewrite(newValues: ByteBuffer) -> NIOCore.ByteBuffer {
print("[ByteBuffer Rewrite]: received new value: \(newValues.readableBytesView)")
return ByteBuffer(buffer: newValues)
}

func rewrite(newValues: [RewriteData]) -> ByteBuffer {
print("[ByteBuffer Rewrite]: received new value: \(newValues)")
logger.debug("[ByteBuffer Rewrite]: received new value: \(newValues)")
var newBuffer = ByteBuffer(buffer: self)
for newValue in newValues {
newBuffer.setBytes(newValue.byte.data, at: newValue.index)
}
print("ByteBuffer Rewrite: rewritten as \(newBuffer.readableBytesView)")
logger.debug("ByteBuffer Rewrite: rewritten as \(newBuffer.readableBytesView)")
return newBuffer
}
}
Expand Down
Loading

0 comments on commit 61f1220

Please sign in to comment.