|
1 | 1 | #include "MainConsole.hpp" |
2 | 2 |
|
| 3 | +#include <iostream> |
| 4 | + |
3 | 5 | #include <sys/eventfd.h> |
| 6 | +#include <sys/epoll.h> |
4 | 7 |
|
5 | 8 | #include <errno.h> |
6 | 9 | #include <unistd.h> |
7 | 10 |
|
| 11 | +#include "SessionClient.hpp" |
| 12 | +#include "SessionTcp.hpp" |
| 13 | +#include "SessionUdp.hpp" |
| 14 | +#include "console_state.h" |
| 15 | + |
8 | 16 | namespace simpleApp |
9 | 17 | { |
10 | 18 | MainConsole::MainConsole() |
@@ -39,8 +47,227 @@ namespace simpleApp |
39 | 47 | return 0; |
40 | 48 | } |
41 | 49 |
|
| 50 | + void cleanStdin() |
| 51 | + { |
| 52 | + // TODO |
| 53 | + } |
| 54 | + |
| 55 | + int getFromStdin(char* buffer) |
| 56 | + { |
| 57 | + // TODO |
| 58 | + return 0; |
| 59 | + } |
| 60 | + |
42 | 61 | int MainConsole::consoleLoop() |
43 | 62 | { |
| 63 | + int epollfd = epoll_create1(0); |
| 64 | + |
| 65 | + if (epollfd == -1) |
| 66 | + { |
| 67 | + std::cout << "EPoll creation failed with code " << errno << std::endl << std::flush; |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + epoll_event setupEvent; |
| 72 | + setupEvent.data.fd = this->breakEventFd; |
| 73 | + setupEvent.events = EPOLLIN | EPOLLET; |
| 74 | + if (epoll_ctl(epollfd, EPOLL_CTL_ADD, this->breakEventFd, &setupEvent) == -1) |
| 75 | + { |
| 76 | + std::cout << "EPOLL_CTL_ADD of break event fd failed with code " << errno << std::endl << std::flush; |
| 77 | + close(epollfd); |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + setupEvent.data.fd = STDIN_FILENO; |
| 82 | + setupEvent.events = EPOLLIN | EPOLLET; |
| 83 | + if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &setupEvent) == -1) |
| 84 | + { |
| 85 | + std::cout << "EPOLL_CTL_ADD of break event fd failed with code " << errno << std::endl << std::flush; |
| 86 | + epoll_ctl(epollfd, EPOLL_CTL_DEL, this->breakEventFd, nullptr); |
| 87 | + close(epollfd); |
| 88 | + return -1; |
| 89 | + } |
| 90 | + |
| 91 | + bool isExit = false; |
| 92 | + console_state currentState = console_state::protocol_selection; |
| 93 | + SessionClient* currentSession = nullptr; |
| 94 | + |
| 95 | + std::cout << "Select protocol: [u]dp or [t]cp - or press Ctrl+C for exit" << std::endl << std::flush; |
| 96 | + std::cout << " >> " << std::flush; |
| 97 | + |
| 98 | + while (!isExit) |
| 99 | + { |
| 100 | + const size_t eventsSize = 4; |
| 101 | + epoll_event events[eventsSize]; |
| 102 | + |
| 103 | + console_state nextState = console_state::none; |
| 104 | + |
| 105 | + int len = epoll_wait(epollfd, events, eventsSize, -1); |
| 106 | + for(size_t i = 0; static_cast<int>(i) < len; i++) |
| 107 | + { |
| 108 | + if (events[i].data.fd == this->breakEventFd) |
| 109 | + { |
| 110 | + switch (currentState) |
| 111 | + { |
| 112 | + case console_state::protocol_selection: |
| 113 | + nextState = console_state::none; |
| 114 | + isExit = true; |
| 115 | + break; |
| 116 | + |
| 117 | + case console_state::msg_input: |
| 118 | + case console_state::wait_result: |
| 119 | + case console_state::connect_wait: |
| 120 | + delete currentSession; |
| 121 | + currentSession = nullptr; |
| 122 | + case console_state::address_input: |
| 123 | + nextState = console_state::protocol_selection; |
| 124 | + break; |
| 125 | + default: |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + cleanStdin(); |
| 130 | + |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + // Non-console event and have session |
| 135 | + if (events[i].data.fd != STDIN_FILENO && currentSession != nullptr) |
| 136 | + { |
| 137 | + //cleanStdin(); |
| 138 | + |
| 139 | + switch (currentState) |
| 140 | + { |
| 141 | + case console_state::msg_input: |
| 142 | + /* code */ |
| 143 | + break; |
| 144 | + |
| 145 | + default: |
| 146 | + break; |
| 147 | + } |
| 148 | + // TODO |
| 149 | + } |
| 150 | + |
| 151 | + // Console input |
| 152 | + if (events[i].data.fd == STDIN_FILENO) |
| 153 | + { |
| 154 | + char buffer[MESSAGE_MAX_BUFFER - sizeof(msg_headers)]; |
| 155 | + auto len = getFromStdin(buffer); |
| 156 | + |
| 157 | + if (len < 1) |
| 158 | + { |
| 159 | + cleanStdin(); |
| 160 | + break; |
| 161 | + } |
| 162 | + |
| 163 | + switch (currentState) |
| 164 | + { |
| 165 | + case console_state::protocol_selection: |
| 166 | + if (buffer[0] == 't') |
| 167 | + { |
| 168 | + currentSession = new SessionTcp(epollfd); |
| 169 | + nextState = console_state::address_input; |
| 170 | + } |
| 171 | + else if (buffer[0] == 'u') |
| 172 | + { |
| 173 | + currentSession = new SessionUdp(epollfd); |
| 174 | + nextState = console_state::address_input; |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + std::cout << "Incorrect choice" << std::endl << std::flush; |
| 179 | + } |
| 180 | + break; |
| 181 | + |
| 182 | + case console_state::address_input: |
| 183 | + { |
| 184 | + auto result = currentSession->init(std::string(buffer, len)); |
| 185 | + switch (result.status) |
| 186 | + { |
| 187 | + case session_status::init_success: |
| 188 | + nextState = console_state::msg_input; |
| 189 | + break; |
| 190 | + |
| 191 | + case session_status::init_udp_conn_wait: |
| 192 | + nextState = console_state::connect_wait; |
| 193 | + break; |
| 194 | + |
| 195 | + default: |
| 196 | + std::cout << "Connection error" << std::endl << std::flush; |
| 197 | + delete currentSession; |
| 198 | + currentSession = nullptr; |
| 199 | + nextState = console_state::protocol_selection; |
| 200 | + break; |
| 201 | + } |
| 202 | + break; |
| 203 | + } |
| 204 | + case console_state::msg_input: |
| 205 | + { |
| 206 | + auto result = currentSession->proceed(events[i].data.fd, buffer, len); |
| 207 | + switch (result.status) |
| 208 | + { |
| 209 | + case session_status::proceed_msg_send: |
| 210 | + |
| 211 | + break; |
| 212 | + |
| 213 | + default: |
| 214 | + break; |
| 215 | + } |
| 216 | + } |
| 217 | + default: |
| 218 | + break; |
| 219 | + } |
| 220 | + |
| 221 | + cleanStdin(); |
| 222 | + break; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + // Switch state |
| 227 | + switch (nextState) |
| 228 | + { |
| 229 | + case console_state::protocol_selection: |
| 230 | + { |
| 231 | + std::cout << "Select protocol: [u]dp or [t]cp - or press Ctrl+C for exit" << std::endl << std::flush; |
| 232 | + break; |
| 233 | + } |
| 234 | + |
| 235 | + case console_state::address_input: |
| 236 | + { |
| 237 | + std::cout << "Input IP address of server. To change protocol press Ctrl+C" << std::endl << std::flush; |
| 238 | + break; |
| 239 | + } |
| 240 | + |
| 241 | + case console_state::msg_input: |
| 242 | + { |
| 243 | + std::cout << "Connected to server. For disconnect press Ctrl+C" << std::endl << std::flush; |
| 244 | + break; |
| 245 | + } |
| 246 | + default: |
| 247 | + break; |
| 248 | + } |
| 249 | + |
| 250 | + if (nextState != console_state::none) |
| 251 | + currentState = nextState; |
| 252 | + |
| 253 | + switch (currentState) |
| 254 | + { |
| 255 | + case console_state::protocol_selection: |
| 256 | + case console_state::address_input: |
| 257 | + case console_state::msg_input: |
| 258 | + std::cout << " >> " << std::endl; |
| 259 | + break; |
| 260 | + default: |
| 261 | + break; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + if (currentSession != nullptr) |
| 266 | + delete currentSession; |
| 267 | + |
| 268 | + epoll_ctl(epollfd, EPOLL_CTL_DEL, this->breakEventFd, 0); |
| 269 | + close(epollfd); |
| 270 | + |
44 | 271 | return 0; |
45 | 272 | } |
46 | 273 | } |
0 commit comments