Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced the output of the NIOEchoClient Example #585

Merged
merged 5 commits into from
Aug 20, 2018
Merged

Enhanced the output of the NIOEchoClient Example #585

merged 5 commits into from
Aug 20, 2018

Conversation

agnosticdev
Copy link
Contributor

Motivation:

I was testing the NIOEchoClient example on Linux and noticed that I received no character output.
I thought this would be handy and added this code to do so.

Modifications:

Small modification to the channelRead method in NIOEchoClient to read the bytes and transform to a string.

Result:

The display of the characters on your echo client before the channel closes.

Motivation:

I was testing the NIOEchoClient example on Linux and noticed that I received no character output.
I thought this would be handy and added this code to do so.

Modifications:

Small modification to the channelRead method in NIOEchoClient to read the bytes and transform to a string.

Result:

The display of the characters on your echo client before the channel closes.
@swift-nio-bot
Copy link

Can one of the admins verify this patch?

1 similar comment
@swift-nio-bot
Copy link

Can one of the admins verify this patch?

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this seems like a good enhancement. I’ve left some notes in the diff.


while let byte: UInt8 = byteBuffer.readInteger() {
charArray.append(byte)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace almost all of this with byteBuffer.readBytes(length: ByteBuffer.readableBytes).

let charString = String(bytes: charArray, encoding: .utf8) {
print("Received: '\(charString)' back from the server, closing channel.")
} else {
print("Received line back from the server, closing channel.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this else branch should be reachable.

@@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//
import NIO
import Foundation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn’t need the Foundation import here, it should be possible to use the Swift standard library for the decoding. Reducing the number of imports in the examples is helpful for clarity, so it’d be good to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a loop converting bytes with UnicodeScalar. Let me know your thoughts.

Motivation:

Improved the PR based upon feedback provided.

Modifications:

Cleaned up the code and removed Foundation.

Result:

A more concise example of my enhancement without Foundation.
var str = ""
for byte in charArray {
str += String(UnicodeScalar(byte))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, with this tighter loop it's a lot easier to see what we're getting into here. This can all be replaced with if let string = byteBuffer.readString(length: byteBuffer.readableBytes). We'll also want an error message for the non-bound branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Using if let string = byteBuffer.readString(length: byteBuffer.readableBytes) is a lot more concise than using the loop I have above.

public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
numBytes -= self.unwrapInboundIn(data).readableBytes

var byteBuffer = self.unwrapInboundIn(data) as ByteBuffer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the as or the extra whitespace line.

Motivation:

To make the PR easier to understand with less overhead.

Modifications:

Removed a whitespace, a cast, and a byte loop.

Result:

A more concise and easier to read PR.
@@ -21,13 +21,17 @@ private final class EchoHandler: ChannelInboundHandler {
public typealias OutboundOut = ByteBuffer
private var numBytes = 0


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this extra whitespace line either.

print("Received the line back from the server, closing channel")
if let string = byteBuffer.readString(length: byteBuffer.readableBytes) {
print("Received: '\(string)' back from the server, closing channel.")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a print clause in the implicit else branch here too.

Motivation:

Making PR more concise.

Modifications:

Added else branch and removed whitespace.

Result:

A more concise PR to view character output.
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful, thanks so much! ✨

@Lukasa
Copy link
Contributor

Lukasa commented Aug 20, 2018

@swift-nio-bot test this please

@Lukasa Lukasa merged commit 7895bc8 into apple:master Aug 20, 2018
@weissi weissi added the semver/patch No public API change. label Aug 24, 2018
@weissi weissi modified the milestones: 1.10.0, 1.9.3 Aug 24, 2018
weissi pushed a commit that referenced this pull request Aug 29, 2018
Motivation:

I was testing the NIOEchoClient example on Linux and noticed that I received no character output.
I thought this would be handy and added this code to do so.

Modifications:

Small modification to the channelRead method in NIOEchoClient to read the bytes and transform to a string.

Result:

The display of the characters on your echo client before the channel closes.

Motivation:

Explain here the context, and why you're making that change.
What is the problem you're trying to solve.

Modifications:

Describe the modifications you've done.

Result:

After your change, what will change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants