2 Commits 1094b6e983 ... df5df932c7

Autore SHA1 Messaggio Data
  herlanS_ df5df932c7 add resource not found for unregistered client 3 anni fa
  herlanS_ f57c30caf9 dynamic port xml setting 3 anni fa

+ 7 - 1
src/main/kotlin/co/id/datacomsolusindo/ipphonebridge/BridgeFIlter.kt

@@ -12,6 +12,7 @@ import org.springframework.http.HttpMethod
 import org.springframework.http.HttpStatus
 import org.springframework.messaging.simp.SimpMessagingTemplate
 import org.springframework.stereotype.Component
+import org.springframework.web.server.ResponseStatusException
 import org.springframework.web.servlet.config.annotation.EnableWebMvc
 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@@ -23,7 +24,6 @@ import java.net.InetAddress
 import java.time.Duration
 import java.time.LocalDateTime
 import java.util.*
-import java.util.Collections.synchronizedList
 import java.util.concurrent.ConcurrentHashMap
 import java.util.concurrent.ConcurrentMap
 import java.util.concurrent.TimeUnit
@@ -81,7 +81,13 @@ class BridgeFilter(private val template: SimpMessagingTemplate, val resourceLoad
             if (search != null && !search.endsWith("assets/bootstrap.js")) {
                 res.sendRedirect("/resources/$search")
             } else {
+                if(!ClientHolder.get().containsKey(clientNumber.toString())){
+                    res.status = HttpStatus.NOT_FOUND.value()
+                    res.writer.write("{\"message\":\"resource not found\"}")
+                    res.flushBuffer()
+                    return
 
+                }
                 LogManager.getLogger("client-$client")
                     .info("Request Start, UA: " + req.getHeader("User-Agent") + " From: " + InetAddress.getLocalHost().hostAddress + " To: " + req.requestURL.toString() + "?" + req.queryString)
 

+ 33 - 66
src/main/kotlin/co/id/datacomsolusindo/ipphonebridge/IpPhoneBridgeApplication.kt

@@ -30,6 +30,8 @@ import java.net.URI
 import java.time.LocalDateTime
 import java.time.format.DateTimeFormatter
 import java.util.*
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.ConcurrentMap
 import kotlin.system.exitProcess
 import kotlin.system.measureTimeMillis
 
@@ -74,7 +76,8 @@ fun main(args: Array<String>) {
 class Client(
     val number: String,
     var connectAt: String,
-    var lastRequest: String
+    var lastRequest: String,
+    val sessionId: String?
 ) : Serializable {
     var reqSuccess = 0
     var reqFailed = 0
@@ -83,7 +86,7 @@ class Client(
 
 object ClientHolder {
     private val mutexSucReq = Mutex()
-    private val clientMap: MutableMap<String, Client> = mutableMapOf()
+    private val clientMap: ConcurrentMap<String, Client> by lazy { ConcurrentHashMap() }
     private suspend fun massiveRun(action: suspend () -> Unit) {
         measureTimeMillis {
             coroutineScope { // sc
@@ -94,11 +97,17 @@ object ClientHolder {
         }
     }
 
+    fun removeBySessionId(sessionId: String?) {
+        clientMap.entries.filter { it.value.sessionId == sessionId }.forEach {
+            clientMap.remove(it.key)
+        }
+    }
+
     fun put(key: String, client: Client) {
-        clientMap[key] = client
+        clientMap.getOrPut(key) { client }
     }
 
-    fun get() = clientMap.toMap()
+    fun get() = clientMap
 
     fun addFailedRequest(clNum: String) {
         runBlocking {
@@ -142,69 +151,25 @@ object ClientHolder {
 class WebSocketEventListener {
     @EventListener(ApplicationReadyEvent::class)
     fun doSomethingAfterStartup() {
-        Configurator.reconfigure(URI("http://127.0.0.1:9090/log-setting.xml"))
+        logBuilder()
     }
 
-    fun logBuilder(clNum: String) {
-//        val builder = ConfigurationBuilderFactory.newConfigurationBuilder()
-//        val consoleAppender = builder.newAppender("stdout", "Console")
-//
-//        val appLog = builder.newAppender("appLog", "RollingFile")
-//        appLog.addAttribute("fileName", "log/app/app.log")
-//        appLog.addAttribute("filePattern", "log/app/\$\${date:yyyy - MM}/app-%d{yyyy-MM-dd}-%i.log.zip")
-//
-//        val flow: FilterComponentBuilder = builder.newFilter(
-//            "MarkerFilter",
-//            Filter.Result.ACCEPT,
-//            Filter.Result.DENY
-//        )
-//        flow.addAttribute("marker", "FLOW")
-//        consoleAppender.add(flow)
-//        val standard = builder.newLayout("PatternLayout")
-//        standard.addAttribute("pattern", "%d %p [%c{1}] %m%n")
-//        val policies = builder.newLayout("Policies")
-//        val timeBasedTriggeringPolicy = builder.newLayout("TimeBasedTriggeringPolicy")
-//        val sizeBasedTriggeringPolicy = builder.newLayout("SizeBasedTriggeringPolicy")
-//
-//        sizeBasedTriggeringPolicy.addAttribute("size", "10 MB")
-//        policies.addComponent(timeBasedTriggeringPolicy)
-//        policies.addComponent(sizeBasedTriggeringPolicy)
-//        consoleAppender.add(standard)
-//        builder.add(consoleAppender)
-//
-//        appLog.add(standard)
-//        appLog.addComponent(policies)
-//        builder.add(appLog)
-//
-//        val rootLogger: RootLoggerComponentBuilder = builder.newRootLogger(Level.INFO)
-//        rootLogger.add(builder.newAppenderRef("stdout"))
-//        builder.add(rootLogger)
-//
-//        val logger: LoggerComponentBuilder = builder.newLogger("co.id.datacomsolusindo.ipphonebridge", Level.DEBUG)
-//        logger.add(builder.newAppenderRef("appLog"))
-//        logger.addAttribute("additivity", false)
-//        builder.add(logger)
-//        rootLogger.add(builder.newAppenderRef("appLog"))
-//
-//        ClientHolder.get().entries.forEach {
-//            val clientLog = builder.newAppender("client-${it.key}", "RollingFile")
-//            clientLog.addAttribute("fileName", "log/${it.key}/client-${it.key}.log")
-//            clientLog.addAttribute(
-//                "filePattern",
-//                "log/${it.key}/\$\${date:yyyy - MM}/app-%d{yyyy-MM-dd}-%i.client-${it.key}.zip"
-//            )
-//            clientLog.add(standard)
-//            clientLog.addComponent(policies)
-//            builder.add(clientLog)
-//
-//            val loggerClient: LoggerComponentBuilder = builder.newLogger("client-${it.key}", Level.DEBUG)
-//            loggerClient.add(builder.newAppenderRef("client-${it.key}"))
-//            loggerClient.addAttribute("additivity", false)
-//            builder.add(loggerClient)
-//            rootLogger.add(builder.newAppenderRef("client-${it.key}"))
-//        }
+    fun logBuilder() {
+        val confFile = File("conf/general.ini")
+        var port = if (confFile.exists()) {
+            val ini = Wini(File("conf/general.ini"))
+            ini.get("server", "port")
+        } else {
+            System.getenv("PORT")
+        }
+        port = if (port == null) {
+//        println("undefined port")
+            "9090"
+        } else {
+            port
+        }
 
-        Configurator.reconfigure(URI("http://127.0.0.1:9090/log-setting.xml"))
+        Configurator.reconfigure(URI("http://127.0.0.1:$port/log-setting.xml"))
     }
 
     //    @EventListener
@@ -227,6 +192,7 @@ class WebSocketEventListener {
     @EventListener
     fun onDisconnect(event: SessionDisconnectEvent) {
         AppLog.write(this.javaClass).info("disconnect with session id ${event.sessionId}")
+        ClientHolder.removeBySessionId(event.sessionId)
     }
 
     @EventListener
@@ -245,12 +211,13 @@ class WebSocketEventListener {
                 clNum, Client(
                     clNum,
                     LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),
-                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
+                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),
+                    sessionId
                 )
             )
 
             LogManager.getLogger(this.javaClass).info("clientConnected $simDestination")
-            logBuilder(clNum)
+            logBuilder()
             LogManager.getLogger("client-$clNum").info("Client $clNum Connected")
         }
     }