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.

231 lines
5.2 KiB

  1. /*
  2. * Simulation.cpp
  3. *
  4. * Created on: 16.01.2017
  5. * Author: piotrek
  6. */
  7. #include "Simulation.h"
  8. #include "Node.h"
  9. #include "NATRouter.h"
  10. #include "Peer.h"
  11. Simulation::Simulation()
  12. {
  13. this->rows = 0;
  14. this->cols = 0;
  15. #ifndef DEBUG
  16. // inicjalizacja ncurses
  17. initscr();
  18. getmaxyx(stdscr, this->rows, this->cols);
  19. if (has_colors())
  20. {
  21. start_color();
  22. init_pair(0, COLOR_BLACK, COLOR_WHITE);
  23. init_pair(1, COLOR_RED, COLOR_BLACK);
  24. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  25. init_pair(3, COLOR_YELLOW, COLOR_BLACK);
  26. init_pair(4, COLOR_BLUE, COLOR_BLACK);
  27. init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
  28. init_pair(6, COLOR_CYAN, COLOR_BLACK);
  29. init_pair(7, COLOR_WHITE, COLOR_BLACK);
  30. }
  31. #endif
  32. this->name = "Simulation";
  33. this->setLineNumber(rows -1);
  34. this->setDelimiter("\t");
  35. this->setObjectName(&this->name);
  36. this->setColor(WHITE);
  37. this->startTime = time(NULL);
  38. #ifndef DEBUG
  39. this->createThread("resizeWnd", SIM_RESIZE, this);
  40. #endif
  41. }
  42. Simulation::~Simulation()
  43. {
  44. //niestety nie dziala...
  45. endwin();
  46. }
  47. void Simulation::createThread(string name, THREAD_TYPE type, void* context)
  48. {
  49. struct threadParams params;
  50. params.type = type;
  51. params.context = context;
  52. this->threads.insert(pair<string, struct threadParams>(name, params));
  53. map<string, struct threadParams>::iterator it = threads.find(name);
  54. if (pthread_create(&it->second.thread_id, NULL, Simulation::threadWrapper, &it->second) == -1)
  55. {
  56. cerr << "pthread_create() failed" << endl;
  57. exit(1);
  58. }
  59. this->threads.insert(pair<string, struct threadParams>(name, params));
  60. usleep(3000); // 3ms
  61. }
  62. /*
  63. * przy niszczeniu watku niszczy sie ncurses :(
  64. * TODO poszukac rozwiazania w miare mozliwosci
  65. */
  66. void Simulation::destroyThread(string name)
  67. {
  68. void * state;
  69. map<string, struct threadParams>::iterator it = threads.find(name);
  70. if (it == threads.end())
  71. {
  72. cerr << "Thread: " << name << " does not exist!" << endl;
  73. exit(1);
  74. }
  75. if (pthread_cancel(it->second.thread_id) != 0)
  76. {
  77. cerr << "pthread_cancel() failed" << endl;
  78. exit(1);
  79. }
  80. if (pthread_join(it->second.thread_id, &state) == -1)
  81. {
  82. cerr << "pthread_cancel() failed" << endl;
  83. exit(1);
  84. }
  85. if (pthread_detach(it->second.thread_id) == -1)
  86. {
  87. cerr << "pthread_detach() failed" << endl;
  88. exit(1);
  89. }
  90. threads.erase(it);
  91. }
  92. /*
  93. * wrapper, w ktory trzeba opakowac metody do zastosowania w watku
  94. */
  95. void * Simulation::threadWrapper(void * context)
  96. {
  97. struct threadParams *params = (struct threadParams*)context;
  98. switch(params->type)
  99. {
  100. case NODE_RECV:
  101. ((Node *)params->context)->onRecv();
  102. // Node* c;
  103. // c = static_cast<Node*>(params->context);
  104. // c->onRecv();
  105. break;
  106. case SIM_TIMER:
  107. ((Simulation *)params->context)->timer();
  108. break;
  109. case SIM_RESIZE:
  110. ((Simulation *)params->context)->resizeWnd();
  111. break;
  112. }
  113. return 0;
  114. }
  115. void Simulation::resizeWnd()
  116. {
  117. while(true)
  118. {
  119. #ifndef DEBUG
  120. getmaxyx(stdscr, this->rows, this->cols);
  121. if (this->getLineNumber() != this->rows-1)
  122. this->setLineNumber(rows -1);
  123. //refresh();
  124. #endif
  125. }
  126. }
  127. void Simulation::timer()
  128. {
  129. char str[10];
  130. int h,m, s;
  131. unsigned long timeElapsed;
  132. while(true)
  133. {
  134. refresh();
  135. timeElapsed = time(NULL) - this->startTime;
  136. h = timeElapsed / 3600;
  137. m = (timeElapsed % 3600) / 60;
  138. s = (timeElapsed % 3600) % 60;
  139. //refresh();
  140. sprintf(str, "%02d:%02d:%02d", h, m, s);
  141. move(0, this->cols-strlen(str));
  142. printw(str);
  143. usleep(100000); // 100 ms
  144. //sleep(1);
  145. }
  146. }
  147. void Simulation::p2pSimulation()
  148. {
  149. this->print("Creating P2P peers");
  150. Peer peer1("Peer 1", "192.168.1.2", "255.255.255.0", "192.168.1.1");
  151. peer1.setLogParams(0, GREEN, "\t\t");
  152. Peer peer2("Peer 2", "10.0.0.2", "255.255.255.0", "10.0.0.1");
  153. peer2.setLogParams(4, CYAN, "\t\t");
  154. peer1.print("IP 192.168.1.2/24, Gateway: 192.168.1.1");
  155. peer2.print("IP 10.0.0.2/24, Gateway: 10.0.0.1");
  156. sleep(1);
  157. this->print("Creating NAT devices");
  158. NATRouter r1("Router 1", "192.168.1.1", "255.255.255.0");
  159. NATRouter r2("Router 2", "10.0.0.1", "255.255.255.0");
  160. r1.setLogParams(1, RED, "\t");
  161. r2.setLogParams(3, MAGENTA, "\t");
  162. r1.print("IP 192.168.1.1/24");
  163. r2.print("IP 10.0.0.1/24");
  164. sleep(1);
  165. this->print("Setting WAN configuration");
  166. r1.setWanIp("41.42.43.44");
  167. r1.setWanMask("255.255.255.255");
  168. r2.setWanIp("45.46.47.48");
  169. r2.setWanMask("255.255.255.255");
  170. r1.print("WAN IP 41.42.43.44/32");
  171. r2.print("WAN IP 45.46.47.48/32");
  172. sleep(1);
  173. this->print("Creating P2P server");
  174. Node server("Server", "80.80.90.91", "255.255.255.255");
  175. server.setLogParams(2, YELLOW, "\t\t");
  176. server.print("IP 80.80.90.91/32");
  177. sleep(1);
  178. this->print("Plugging Peer1 <---> Router 1");
  179. r1.connectNode(&peer1);
  180. sleep(1);
  181. this->print("Plugging Peer2 <---> Router 2");
  182. r2.connectNode(&peer2);
  183. sleep(1);
  184. this->print("Plugging Router 1 <---> Router 2");
  185. r1.connectNode(&r2, true);
  186. sleep(1);
  187. this->print("Plugging Server <---> Router 1");
  188. server.connectNode(&r1);
  189. sleep(1);
  190. this->print("Plugging Server <---> Router 2");
  191. server.connectNode(&r2);
  192. sleep(1);
  193. sleep(1);
  194. this->print("Starting simulation...");
  195. this->createThread("r1", NODE_RECV, &r1);
  196. this->createThread("r2", NODE_RECV, &r2);
  197. this->createThread("s1", NODE_RECV, &server);
  198. peer1.connectToServer("80.80.90.91", 6565);
  199. while(1);
  200. }