Skip to content

Commit

Permalink
Merge gateway to services (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-v authored Jun 21, 2023
1 parent 2aaa45e commit bcb7d9c
Show file tree
Hide file tree
Showing 79 changed files with 6,404 additions and 9 deletions.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -85,6 +87,7 @@
<module>services</module>
<module>services-api</module>
<module>services-transport-parent</module>
<module>services-gateway</module>
<module>services-discovery</module>
<module>services-security</module>
<module>services-examples</module>
Expand Down
4 changes: 3 additions & 1 deletion services-api/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">

<modelVersion>4.0.0</modelVersion>

Expand Down
4 changes: 3 additions & 1 deletion services-discovery/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<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">
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down
4 changes: 3 additions & 1 deletion services-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.scalecube.services.examples.gateway;

import io.scalecube.net.Address;
import io.scalecube.services.gateway.Gateway;
import io.scalecube.services.gateway.GatewayOptions;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;
import reactor.core.publisher.Mono;

public class HttpGatewayExample implements Gateway {

private final GatewayOptions options;
private final InetSocketAddress address;

public HttpGatewayExample(GatewayOptions options) {
this.options = options;
this.address = new InetSocketAddress(options.port());
}

@Override
public String id() {
return options.id();
}

@Override
public Address address() {
return Address.create(address.getHostString(), address.getPort());
}

@Override
public Mono<Gateway> start() {
return Mono.defer(
() -> {
System.out.println("Starting HTTP gateway...");

return Mono.delay(Duration.ofMillis(ThreadLocalRandom.current().nextInt(100, 500)))
.map(tick -> this)
.doOnSuccess(gw -> System.out.println("HTTP gateway is started on " + gw.address));
});
}

@Override
public Mono<Void> stop() {
return Mono.defer(
() -> {
System.out.println("Stopping HTTP gateway...");
return Mono.empty();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.scalecube.services.examples.gateway;

import io.scalecube.net.Address;
import io.scalecube.services.gateway.Gateway;
import io.scalecube.services.gateway.GatewayOptions;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;
import reactor.core.publisher.Mono;

public class WebsocketGatewayExample implements Gateway {

private final GatewayOptions options;
private final InetSocketAddress address;

public WebsocketGatewayExample(GatewayOptions options) {
this.options = options;
this.address = new InetSocketAddress(options.port());
}

@Override
public String id() {
return options.id();
}

@Override
public Address address() {
return Address.create(address.getHostString(), address.getPort());
}

@Override
public Mono<Gateway> start() {
return Mono.defer(
() -> {
System.out.println("Starting WS gateway...");

return Mono.delay(Duration.ofMillis(ThreadLocalRandom.current().nextInt(100, 500)))
.map(tick -> this)
.doOnSuccess(gw -> System.out.println("WS gateway is started on " + gw.address));
});
}

@Override
public Mono<Void> stop() {
return Mono.defer(
() -> {
System.out.println("Stopping WS gateway...");
return Mono.empty();
});
}
}
92 changes: 92 additions & 0 deletions services-gateway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-services-parent</artifactId>
<version>2.10.26-SNAPSHOT</version>
</parent>

<artifactId>services-gateway</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-services</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>org.jctools</groupId>
<artifactId>jctools-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<!-- Tests -->
<dependency>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-services-examples</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-services-discovery</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-services-transport-rsocket</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-services-transport-jackson</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.scalecube.services.gateway;

import java.util.Map;

public interface GatewaySession {

/**
* Session id representation to be unique per client session.
*
* @return session id
*/
long sessionId();

/**
* Returns headers associated with session.
*
* @return headers map
*/
Map<String, String> headers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.scalecube.services.gateway;

import io.netty.buffer.ByteBuf;
import io.scalecube.services.api.ServiceMessage;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import reactor.util.context.Context;

public interface GatewaySessionHandler {

Logger LOGGER = LoggerFactory.getLogger(GatewaySessionHandler.class);

GatewaySessionHandler DEFAULT_INSTANCE = new GatewaySessionHandler() {};

/**
* Message mapper function.
*
* @param session webscoket session (not null)
* @param message request message (not null)
* @return message
*/
default ServiceMessage mapMessage(
GatewaySession session, ServiceMessage message, Context context) {
return message;
}

/**
* Request mapper function.
*
* @param session session
* @param byteBuf request buffer
* @param context subscriber context
* @return context
*/
default Context onRequest(GatewaySession session, ByteBuf byteBuf, Context context) {
return context;
}

/**
* On response handler.
*
* @param session session
* @param byteBuf response buffer
* @param message response message
* @param context subscriber context
*/
default void onResponse(
GatewaySession session, ByteBuf byteBuf, ServiceMessage message, Context context) {
// no-op
}

/**
* Error handler function.
*
* @param session webscoket session (not null)
* @param throwable an exception that occurred (not null)
* @param context subscriber context
*/
default void onError(GatewaySession session, Throwable throwable, Context context) {
LOGGER.error(
"Exception occurred on session: {}, on context: {}, cause:",
session.sessionId(),
context,
throwable);
}

/**
* Error handler function.
*
* @param session webscoket session (not null)
* @param throwable an exception that occurred (not null)
*/
default void onSessionError(GatewaySession session, Throwable throwable) {
LOGGER.error("Exception occurred on session: {}, cause:", session.sessionId(), throwable);
}

/**
* On connection open handler.
*
* @param sessionId session id
* @param headers connection/session headers
* @return mono result
*/
default Mono<Void> onConnectionOpen(long sessionId, Map<String, String> headers) {
return Mono.fromRunnable(
() ->
LOGGER.debug(
"Connection opened, sessionId: {}, headers({})", sessionId, headers.size()));
}

/**
* On session open handler.
*
* @param session websocket session (not null)
*/
default void onSessionOpen(GatewaySession session) {
LOGGER.info("Session opened: {}", session);
}

/**
* On session close handler.
*
* @param session websocket session (not null)
*/
default void onSessionClose(GatewaySession session) {
LOGGER.info("Session closed: {}", session);
}
}
Loading

0 comments on commit bcb7d9c

Please sign in to comment.