Symulacja NAT na przedmiot Symulacje Komputerowe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.5 KiB

  1. /*
  2. * P2PServer.cpp
  3. *
  4. * Created on: 17.01.2017
  5. * Author: Piotr Dergun
  6. */
  7. #include "P2PServer.h"
  8. P2PServer::P2PServer()
  9. {
  10. this->seedIp = "";
  11. this->peerIp = "";
  12. this->seedPort = 0;
  13. this->peerPort = 0;
  14. }
  15. P2PServer::P2PServer(string hostname, string ip, string mask) : Node(hostname, ip, mask)
  16. {
  17. this->seedIp = "";
  18. this->peerIp = "";
  19. this->seedPort = 0;
  20. this->peerPort = 0;
  21. }
  22. P2PServer::~P2PServer()
  23. {
  24. }
  25. void P2PServer::onRecv()
  26. {
  27. stringstream ss;
  28. while (true)
  29. {
  30. ss.str("");
  31. Packet p = this->recv();
  32. if (p.getSrcPort() != 0)
  33. {
  34. ss << "onRecv() Received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort();
  35. this->print(ss.str());
  36. sleep(1);
  37. if (p.getMsg() == "getRemoteIPPort")
  38. {
  39. this->seedIp = p.getSrcIp();
  40. this->seedPort = p.getSrcPort();
  41. }
  42. else if (p.getMsg() == "helloDownload")
  43. {
  44. this->peerIp = p.getSrcIp();
  45. this->peerPort = p.getSrcPort();
  46. }
  47. if (this->seedPort != 0 && this->peerPort != 0)
  48. {
  49. ss.str("");
  50. ss << "peerConnectionInfo:" << this->peerIp << ":" << this->peerPort;
  51. Packet pp(ss.str());
  52. pp.setDstIp(this->seedIp);
  53. pp.setDstPort(this->seedPort);
  54. pp.setSrcPort(p.getDstPort());
  55. ss.str("");
  56. ss << "Sending \"" << pp.getMsg() << "\" to " << pp.getDstIp() << ":" << pp.getDstPort();
  57. this->print(ss.str());
  58. this->send(pp);
  59. sleep(1);
  60. }
  61. }
  62. else
  63. {
  64. #ifndef DEBUG
  65. this->print("onRecv() sleeping...");
  66. #endif
  67. sleep(1);
  68. }
  69. }
  70. }