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