2020-08-19 15:29:35 +02:00
/ *
2023-03-17 15:00:00 +01:00
* Copyright 2023 dorkbox , llc
2020-08-19 15:29:35 +02:00
*
* Licensed under the Apache License , Version 2.0 ( the " License " ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
2020-08-12 23:38:56 +02:00
package dorkboxTest.network
2020-07-03 01:45:18 +02:00
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.joran.JoranConfigurator
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.ConsoleAppender
2020-08-12 23:38:56 +02:00
import dorkbox.network.Client
2022-07-29 04:52:11 +02:00
import dorkbox.network.ClientConfiguration
2020-07-03 01:45:18 +02:00
import dorkbox.network.connection.Connection
2021-08-23 08:39:55 +02:00
import dorkbox.storage.Storage
2020-07-03 01:45:18 +02:00
import org.slf4j.LoggerFactory
import sun.misc.Unsafe
import java.lang.reflect.Field
import java.text.SimpleDateFormat
import java.util.*
/ * *
*
* /
2020-09-25 14:51:35 +02:00
@Suppress ( " UNUSED_ANONYMOUS_PARAMETER " )
2020-07-03 01:45:18 +02:00
object AeronClient {
init {
2022-05-25 00:47:36 +02:00
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 ( )
}
2020-07-03 01:45:18 +02:00
// 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.INFO);
2022-07-29 04:52:11 +02:00
// rootLogger.level = Level.DEBUG
rootLogger . level = Level . TRACE
2020-07-03 01:45:18 +02:00
// rootLogger.setLevel(Level.ALL);
// we only want error messages
val nettyLogger = LoggerFactory . getLogger ( " io.netty " ) as Logger
nettyLogger . level = Level . ERROR
// we only want error messages
val kryoLogger = LoggerFactory . getLogger ( " com.esotericsoftware " ) as Logger
kryoLogger . level = Level . ERROR
val encoder = PatternLayoutEncoder ( )
encoder . context = context
encoder . pattern = " %date{HH:mm:ss.SSS} %-5level [%logger{35}] %msg%n "
encoder . start ( )
val consoleAppender = ConsoleAppender < ILoggingEvent > ( )
consoleAppender . context = context
consoleAppender . encoder = encoder
consoleAppender . start ( )
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 > ) {
2022-07-29 04:52:11 +02:00
val configuration = ClientConfiguration ( )
2021-08-23 08:39:55 +02:00
configuration . settingsStore = Storage . Memory ( ) // don't want to persist anything on disk!
2022-08-02 12:11:36 +02:00
2022-08-03 01:52:30 +02:00
configuration . enableIpc = false
2022-08-20 11:10:19 +02:00
configuration . enableIPv4 = true
configuration . enableIPv6 = false
2022-08-04 03:39:14 +02:00
configuration . uniqueAeronDirectory = true
2022-07-14 04:54:50 +02:00
2020-07-03 01:45:18 +02:00
val client = Client < Connection > ( configuration )
2022-08-04 03:39:14 +02:00
client . onInit {
logger . error ( " initialized " )
}
2021-04-30 22:39:36 +02:00
client . onConnect {
2022-08-04 03:39:14 +02:00
logger . error ( " connected " )
send ( " HI THERE! " )
2020-07-03 01:45:18 +02:00
}
2021-04-30 22:39:36 +02:00
client . onDisconnect {
2022-08-04 03:39:14 +02:00
logger . error ( " disconnect " )
2020-07-03 01:45:18 +02:00
}
2021-04-30 22:39:36 +02:00
client . onError { throwable ->
2022-08-04 03:39:14 +02:00
logger . error ( " has error " )
2020-07-03 01:45:18 +02:00
throwable . printStackTrace ( )
}
2021-04-30 22:39:36 +02:00
client . onMessage < String > { message ->
2022-08-04 03:39:14 +02:00
logger . error ( " HAS MESSAGE! $message " )
2020-07-03 01:45:18 +02:00
}
2023-06-26 19:28:55 +02:00
client . connect ( " 127.0.0.1 " , 2000 ) // UDP connection via loopback
2020-07-03 01:45:18 +02:00
// different ones needed
// send - reliable
// 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 - IPC/local
2020-09-02 03:18:10 +02:00
// runBlocking {
// while (!client.isShutdown()) {
// client.send("ECHO " + java.lang.Long.toUnsignedString(client.crypto.secureRandom.nextLong(), 16))
// }
// }
2020-07-03 01:45:18 +02:00
// connection needs to know
// is UDP or IPC
// host address
// RMI
// 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
// 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.
2022-08-04 03:39:14 +02:00
// Once created though, as NEW ONE with the same ID cannot be created until the old one is removed!
Thread . sleep ( 2000L )
2020-07-03 01:45:18 +02:00
client . close ( )
}
}