-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
79 changed files
with
6,404 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...ces-examples/src/main/java/io/scalecube/services/examples/gateway/HttpGatewayExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...xamples/src/main/java/io/scalecube/services/examples/gateway/WebsocketGatewayExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
20 changes: 20 additions & 0 deletions
20
services-gateway/src/main/java/io/scalecube/services/gateway/GatewaySession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
110 changes: 110 additions & 0 deletions
110
services-gateway/src/main/java/io/scalecube/services/gateway/GatewaySessionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.