-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added README.md information and ALMI description
Adde ping pong almi example
- Loading branch information
1 parent
78b7b38
commit 18c56e4
Showing
13 changed files
with
465 additions
and
205 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,86 @@ | ||
# ALMI (Abstraction Layer for Multiserver Infrastructure) | ||
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.matteobattilana/almi/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.matteobattilana/almi) | ||
[![CircleCI](https://circleci.com/gh/MatteoBattilana/ALMI.svg?style=svg)](https://circleci.com/gh/MatteoBattilana/ALMI) | ||
|
||
## Description | ||
ALMI is a JAVA multiserver framework for remote method calls management based on Netty and Jackson serialization, that adds an abstraction layer for multiserver communication. | ||
ALMI is a Java multiserver framework for remote method calls management based on Netty and Jackson serialization, that adds an abstraction layer for multiserver communication. | ||
|
||
This framework will semplify the cluster management, making methods accessibile from other server, that has ALMI running on a node in the cluster. | ||
|
||
## Setup | ||
Add ALMI dependencies in your pom.xml or eventually, go to the [Maven Repository](https://search.maven.org/artifact/com.matteobattilana/almi/0.1.0/jar) and download the jar library file. | ||
|
||
```xml | ||
<dependencies> | ||
... | ||
<dependency> | ||
<groupId>com.matteobattilana</groupId> | ||
<artifactId>almi</artifactId> | ||
<version>0.1.0</version> | ||
</dependency> | ||
</dependencies> | ||
``` | ||
|
||
## Simple usage | ||
|
||
The following code will instantiate an ALMI server that is listening on the 8888 port (if not set, it will use the one described in the [Default configuration](#Default configuration) section). | ||
|
||
```java | ||
Almi server = AlmiBootstrap.bootstrap() | ||
.withPort(8888) | ||
.withRemoteCallTimeout(2000) | ||
.withMethodsMapper(new MethodsMapper() | ||
{ | ||
@Override | ||
public void configure() | ||
throws Exception | ||
{ | ||
addMethods( | ||
bindStatic(Calculator.class).method("execute", double.class, Calculator.Operation.class, double.class).withDefaultName(), | ||
bind(new Calculator()).method(Calculator.class.getMethod("sqrt", double.class)).withName("positiveSqrt") | ||
); | ||
} | ||
}) | ||
.start(); | ||
``` | ||
|
||
### MethodsMapper | ||
MethodsMapper will let you define the mapping beetwen the local method implementation and its remote name. This is used to make methods accessibile from other ALMI server, running on a node in the cluster. | ||
|
||
A MethodsMapper can be defined in-line as the one in previous example, or in a specific class. The `addMethods` will configure the methods passed as arguments, in order to expose them. | ||
```java | ||
public class MethodMapperImpl extends MethodsMapper | ||
{ | ||
private final ClientInformation mInstance1; | ||
|
||
public MethodMapperImpl( | ||
ClientInformation instance1 | ||
) | ||
{ | ||
mInstance1 = instance1; | ||
} | ||
|
||
@Override | ||
public void configure() | ||
throws Exception | ||
{ | ||
addMethods( | ||
bind(mInstance1).method(ClientInformation.class.getMethod("helloWorld")).withDefaultName(), | ||
bindStatic(ClientInformation.class).method(ClientInformation.class.getMethod("ping")).withDefaultName() | ||
); | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Configuration | ||
### Default configuration | ||
The defaul configuration loaded from static constants from the Constants.java: | ||
|
||
```java | ||
public static final int DEFAULT_PORT = 8888; | ||
public static final int DEFAULT_CONNECTION_TIMEOUT = 10000; | ||
public static final int DEFAULT_PROMISE_TIMEOUT = 10000; | ||
public static final String DEFAULT_THREAD_NAME = "almi-service"; | ||
public static final String DEFAULT_ADDRESS = "localhost"; | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package pingpong; | ||
|
||
import socket.Almi; | ||
import socket.bootstrap.AlmiBootstrap; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
public class AlmiPingPong | ||
{ | ||
public static void main(String[] args) | ||
throws Exception | ||
{ | ||
Almi server = AlmiBootstrap.bootstrap() | ||
.withPort(8888) | ||
.withRemoteCallTimeout(2000) | ||
.withMethodsMapper(new MethodsMapperImpl(new PingPong())) | ||
.start(); | ||
|
||
PingPong.State state = PingPong.State.PING; | ||
for(int i = 0; i < 10; i++) | ||
{ | ||
state = server.callMethod( | ||
"localhost", | ||
8888, | ||
"ping", | ||
Collections.singletonList(state) | ||
); | ||
System.out.println(state); | ||
} | ||
server.stop(); | ||
} | ||
} |
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,22 @@ | ||
package pingpong; | ||
|
||
import socket.bootstrap.MethodsMapper; | ||
|
||
public class MethodsMapperImpl extends MethodsMapper | ||
{ | ||
private final PingPong mPingPong; | ||
|
||
public MethodsMapperImpl(PingPong pingPong) | ||
{ | ||
mPingPong = pingPong; | ||
} | ||
|
||
@Override | ||
public void configure() | ||
throws Exception | ||
{ | ||
addMethods( | ||
bind(mPingPong).method("getReponse", PingPong.State.class).withName("ping") | ||
); | ||
} | ||
} |
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,37 @@ | ||
package pingpong; | ||
|
||
public class PingPong | ||
{ | ||
public enum State | ||
{ | ||
PING("ping"), | ||
PONG("pong"), | ||
UNKNOWN(""); | ||
|
||
private final String mState; | ||
|
||
State(String state) | ||
{ | ||
mState = state; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return mState; | ||
} | ||
} | ||
|
||
public State getReponse(State request) | ||
{ | ||
switch(request) | ||
{ | ||
case PING: | ||
return State.PONG; | ||
case PONG: | ||
return State.PING; | ||
default: | ||
return State.UNKNOWN; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.