Przeglądaj źródła

add event listener

herlanS_ 3 lat temu
rodzic
commit
8ea303b52f

+ 54 - 13
src/main/kotlin/co/id/datacomsolusindo/ipphonebridge/IpPhoneBridgeApplication.kt

@@ -16,9 +16,12 @@ import org.ini4j.Wini
 import org.springframework.boot.SpringApplication
 import org.springframework.boot.autoconfigure.SpringBootApplication
 import org.springframework.context.event.EventListener
+import org.springframework.messaging.simp.stomp.StompHeaderAccessor
 import org.springframework.messaging.support.GenericMessage
 import org.springframework.scheduling.annotation.EnableScheduling
 import org.springframework.stereotype.Component
+import org.springframework.web.socket.messaging.SessionConnectEvent
+import org.springframework.web.socket.messaging.SessionDisconnectEvent
 import org.springframework.web.socket.messaging.SessionSubscribeEvent
 import java.io.File
 import java.io.Serializable
@@ -53,12 +56,14 @@ fun main(args: Array<String>) {
     sApp.setDefaultProperties(properties)
     sApp.run(*args)
     LogManager.getLogger("co.id.datacomsolusindo.ipphonebridge.IpPhoneBridgeApplication").info("Bridge Start")
+    val hostName = System.getenv("HOSTNAME")
+    println("hostname $hostName")
 }
 
 class Client(
-        val number: String,
-        var connectAt: String,
-        var lastRequest: String
+    val number: String,
+    var connectAt: String,
+    var lastRequest: String
 ) : Serializable {
     var reqSuccess = 0
     var reqFailed = 0
@@ -91,7 +96,8 @@ object ClientHolder {
                     mutexSucReq.withLock {
                         val cl = clientMap[clNum]
                         if (cl != null) {
-                            cl.lastRequest = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
+                            cl.lastRequest =
+                                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
                             cl.reqFailed += 1
                         }
                     }
@@ -108,8 +114,10 @@ object ClientHolder {
                     mutexSucReq.withLock {
                         val cl = clientMap[clNum]
                         if (cl != null) {
-                            cl.lastRequest = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
-                            cl.avgReqSuccessTime = ((cl.avgReqSuccessTime * cl.reqSuccess) + duration) / (cl.reqSuccess + 1)
+                            cl.lastRequest =
+                                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
+                            cl.avgReqSuccessTime =
+                                ((cl.avgReqSuccessTime * cl.reqSuccess) + duration) / (cl.reqSuccess + 1)
                             cl.reqSuccess += 1
                         }
                     }
@@ -130,9 +138,10 @@ class WebSocketEventListener {
         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)
+            "MarkerFilter",
+            Filter.Result.ACCEPT,
+            Filter.Result.DENY
+        )
         flow.addAttribute("marker", "FLOW")
         console.add(flow)
         val standard = builder.newLayout("PatternLayout")
@@ -164,7 +173,10 @@ class WebSocketEventListener {
         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.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)
@@ -179,18 +191,47 @@ class WebSocketEventListener {
         Configurator.reconfigure(builder.build())
     }
 
-    @EventListener(SessionSubscribeEvent::class)
+    //    @EventListener
+//    private void handleSessionConnected(SessionConnectEvent event) {
+//        ...
+//    }
+//
+//    @EventListener
+//    private void handleSessionDisconnect(SessionDisconnectEvent event) {
+//        ...
+//    }
+    @EventListener
+    fun onConnect(event: SessionConnectEvent) {
+        val accessor = StompHeaderAccessor.wrap(event.message)
+        val sessionId = accessor.sessionId
+        println("connect with session id $sessionId")
+
+    }
+
+    @EventListener
+    fun onDisconnect(event: SessionDisconnectEvent) {
+        println("disconnect with session id ${event.sessionId}")
+    }
+
+    @EventListener
     fun handleSessionSubscribeEvent(event: SessionSubscribeEvent) {
         val message = event.message as GenericMessage<*>
         val simDestination = message.headers["simpDestination"] as String?
+        val accessor = StompHeaderAccessor.wrap(event.message)
+        val sessionId = accessor.sessionId
+        println("subscribe to $simDestination with session id $sessionId")
+
+
         if (!(simDestination!!.startsWith("/topic/healthCheck") || simDestination.startsWith("/topic/notification"))) {
             // do stuff
             val clNum = simDestination.split("/")[3]
-            ClientHolder.put(clNum, Client(
+            ClientHolder.put(
+                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"))
-            ))
+                )
+            )
 
             LogManager.getLogger(this.javaClass).info("clientConnected $simDestination")
             logBuilder(clNum)