Added test jar and classes for aeron server/client
This commit is contained in:
parent
4af38ef212
commit
62ec43002b
@ -30,6 +30,8 @@ plugins {
|
|||||||
id("com.dorkbox.VersionUpdate") version "2.5"
|
id("com.dorkbox.VersionUpdate") version "2.5"
|
||||||
id("com.dorkbox.GradlePublish") version "1.12"
|
id("com.dorkbox.GradlePublish") version "1.12"
|
||||||
|
|
||||||
|
id("com.github.johnrengelman.shadow") version "7.1.2"
|
||||||
|
|
||||||
kotlin("jvm") version "1.6.10"
|
kotlin("jvm") version "1.6.10"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,6 +141,26 @@ tasks.jar.get().apply {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
val shadowJar: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar by tasks
|
||||||
|
shadowJar.apply {
|
||||||
|
manifest.inheritFrom(tasks.jar.get().manifest)
|
||||||
|
|
||||||
|
manifest.attributes.apply {
|
||||||
|
put("Main-Class", "dorkboxTest.network.AeronClientServer")
|
||||||
|
}
|
||||||
|
|
||||||
|
mergeServiceFiles()
|
||||||
|
|
||||||
|
duplicatesStrategy = DuplicatesStrategy.INCLUDE
|
||||||
|
|
||||||
|
from(sourceSets.test.get().output)
|
||||||
|
configurations = listOf(project.configurations.testRuntimeClasspath.get())
|
||||||
|
|
||||||
|
archiveBaseName.set(project.name + "-all")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api("org.jetbrains.kotlinx:atomicfu:0.17.3")
|
api("org.jetbrains.kotlinx:atomicfu:0.17.3")
|
||||||
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2")
|
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2")
|
||||||
|
@ -32,188 +32,139 @@ import dorkbox.storage.Storage
|
|||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import sun.misc.Unsafe
|
import sun.misc.Unsafe
|
||||||
import java.lang.reflect.Field
|
import java.lang.reflect.Field
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Suppress("UNUSED_ANONYMOUS_PARAMETER")
|
@Suppress("UNUSED_ANONYMOUS_PARAMETER")
|
||||||
object AeronClientServer {
|
class AeronClientServer {
|
||||||
|
companion object {
|
||||||
|
init {
|
||||||
|
try {
|
||||||
|
val theUnsafe = Unsafe::class.java.getDeclaredField("theUnsafe")
|
||||||
|
theUnsafe.isAccessible = true
|
||||||
|
val u = theUnsafe.get(null) as Unsafe
|
||||||
|
val cls = Class.forName("jdk.internal.module.IllegalAccessLogger")
|
||||||
|
val logger: Field = cls.getDeclaredField("logger")
|
||||||
|
u.putObjectVolatile(cls, u.staticFieldOffset(logger), null)
|
||||||
|
} catch (e: NoSuchFieldException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
} catch (e: IllegalAccessException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
} catch (e: ClassNotFoundException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
// assume SLF4J is bound to logback in the current environment
|
||||||
try {
|
val rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME) as Logger
|
||||||
val theUnsafe = Unsafe::class.java.getDeclaredField("theUnsafe")
|
val context = rootLogger.loggerContext
|
||||||
theUnsafe.isAccessible = true
|
val jc = JoranConfigurator()
|
||||||
val u = theUnsafe.get(null) as Unsafe
|
jc.context = context
|
||||||
val cls = Class.forName("jdk.internal.module.IllegalAccessLogger")
|
context.reset() // override default configuration
|
||||||
val logger: Field = cls.getDeclaredField("logger")
|
|
||||||
u.putObjectVolatile(cls, u.staticFieldOffset(logger), null)
|
|
||||||
} catch (e: NoSuchFieldException) {
|
|
||||||
e.printStackTrace()
|
|
||||||
} catch (e: IllegalAccessException) {
|
|
||||||
e.printStackTrace()
|
|
||||||
} catch (e: ClassNotFoundException) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
|
|
||||||
// assume SLF4J is bound to logback in the current environment
|
|
||||||
val rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME) as Logger
|
|
||||||
val context = rootLogger.loggerContext
|
|
||||||
val jc = JoranConfigurator()
|
|
||||||
jc.context = context
|
|
||||||
context.reset() // override default configuration
|
|
||||||
|
|
||||||
// rootLogger.setLevel(Level.OFF);
|
// rootLogger.setLevel(Level.OFF);
|
||||||
|
|
||||||
// rootLogger.setLevel(Level.INFO);
|
// rootLogger.setLevel(Level.INFO);
|
||||||
// rootLogger.level = Level.DEBUG
|
// rootLogger.level = Level.DEBUG
|
||||||
rootLogger.level = Level.TRACE
|
rootLogger.level = Level.TRACE
|
||||||
// rootLogger.setLevel(Level.ALL);
|
// rootLogger.setLevel(Level.ALL);
|
||||||
|
|
||||||
|
|
||||||
// we only want error messages
|
// we only want error messages
|
||||||
val nettyLogger = LoggerFactory.getLogger("io.netty") as Logger
|
val nettyLogger = LoggerFactory.getLogger("io.netty") as Logger
|
||||||
nettyLogger.level = Level.ERROR
|
nettyLogger.level = Level.ERROR
|
||||||
|
|
||||||
// we only want error messages
|
// we only want error messages
|
||||||
val kryoLogger = LoggerFactory.getLogger("com.esotericsoftware") as Logger
|
val kryoLogger = LoggerFactory.getLogger("com.esotericsoftware") as Logger
|
||||||
kryoLogger.level = Level.ERROR
|
kryoLogger.level = Level.ERROR
|
||||||
|
|
||||||
|
|
||||||
val encoder = PatternLayoutEncoder()
|
val encoder = PatternLayoutEncoder()
|
||||||
encoder.context = context
|
encoder.context = context
|
||||||
encoder.pattern = "%date{HH:mm:ss.SSS} %-5level [%logger{35}] %msg%n"
|
encoder.pattern = "%date{HH:mm:ss.SSS} %-5level [%logger{35}] %msg%n"
|
||||||
encoder.start()
|
encoder.start()
|
||||||
val consoleAppender = ConsoleAppender<ILoggingEvent>()
|
val consoleAppender = ConsoleAppender<ILoggingEvent>()
|
||||||
consoleAppender.context = context
|
consoleAppender.context = context
|
||||||
consoleAppender.encoder = encoder
|
consoleAppender.encoder = encoder
|
||||||
consoleAppender.start()
|
consoleAppender.start()
|
||||||
rootLogger.addAppender(consoleAppender)
|
rootLogger.addAppender(consoleAppender)
|
||||||
}
|
|
||||||
|
|
||||||
fun convertLongToTime(time: Long): String {
|
|
||||||
val date = Date(time)
|
|
||||||
val format = SimpleDateFormat("yyyy.MM.dd HH:mm:ss")
|
|
||||||
return format.format(date)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Command-line entry point.
|
|
||||||
*
|
|
||||||
* @param args Command-line arguments
|
|
||||||
*
|
|
||||||
* @throws Exception On any error
|
|
||||||
*/
|
|
||||||
@Throws(Exception::class)
|
|
||||||
@JvmStatic
|
|
||||||
fun main(args: Array<String>) {
|
|
||||||
run {
|
|
||||||
val configuration = ServerConfiguration()
|
|
||||||
configuration.settingsStore = Storage.Memory() // don't want to persist anything on disk!
|
|
||||||
configuration.listenIpAddress = "*"
|
|
||||||
configuration.port = 2000
|
|
||||||
configuration.maxClientCount = 50
|
|
||||||
|
|
||||||
configuration.enableIpc = false
|
|
||||||
// configuration.enableIPv4 = false
|
|
||||||
// configuration.enableIPv6 = false
|
|
||||||
|
|
||||||
configuration.maxConnectionsPerIpAddress = 50
|
|
||||||
|
|
||||||
val server: Server<*> = Server<Connection>(configuration)
|
|
||||||
|
|
||||||
// we must always make sure that aeron is shut-down before starting again.
|
|
||||||
while (server.isRunning()) {
|
|
||||||
server.logger.error("Aeron was still running. Waiting for it to stop...")
|
|
||||||
Thread.sleep(2000)
|
|
||||||
}
|
|
||||||
|
|
||||||
server.filter {
|
|
||||||
println("should the connection $this be allowed?")
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
server.onInit {
|
|
||||||
logger.error("initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
server.onConnect {
|
|
||||||
logger.error("connected: $this")
|
|
||||||
}
|
|
||||||
|
|
||||||
server.onDisconnect {
|
|
||||||
logger.error("disconnect: $this")
|
|
||||||
}
|
|
||||||
|
|
||||||
server.onErrorGlobal { throwable ->
|
|
||||||
server.logger.error("from test: has error")
|
|
||||||
throwable.printStackTrace()
|
|
||||||
}
|
|
||||||
|
|
||||||
server.onError { throwable ->
|
|
||||||
logger.error("from test: has connection error: $this")
|
|
||||||
throwable.printStackTrace()
|
|
||||||
}
|
|
||||||
|
|
||||||
server.onMessage<String> { message ->
|
|
||||||
logger.error("got message! $message")
|
|
||||||
send("ECHO $message")
|
|
||||||
}
|
|
||||||
|
|
||||||
server.bind()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
run {
|
/**
|
||||||
val configuration = ClientConfiguration()
|
* Command-line entry point.
|
||||||
configuration.settingsStore = Storage.Memory() // don't want to persist anything on disk!
|
*
|
||||||
configuration.port = 2000
|
* @param args Command-line arguments
|
||||||
|
*
|
||||||
|
* @throws Exception On any error
|
||||||
|
*/
|
||||||
|
@Throws(Exception::class)
|
||||||
|
@JvmStatic
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val acs = AeronClientServer()
|
||||||
|
|
||||||
configuration.enableIpc = false
|
if (args.contains("client")) {
|
||||||
|
acs.client("172.31.79.129")
|
||||||
|
} else if (args.contains("server")) {
|
||||||
|
val server = acs.server()
|
||||||
|
server.waitForClose()
|
||||||
|
} else {
|
||||||
|
acs.server()
|
||||||
|
acs.client("localhost")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun client(remoteAddress: String) {
|
||||||
|
val configuration = ClientConfiguration()
|
||||||
|
configuration.settingsStore = Storage.Memory() // don't want to persist anything on disk!
|
||||||
|
configuration.port = 2000
|
||||||
|
|
||||||
|
configuration.enableIpc = false
|
||||||
// configuration.enableIPv4 = false
|
// configuration.enableIPv4 = false
|
||||||
// configuration.enableIPv6 = false
|
configuration.enableIPv6 = false
|
||||||
|
|
||||||
// configuration.uniqueAeronDirectory = true
|
// configuration.uniqueAeronDirectory = true
|
||||||
|
|
||||||
val client = Client<Connection>(configuration)
|
val client = Client<Connection>(configuration)
|
||||||
|
|
||||||
client.filter(IpSubnetFilterRule(IPv4.LOCALHOST, 32))
|
client.filter(IpSubnetFilterRule(IPv4.LOCALHOST, 32))
|
||||||
|
|
||||||
client.filter {
|
client.filter {
|
||||||
println("should this connection be allowed?")
|
println("should this connection be allowed?")
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
client.onInit {
|
client.onInit {
|
||||||
logger.error("initialized")
|
logger.error("initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
client.onConnect {
|
client.onConnect {
|
||||||
logger.error("connected")
|
logger.error("connected")
|
||||||
send("HI THERE!")
|
send("HI THERE!")
|
||||||
}
|
}
|
||||||
|
|
||||||
client.onDisconnect {
|
client.onDisconnect {
|
||||||
logger.error("disconnect")
|
logger.error("disconnect")
|
||||||
}
|
}
|
||||||
|
|
||||||
client.onError { throwable ->
|
client.onError { throwable ->
|
||||||
logger.error("has error")
|
logger.error("has error")
|
||||||
throwable.printStackTrace()
|
throwable.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
client.onMessage<String> { message ->
|
client.onMessage<String> { message ->
|
||||||
logger.error("HAS MESSAGE! $message")
|
logger.error("HAS MESSAGE! $message")
|
||||||
}
|
}
|
||||||
|
|
||||||
client.connect("localhost") // UDP connection via loopback
|
client.connect(remoteAddress) // UDP connection via loopback
|
||||||
|
|
||||||
|
|
||||||
// different ones needed
|
// different ones needed
|
||||||
// send - reliable
|
// send - reliable
|
||||||
// send - unreliable
|
// send - unreliable
|
||||||
// send - priority (0-255 -- 255 is MAX priority) when sending, max is always sent immediately, then lower priority is sent if there is no backpressure from the MediaDriver.
|
// send - priority (0-255 -- 255 is MAX priority) when sending, max is always sent immediately, then lower priority is sent if there is no backpressure from the MediaDriver.
|
||||||
// send - IPC/local
|
// send - IPC/local
|
||||||
// runBlocking {
|
// runBlocking {
|
||||||
// while (!client.isShutdown()) {
|
// while (!client.isShutdown()) {
|
||||||
// client.send("ECHO " + java.lang.Long.toUnsignedString(client.crypto.secureRandom.nextLong(), 16))
|
// client.send("ECHO " + java.lang.Long.toUnsignedString(client.crypto.secureRandom.nextLong(), 16))
|
||||||
@ -221,20 +172,78 @@ object AeronClientServer {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
// connection needs to know
|
// connection needs to know
|
||||||
// is UDP or IPC
|
// is UDP or IPC
|
||||||
// host address
|
// host address
|
||||||
|
|
||||||
// RMI
|
// RMI
|
||||||
// client.get(5) -> gets from the server connection, if exists, then global.
|
// client.get(5) -> gets from the server connection, if exists, then global.
|
||||||
// on server, a connection local RMI object "uses" an id for global, so there will never be a conflict
|
// on server, a connection local RMI object "uses" an id for global, so there will never be a conflict
|
||||||
// using some tricks, we can make it so that it DOESN'T matter the order in which objects are created,
|
// using some tricks, we can make it so that it DOESN'T matter the order in which objects are created,
|
||||||
// and can specify, if we want, the object created.
|
// and can specify, if we want, the object created.
|
||||||
// Once created though, as NEW ONE with the same ID cannot be created until the old one is removed!
|
// Once created though, as NEW ONE with the same ID cannot be created until the old one is removed!
|
||||||
|
|
||||||
Thread.sleep(2000L)
|
Thread.sleep(2000L)
|
||||||
client.close()
|
client.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun server(): Server<Connection> {
|
||||||
|
val configuration = ServerConfiguration()
|
||||||
|
configuration.settingsStore = Storage.Memory() // don't want to persist anything on disk!
|
||||||
|
configuration.listenIpAddress = "*"
|
||||||
|
configuration.port = 2000
|
||||||
|
configuration.maxClientCount = 50
|
||||||
|
|
||||||
|
configuration.enableIpc = false
|
||||||
|
// configuration.enableIPv4 = false
|
||||||
|
configuration.enableIPv6 = false
|
||||||
|
|
||||||
|
configuration.maxConnectionsPerIpAddress = 50
|
||||||
|
|
||||||
|
val server: Server<*> = Server<Connection>(configuration)
|
||||||
|
|
||||||
|
// we must always make sure that aeron is shut-down before starting again.
|
||||||
|
while (server.isRunning()) {
|
||||||
|
server.logger.error("Aeron was still running. Waiting for it to stop...")
|
||||||
|
Thread.sleep(2000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server.filter {
|
||||||
|
println("should the connection $this be allowed?")
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
server.onInit {
|
||||||
|
logger.error("initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
server.onConnect {
|
||||||
|
logger.error("connected: $this")
|
||||||
|
}
|
||||||
|
|
||||||
|
server.onDisconnect {
|
||||||
|
logger.error("disconnect: $this")
|
||||||
|
}
|
||||||
|
|
||||||
|
server.onErrorGlobal { throwable ->
|
||||||
|
server.logger.error("from test: has error")
|
||||||
|
throwable.printStackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
|
server.onError { throwable ->
|
||||||
|
logger.error("from test: has connection error: $this")
|
||||||
|
throwable.printStackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
|
server.onMessage<String> { message ->
|
||||||
|
logger.error("got message! $message")
|
||||||
|
send("ECHO $message")
|
||||||
|
}
|
||||||
|
|
||||||
|
server.bind()
|
||||||
|
|
||||||
|
return server as Server<Connection>
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user