Skip to content

Commit 3f65c48

Browse files
Oleh DokukaOlegDokuka
authored andcommitted
adds routing example with TaggingMetadata and CompositeMetadata
Signed-off-by: Oleh Dokuka <[email protected]> Signed-off-by: Oleh Dokuka <[email protected]> Signed-off-by: Oleh Dokuka <[email protected]>
1 parent 7d9c28f commit 3f65c48

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
if ("my.test.route".equals(route)) {
53+
payload.release();
54+
return Mono.just(ByteBufPayload.create("Hello From My Test Route"));
55+
}
56+
57+
return Mono.error(new IllegalArgumentException("Route " + route + " not found"));
58+
}))
59+
.bindNow(TcpServerTransport.create("localhost", 7000));
60+
61+
RSocket socket =
62+
RSocketConnector.create()
63+
// here we specify that every metadata payload will be encoded using
64+
// CompositeMetadata layout as specified in the following subspec
65+
// https://github.com/rsocket/rsocket/blob/master/Extensions/CompositeMetadata.md
66+
.metadataMimeType(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString())
67+
.connect(TcpClientTransport.create("localhost", 7000))
68+
.block();
69+
70+
final ByteBuf routeMetadata =
71+
TaggingMetadataCodec.createTaggingContent(
72+
ByteBufAllocator.DEFAULT, Collections.singletonList("my.test.route"));
73+
final CompositeByteBuf compositeMetadata = ByteBufAllocator.DEFAULT.compositeBuffer();
74+
75+
CompositeMetadataCodec.encodeAndAddMetadata(
76+
compositeMetadata,
77+
ByteBufAllocator.DEFAULT,
78+
WellKnownMimeType.MESSAGE_RSOCKET_ROUTING,
79+
routeMetadata);
80+
81+
socket
82+
.requestResponse(
83+
ByteBufPayload.create(
84+
ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, "HelloWorld"), compositeMetadata))
85+
.log()
86+
.block();
87+
}
88+
89+
static String decodeRoute(ByteBuf metadata) {
90+
final CompositeMetadata compositeMetadata = new CompositeMetadata(metadata, false);
91+
92+
for (CompositeMetadata.Entry metadatum : compositeMetadata) {
93+
if (Objects.requireNonNull(metadatum.getMimeType())
94+
.equals(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString())) {
95+
return new RoutingMetadata(metadatum.getContent()).iterator().next();
96+
}
97+
}
98+
99+
return null;
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
if ("my.test.route".equals(route)) {
49+
payload.release();
50+
return Mono.just(ByteBufPayload.create("Hello From My Test Route"));
51+
}
52+
53+
return Mono.error(new IllegalArgumentException("Route " + route + " not found"));
54+
}))
55+
.bindNow(TcpServerTransport.create("localhost", 7000));
56+
57+
RSocket socket =
58+
RSocketConnector.create()
59+
// here we specify that route will be encoded using
60+
// Routing&Tagging Metadata layout specified at this
61+
// subspec https://github.com/rsocket/rsocket/blob/master/Extensions/Routing.md
62+
.metadataMimeType(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString())
63+
.connect(TcpClientTransport.create("localhost", 7000))
64+
.block();
65+
66+
final ByteBuf routeMetadata =
67+
TaggingMetadataCodec.createTaggingContent(
68+
ByteBufAllocator.DEFAULT, Collections.singletonList("my.test.route"));
69+
socket
70+
.requestResponse(
71+
ByteBufPayload.create(
72+
ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, "HelloWorld"), routeMetadata))
73+
.log()
74+
.block();
75+
}
76+
77+
static String decodeRoute(ByteBuf metadata) {
78+
final RoutingMetadata routingMetadata = new RoutingMetadata(metadata);
79+
80+
return routingMetadata.iterator().next();
81+
}
82+
}

0 commit comments

Comments
 (0)