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.

93 lines
1.7 KiB

  1. /**
  2. * @file 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. this->sim = NULL;
  15. }
  16. P2PServer::P2PServer(string hostname, string ip, string mask) : Node(hostname, ip, mask)
  17. {
  18. this->seedIp = "";
  19. this->peerIp = "";
  20. this->seedPort = 0;
  21. this->peerPort = 0;
  22. this->sim = NULL;
  23. }
  24. P2PServer::~P2PServer()
  25. {
  26. }
  27. void P2PServer::onRecv()
  28. {
  29. stringstream ss;
  30. while (true)
  31. {
  32. ss.str("");
  33. Packet p = this->recv();
  34. if (p.getSrcPort() != 0)
  35. {
  36. ss << "onRecv() Received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort();
  37. this->print(ss.str());
  38. sleep(2);
  39. if (p.getMsg() == "getRemoteIPPort")
  40. {
  41. this->seedIp = p.getSrcIp();
  42. this->seedPort = p.getSrcPort();
  43. }
  44. else if (p.getMsg() == "helloDownload")
  45. {
  46. this->peerIp = p.getSrcIp();
  47. this->peerPort = p.getSrcPort();
  48. }
  49. if (this->seedPort != 0 && this->peerPort != 0)
  50. {
  51. ss.str("");
  52. ss << "peerConnectionInfo:" << this->peerIp << ":" << this->peerPort;
  53. Packet pp(ss.str());
  54. pp.setDstIp(this->seedIp);
  55. pp.setDstPort(this->seedPort);
  56. pp.setSrcPort(p.getDstPort());
  57. ss.str("");
  58. if (this->sim)
  59. this->sim->print("Sending to seed information about peer IP and port");
  60. ss << "Sending \"" << pp.getMsg() << "\" to " << pp.getDstIp() << ":" << pp.getDstPort();
  61. this->print(ss.str());
  62. this->send(pp);
  63. sleep(3);
  64. }
  65. }
  66. else
  67. {
  68. #ifndef DEBUG
  69. this->print("onRecv() sleeping...");
  70. #endif
  71. sleep(1);
  72. }
  73. }
  74. }
  75. const Simulation* P2PServer::getSim()
  76. {
  77. return sim;
  78. }
  79. void P2PServer::setSim(Simulation* sim)
  80. {
  81. this->sim = sim;
  82. }