Skip to content

Commit f3bc751

Browse files
authored
Merge pull request vert-x3#253 from tsegismont/net-record-parser
Added an example on how to combine net client/server with RecordParser
2 parents bd1ae00 + 4ff7113 commit f3bc751

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.example.core.net.greeter;
18+
19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.net.NetSocket;
21+
import io.vertx.core.parsetools.RecordParser;
22+
import io.vertx.example.util.Runner;
23+
24+
import java.util.stream.Stream;
25+
26+
/*
27+
* @author Thomas Segismont
28+
*/
29+
public class Client extends AbstractVerticle {
30+
31+
// Convenience method so you can run it in your IDE
32+
public static void main(String[] args) {
33+
Runner.runExample(Client.class);
34+
}
35+
36+
@Override
37+
public void start() throws Exception {
38+
vertx.createNetClient().connect(1234, "localhost", res -> {
39+
40+
if (res.succeeded()) {
41+
NetSocket socket = res.result();
42+
43+
RecordParser.newDelimited("\n", socket)
44+
.endHandler(v -> socket.close())
45+
.exceptionHandler(t -> {
46+
t.printStackTrace();
47+
socket.close();
48+
})
49+
.handler(buffer -> {
50+
String greeting = buffer.toString("UTF-8");
51+
System.out.println("Net client receiving: " + greeting);
52+
});
53+
54+
// Now send some data
55+
Stream.of("John", "Joe", "Lisa", "Bill").forEach(name -> {
56+
System.out.println("Net client sending: " + name);
57+
socket.write(name).write("\n");
58+
});
59+
60+
} else {
61+
System.out.println("Failed to connect " + res.cause());
62+
}
63+
});
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.example.core.net.greeter;
18+
19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.parsetools.RecordParser;
21+
import io.vertx.example.util.Runner;
22+
23+
/*
24+
* @author Thomas Segismont
25+
*/
26+
public class Server extends AbstractVerticle {
27+
28+
// Convenience method so you can run it in your IDE
29+
public static void main(String[] args) {
30+
Runner.runExample(Server.class);
31+
}
32+
33+
@Override
34+
public void start() throws Exception {
35+
36+
vertx.createNetServer().connectHandler(sock -> {
37+
38+
RecordParser parser = RecordParser.newDelimited("\n", sock);
39+
40+
parser
41+
.endHandler(v -> sock.close())
42+
.exceptionHandler(t -> {
43+
t.printStackTrace();
44+
sock.close();
45+
})
46+
.handler(buffer -> {
47+
String name = buffer.toString("UTF-8");
48+
sock.write("Hello " + name + "\n", "UTF-8");
49+
});
50+
51+
}).listen(1234);
52+
53+
System.out.println("Echo server is now listening");
54+
55+
}
56+
}

rxjava-2-examples/README.adoc

+13
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,16 @@ examples shows how blocking operation can be scheduled on Vert.x
168168

169169
link:src/main/java/io/vertx/example/reactivex/scheduler/blocking/Scheduled.java[Blocking scheduled action]
170170
link:src/main/java/io/vertx/example/reactivex/scheduler/blocking/Scheduled.java[Blocking scheduled action]
171+
172+
== Scheduler examples
173+
174+
These examples demonstrate usage of Vert.x net servers and clients with RxJava2
175+
176+
=== Greeter
177+
178+
This example combines `RecordParser` and RxJava2 for a TCP client/server exchange.
179+
When the client sends a name to the server, it replies with a greeting.
180+
Names and greetings are line-separated.
181+
182+
link:src/main/java/io/vertx/example/reactivex/net/greeter/Client.java[Greeting client]
183+
link:src/main/java/io/vertx/example/reactivex/net/greeter/Server.java[Greeting Server]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.example.reactivex.net.greeter;
18+
19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.net.NetSocket;
21+
import io.vertx.core.parsetools.RecordParser;
22+
import io.vertx.example.util.Runner;
23+
import io.vertx.reactivex.FlowableHelper;
24+
25+
import java.util.stream.Stream;
26+
27+
/*
28+
* @author Thomas Segismont
29+
*/
30+
public class Client extends AbstractVerticle {
31+
32+
// Convenience method so you can run it in your IDE
33+
public static void main(String[] args) {
34+
Runner.runExample(Client.class);
35+
}
36+
37+
@Override
38+
public void start() throws Exception {
39+
vertx.createNetClient().connect(1234, "localhost", res -> {
40+
41+
if (res.succeeded()) {
42+
NetSocket socket = res.result();
43+
44+
RecordParser parser = RecordParser.newDelimited("\n", socket);
45+
46+
FlowableHelper.toFlowable(parser)
47+
.map(buffer -> buffer.toString("UTF-8"))
48+
.subscribe(greeting -> System.out.println("Net client receiving: " + greeting), t -> {
49+
t.printStackTrace();
50+
socket.close();
51+
}, socket::close);
52+
53+
// Now send some data
54+
Stream.of("John", "Joe", "Lisa", "Bill").forEach(name -> {
55+
System.out.println("Net client sending: " + name);
56+
socket.write(name).write("\n");
57+
});
58+
59+
} else {
60+
System.out.println("Failed to connect " + res.cause());
61+
}
62+
});
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.example.reactivex.net.greeter;
18+
19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.parsetools.RecordParser;
21+
import io.vertx.example.util.Runner;
22+
import io.vertx.reactivex.FlowableHelper;
23+
24+
/*
25+
* @author Thomas Segismont
26+
*/
27+
public class Server extends AbstractVerticle {
28+
29+
// Convenience method so you can run it in your IDE
30+
public static void main(String[] args) {
31+
Runner.runExample(Server.class);
32+
}
33+
34+
@Override
35+
public void start() throws Exception {
36+
37+
vertx.createNetServer().connectHandler(sock -> {
38+
39+
RecordParser parser = RecordParser.newDelimited("\n", sock);
40+
41+
FlowableHelper.toFlowable(parser)
42+
.map(buffer -> buffer.toString("UTF-8"))
43+
.map(name -> "Hello " + name)
44+
.subscribe(greeting -> sock.write(greeting + "\n", "UTF-8"), throwable -> {
45+
throwable.printStackTrace();
46+
sock.close();
47+
}, sock::close);
48+
49+
}).listen(1234);
50+
51+
System.out.println("Echo server is now listening");
52+
53+
}
54+
}

0 commit comments

Comments
 (0)