Skip to content

Commit

Permalink
a bit better on the networking forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Gunnar Lauterer committed May 19, 2023
1 parent c2d362b commit 8a85fca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Binary file modified bin/main
Binary file not shown.
17 changes: 13 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,25 @@ int main() {
}

//network test
std::cout << std::endl << std::endl << "Testing networking " << std::endl;

std::vector<std::string> keys5 = Encryption::generateKeypair();
Mesh mesh3;
Mesh::Node node5 = mesh3.addNode("127.0.0.1:8080", keys5[1]);
//path
std::vector<Mesh::Node> path3 = { node5, node5};
//message
Message message5("NULL", "Hello, world!");
message5 = message5.encode(path3);

//send a message from node1 to node1 using the networking class
Networking networking(node1.bindAddress(), node1.bindPort());
networking.startListeningThread();
std::cout << "Started Listening for messages..." << std::endl;

//encode the message but use the manual path instead of the mesh generated path as decoding with private keys only works if there is multiple nodes and we are simulating a mesh here
Message message3 = message1.encode(path2);
//send the message
networking.sendMessage(node1.bindAddress(), node1.bindPort(), message3.toString());

std::cout << "sending message..." << std::endl;
networking.sendMessage(node1.bindAddress(), node1.bindPort(), message5.toString());


return 0;
Expand Down
6 changes: 5 additions & 1 deletion src/networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ void Networking::listenForMessages() {

//parse the message to get the next address
Message message(receivedMessage);
//decrypt the message with the private key of this node to get the next address and content
message.decode(Encryption::getPrivateKey());
std::string nextAddress = message.getNextAddress();
int nextPort = message.getNextPort(message.getNextAddressFull());
if (nextAddress == ipAddress || message.isDestination()) {

if (message.isDestination()) {
std::cout << "Message has reached destination" << std::endl;
std::cout << "Message content: " << message.getContent() << std::endl;
} else {
std::cout << "Message is not for this node, sending to " << message.getNextAddressFull() << std::endl;
sendMessage(nextAddress, nextPort, message.getContent());
Expand Down

0 comments on commit 8a85fca

Please sign in to comment.