-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/examples/java/reactor/netty/examples/tcp/echo/EchoClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2011-Present Pivotal Software Inc, All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package reactor.netty.examples.tcp.echo; | ||
|
||
import io.netty.handler.ssl.SslContextBuilder; | ||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.netty.ByteBufFlux; | ||
import reactor.netty.Connection; | ||
import reactor.netty.tcp.TcpClient; | ||
|
||
/** | ||
* A TCP client that sends a package to the TCP server and | ||
* later echoes the received content thus initiating ping/pong. | ||
* | ||
* @author Violeta Georgieva | ||
*/ | ||
public final class EchoClient { | ||
|
||
static final boolean SECURE = System.getProperty("secure") != null; | ||
static final int PORT = Integer.parseInt(System.getProperty("port", SECURE ? "8443" : "8080")); | ||
static final boolean WIRETAP = System.getProperty("wiretap") != null; | ||
|
||
public static void main(String[] args) { | ||
TcpClient client = | ||
TcpClient.create() | ||
.port(PORT) | ||
.wiretap(WIRETAP); | ||
|
||
if (SECURE) { | ||
client = client.secure( | ||
spec -> spec.sslContext(SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE))); | ||
} | ||
|
||
Connection connection = | ||
client.handle((in, out) -> out.send(Flux.concat(ByteBufFlux.fromString(Mono.just("echo")), | ||
in.receive().retain()))) | ||
.connectNow(); | ||
|
||
connection.onDispose() | ||
.block(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/examples/java/reactor/netty/examples/tcp/echo/EchoServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2011-Present Pivotal Software Inc, All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package reactor.netty.examples.tcp.echo; | ||
|
||
import io.netty.handler.ssl.SslContextBuilder; | ||
import io.netty.handler.ssl.util.SelfSignedCertificate; | ||
import reactor.netty.tcp.TcpServer; | ||
|
||
/** | ||
* A TCP server that sends back the received content. | ||
* | ||
* @author Violeta Georgieva | ||
*/ | ||
public final class EchoServer { | ||
|
||
static final boolean SECURE = System.getProperty("secure") != null; | ||
static final int PORT = Integer.parseInt(System.getProperty("port", SECURE ? "8443" : "8080")); | ||
static final boolean WIRETAP = System.getProperty("wiretap") != null; | ||
|
||
public static void main(String[] args) throws Exception { | ||
TcpServer server = | ||
TcpServer.create() | ||
.port(PORT) | ||
.wiretap(WIRETAP) | ||
.handle((in, out) -> out.send(in.receive().retain())); | ||
|
||
if (SECURE) { | ||
SelfSignedCertificate ssc = new SelfSignedCertificate(); | ||
server = server.secure( | ||
spec -> spec.sslContext(SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()))); | ||
} | ||
|
||
server.bindNow() | ||
.onDispose() | ||
.block(); | ||
} | ||
} |