|
4 | 4 |
|
5 | 5 | #include <sys/eventfd.h> |
6 | 6 | #include <sys/select.h> |
| 7 | +#include <arpa/inet.h> |
7 | 8 |
|
8 | 9 | #include <errno.h> |
9 | 10 | #include <unistd.h> |
@@ -173,7 +174,116 @@ namespace simpleApp |
173 | 174 |
|
174 | 175 | if (FD_ISSET(STDIN_FILENO, &fd_in)) |
175 | 176 | { |
176 | | - // TODO |
| 177 | + char buffer[MESSAGE_MAX_BUFFER - sizeof(msg_headers)]; |
| 178 | + |
| 179 | + int len = 0; |
| 180 | + // TODO read from stdin |
| 181 | + // len = read(stdin, buffer); |
| 182 | + |
| 183 | + if (len == 0) |
| 184 | + { |
| 185 | + std::cout << " >> " << std::flush; |
| 186 | + } |
| 187 | + else if (currentState == console_state::protocol_selection) |
| 188 | + { |
| 189 | + if (len != 1 || (buffer[0] != 'u' && buffer[0] != 't')) |
| 190 | + { |
| 191 | + std::cout << "Invalid input. [u]dp or [t]cp?" << std::endl << |
| 192 | + " >> " << std::flush; |
| 193 | + } |
| 194 | + else |
| 195 | + { |
| 196 | + if (buffer[0] == 'u') |
| 197 | + currentSession = new SessionTcp(); |
| 198 | + else |
| 199 | + currentSession = new SessionUdp(); |
| 200 | + |
| 201 | + switchState(console_state::address_input); |
| 202 | + } |
| 203 | + } |
| 204 | + else if (currentState == console_state::address_input) |
| 205 | + { |
| 206 | + auto address = inet_addr(buffer); |
| 207 | + |
| 208 | + if (address == INADDR_NONE) |
| 209 | + { |
| 210 | + std::cout << "The entered IP address is incorrect. Try again" << std::endl |
| 211 | + << " >> " << std::flush; |
| 212 | + } |
| 213 | + else |
| 214 | + { |
| 215 | + auto result = currentSession->init(address); |
| 216 | + if (result.status == session_status::init_success) |
| 217 | + { |
| 218 | + switchState(console_state::connected); |
| 219 | + } |
| 220 | + else |
| 221 | + { |
| 222 | + switch(result.status) |
| 223 | + { |
| 224 | + case session_status::init_socket_fail: |
| 225 | + std::cout << "Socket initialization failed"; |
| 226 | + break; |
| 227 | + case session_status::init_connection_fail: |
| 228 | + std::cout << "Connection failed"; |
| 229 | + break; |
| 230 | + case session_status::init_udp_send_up_fail: |
| 231 | + case session_status::init_udp_recv_up_fail: |
| 232 | + std::cout << "Connection request failed"; |
| 233 | + break; |
| 234 | + case session_status::recv_wrong_length: |
| 235 | + case session_status::recv_wrong_header: |
| 236 | + std::cout << "Received message corrupted"; |
| 237 | + break; |
| 238 | + default: |
| 239 | + std::cout << "Unknown error"; |
| 240 | + break; |
| 241 | + } |
| 242 | + |
| 243 | + if (result.err != 0) |
| 244 | + std::cout << " with code " << result.err << std::endl << std::flush; |
| 245 | + else |
| 246 | + std::cout << std::endl << std::flush; |
| 247 | + |
| 248 | + switchState(console_state::protocol_selection); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + else if (currentState == console_state::connected) |
| 253 | + { |
| 254 | + auto result = currentSession->proceed(buffer, len); |
| 255 | + if (result.status == session_status::proceed_msg) |
| 256 | + { |
| 257 | + std::cout << "Received answer: '" << result.recvMsg << "'" << std::endl << |
| 258 | + " >> " << std::flush; |
| 259 | + } |
| 260 | + else |
| 261 | + { |
| 262 | + switch(result.status) |
| 263 | + { |
| 264 | + case session_status::proceed_msg_send_fail: |
| 265 | + std::cout << "Message sending failed"; |
| 266 | + break; |
| 267 | + case session_status::proceed_msg_recv_fail: |
| 268 | + std::cout << "Message receiving failed"; |
| 269 | + break; |
| 270 | + case session_status::recv_wrong_length: |
| 271 | + case session_status::recv_wrong_header: |
| 272 | + std::cout << "Message has been corrupted"; |
| 273 | + break; |
| 274 | + default: |
| 275 | + std::cout << "Unknown error"; |
| 276 | + break; |
| 277 | + } |
| 278 | + |
| 279 | + if (result.err != 0) |
| 280 | + std::cout << " with code " << result.err << std::endl << std::flush; |
| 281 | + else |
| 282 | + std::cout << std::endl << std::flush; |
| 283 | + |
| 284 | + switchState(console_state::protocol_selection); |
| 285 | + } |
| 286 | + } |
177 | 287 | } |
178 | 288 | } |
179 | 289 |
|
|
0 commit comments