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.

246 lines
5.5 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 seed("Seed", "192.168.1.2", "255.255.255.0", "192.168.1.1");
  154. seed.setSender(true);
  155. seed.setSim(this);
  156. seed.setLogParams(0, GREEN, "\t\t");
  157. Peer peer("Peer", "10.0.0.2", "255.255.255.0", "10.0.0.1");
  158. peer.setSim(this);
  159. peer.setLogParams(4, CYAN, "\t\t");
  160. seed.print("IP 192.168.1.2/24, Gateway: 192.168.1.1");
  161. peer.print("IP 10.0.0.2/24, Gateway: 10.0.0.1");
  162. sleep(1);
  163. this->print("Creating NAT devices");
  164. NATRouter r1("Router 1", "192.168.1.1", "255.255.255.0");
  165. NATRouter r2("Router 2", "10.0.0.1", "255.255.255.0");
  166. r1.setLogParams(1, RED, "\t");
  167. r2.setLogParams(3, MAGENTA, "\t");
  168. r1.print("IP 192.168.1.1/24");
  169. r2.print("IP 10.0.0.1/24");
  170. sleep(1);
  171. this->print("Setting WAN configuration");
  172. r1.setWanIp("41.42.43.44");
  173. r1.setWanMask("255.255.255.255");
  174. r2.setWanIp("45.46.47.48");
  175. r2.setWanMask("255.255.255.255");
  176. r1.print("WAN IP 41.42.43.44/32");
  177. r2.print("WAN IP 45.46.47.48/32");
  178. sleep(1);
  179. this->print("Creating P2P server");
  180. P2PServer server("Server", "80.80.90.91", "255.255.255.255");
  181. server.setSim(this);
  182. server.setLogParams(2, YELLOW, "\t\t");
  183. server.print("IP 80.80.90.91/32");
  184. sleep(1);
  185. this->print("Plugging Seed <---> Router 1");
  186. r1.connectNode(&seed);
  187. usleep(600000);
  188. this->print("Plugging Peer <---> Router 2");
  189. r2.connectNode(&peer);
  190. usleep(600000);
  191. this->print("Plugging Router 1 <---> Router 2");
  192. r1.connectNode(&r2, true);
  193. usleep(600000);
  194. this->print("Plugging Server <---> Router 1");
  195. server.connectNode(&r1);
  196. usleep(600000);
  197. this->print("Plugging Server <---> Router 2");
  198. server.connectNode(&r2);
  199. usleep(600000);
  200. this->print("Starting simulation...");
  201. sleep(1);
  202. this->createThread("r1", NODE_RECV, &r1);
  203. this->createThread("r2", NODE_RECV, &r2);
  204. this->createThread("s1", NODE_RECV, &server);
  205. this->print("Connecting peers to P2P server");
  206. seed.connectToServer("80.80.90.91", 6565);
  207. peer.connectToServer("80.80.90.91", 6565);
  208. sleep(3);
  209. this->createThread("p1", NODE_RECV, &seed);
  210. this->createThread("p2", NODE_RECV, &peer);
  211. while(1);
  212. }