You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/11/22 05:23:51 UTC

[incubator-tuweni] branch master updated: Make DNS Daemon API easier to use in Java

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/master by this push:
     new 9423019  Make DNS Daemon API easier to use in Java
     new a700813  Merge pull request #169 from atoulme/nicer_dns_daemon_api
9423019 is described below

commit 94230196e29eca77972d96e4551d751669cd2142
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Sat Nov 21 21:11:37 2020 -0800

    Make DNS Daemon API easier to use in Java
---
 .../org/apache/tuweni/discovery/DNSDaemon.kt       | 17 +++++++++---
 .../apache/tuweni/discovery/DNSDaemonListener.kt   | 31 ++++++++++++++++++++++
 .../org/apache/tuweni/ethclient/DNSClient.kt       | 18 ++++++++-----
 3 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSDaemon.kt b/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSDaemon.kt
index 74a28a7..98e4fd7 100644
--- a/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSDaemon.kt
+++ b/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSDaemon.kt
@@ -26,7 +26,7 @@ import java.util.TimerTask
  * Resolves DNS records over time, refreshing records.
  *
  * @param enrLink the ENR link to start with, of the form enrtree://PUBKEY@domain
- * @param listeners Listeners notified when records are read and whenever they are updated.
+ * @param listener Listener notified when records are read and whenever they are updated.
  * @param dnsServer the DNS server to use for DNS query. If null, the default DNS server will be used.
  * @param seq the sequence number of the root record. If the root record seq is higher, proceed with visit.
  * @param period the period at which to poll DNS records
@@ -34,20 +34,29 @@ import java.util.TimerTask
  */
 class DNSDaemon @JvmOverloads constructor(
   private val enrLink: String,
-  val listeners: Set<(Long, List<EthereumNodeRecord>) -> Unit> = HashSet(),
+  private val listener: DNSDaemonListener?,
   private val seq: Long = 0,
   private val period: Long = 60000L,
   private val dnsServer: String? = null,
   private val resolver: Resolver = SimpleResolver(dnsServer)
 ) {
+
+  val listeners = HashSet<DNSDaemonListener>()
+
   private val timer: Timer = Timer(false)
 
   init {
-    timer.scheduleAtFixedRate(DNSTimerTask(resolver, seq, enrLink, this::updateRecords), 0, period)
+    listener?.let {
+      listeners.add(listener)
+    }
   }
 
   private fun updateRecords(records: List<EthereumNodeRecord>) {
-    listeners.forEach { it(seq, records) }
+    listeners.forEach { it.newRecords(seq, records) }
+  }
+
+  public fun start() {
+    timer.scheduleAtFixedRate(DNSTimerTask(resolver, seq, enrLink, this::updateRecords), 0, period)
   }
 
   /**
diff --git a/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSDaemonListener.kt b/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSDaemonListener.kt
index ffe31d6..f1f5f89 100644
--- a/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSDaemonListener.kt
+++ b/dns-discovery/src/main/kotlin/org/apache/tuweni/discovery/DNSDaemonListener.kt
@@ -1,2 +1,33 @@
+/*
+ * 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.discovery
 
+import org.apache.tuweni.devp2p.EthereumNodeRecord
+
+/**
+ * Callback listening to updates of the DNS records.
+ */
+@FunctionalInterface
+interface DNSDaemonListener {
+
+  /**
+   * Callback called when the seq is updated on the DNS server
+   * @param seq the update identifier of the records
+   * @param records the records stored on the server
+   */
+  fun newRecords(seq: Long, records: List<EthereumNodeRecord>)
+}
diff --git a/eth-client/src/main/kotlin/org/apache/tuweni/ethclient/DNSClient.kt b/eth-client/src/main/kotlin/org/apache/tuweni/ethclient/DNSClient.kt
index 58bf5ae..be9461c 100644
--- a/eth-client/src/main/kotlin/org/apache/tuweni/ethclient/DNSClient.kt
+++ b/eth-client/src/main/kotlin/org/apache/tuweni/ethclient/DNSClient.kt
@@ -17,7 +17,9 @@
 package org.apache.tuweni.ethclient
 
 import kotlinx.coroutines.runBlocking
+import org.apache.tuweni.devp2p.EthereumNodeRecord
 import org.apache.tuweni.discovery.DNSDaemon
+import org.apache.tuweni.discovery.DNSDaemonListener
 import org.apache.tuweni.kv.KeyValueStore
 import org.apache.tuweni.peer.repository.PeerRepository
 
@@ -55,19 +57,23 @@ class DNSClient(
    */
   suspend fun start() {
     config.domain().let { domain ->
-      dnsDaemon = DNSDaemon(
+      val daemon = DNSDaemon(
         seq = seq(),
         enrLink = domain,
         period = config.pollingPeriod(),
-        listeners = setOf { seq, enrs ->
-          runBlocking {
-            seq(seq)
-            enrs.map {
-              peerRepository.storeIdentity(it.ip().hostAddress, it.tcp()!!, it.publicKey())
+        listener = object : DNSDaemonListener {
+          override fun newRecords(seq: Long, records: List<EthereumNodeRecord>) {
+            runBlocking {
+              seq(seq)
+              records.map {
+                peerRepository.storeIdentity(it.ip().hostAddress, it.tcp()!!, it.publicKey())
+              }
             }
           }
         }
       )
+      daemon.start()
+      dnsDaemon = daemon
     }
   }
 


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