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.

137 lines
2.7 KiB

  1. /*
  2. * Peer.cpp
  3. *
  4. * Created on: 17.01.2017
  5. * Author: piotrek
  6. */
  7. #include "Peer.h"
  8. Peer::Peer()
  9. {
  10. this->partId = 1;
  11. this->srcPort = 0;
  12. this->remotePort = 0;
  13. this->sender = false;
  14. }
  15. Peer::Peer(string hostname, string ip, string mask, string gatewayIp) : Node(hostname, ip, mask, gatewayIp)
  16. {
  17. this->partId = 1;
  18. this->srcPort = 0;
  19. this->remotePort = 0;
  20. this->sender = false;
  21. }
  22. void Peer::onRecv()
  23. {
  24. stringstream ss;
  25. while (true)
  26. {
  27. ss.str("");
  28. Packet p = this->recv();
  29. if (p.getSrcPort() != 0)
  30. {
  31. // jezeli informacje o drugiej stronie
  32. if (this->isSender() && this->remotePort == 0)
  33. {
  34. vector<string> x = split(p.getMsg(), ':');
  35. this->remoteIp = x[1];
  36. this->remotePort = atoi(x[2].c_str());
  37. ss << "onRecv() REMOTE peer is " << this->remoteIp << ":" << this->remotePort;
  38. this->print(ss.str());
  39. sleep(1);
  40. this->sendData();
  41. }
  42. else if (!this->isSender() && p.getMsg().find("data") != string::npos)
  43. {
  44. ss << "onRecv() Received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort();
  45. this->print(ss.str());
  46. sleep(1);
  47. this->sendAck(p.getSrcIp(), p.getSrcPort());
  48. }
  49. else if (this->isSender() && p.getMsg().find("Ack") != string::npos)
  50. {
  51. ss << "onRecv() Received \"" << p.getMsg() << "\" from " << p.getSrcIp() << ":" << p.getSrcPort();
  52. this->print(ss.str());
  53. sleep(1);
  54. this->sendData();
  55. }
  56. }
  57. else
  58. {
  59. #ifndef DEBUG
  60. this->print("onRecv() sleeping...");
  61. #endif
  62. }
  63. sleep(1);
  64. }
  65. }
  66. void Peer::connectToServer(string serverIp, int serverPort)
  67. {
  68. stringstream ss;
  69. ss << "Connecting to " << serverIp << ":" << serverPort;
  70. Packet p;
  71. if (this->isSender())
  72. p.setMsg("getRemoteIPPort");
  73. else
  74. p.setMsg("helloDownload");
  75. p.setDstIp(serverIp);
  76. p.setDstPort(serverPort);
  77. this->srcPort = this->send(p);
  78. ss << "\t RESULT: " << this->srcPort;
  79. this->print(ss.str());
  80. }
  81. void Peer::sendData()
  82. {
  83. stringstream ss;
  84. Packet p;
  85. ss << "data-Part" << this->partId++;
  86. p.setDstIp(this->remoteIp);
  87. p.setDstPort(this->remotePort);
  88. p.setSrcPort(this->srcPort);
  89. p.setMsg(ss.str());
  90. ss.str("");
  91. ss << "Sending \"" << p.getMsg() << "\" to " << p.getDstIp() << ":" << p.getDstPort();
  92. this->print(ss.str());
  93. this->send(p);
  94. }
  95. bool Peer::isSender() const
  96. {
  97. return sender;
  98. }
  99. void Peer::setSender(bool sender)
  100. {
  101. this->sender = sender;
  102. }
  103. void Peer::sendAck(string dstIp, int dstPort)
  104. {
  105. stringstream ss;
  106. Packet p;
  107. ss << "data-Ack" << this->partId++;
  108. p.setDstIp(dstIp);
  109. p.setDstPort(dstPort);
  110. p.setSrcPort(this->srcPort);
  111. p.setMsg(ss.str());
  112. ss.str("");
  113. ss << "Sending \"" << p.getMsg() << "\" to " << p.getDstIp() << ":" << p.getDstPort();
  114. this->print(ss.str());
  115. this->send(p);
  116. }