Skip to content

Commit d8cccbe

Browse files
authored
adds routing example with TaggingMetadata and CompositeMetadata (#1021)
1 parent bf0c608 commit d8cccbe

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2015-Present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.examples.transport.tcp.metadata.routing;
18+
19+
import io.netty.buffer.ByteBuf;
20+
import io.netty.buffer.ByteBufAllocator;
21+
import io.netty.buffer.ByteBufUtil;
22+
import io.netty.buffer.CompositeByteBuf;
23+
import io.rsocket.RSocket;
24+
import io.rsocket.SocketAcceptor;
25+
import io.rsocket.core.RSocketConnector;
26+
import io.rsocket.core.RSocketServer;
27+
import io.rsocket.metadata.CompositeMetadata;
28+
import io.rsocket.metadata.CompositeMetadataCodec;
29+
import io.rsocket.metadata.RoutingMetadata;
30+
import io.rsocket.metadata.TaggingMetadataCodec;
31+
import io.rsocket.metadata.WellKnownMimeType;
32+
import io.rsocket.transport.netty.client.TcpClientTransport;
33+
import io.rsocket.transport.netty.server.TcpServerTransport;
34+
import io.rsocket.util.ByteBufPayload;
35+
import java.util.Collections;
36+
import java.util.Objects;
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
39+
import reactor.core.publisher.Mono;
40+
41+
public class CompositeMetadataExample {
42+
static final Logger logger = LoggerFactory.getLogger(CompositeMetadataExample.class);
43+
44+
public static void main(String[] args) {
45+
RSocketServer.create(
46+
SocketAcceptor.forRequestResponse(
47+
payload -> {
48+
final String route = decodeRoute(payload.sliceMetadata());
49+
50+
logger.info("Received RequestResponse[route={}]", route);
51+
52+
payload.release();
53+
54+
if ("my.test.route".equals(route)) {
55+
return Mono.just(ByteBufPayload.create("Hello From My Test Route"));
56+
}
57+
58+
return Mono.error(new IllegalArgumentException("Route " + route + " not found"));
59+
}))
60+
.bindNow(TcpServerTransport.create("localhost", 7000));
61+
62+
RSocket socket =
63+
RSocketConnector.create()
64+
// here we specify that every metadata payload will be encoded using
65+
// CompositeMetadata layout as specified in the following subspec
66+
// https://github.com/rsocket/rsocket/blob/master/Extensions/CompositeMetadata.md
67+
.metadataMimeType(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString())
68+
.connect(TcpClientTransport.create("localhost", 7000))
69+
.block();
70+
71+
final ByteBuf routeMetadata =
72+
TaggingMetadataCodec.createTaggingContent(
73+
ByteBufAllocator.DEFAULT, Collections.singletonList("my.test.route"));
74+
final CompositeByteBuf compositeMetadata = ByteBufAllocator.DEFAULT.compositeBuffer();
75+
76+
CompositeMetadataCodec.encodeAndAddMetadata(
77+
compositeMetadata,
78+
ByteBufAllocator.DEFAULT,
79+
WellKnownMimeType.MESSAGE_RSOCKET_ROUTING,
80+
routeMetadata);
81+
82+
socket
83+
.requestResponse(
84+
ByteBufPayload.create(
85+
ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, "HelloWorld"), compositeMetadata))
86+
.log()
87+
.block();
88+
}
89+
90+
static String decodeRoute(ByteBuf metadata) {
91+
final CompositeMetadata compositeMetadata = new CompositeMetadata(metadata, false);
92+
93+
for (CompositeMetadata.Entry metadatum : compositeMetadata) {
94+
if (Objects.requireNonNull(metadatum.getMimeType())
95+
.equals(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString())) {
96+
return new RoutingMetadata(metadatum.getContent()).iterator().next();
97+
}
98+
}
99+
100+
return null;
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2015-Present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.rsocket.examples.transport.tcp.metadata.routing;
18+
19+
import io.netty.buffer.ByteBuf;
20+
import io.netty.buffer.ByteBufAllocator;
21+
import io.netty.buffer.ByteBufUtil;
22+
import io.rsocket.RSocket;
23+
import io.rsocket.SocketAcceptor;
24+
import io.rsocket.core.RSocketConnector;
25+
import io.rsocket.core.RSocketServer;
26+
import io.rsocket.metadata.RoutingMetadata;
27+
import io.rsocket.metadata.TaggingMetadataCodec;
28+
import io.rsocket.metadata.WellKnownMimeType;
29+
import io.rsocket.transport.netty.client.TcpClientTransport;
30+
import io.rsocket.transport.netty.server.TcpServerTransport;
31+
import io.rsocket.util.ByteBufPayload;
32+
import java.util.Collections;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
35+
import reactor.core.publisher.Mono;
36+
37+
public class RoutingMetadataExample {
38+
static final Logger logger = LoggerFactory.getLogger(RoutingMetadataExample.class);
39+
40+
public static void main(String[] args) {
41+
RSocketServer.create(
42+
SocketAcceptor.forRequestResponse(
43+
payload -> {
44+
final String route = decodeRoute(payload.sliceMetadata());
45+
46+
logger.info("Received RequestResponse[route={}]", route);
47+
48+
payload.release();
49+
50+
if ("my.test.route".equals(route)) {
51+
return Mono.just(ByteBufPayload.create("Hello From My Test Route"));
52+
}
53+
54+
return Mono.error(new IllegalArgumentException("Route " + route + " not found"));
55+
}))
56+
.bindNow(TcpServerTransport.create("localhost", 7000));
57+
58+
RSocket socket =
59+
RSocketConnector.create()
60+
// here we specify that route will be encoded using
61+
// Routing&Tagging Metadata layout specified at this
62+
// subspec https://github.com/rsocket/rsocket/blob/master/Extensions/Routing.md
63+
.metadataMimeType(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString())
64+
.connect(TcpClientTransport.create("localhost", 7000))
65+
.block();
66+
67+
final ByteBuf routeMetadata =
68+
TaggingMetadataCodec.createTaggingContent(
69+
ByteBufAllocator.DEFAULT, Collections.singletonList("my.test.route"));
70+
socket
71+
.requestResponse(
72+
ByteBufPayload.create(
73+
ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, "HelloWorld"), routeMetadata))
74+
.log()
75+
.block();
76+
}
77+
78+
static String decodeRoute(ByteBuf metadata) {
79+
final RoutingMetadata routingMetadata = new RoutingMetadata(metadata);
80+
81+
return routingMetadata.iterator().next();
82+
}
83+
}

0 commit comments

Comments
 (0)