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.

189 lines
4.0 KiB

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