| @ -0,0 +1,79 @@ | |||
| /* | |||
| * 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); | |||
| } | |||
| } | |||
| } | |||
| @ -0,0 +1,26 @@ | |||
| /* | |||
| * P2PServer.h | |||
| * | |||
| * Created on: 17.01.2017 | |||
| * Author: Piotr Dergun | |||
| */ | |||
| #ifndef P2PSERVER_H_ | |||
| #define P2PSERVER_H_ | |||
| #include "common.h" | |||
| #include "Node.h" | |||
| class P2PServer : public Node | |||
| { | |||
| string seedIp, peerIp; | |||
| int seedPort, peerPort; | |||
| public: | |||
| P2PServer(); | |||
| P2PServer(string hostname, string ip, string mask); | |||
| virtual ~P2PServer(); | |||
| virtual void onRecv(); | |||
| }; | |||
| #endif /* P2PSERVER_H_ */ | |||
| @ -0,0 +1,137 @@ | |||
| /* | |||
| * Peer.cpp | |||
| * | |||
| * Created on: 17.01.2017 | |||
| * Author: piotrek | |||
| */ | |||
| #include "Peer.h" | |||
| Peer::Peer() | |||
| { | |||
| this->partId = 1; | |||
| this->srcPort = 0; | |||
| this->remotePort = 0; | |||
| this->sender = false; | |||
| } | |||
| 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; | |||
| } | |||
| 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(""); | |||
| 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(""); | |||
| ss << "Sending \"" << p.getMsg() << "\" to " << p.getDstIp() << ":" << p.getDstPort(); | |||
| this->print(ss.str()); | |||
| this->send(p); | |||
| } | |||
| @ -0,0 +1,37 @@ | |||
| /* | |||
| * Peer.h | |||
| * | |||
| * Created on: 17.01.2017 | |||
| * Author: piotrek | |||
| */ | |||
| #ifndef PEER_H_ | |||
| #define PEER_H_ | |||
| #include "common.h" | |||
| #include "Node.h" | |||
| class Peer : public Node | |||
| { | |||
| bool sender; | |||
| int srcPort; | |||
| string remoteIp; | |||
| int remotePort; | |||
| int partId; | |||
| public: | |||
| Peer(); | |||
| Peer(string hostname, string ip, string mask, string gatewayIp); | |||
| virtual ~Peer() {}; | |||
| virtual void onRecv(); | |||
| void connectToServer(string serverIp, int serverPort); | |||
| bool isSender() const; | |||
| void setSender(bool sender); | |||
| protected: | |||
| void sendData(); | |||
| void sendAck(string dstIp, int dstPort); | |||
| }; | |||
| #endif /* PEER_H_ */ | |||