Repository navigation
Expand file tree
/
Copy pathNodeCPP.cpp
More file actions
126 lines (88 loc) · 4.06 KB
/
Copy pathNodeCPP.cpp
File metadata and controls
126 lines (88 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// NodeCPP.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <boost/asio.hpp>
#include <thread>
#include "P2PServer.h"
#include "SeedFunctions.h"
#include "DataStructures.h"
using namespace boost::asio;
using ip::tcp;
std::string ProgramVersion = "0.4.2";
std::string Subversion = "Cb1";
std::string GetNodePresentation()
{
std::string UTCTime = GetUTCTimeString();
std::cout << "\nUTC Time: " << UTCTime << std::endl;
std::string presentation = "PSK " + GetNodePublicIP() + " " + ProgramVersion + Subversion + " " + UTCTime + "\n";
std::cout << "\nNode Presentation: " << presentation << std::endl;
return presentation;
}
int main() {
int DefaultSeedPort = 4040;
int DefaultNodePort = 4040;
int TestnetPort = 4040;
bool UseTestnet = "True";
//std::string ProgramVersion="0.4.2";
//std::string Subversion="Cb1";
// STEP 1: Get Seed IP Addresses, and save them to a SeedIPAddress Vector and a local file SeedIPAddresses.txt
if (UseTestnet)
{
DefaultSeedPort = TestnetPort;
}
std::vector<std::string> SeedIpAddresses = GetSeedIPAddresses();
SaveToTextFile(SeedIpAddresses, "SeedIPAddresses.txt");
std::cout << "Total Seed Nodes : " << SeedIpAddresses.size() << std::endl;
//Create a vector of Nodes with structure Node to save data , IP, Port, NodeStatus, MerkleTree. .. etc
std::vector<Node> SeedNodes;
std::vector<std::thread> threads;
for (int i = 0; i < SeedIpAddresses.size(); i++) {
// Crear un nuevo nodo con la dirección IP y el puerto predeterminados
Node SeedNode(SeedIpAddresses[i], DefaultSeedPort);
// Lanzar un hilo para llamar a SetNodeStatus para el nodo actual
threads.emplace_back([&SeedNode]() {
SeedNode.SetNodeStatus();
SeedNode.CalculateMerkle();
//SeedNodes.push_back(SeedNode);
//SeedNode.CalculateMerkle();
});
// Agregar el nodo al vector de nodos semilla
std::this_thread::sleep_for(std::chrono::milliseconds(500));
SeedNodes.push_back(SeedNode);
}
//Print Seed Nodes in table format
std::cout << std::left << std::setw(15) << "Node IP" << std::setw(10) << "Node Port" << std::setw(15) << "Node Merkle" << std::endl;
for (int i = 0; i < SeedNodes.size(); i++) {
SeedNodes[i].PrintNodeTable();
}
std::string UTCTime = GetUTCTimeString();
std::cout << "\nUTC Time: " << UTCTime << std::endl;
std::string NodePublicIP = GetNodePublicIP();
std::cout << "Public IP: " << NodePublicIP << std::endl;
std::string MyNodePresentation=GetNodePresentation();
std::string presentationResponse= SendStringToNode("38.242.252.234", DefaultSeedPort, MyNodePresentation);
std::cout << "Presentation Response: " << presentationResponse << std::endl;
//STEP2 Get Nodestatus from all Seed nodes, and verify all fields are equal on all seed nodes.
// GetSeedNodeConsensus. only if more more than 75% nodes are equal you can go on. Mínimum 3 seed nodes ??
//STEP3 Get all Nodes running NOSOCFG command
//std::string NOSOCFG_COMMAND = "NSLMNS\n";
std::string ping="$PING";
std::string pong = SendStringToNode(SeedIpAddresses[0], DefaultSeedPort, ping);
std::cout << "Pong: " << pong << std::endl;
std::string NOSOCFG_COMMAND = "NSLCFG\n";
std::string NodeList = SendStringToNode(SeedIpAddresses[0], DefaultSeedPort, NOSOCFG_COMMAND);
std::cout << "Node List: " << NodeList << std::endl;
//Print NodePresentation
//STEP 2: Select a random seed server from the SeedIPAddress Vector and get all Nodes IP
try {
boost::asio::io_service ioService;
unsigned short port = 12345; // Default Port, can be changed using argument
P2PServer server(ioService, port);
std::cout << "-Server listening on port " << port << std::endl;
ioService.run();
}
catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}