/** * @file P2PServer.cpp * * Created on: 17.01.2017 * @author Piotr Dergun */ #include "P2PServer.h" P2PServer::P2PServer() { this->seedIp = ""; this->peerIp = ""; this->seedPort = 0; this->peerPort = 0; this->sim = NULL; } P2PServer::P2PServer(string hostname, string ip, string mask) : Node(hostname, ip, mask) { this->seedIp = ""; this->peerIp = ""; this->seedPort = 0; this->peerPort = 0; this->sim = NULL; } P2PServer::~P2PServer() { } void P2PServer::onRecv() { stringstream ss; while (true) { ss.str(""); Packet p = this->recv(); if (p.getSrcPort() != 0) { ss << "onRecv() Received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort(); this->print(ss.str()); sleep(2); if (p.getMsg() == "getRemoteIPPort") { this->seedIp = p.getSrcIp(); this->seedPort = p.getSrcPort(); } else if (p.getMsg() == "helloDownload") { this->peerIp = p.getSrcIp(); this->peerPort = p.getSrcPort(); } if (this->seedPort != 0 && this->peerPort != 0) { ss.str(""); ss << "peerConnectionInfo:" << this->peerIp << ":" << this->peerPort; Packet pp(ss.str()); pp.setDstIp(this->seedIp); pp.setDstPort(this->seedPort); pp.setSrcPort(p.getDstPort()); ss.str(""); if (this->sim) this->sim->print("Sending to seed information about peer IP and port"); ss << "Sending \"" << pp.getMsg() << "\" to " << pp.getDstIp() << ":" << pp.getDstPort(); this->print(ss.str()); this->send(pp); sleep(3); } } else { #ifndef DEBUG this->print("onRecv() sleeping..."); #endif sleep(1); } } } const Simulation* P2PServer::getSim() { return sim; } void P2PServer::setSim(Simulation* sim) { this->sim = sim; }