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.

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