/**
|
|
* @file Peer.cpp
|
|
*
|
|
* Created on: 17.01.2017
|
|
* @author Piotr Dergun
|
|
*/
|
|
|
|
#include "Peer.h"
|
|
|
|
Peer::Peer()
|
|
{
|
|
this->partId = 1;
|
|
this->srcPort = 0;
|
|
this->remotePort = 0;
|
|
this->sender = false;
|
|
this->sim = NULL;
|
|
}
|
|
|
|
Peer::Peer(string hostname, string ip, string mask, string gatewayIp) : Node(hostname, ip, mask, gatewayIp)
|
|
{
|
|
this->partId = 1;
|
|
this->srcPort = 0;
|
|
this->remotePort = 0;
|
|
this->sender = false;
|
|
this->sim = NULL;
|
|
}
|
|
|
|
void Peer::onRecv()
|
|
{
|
|
stringstream ss;
|
|
while (true)
|
|
{
|
|
ss.str("");
|
|
Packet p = this->recv();
|
|
|
|
if (p.getSrcPort() != 0)
|
|
{
|
|
// jezeli informacje o drugiej stronie
|
|
if (this->isSender() && this->remotePort == 0)
|
|
{
|
|
vector<string> x = split(p.getMsg(), ':');
|
|
this->remoteIp = x[1];
|
|
this->remotePort = atoi(x[2].c_str());
|
|
ss << "onRecv() REMOTE peer is " << this->remoteIp << ":" << this->remotePort;
|
|
this->print(ss.str());
|
|
sleep(1);
|
|
|
|
this->sendData();
|
|
}
|
|
else if (!this->isSender() && p.getMsg().find("data") != string::npos)
|
|
{
|
|
ss << "onRecv() Received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort();
|
|
this->print(ss.str());
|
|
sleep(1);
|
|
|
|
this->sendAck(p.getSrcIp(), p.getSrcPort());
|
|
}
|
|
else if (this->isSender() && p.getMsg().find("Ack") != string::npos)
|
|
{
|
|
ss << "onRecv() Received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort();
|
|
this->print(ss.str());
|
|
sleep(1);
|
|
|
|
this->sendData();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
#ifndef DEBUG
|
|
this->print("onRecv() sleeping...");
|
|
#endif
|
|
}
|
|
sleep(1);
|
|
}
|
|
}
|
|
|
|
void Peer::connectToServer(string serverIp, int serverPort)
|
|
{
|
|
stringstream ss;
|
|
ss << "Connecting to " << serverIp << ":" << serverPort;
|
|
|
|
Packet p;
|
|
if (this->isSender())
|
|
p.setMsg("getRemoteIPPort");
|
|
else
|
|
p.setMsg("helloDownload");
|
|
|
|
p.setDstIp(serverIp);
|
|
p.setDstPort(serverPort);
|
|
this->srcPort = this->send(p);
|
|
ss << "\t RESULT: " << this->srcPort;
|
|
|
|
this->print(ss.str());
|
|
}
|
|
|
|
void Peer::sendData()
|
|
{
|
|
stringstream ss;
|
|
Packet p;
|
|
ss << "data-Part" << this->partId++;
|
|
p.setDstIp(this->remoteIp);
|
|
p.setDstPort(this->remotePort);
|
|
p.setSrcPort(this->srcPort);
|
|
p.setMsg(ss.str());
|
|
ss.str("");
|
|
|
|
if (this->sim)
|
|
this->sim->print("Sending a data to peer");
|
|
ss << "Sending \"" << p.getMsg() << "\" to " << p.getDstIp() << ":" << p.getDstPort();
|
|
this->print(ss.str());
|
|
this->send(p);
|
|
}
|
|
|
|
bool Peer::isSender() const
|
|
{
|
|
return sender;
|
|
}
|
|
|
|
void Peer::setSender(bool sender)
|
|
{
|
|
this->sender = sender;
|
|
}
|
|
|
|
void Peer::sendAck(string dstIp, int dstPort)
|
|
{
|
|
stringstream ss;
|
|
Packet p;
|
|
ss << "data-Ack" << this->partId++;
|
|
p.setDstIp(dstIp);
|
|
p.setDstPort(dstPort);
|
|
p.setSrcPort(this->srcPort);
|
|
p.setMsg(ss.str());
|
|
ss.str("");
|
|
|
|
if (this->sim)
|
|
this->sim->print("Sending an acknowledgement to seed");
|
|
ss << "Sending \"" << p.getMsg() << "\" to " << p.getDstIp() << ":" << p.getDstPort();
|
|
this->print(ss.str());
|
|
this->send(p);
|
|
}
|
|
|
|
const Simulation* Peer::getSim()
|
|
{
|
|
return sim;
|
|
}
|
|
|
|
void Peer::setSim(Simulation* sim)
|
|
{
|
|
this->sim = sim;
|
|
}
|