https://github.com/tix320/sonder
Client-server high level abstraction library.
https://github.com/tix320/sonder
annotations-marks java lightweight network rpc rpc-interfaces rpc-service socket socket-programming tcp tcp-protocol topic
Last synced: about 2 months ago
JSON representation
Client-server high level abstraction library.
- Host: GitHub
- URL: https://github.com/tix320/sonder
- Owner: tix320
- License: apache-2.0
- Created: 2020-01-18T19:50:13.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-16T02:41:44.000Z (over 3 years ago)
- Last Synced: 2023-07-26T22:09:54.215Z (over 2 years ago)
- Topics: annotations-marks, java, lightweight, network, rpc, rpc-interfaces, rpc-service, socket, socket-programming, tcp, tcp-protocol, topic
- Language: Java
- Homepage:
- Size: 727 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# Sonder
## Description
Sonder is a Java library providing TCP client-server asynchronous communication on high level abstraction. It's
implemented over NIO encapsulating hard work of selectors and some byte-level packet protocol.
## Requirements
```
Java 11 or higher.
```
## Maven Dependency
```xml
com.github.tix320
sonder
2.2.1
```
## Usage
Bellow a small example to demonstrate client-server communication. For the detailed information about the internal
working and usage please see the [documentation](https://github.com/tix320/sonder/wiki).
##### Example
```java
package com.github.tix320.sonder.readme;
import java.io.IOException;
import java.net.InetSocketAddress;
import com.github.tix320.kiwi.api.reactive.observable.MonoObservable;
import com.github.tix320.sonder.api.client.SonderClient;
import com.github.tix320.sonder.api.client.rpc.ClientRPCProtocol;
import com.github.tix320.sonder.api.common.rpc.Endpoint;
import com.github.tix320.sonder.api.common.rpc.Origin;
import com.github.tix320.sonder.api.server.SonderServer;
import com.github.tix320.sonder.api.server.rpc.ServerRPCProtocol;
@Endpoint("someService")
class ServerEndpoint {
@Endpoint("someMethod")
public Integer getLengthOfString(String s) {
return s.length();
}
}
class ServerTest {
public static void main(String[] args) throws IOException {
// Creating protocol for communication
ServerRPCProtocol rpcProtocol = ServerRPCProtocol.builder()
.registerEndpointClasses(ServerEndpoint.class)
.build();
// Creating the server itself and registering the protocol
SonderServer sonderServer = SonderServer.forAddress(new InetSocketAddress(8888))
.registerProtocol(rpcProtocol)
.build();
sonderServer.events()
.newConnections()
.subscribe(client -> System.out.printf("Client %s connected", client.getId()));
sonderServer.events()
.deadConnections()
.subscribe(client -> System.out.printf("Client %s disconnected", client.getId()));
// start the server
sonderServer.start();
}
}
@Origin("someService")
interface ClientOrigin {
@Origin("someMethod")
MonoObservable getLengthOfString(String s);
}
class ClientTest {
public static void main(String[] args) throws IOException {
// Creating protocol for communication
ClientRPCProtocol rpcProtocol = ClientRPCProtocol.builder()
.registerOriginInterfaces(ClientOrigin.class)
.build();
// Creating the client itself and registering the protocol
SonderClient sonderClient = SonderClient.forAddress(new InetSocketAddress("localhost", 8888))
.registerProtocol(rpcProtocol)
.build();
sonderClient.events().connectionState().subscribe(state -> {
switch (state) {
case IDLE:
System.out.println("Initial state or disconnected");
case CONNECTED:
System.out.println("Connected to server");
break;
case CLOSED:
System.out.println("Connection closed by user");
break;
}
});
// connect to server
sonderClient.start();
// Get RPC interface for some request
ClientOrigin clientOrigin = rpcProtocol.getOrigin(ClientOrigin.class);
// Call RPC method and subscribe to response
clientOrigin.getLengthOfString("my_first_rpc_call").subscribe(length -> {
System.out.println(length);
assert length == 17;
});
}
}
```
## Credits
- Tigran Sargsyan - **[tix320](https://github.com/tix320).**
## License
This project is licensed under the Apache License - see the [LICENSE](https://github.com/tix320/sonder/blob/master/LICENSE) file for details.