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.

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