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.Server
|
|
|
|
import dorkbox.network.ServerConfiguration
|
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
|
2022-05-25 00:47:36 +02:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2020-07-03 01:45:18 +02:00
|
|
|
import org.slf4j.LoggerFactory
|
|
|
|
import sun.misc.Unsafe
|
|
|
|
import java.lang.reflect.Field
|
|
|
|
|
|
|
|
object AeronServer {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Throws(Exception::class)
|
|
|
|
@JvmStatic
|
|
|
|
fun main(args: Array<String>) {
|
|
|
|
val configuration = ServerConfiguration()
|
2021-08-23 08:39:55 +02:00
|
|
|
configuration.settingsStore = Storage.Memory() // don't want to persist anything on disk!
|
2022-08-20 11:10:19 +02:00
|
|
|
configuration.listenIpAddress = "*"
|
|
|
|
// configuration.listenIpAddress = "127.0.0.1"
|
2022-08-02 12:11:36 +02:00
|
|
|
configuration.maxClientCount = 50
|
|
|
|
|
2022-08-20 11:10:19 +02:00
|
|
|
// configuration.enableIpc = true
|
2022-08-04 03:39:14 +02:00
|
|
|
configuration.enableIpc = false
|
2022-08-20 11:10:19 +02:00
|
|
|
configuration.enableIPv4 = true
|
|
|
|
configuration.enableIPv6 = true
|
2022-08-02 12:11:36 +02:00
|
|
|
|
|
|
|
configuration.maxConnectionsPerIpAddress = 50
|
2020-07-03 01:45:18 +02:00
|
|
|
|
|
|
|
val server: Server<*> = Server<Connection>(configuration)
|
|
|
|
|
2022-07-14 04:54:50 +02:00
|
|
|
// we must always make sure that aeron is shut-down before starting again.
|
2023-03-17 15:05:59 +01:00
|
|
|
runBlocking {
|
|
|
|
if (!server.ensureStopped()) {
|
|
|
|
throw IllegalStateException("Aeron was unable to shut down in a timely manner.")
|
|
|
|
}
|
2022-07-14 04:54:50 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.filter {
|
|
|
|
println("should the connection $this be allowed?")
|
2020-07-03 01:45:18 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-08-04 03:39:14 +02:00
|
|
|
server.onInit {
|
|
|
|
logger.error("initialized")
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onConnect {
|
2022-08-04 03:39:14 +02:00
|
|
|
logger.error("connected: $this")
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onDisconnect {
|
2022-08-04 03:39:14 +02:00
|
|
|
logger.error("disconnect: $this")
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 00:47:36 +02:00
|
|
|
server.onErrorGlobal { throwable ->
|
2022-08-04 03:39:14 +02:00
|
|
|
server.logger.error("from test: has error")
|
2020-07-03 01:45:18 +02:00
|
|
|
throwable.printStackTrace()
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onError { throwable ->
|
2022-08-04 03:39:14 +02:00
|
|
|
logger.error("from test: has connection error: $this")
|
2020-07-03 01:45:18 +02:00
|
|
|
throwable.printStackTrace()
|
|
|
|
}
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onMessage<String> { message ->
|
2022-08-04 03:39:14 +02:00
|
|
|
logger.error("got message! $message")
|
2021-04-30 22:39:36 +02:00
|
|
|
send("ECHO $message")
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-06-26 19:28:55 +02:00
|
|
|
server.bind(2000)
|
2020-07-03 01:45:18 +02:00
|
|
|
|
2022-05-25 00:47:36 +02:00
|
|
|
runBlocking {
|
|
|
|
server.waitForClose()
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|