EXPERIMENTAL: custom peer collector
authorCryptoManiac <balthazar@yandex.ru>
Tue, 22 Mar 2016 21:43:45 +0000 (00:43 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Tue, 22 Mar 2016 21:43:45 +0000 (00:43 +0300)
You can specify a custom peer collector using syntax like -peercollector=<cmd>. Sample collector can be found in the contriob/seeds/getseeds.py file.

novacoin-qt.pro
src/init.cpp
src/ipcollector.cpp [new file with mode: 0644]
src/ipcollector.h [new file with mode: 0644]
src/makefile.bsd
src/makefile.linux-mingw
src/makefile.mingw
src/makefile.osx
src/makefile.unix
src/net.h

index e005090..4bdce2a 100644 (file)
@@ -271,7 +271,8 @@ HEADERS += src/qt/bitcoingui.h \
     src/qt/multisigdialog.h \
     src/qt/secondauthdialog.h \
     src/qt/qrcodedialog.h \
-    src/ies.h
+    src/ies.h \
+    src/ipcollector.h
 
 SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
     src/qt/intro.cpp \
@@ -353,7 +354,8 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
     src/qt/qrcodedialog.cpp \
     src/base58.cpp \
     src/cryptogram.cpp \
-    src/ecies.cpp
+    src/ecies.cpp \
+    src/ipcollector.cpp
 
 RESOURCES += \
     src/qt/bitcoin.qrc
index 994c784..a636b6e 100644 (file)
@@ -8,6 +8,7 @@
 #include "net.h"
 #include "init.h"
 #include "util.h"
+#include "ipcollector.h"
 #include "ui_interface.h"
 #include "checkpoints.h"
 #include <boost/format.hpp>
@@ -292,6 +293,7 @@ std::string HelpMessage()
         "  -rpcconnect=<ip>       " + _("Send commands to node running on <ip> (default: 127.0.0.1)") + "\n" +
         "  -blocknotify=<cmd>     " + _("Execute command when the best block changes (%s in cmd is replaced by block hash)") + "\n" +
         "  -walletnotify=<cmd>    " + _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)") + "\n" +
+        "  -peercollector=<cmd>     " + _("Execute command to collect peer addresses") + "\n" +
         "  -confchange            " + _("Require a confirmations for change (default: 0)") + "\n" +
         "  -upgradewallet         " + _("Upgrade wallet to latest format") + "\n" +
         "  -keypool=<n>           " + _("Set key pool size to <n> (default: 100)") + "\n" +
@@ -997,7 +999,11 @@ bool AppInit2()
     if (fServer)
         NewThread(ThreadRPCServer, NULL);
 
-    // ********************************************************* Step 12: finished
+    // ********************************************************* Step 13: IP collection thread
+    strCollectorCommand = GetArg("-peercollector", "");
+    if (strCollectorCommand != "")
+        NewThread(ThreadIPCollector, NULL);
+    // ********************************************************* Step 14: finished
 
     uiInterface.InitMessage(_("Done loading"));
     printf("Done loading\n");
diff --git a/src/ipcollector.cpp b/src/ipcollector.cpp
new file mode 100644 (file)
index 0000000..f50d098
--- /dev/null
@@ -0,0 +1,70 @@
+#include <string>
+#include <iostream>
+#include <cstdio>
+
+#include "net.h"
+#include "ui_interface.h"
+
+#ifdef WIN32
+#define popen    _popen
+#define pclose   _pclose
+#else
+#endif
+
+std::string strCollectorCommand;
+
+std::string exec(const char* cmd) {
+    std::string result = "";
+    char buffer[128];
+    FILE *fp;
+
+    fp = popen(cmd, "r");
+    while (fgets(buffer, sizeof(buffer), fp) != NULL) {
+        result += buffer;
+    }
+    pclose(fp);
+    return result;
+}
+
+bool AddPeer(std::string &strIpAddr) {
+    LOCK(cs_vAddedNodes);
+    std::vector<std::string>::iterator it = vAddedNodes.begin();
+    for(; it != vAddedNodes.end(); it++) {
+        if (strIpAddr == *it) break;
+    }
+    if (it != vAddedNodes.end())
+        return false;
+
+    vAddedNodes.push_back(strIpAddr);
+    return true;
+}
+
+
+void ThreadIPCollector(void* parg) {
+    printf("ThreadIPCollector started\n");
+    vnThreadsRunning[THREAD_IPCOLLECTOR]++;
+
+    while(!fShutdown) {
+        if (!fServer) {
+            // If RPC server is enabled then we don't have to parse anything.
+            std::string strCollectorOutput = exec(strCollectorCommand.c_str());
+            printf("Peer collector output: %s\n", strCollectorOutput.c_str());
+        } else {
+            // Otherwise, there is a work to be done.
+            std::string strCollectorOutput = exec((strCollectorCommand + " norpc").c_str());
+            std::istringstream collectorStream(strCollectorOutput);
+
+            std::string strIpAddr;
+            while (std::getline(collectorStream, strIpAddr)) {
+                AddPeer(strIpAddr);
+            }
+        }
+
+        int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours.
+        for (int i = 0; i < nSleepHours * 3600 && !fShutdown; i++)
+            Sleep(1000);
+    }
+
+    printf("ThreadIPCollector stopped\n");
+    vnThreadsRunning[THREAD_IPCOLLECTOR]--;
+}
diff --git a/src/ipcollector.h b/src/ipcollector.h
new file mode 100644 (file)
index 0000000..e16279f
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef IPCOLLECTOR_H
+#define IPCOLLECTOR_H
+
+extern std::string strCollectorCommand;
+extern void ThreadIPCollector(void* parg);
+
+#endif
\ No newline at end of file
index b31e457..c7a2f24 100644 (file)
@@ -137,7 +137,8 @@ OBJS= \
     obj/kernel.o \
     obj/kernel_worker.o \
     obj/ecies.o \
-    obj/cryptogram.o
+    obj/cryptogram.o \
+    obj/ipcollector.o
 
 all: novacoind
 
index 12ed6d8..2837218 100644 (file)
@@ -101,7 +101,8 @@ OBJS= \
     obj/kernel.o \
     obj/kernel_worker.o \
     obj/ecies.o \
-    obj/cryptogram.o
+    obj/cryptogram.o \
+    obj/ipcollector.o
 
 all: novacoind.exe
 
index 368ca62..c83e234 100644 (file)
@@ -91,7 +91,8 @@ OBJS= \
     obj/kernel.o \
     obj/kernel_worker.o \
     obj/ecies.o \
-    obj/cryptogram.o
+    obj/cryptogram.o \
+    obj/ipcollector.o
 
 all: novacoind.exe
 
index 0fc1052..e0a00c8 100644 (file)
@@ -99,7 +99,8 @@ OBJS= \
     obj/kernel.o \
     obj/kernel_worker.o \
     obj/ecies.o \
-    obj/cryptogram.o
+    obj/cryptogram.o \
+    obj/ipcollector.o
 
 ifneq (${USE_IPV6}, -)
        DEFS += -DUSE_IPV6=$(USE_IPV6)
index f4da324..69749c2 100644 (file)
@@ -138,7 +138,8 @@ OBJS= \
     obj/kernel.o \
     obj/kernel_worker.o \
     obj/ecies.o \
-    obj/cryptogram.o
+    obj/cryptogram.o \
+    obj/ipcollector.o
 
 all: novacoind
 
index a98a416..8569d23 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -112,6 +112,7 @@ enum threadId
     THREAD_MINTER,
     THREAD_SCRIPTCHECK,
     THREAD_NTP,
+    THREAD_IPCOLLECTOR,
 
     THREAD_MAX
 };