You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tuweni.apache.org by GitBox <gi...@apache.org> on 2021/05/07 03:46:22 UTC

[GitHub] [incubator-tuweni] atoulme commented on a change in pull request #232: move ethstats module to use kotlin

atoulme commented on a change in pull request #232:
URL: https://github.com/apache/incubator-tuweni/pull/232#discussion_r627904372



##########
File path: ethstats/src/main/kotlin/org/apache/tuweni/ethstats/EthStatsServer.kt
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.tuweni.ethstats
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.databind.node.ArrayNode
+import io.vertx.core.Vertx
+import io.vertx.core.http.HttpServer
+import io.vertx.core.http.ServerWebSocket
+import io.vertx.core.net.SocketAddress
+import io.vertx.kotlin.core.http.closeAwait
+import io.vertx.kotlin.core.http.listenAwait
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import org.apache.tuweni.eth.EthJsonModule
+import org.slf4j.LoggerFactory
+import java.io.IOException
+import java.time.Instant
+import java.util.concurrent.atomic.AtomicBoolean
+import kotlin.coroutines.CoroutineContext
+
+class EthStatsServer(
+  private val vertx: Vertx,
+  val networkInterface: String,
+  var port: Int,
+  val secret: String,
+  private val timeSupplier: () -> Instant,
+  private val nodeInfoReader: (String, NodeInfo) -> Unit,
+  private val latencyReader: (String, SocketAddress, Long) -> Unit,
+  private val pendingTxReader: (String, Long) -> Unit,
+  private val nodeStatsReader: (String, NodeStats) -> Unit,
+  private val disconnectionReader: (String) -> Unit,
+  private val blockReader: (String, BlockStats) -> Unit,
+  override val coroutineContext: CoroutineContext = Dispatchers.Default,
+) : CoroutineScope {
+
+  companion object {
+    val logger = LoggerFactory.getLogger(EthStatsServer::class.java)
+    val mapper = ObjectMapper()
+
+    init {
+      mapper.registerModule(EthJsonModule())
+    }
+  }
+
+  private var server: HttpServer? = null
+  private val started = AtomicBoolean(false)
+
+  suspend fun start() {
+    if (started.compareAndSet(false, true)) {
+      server = vertx.createHttpServer().webSocketHandler(this::connect).exceptionHandler {
+        logger.error("Exception occurred", it)
+      }.listenAwait(port, networkInterface)
+    }
+  }
+
+  suspend fun stop() {
+    if (started.compareAndSet(true, false)) {
+      server?.closeAwait()
+    }
+  }
+
+  fun connect(serverWebSocket: ServerWebSocket) {
+    logger.debug("New connection")
+    val websocket = serverWebSocket
+    websocket.accept()
+    websocket.exceptionHandler {
+      logger.debug("Error reading message", it)
+      websocket.close()
+    }
+    websocket.textMessageHandler {
+      try {
+        handleMessage(it, websocket)
+      } catch (e: IOException) {
+        logger.debug("Invalid payload", e)
+        websocket.close()
+      }
+    }
+  }
+
+  private fun handleMessage(message: String, websocket: ServerWebSocket) {
+    logger.debug("Received {}", message)
+    val event = mapper.readTree(message).get("emit") as ArrayNode
+    val command = event.get(0).textValue()
+    when (command) {
+      ("hello") -> {
+        val clientSecret = event.get(1).get("secret").textValue()
+        if (clientSecret != secret) {
+          logger.info("Client {} connected with wrong secret {}, disconnecting", websocket.remoteAddress(), clientSecret)
+          websocket.close()
+          return
+        }
+        websocket.writeTextMessage("{\"emit\":[\"ready\"]}")
+        val id = event.get(1).get("id").textValue()
+        val nodeInfo = mapper.readerFor(NodeInfo::class.java).readValue<NodeInfo>(event.get(1).get("info"))
+        nodeInfoReader(id, nodeInfo)
+      }
+      ("node-ping") -> {
+        logger.debug("Received a ping {}", event.get(1))
+        val clientTime = event.get(1).get("clientTime").longValue()
+        val payload = mapOf(Pair("clientTime", clientTime), Pair("serverTime", timeSupplier().toEpochMilli()))
+
+        val response = mapper.writer().writeValueAsString(mapOf(Pair("emit", listOf("node-pong", payload))))
+        EthStatsReporter.logger.debug("Sending {} message {}", command, message)
+        websocket.writeTextMessage(response)
+      }
+      ("latency") -> {
+        val id = event.get(1).get("id").textValue()
+        val latency = event.get(1).get("latency").longValue()
+        latencyReader(id, websocket.remoteAddress(), latency)
+      }
+      ("end") -> {
+        val id = event.get(1).get("id").textValue()
+        disconnectionReader(id)
+      }
+      ("stats") -> {
+        val id = event.get(1).get("id").textValue()
+        val nodeStats = mapper.readerFor(NodeStats::class.java).readValue<NodeStats>(event.get(1).get("stats"))
+        nodeStatsReader(id, nodeStats)
+      }
+      ("pending") -> {
+        val id = event.get(1).get("id").textValue()
+        val pendingTx = event.get(1).get("stats").get("pending").longValue()
+        pendingTxReader(id, pendingTx)
+      }
+      ("block") -> {
+        val id = event.get(1).get("id").textValue()
+        val block = mapper.readerFor(BlockStats::class.java).readValue<BlockStats>(event.get(1).get("block"))
+        blockReader(id, block)
+      }
+      ("update") -> {
+        // TODO implement

Review comment:
       if we throw an exception, it will bork the connection. We can add a handler for this behavior in a bit.
   
   For now just getting stats is great.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tuweni.apache.org
For additional commands, e-mail: dev-help@tuweni.apache.org