Skip to content

Commit bcb7d9c

Browse files
authored
Merge gateway to services (#840)
1 parent 2aaa45e commit bcb7d9c

File tree

79 files changed

+6404
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+6404
-9
lines changed

pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
35
<modelVersion>4.0.0</modelVersion>
46

57
<parent>
@@ -85,6 +87,7 @@
8587
<module>services</module>
8688
<module>services-api</module>
8789
<module>services-transport-parent</module>
90+
<module>services-gateway</module>
8891
<module>services-discovery</module>
8992
<module>services-security</module>
9093
<module>services-examples</module>

services-api/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
35

46
<modelVersion>4.0.0</modelVersion>
57

services-discovery/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24
<modelVersion>4.0.0</modelVersion>
35

46
<parent>

services-examples/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
35
<modelVersion>4.0.0</modelVersion>
46

57
<parent>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.scalecube.services.examples.gateway;
2+
3+
import io.scalecube.net.Address;
4+
import io.scalecube.services.gateway.Gateway;
5+
import io.scalecube.services.gateway.GatewayOptions;
6+
import java.net.InetSocketAddress;
7+
import java.time.Duration;
8+
import java.util.concurrent.ThreadLocalRandom;
9+
import reactor.core.publisher.Mono;
10+
11+
public class HttpGatewayExample implements Gateway {
12+
13+
private final GatewayOptions options;
14+
private final InetSocketAddress address;
15+
16+
public HttpGatewayExample(GatewayOptions options) {
17+
this.options = options;
18+
this.address = new InetSocketAddress(options.port());
19+
}
20+
21+
@Override
22+
public String id() {
23+
return options.id();
24+
}
25+
26+
@Override
27+
public Address address() {
28+
return Address.create(address.getHostString(), address.getPort());
29+
}
30+
31+
@Override
32+
public Mono<Gateway> start() {
33+
return Mono.defer(
34+
() -> {
35+
System.out.println("Starting HTTP gateway...");
36+
37+
return Mono.delay(Duration.ofMillis(ThreadLocalRandom.current().nextInt(100, 500)))
38+
.map(tick -> this)
39+
.doOnSuccess(gw -> System.out.println("HTTP gateway is started on " + gw.address));
40+
});
41+
}
42+
43+
@Override
44+
public Mono<Void> stop() {
45+
return Mono.defer(
46+
() -> {
47+
System.out.println("Stopping HTTP gateway...");
48+
return Mono.empty();
49+
});
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.scalecube.services.examples.gateway;
2+
3+
import io.scalecube.net.Address;
4+
import io.scalecube.services.gateway.Gateway;
5+
import io.scalecube.services.gateway.GatewayOptions;
6+
import java.net.InetSocketAddress;
7+
import java.time.Duration;
8+
import java.util.concurrent.ThreadLocalRandom;
9+
import reactor.core.publisher.Mono;
10+
11+
public class WebsocketGatewayExample implements Gateway {
12+
13+
private final GatewayOptions options;
14+
private final InetSocketAddress address;
15+
16+
public WebsocketGatewayExample(GatewayOptions options) {
17+
this.options = options;
18+
this.address = new InetSocketAddress(options.port());
19+
}
20+
21+
@Override
22+
public String id() {
23+
return options.id();
24+
}
25+
26+
@Override
27+
public Address address() {
28+
return Address.create(address.getHostString(), address.getPort());
29+
}
30+
31+
@Override
32+
public Mono<Gateway> start() {
33+
return Mono.defer(
34+
() -> {
35+
System.out.println("Starting WS gateway...");
36+
37+
return Mono.delay(Duration.ofMillis(ThreadLocalRandom.current().nextInt(100, 500)))
38+
.map(tick -> this)
39+
.doOnSuccess(gw -> System.out.println("WS gateway is started on " + gw.address));
40+
});
41+
}
42+
43+
@Override
44+
public Mono<Void> stop() {
45+
return Mono.defer(
46+
() -> {
47+
System.out.println("Stopping WS gateway...");
48+
return Mono.empty();
49+
});
50+
}
51+
}

services-gateway/pom.xml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.scalecube</groupId>
9+
<artifactId>scalecube-services-parent</artifactId>
10+
<version>2.10.26-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>services-gateway</artifactId>
14+
<packaging>jar</packaging>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.scalecube</groupId>
19+
<artifactId>scalecube-services</artifactId>
20+
<version>${project.parent.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>io.projectreactor.netty</groupId>
25+
<artifactId>reactor-netty</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.datatype</groupId>
30+
<artifactId>jackson-datatype-jsr310</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.fasterxml.jackson.core</groupId>
34+
<artifactId>jackson-core</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.fasterxml.jackson.core</groupId>
38+
<artifactId>jackson-databind</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.jctools</groupId>
43+
<artifactId>jctools-core</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.slf4j</groupId>
47+
<artifactId>slf4j-api</artifactId>
48+
</dependency>
49+
50+
<!-- Tests -->
51+
<dependency>
52+
<groupId>io.scalecube</groupId>
53+
<artifactId>scalecube-services-examples</artifactId>
54+
<version>${project.parent.version}</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>io.scalecube</groupId>
59+
<artifactId>scalecube-services-discovery</artifactId>
60+
<version>${project.parent.version}</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>io.scalecube</groupId>
65+
<artifactId>scalecube-services-transport-rsocket</artifactId>
66+
<version>${project.parent.version}</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>io.scalecube</groupId>
71+
<artifactId>scalecube-services-transport-jackson</artifactId>
72+
<version>${project.parent.version}</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.apache.logging.log4j</groupId>
77+
<artifactId>log4j-slf4j-impl</artifactId>
78+
<scope>test</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.apache.logging.log4j</groupId>
82+
<artifactId>log4j-core</artifactId>
83+
<scope>test</scope>
84+
</dependency>
85+
<dependency>
86+
<groupId>com.lmax</groupId>
87+
<artifactId>disruptor</artifactId>
88+
<scope>test</scope>
89+
</dependency>
90+
</dependencies>
91+
92+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.scalecube.services.gateway;
2+
3+
import java.util.Map;
4+
5+
public interface GatewaySession {
6+
7+
/**
8+
* Session id representation to be unique per client session.
9+
*
10+
* @return session id
11+
*/
12+
long sessionId();
13+
14+
/**
15+
* Returns headers associated with session.
16+
*
17+
* @return headers map
18+
*/
19+
Map<String, String> headers();
20+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package io.scalecube.services.gateway;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.scalecube.services.api.ServiceMessage;
5+
import java.util.Map;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import reactor.core.publisher.Mono;
9+
import reactor.util.context.Context;
10+
11+
public interface GatewaySessionHandler {
12+
13+
Logger LOGGER = LoggerFactory.getLogger(GatewaySessionHandler.class);
14+
15+
GatewaySessionHandler DEFAULT_INSTANCE = new GatewaySessionHandler() {};
16+
17+
/**
18+
* Message mapper function.
19+
*
20+
* @param session webscoket session (not null)
21+
* @param message request message (not null)
22+
* @return message
23+
*/
24+
default ServiceMessage mapMessage(
25+
GatewaySession session, ServiceMessage message, Context context) {
26+
return message;
27+
}
28+
29+
/**
30+
* Request mapper function.
31+
*
32+
* @param session session
33+
* @param byteBuf request buffer
34+
* @param context subscriber context
35+
* @return context
36+
*/
37+
default Context onRequest(GatewaySession session, ByteBuf byteBuf, Context context) {
38+
return context;
39+
}
40+
41+
/**
42+
* On response handler.
43+
*
44+
* @param session session
45+
* @param byteBuf response buffer
46+
* @param message response message
47+
* @param context subscriber context
48+
*/
49+
default void onResponse(
50+
GatewaySession session, ByteBuf byteBuf, ServiceMessage message, Context context) {
51+
// no-op
52+
}
53+
54+
/**
55+
* Error handler function.
56+
*
57+
* @param session webscoket session (not null)
58+
* @param throwable an exception that occurred (not null)
59+
* @param context subscriber context
60+
*/
61+
default void onError(GatewaySession session, Throwable throwable, Context context) {
62+
LOGGER.error(
63+
"Exception occurred on session: {}, on context: {}, cause:",
64+
session.sessionId(),
65+
context,
66+
throwable);
67+
}
68+
69+
/**
70+
* Error handler function.
71+
*
72+
* @param session webscoket session (not null)
73+
* @param throwable an exception that occurred (not null)
74+
*/
75+
default void onSessionError(GatewaySession session, Throwable throwable) {
76+
LOGGER.error("Exception occurred on session: {}, cause:", session.sessionId(), throwable);
77+
}
78+
79+
/**
80+
* On connection open handler.
81+
*
82+
* @param sessionId session id
83+
* @param headers connection/session headers
84+
* @return mono result
85+
*/
86+
default Mono<Void> onConnectionOpen(long sessionId, Map<String, String> headers) {
87+
return Mono.fromRunnable(
88+
() ->
89+
LOGGER.debug(
90+
"Connection opened, sessionId: {}, headers({})", sessionId, headers.size()));
91+
}
92+
93+
/**
94+
* On session open handler.
95+
*
96+
* @param session websocket session (not null)
97+
*/
98+
default void onSessionOpen(GatewaySession session) {
99+
LOGGER.info("Session opened: {}", session);
100+
}
101+
102+
/**
103+
* On session close handler.
104+
*
105+
* @param session websocket session (not null)
106+
*/
107+
default void onSessionClose(GatewaySession session) {
108+
LOGGER.info("Session closed: {}", session);
109+
}
110+
}

0 commit comments

Comments
 (0)