Merge pull request #295 from svost/patch
[novacoin.git] / src / ipcollector.cpp
1 // Copyright (c) 2012-2016 The Novacoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or https://opensource.org/licenses/mit-license.php.
4
5 #ifdef WIN32
6 #include <winsock2.h>
7 #define popen    _popen
8 #define pclose   _pclose
9 #endif
10
11 #include "net.h"
12
13 std::string strCollectorCommand;
14
15 std::string exec(const char* cmd) {
16     std::string result = "";
17     char buffer[128];
18     FILE *fp;
19
20     fp = popen(cmd, "r");
21     while (fgets(buffer, sizeof(buffer), fp) != NULL) {
22         result += buffer;
23     }
24     pclose(fp);
25     return result;
26 }
27
28 bool AddPeer(std::string &strIpAddr) {
29     LOCK(cs_vAddedNodes);
30     std::vector<std::string>::iterator it = vAddedNodes.begin();
31     for(; it != vAddedNodes.end(); it++) {
32         if (strIpAddr == *it) break;
33     }
34     if (it != vAddedNodes.end())
35         return false;
36
37     printf("Adding node %s\n", strIpAddr.c_str());
38     vAddedNodes.push_back(strIpAddr);
39
40     return true;
41 }
42
43 void ThreadIPCollector(void* parg) {
44     printf("ThreadIPCollector started\n");
45     vnThreadsRunning[THREAD_IPCOLLECTOR]++;
46
47     std::string strExecutableFilePath = "";
48 #ifdef MAC_OSX
49     size_t nameEnd = strCollectorCommand.rfind(".app");
50     if (nameEnd != std::string::npos) {
51         size_t nameBeginning = strCollectorCommand.rfind("/");
52         if (nameBeginning == std::string::npos)
53             nameBeginning = 0;
54
55         std::string strFileName = strCollectorCommand.substr(nameBeginning, nameEnd - nameBeginning);
56         strExecutableFilePath = strCollectorCommand + "/Contents/MacOS/" + strFileName;
57     }
58     else
59         strExecutableFilePath = strCollectorCommand;
60 #else
61         strExecutableFilePath = strCollectorCommand;
62 #endif
63
64     if (!strExecutableFilePath.empty())
65     {
66         while(!fShutdown) {
67             if (fServer) {
68                 // If RPC server is enabled then we don't have to parse anything.
69                 std::string strCollectorOutput = exec(strExecutableFilePath.c_str());
70                 printf("Peer collector output: %s\n", strCollectorOutput.c_str());
71             } else {
72                 // Otherwise, there is a work to be done.
73                 std::string strCollectorOutput = exec((strExecutableFilePath + " norpc").c_str());
74                 std::istringstream collectorStream(strCollectorOutput);
75
76                 std::string strIpAddr;
77                 while (std::getline(collectorStream, strIpAddr)) {
78                     AddPeer(strIpAddr);
79                 }
80             }
81
82             int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours.
83             for (int i = 0; i < nSleepHours * 3600 && !fShutdown; i++)
84                 Sleep(1000);
85         }
86     }
87
88     printf("ThreadIPCollector stopped\n");
89     vnThreadsRunning[THREAD_IPCOLLECTOR]--;
90 }