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.

202 lines
4.2 KiB

5 years ago
5 years ago
  1. #include "../includes/client.hpp"
  2. namespace betacore
  3. {
  4. Client::Client(
  5. MODE mode,
  6. int port,
  7. std::string url,
  8. std::function<void(SHAPE &S, MODE &M)> &update) : _mode(mode),
  9. _port(port),
  10. _url(url),
  11. _update(update)
  12. {
  13. lookup["TRIANGLE"] = "APPLE";
  14. lookup["CIRCLE"] = "ORANGE";
  15. lookup["SQUARE"] = "BANANA";
  16. lookup["PENTAGON"] = "PINEAPPLE";
  17. lookup["APPLE"] = "TRIANGLE";
  18. lookup["ORANGE"] = "CIRCLE";
  19. lookup["BANANA"] = "SQUARE";
  20. lookup["PINEAPPLE"] = "PENTAGON";
  21. this->start();
  22. }
  23. Client::~Client(){
  24. this->_running= false;
  25. this->_online=false;
  26. }
  27. void Client::start()
  28. {
  29. std::cout
  30. << "Start"
  31. << "\nConnecting to: " << _url << ":" << _port
  32. << std::endl;
  33. //Create a socket point
  34. this->_client_socket = socket(AF_INET, SOCK_STREAM, 0);
  35. _server = gethostbyname(_url.c_str());
  36. if (_client_socket < 0)
  37. {
  38. std::cout << "ERROR opening socket" << std::endl;
  39. return;
  40. }
  41. std::cout
  42. << "Socket Open"
  43. << std::endl;
  44. bzero((char *)&_server_address, sizeof(_server_address));
  45. _server_address.sin_family = AF_INET;
  46. bcopy(
  47. (char *)_server->h_addr,
  48. (char *)&_server_address.sin_addr.s_addr,
  49. _server->h_length);
  50. _server_address.sin_port = htons(_port);
  51. // Now connect to the server
  52. if (connect(
  53. _client_socket,
  54. (struct sockaddr *)&_server_address,
  55. sizeof(_server_address)) < 0)
  56. {
  57. std::cout << "ERROR connecting" << std::endl;
  58. return;
  59. }
  60. this->_online = true;
  61. this->_running = true;
  62. std::thread listen_thread([this] { this->listener(); });
  63. listen_thread.detach();
  64. std::cout
  65. << "Client Connected"
  66. << std::endl;
  67. }
  68. void Client::stop()
  69. {
  70. std::cout
  71. << "Stop"
  72. << std::endl;
  73. }
  74. void Client::kill()
  75. {
  76. std::cout
  77. << "Kill"
  78. << std::endl;
  79. this->_online = false;
  80. }
  81. bool Client::running()
  82. {
  83. return this->_running;
  84. }
  85. std::vector<std::string> Client::explode(std::string &message){
  86. std::stringstream stream(message);
  87. std::string tmp;
  88. std::vector<std::string> words;
  89. while (std::getline(stream, tmp, ':'))
  90. words.push_back(tmp);
  91. return words;
  92. }
  93. SHAPE Client::parse(const std::string &message)
  94. {
  95. SHAPE result = decode(message);
  96. if (result != UNKOWN)
  97. return result;
  98. if (_mode == EVE)
  99. return UNKOWN;
  100. return decode(crypt(message));
  101. }
  102. SHAPE Client::decode(const std::string &shape)
  103. {
  104. if (shape == "TRIANGLE")
  105. return TRIANGLE;
  106. if (shape == "CIRCLE")
  107. return CIRCLE;
  108. if (shape == "SQUARE")
  109. return SQUARE;
  110. if (shape == "PENTAGON")
  111. return PENTAGON;
  112. return UNKOWN;
  113. }
  114. std::string Client::crypt(const std::string &message)
  115. {
  116. // std::string output = std::string(message);
  117. // for (int i = 0; i < message.size(); i++)
  118. // output.at(i) = message[i] ^ key;
  119. std::cout << "Crypt::" << message << " : " << lookup[message] << std::endl;
  120. return lookup[message];
  121. }
  122. std::string Client::encode(const SHAPE &shape)
  123. {
  124. std::string message;
  125. switch (shape)
  126. {
  127. case TRIANGLE:
  128. message = "TRIANGLE";
  129. break;
  130. case CIRCLE:
  131. message = "CIRCLE";
  132. break;
  133. case SQUARE:
  134. message = "SQUARE";
  135. break;
  136. case PENTAGON:
  137. message = "PENTAGON";
  138. break;
  139. default:
  140. message = "UNKOWN";
  141. break;
  142. }
  143. return message;
  144. }
  145. void Client::send(bool encrypt, SHAPE shape)
  146. {
  147. std::string message = "SHAPE:";
  148. message += encrypt ? crypt(encode(shape)) : encode(shape);
  149. message += ":";
  150. message += Parser::mode(this->_mode);
  151. std::cout << "Sending Message: " << message << "\n"
  152. << std::endl;
  153. char buffer[BUFFER_LENGTH];
  154. bzero(buffer, BUFFER_LENGTH);
  155. strcpy(buffer, message.c_str());
  156. buffer[BUFFER_LENGTH - 1] = '\0';
  157. int x = write(this->_client_socket, buffer, strlen(buffer));
  158. if (x < 0)
  159. {
  160. std::cout << "Failed send to server" << std::endl;
  161. }
  162. }
  163. void Client::listener()
  164. {
  165. std::cout << "listener" << std::endl;
  166. char buffer[BUFFER_LENGTH];
  167. while (_online)
  168. {
  169. // Now read server response
  170. bzero(buffer, BUFFER_LENGTH);
  171. std::cout << "Wating For message" << std::endl;
  172. if (read(_client_socket, buffer, BUFFER_LENGTH - 1) < 0)
  173. {
  174. std::cout << "ERROR reading from socket" << std::endl;
  175. }
  176. std::string message(buffer);
  177. std::cout << "Got Message: " << message << std::endl;
  178. auto parts = explode(message);
  179. if(parts.size() < 3)
  180. return;
  181. SHAPE shape = parse(parts.at(1));
  182. MODE mode = Parser::mode(parts.at(2));
  183. _update(shape,mode);
  184. }
  185. }
  186. } // namespace betacore