@@ -65,9 +65,35 @@ namespace simpleApp
6565 SessionClient * currentSession = nullptr ;
6666 console_state currentState;
6767
68- auto switchState = [¤tState, ¤tSession, &isExit ](console_state newState)
68+ auto switchState = [¤tState, ¤tSession](console_state newState)
6969 {
7070 // TODO
71+
72+ // TODO STDIN clean up
73+
74+ switch (newState)
75+ {
76+ case console_state::protocol_selection:
77+ if (currentSession != nullptr )
78+ {
79+ delete currentSession;
80+ currentSession = nullptr ;
81+ }
82+ std::cout << " Select protocol: [u]dp or [t]cp" << std::endl <<
83+ " >> " << std::flush;
84+ break ;
85+
86+ case console_state::address_input:
87+ std::cout << " Input server IP-address" << std::endl <<
88+ " >> " << std::flush;
89+ break ;
90+ case console_state::connected:
91+ std::cout << " Connected. Input message" << std::endl <<
92+ " >> " << std::flush;
93+ break ;
94+ }
95+
96+ currentState = newState;
7197 };
7298
7399 switchState (console_state::protocol_selection);
@@ -97,7 +123,7 @@ namespace simpleApp
97123 return -1 ;
98124 }
99125
100- if (currentSession != nullptr )
126+ if (currentState == console_state::connected )
101127 {
102128 if (selectResult == 0 )
103129 {
@@ -174,26 +200,26 @@ namespace simpleApp
174200
175201 if (FD_ISSET (STDIN_FILENO, &fd_in))
176202 {
177- char buffer[MESSAGE_MAX_BUFFER - sizeof (msg_headers)];
203+ std::string line;
204+ std::getline (std::cin, line);
205+
206+ int len = line.size ();
207+ const char * linePtr = line.c_str ();
178208
179- int len = 0 ;
180- // TODO read from stdin
181- // len = read(stdin, buffer);
182-
183209 if (len == 0 )
184210 {
185211 std::cout << " >> " << std::flush;
186212 }
187213 else if (currentState == console_state::protocol_selection)
188214 {
189- if (len != 1 || (buffer [0 ] != ' u' && buffer [0 ] != ' t' ))
215+ if (len != 1 || (linePtr [0 ] != ' u' && linePtr [0 ] != ' t' ))
190216 {
191217 std::cout << " Invalid input. [u]dp or [t]cp?" << std::endl <<
192218 " >> " << std::flush;
193219 }
194220 else
195221 {
196- if (buffer [0 ] == ' u ' )
222+ if (linePtr [0 ] == ' t ' )
197223 currentSession = new SessionTcp ();
198224 else
199225 currentSession = new SessionUdp ();
@@ -203,7 +229,7 @@ namespace simpleApp
203229 }
204230 else if (currentState == console_state::address_input)
205231 {
206- auto address = inet_addr (buffer );
232+ auto address = inet_addr (linePtr );
207233
208234 if (address == INADDR_NONE)
209235 {
@@ -235,6 +261,9 @@ namespace simpleApp
235261 case session_status::recv_wrong_header:
236262 std::cout << " Received message corrupted" ;
237263 break ;
264+ case session_status::server_error:
265+ std::cout << " Error on server-side" ;
266+ break ;
238267 default :
239268 std::cout << " Unknown error" ;
240269 break ;
@@ -251,7 +280,7 @@ namespace simpleApp
251280 }
252281 else if (currentState == console_state::connected)
253282 {
254- auto result = currentSession->proceed (buffer , len);
283+ auto result = currentSession->proceed (linePtr , len);
255284 if (result.status == session_status::proceed_msg)
256285 {
257286 std::cout << " Received answer: '" << result.recvMsg << " '" << std::endl <<
@@ -283,10 +312,19 @@ namespace simpleApp
283312
284313 switchState (console_state::protocol_selection);
285314 }
286- }
315+ }
316+
317+ // TODO clear cin
287318 }
288319 }
289320
321+ if (currentSession != nullptr )
322+ {
323+ delete currentSession;
324+ }
325+
326+ // TODO clear cin
327+
290328 return 0 ;
291329 }
292330}
0 commit comments