/*
|
|
* 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;
|
|
}
|
|
|
|
P2PServer::P2PServer(string hostname, string ip, string mask) : Node(hostname, ip, mask)
|
|
{
|
|
this->seedIp = "";
|
|
this->peerIp = "";
|
|
this->seedPort = 0;
|
|
this->peerPort = 0;
|
|
}
|
|
|
|
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(1);
|
|
|
|
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("");
|
|
|
|
ss << "Sending \"" << pp.getMsg() << "\" to " << pp.getDstIp() << ":" << pp.getDstPort();
|
|
this->print(ss.str());
|
|
this->send(pp);
|
|
sleep(1);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
#ifndef DEBUG
|
|
this->print("onRecv() sleeping...");
|
|
#endif
|
|
sleep(1);
|
|
}
|
|
}
|
|
}
|