Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def javaProjects = [

configure(javaProjects) {

version '3.0.0'
version '3.0.1'
repositories {
mavenCentral()
if (project.version.endsWith('-SNAPSHOT')) {
Expand Down
2 changes: 1 addition & 1 deletion lcloud-udp-discovery-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'org.mtbo.lcloud'
version = '3.0.0-SNAPSHOT'
version = '3.0.1-SNAPSHOT'

apply plugin: 'application'
mainClassName = 'ExampleService'
Expand Down
60 changes: 31 additions & 29 deletions lcloud-udp-discovery-example/src/main/java/ExampleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.mtbo.lcloud.discovery.DiscoveryService;
import org.mtbo.lcloud.discovery.udp.UdpConnection;
import org.mtbo.lcloud.discovery.udp.UdpDiscoveryClient;
import org.mtbo.lcloud.discovery.udp.*;

/**
* Demo application
Expand All @@ -28,39 +26,43 @@ public class ExampleService {
* @throws SocketException in case of fatal network error
*/
public static void main(String[] args) throws InterruptedException, SocketException {
DiscoveryService discoveryService =
new DiscoveryService(
Optional.ofNullable(System.getenv("HOSTNAME")).orElse(UUID.randomUUID().toString()),

final int port = 8888;
var serviceConfig =
new UdpServiceConfig(
"xService",
new UdpConnection(8888));
Optional.ofNullable(System.getenv("HOSTNAME")).orElse(UUID.randomUUID().toString()),
port);
var discoveryService = new UdpDiscoveryService(serviceConfig);

discoveryService.setDaemon(true);
discoveryService.start();
var clientConfig = new UdpClientConfig("xService", port);
var discoveryClient = new UdpDiscoveryClient(clientConfig);

var config = new UdpDiscoveryClient.UdpConfig("xService", 8888);
var discoveryClient = new UdpDiscoveryClient(config);
var serviceListener = discoveryService.listen(new UdpConnection(port)).subscribe();

discoveryClient
.startLookup(Duration.ofSeconds(2))
.doOnNext(
instances -> {
String joined = instances.stream().sorted().collect(Collectors.joining("\n"));
var clientListener =
discoveryClient
.startLookup(Duration.ofSeconds(2))
.doOnNext(
instances -> {
String joined = instances.stream().sorted().collect(Collectors.joining("\n"));

System.out.println("***********************************************");
System.out.println(config.serviceName + " instances are discovered:\n\n" + joined);
System.out.println("***********************************************\n");
})
.doOnError(
throwable -> {
logger.severe("Lookup Error: " + throwable.getMessage());
logger.throwing(ExampleService.class.getName(), "main", throwable);
})
.onErrorComplete(throwable -> !(throwable instanceof InterruptedException))
.subscribe();
System.out.println("***********************************************");
System.out.println(
clientConfig.serviceName + " instances are discovered:\n\n" + joined);
System.out.println("***********************************************\n");
})
.doOnError(
throwable -> {
logger.severe("Lookup Error: " + throwable.getMessage());
logger.throwing(ExampleService.class.getName(), "main", throwable);
})
.onErrorComplete(throwable -> !(throwable instanceof InterruptedException))
.subscribe();

Thread.sleep(10000);

discoveryService.shutdown();
clientListener.dispose();
serviceListener.dispose();
}

static Logger logger = Logger.getLogger(ExampleService.class.getName());
Expand Down
34 changes: 17 additions & 17 deletions lcloud-udp-discovery/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'org.mtbo.lcloud'
version = '3.0.0-SNAPSHOT'
version = '3.0.1-SNAPSHOT'

repositories {
mavenCentral()
Expand All @@ -27,24 +27,24 @@ java {

publishing {
repositories {
maven {
url = layout.buildDirectory.dir('staging-deploy')
}

// maven {
// name = "GitHubPackages"
// url = uri("https://maven.pkg.github.com/mtbo-org/lcloud")
// credentials {
// username = findProperty("gpr.user") ?: System.getenv("USERNAME")
// password = findProperty("gpr.token") ?: System.getenv("TOKEN")
// }
//
//// def releasesRepoUrl = layout.buildDirectory.dir('repos/releases')
//// def snapshotsRepoUrl = layout.buildDirectory.dir('repos/snapshots')
//// url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
//
// uri('https://maven.pkg.github.com/mtbo-org/lcloud')
// url = layout.buildDirectory.dir('staging-deploy')
// }

maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/mtbo-org/lcloud")
credentials {
username = findProperty("gpr.user") ?: System.getenv("USERNAME")
password = findProperty("gpr.token") ?: System.getenv("TOKEN")
}

// def releasesRepoUrl = layout.buildDirectory.dir('repos/releases')
// def snapshotsRepoUrl = layout.buildDirectory.dir('repos/snapshots')
// url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

uri('https://maven.pkg.github.com/mtbo-org/lcloud')
}
}

publications {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* (C) 2025 Vladimir E. Koltunov (mtbo.org) */

package org.mtbo.lcloud.discovery;

import java.util.Objects;

/** Discovery config */
public abstract class ClientConfig {
/** unique service identifier */
public final String serviceName;

/** optional clients buffer size, {@link Integer#MAX_VALUE} for unlimited */
public final int endpointsCount;

/**
* @param serviceName unique service identifier
* @param endpointsCount optional clients buffer size, {@link Integer#MAX_VALUE} for unlimited
*/
public ClientConfig(String serviceName, int endpointsCount) {
this.serviceName = serviceName;
this.endpointsCount = endpointsCount;
}

/**
* Constructor with defaults
*
* @param serviceName unique service identifier
*/
public ClientConfig(String serviceName) {
this(serviceName, Integer.MAX_VALUE);
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (ClientConfig) obj;
return Objects.equals(this.serviceName, that.serviceName)
&& this.endpointsCount == that.endpointsCount;
}

@Override
public int hashCode() {
return Objects.hash(serviceName, endpointsCount);
}

@Override
public String toString() {
return "Config["
+ "serviceName="
+ serviceName
+ ", "
+ "port="
+ ", "
+ "clientsCount="
+ endpointsCount
+ ']';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,23 @@

package org.mtbo.lcloud.discovery;

import reactor.core.publisher.Mono;

/** Allows to send and receive packets */
public interface Connection extends Listener, Sender {}
public interface Connection<SocketType, PacketType>
extends Listener<SocketType, PacketType>, Sender<SocketType, PacketType> {

/**
* Create unbounded socket
*
* @return created socket
*/
Mono<SocketType> socket();

/**
* Close socket. Shadow exception.
*
* @param socket to close
*/
void close(SocketType socket);
}
Loading
Loading