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.

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