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.

203 lines
4.3 KiB

  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. std::string tmp = message;
  96. SHAPE result = decode(tmp);
  97. if (result != UNKOWN)
  98. return result;
  99. if (_mode == EVE)
  100. return UNKOWN;
  101. return decode(crypt(tmp));
  102. }
  103. SHAPE Client::decode(const std::string &shape)
  104. {
  105. if (shape == "TRIANGLE")
  106. return TRIANGLE;
  107. if (shape == "CIRCLE")
  108. return CIRCLE;
  109. if (shape == "SQUARE")
  110. return SQUARE;
  111. if (shape == "PENTAGON")
  112. return PENTAGON;
  113. return UNKOWN;
  114. }
  115. std::string Client::crypt(const std::string &message)
  116. {
  117. // std::string output = std::string(message);
  118. // for (int i = 0; i < message.size(); i++)
  119. // output.at(i) = message[i] ^ key;
  120. std::cout << "Crypt::" << message << " : " << lookup[message] << std::endl;
  121. return lookup[message];
  122. }
  123. std::string Client::encode(const SHAPE &shape)
  124. {
  125. std::string message;
  126. switch (shape)
  127. {
  128. case TRIANGLE:
  129. message = "TRIANGLE";
  130. break;
  131. case CIRCLE:
  132. message = "CIRCLE";
  133. break;
  134. case SQUARE:
  135. message = "SQUARE";
  136. break;
  137. case PENTAGON:
  138. message = "PENTAGON";
  139. break;
  140. default:
  141. message = "UNKOWN";
  142. break;
  143. }
  144. return message;
  145. }
  146. void Client::send(bool encrypt, SHAPE shape)
  147. {
  148. std::string message = "SHAPE:";
  149. message += encrypt ? crypt(encode(shape)) : encode(shape);
  150. message += ":";
  151. message += Parser::mode(this->_mode);
  152. std::cout << "Sending Message: " << message << "\n"
  153. << std::endl;
  154. char buffer[BUFFER_LENGTH];
  155. bzero(buffer, BUFFER_LENGTH);
  156. strcpy(buffer, message.c_str());
  157. buffer[BUFFER_LENGTH - 1] = '\0';
  158. int x = write(this->_client_socket, buffer, strlen(buffer));
  159. if (x < 0)
  160. {
  161. std::cout << "Failed send to server" << std::endl;
  162. }
  163. }
  164. void Client::listener()
  165. {
  166. std::cout << "listener" << std::endl;
  167. char buffer[BUFFER_LENGTH];
  168. while (_online)
  169. {
  170. // Now read server response
  171. bzero(buffer, BUFFER_LENGTH);
  172. std::cout << "Wating For message" << std::endl;
  173. if (read(_client_socket, buffer, BUFFER_LENGTH - 1) < 0)
  174. {
  175. std::cout << "ERROR reading from socket" << std::endl;
  176. }
  177. std::string message(buffer);
  178. std::cout << "Got Message: " << message << std::endl;
  179. auto parts = explode(message);
  180. if(parts.size() < 3)
  181. return;
  182. SHAPE shape = parse(parts.at(1));
  183. MODE mode = Parser::mode(parts.at(2));
  184. _update(shape,mode);
  185. }
  186. }
  187. } // namespace betacore