9
9
import java .lang .Thread .State ;
10
10
import java .net .Socket ;
11
11
import java .util .Scanner ;
12
+ import com .chatroom .configuration .Config ;
12
13
import com .chatroom .models .Request ;
13
14
import com .chatroom .models .Response ;
14
15
import com .chatroom .others .Hash ;
15
16
import com .chatroom .others .LogFileWriter ;
16
17
import com .chatroom .others .Message ;
17
18
18
19
public class Client {
19
- private int clientID = -1 ;
20
+ private int clientID = -1 ;
20
21
private int roomId = -1 ;
21
22
private Scanner scanner = new Scanner (System .in );
22
23
private int choice ;
@@ -29,13 +30,11 @@ public class Client {
29
30
private Request request = null ;
30
31
private Response response = null ;
31
32
private MessageListener messageListener ;
32
- public static StringWriter errors ;
33
33
34
34
public Client (String host , int port ) {
35
35
this .host = host ;
36
36
this .port = port ;
37
37
messageListener = new MessageListener ();
38
- errors = new StringWriter ();
39
38
}
40
39
41
40
public void connect () {
@@ -45,8 +44,8 @@ public void connect() {
45
44
objectInputStream = new ObjectInputStream (socket .getInputStream ());
46
45
mainFunc ();
47
46
} catch (IOException e ) {
48
- e .printStackTrace (new PrintWriter (errors ));
49
- LogFileWriter .Log (errors .toString ());
47
+ e .printStackTrace (new PrintWriter (Config . errors ));
48
+ LogFileWriter .Log (Config . errors .toString ());
50
49
}
51
50
}
52
51
@@ -78,14 +77,15 @@ private void mainOptions() {
78
77
}
79
78
}
80
79
catch (Exception e ) {
81
- e .printStackTrace (new PrintWriter (errors ));
82
- LogFileWriter .Log (errors .toString ());
80
+ e .printStackTrace (new PrintWriter (Config . errors ));
81
+ LogFileWriter .Log (Config . errors .toString ());
83
82
}
84
83
}
85
84
86
85
private void logOut () throws Exception {
87
86
cont = "" ;
88
87
request = new Request (Request .Type .LOGOUT .ordinal (),clientID ,roomId ,cont );
88
+ request .setIsConsole (true );
89
89
objectOutputStream .writeObject (request );
90
90
objectOutputStream .flush ();
91
91
response = (Response ) objectInputStream .readObject ();
@@ -105,6 +105,7 @@ private void viewRooms() throws Exception{
105
105
try {
106
106
cont = "" ;
107
107
request = new Request (Request .Type .VIEW_ROOMS .ordinal (),clientID ,roomId ,cont );
108
+ request .setIsConsole (true );
108
109
objectOutputStream .writeObject (request );
109
110
objectOutputStream .flush ();
110
111
response = (Response ) objectInputStream .readObject ();
@@ -123,8 +124,8 @@ private void viewRooms() throws Exception{
123
124
}
124
125
}
125
126
catch (Exception e ) {
126
- e .printStackTrace (new PrintWriter (errors ));
127
- LogFileWriter .Log (errors .toString ());
127
+ e .printStackTrace (new PrintWriter (Config . errors ));
128
+ LogFileWriter .Log (Config . errors .toString ());
128
129
}
129
130
}
130
131
@@ -135,6 +136,7 @@ private void createAndJoinRoom(String rName, boolean create) throws Exception
135
136
else
136
137
request = new Request (Request .Type .JOIN_ROOM .ordinal (),clientID ,roomId ,cont );
137
138
139
+ request .setIsConsole (true );
138
140
objectOutputStream .writeObject (request );
139
141
objectOutputStream .flush ();
140
142
Object obj = objectInputStream .readObject ();
@@ -179,14 +181,16 @@ private void conversation() throws Exception{
179
181
+ "For exiting the room type 'sv_exit' without quotes\n "
180
182
+ "For logging out type 'sv_logout' without quotes" );
181
183
182
- request = new Request (Request .Type .MSG .ordinal (),clientID ,roomId ,"joined the chat" );
184
+ request = new Request (Request .Type .STATUS_MSG .ordinal (),clientID ,roomId ,"joined the chat" );
185
+ request .setIsConsole (true );
183
186
184
187
objectOutputStream .writeObject (request );
185
188
objectOutputStream .flush ();
186
189
187
190
while (true ) {
188
191
cont = scanner .nextLine ();
189
192
request = new Request (Request .Type .MSG .ordinal (),clientID ,roomId ,cont );
193
+ request .setIsConsole (true );
190
194
objectOutputStream .writeObject (request );
191
195
objectOutputStream .flush ();
192
196
if ( cont .equals ("sv_exit" ) || cont .equals ("sv_logout" ))
@@ -213,7 +217,26 @@ public void run()
213
217
while (true ) {
214
218
try {
215
219
response = (Response ) objectInputStream .readObject ();
216
- Message .println (response .getContents ());
220
+
221
+ if (response .getId () == Response .Type .LOGOUT .ordinal ()) {
222
+ Message .println (response .getContents ());
223
+ }
224
+ else if (response .getId () == Response .Type .STATUS_MSG .ordinal () && response .getContents ().equals ("sv_exit_successful" )) {
225
+ Message .println (response .getContents ());
226
+ }
227
+ else if (response .getId () == Response .Type .STATUS_MSG .ordinal () && response .getContents ().contains ("Wrong username " )) {
228
+ Message .println (response .getContents ());
229
+ }
230
+ else if (response .getId () == Response .Type .MSG .ordinal () || response .getId () == Response .Type .STATUS_MSG .ordinal () || response .getId () == Response .Type .P_MSG .ordinal ()){
231
+ String msg = response .getContents ();
232
+ String name = msg .substring (0 , msg .indexOf (" " ));
233
+ msg = msg .substring (msg .indexOf (" " )+1 );
234
+ if (response .getId () == Response .Type .P_MSG .ordinal ())
235
+ Message .println ("\n <" + name + "> (Personal Message): " + msg );
236
+ else
237
+ Message .println ("\n <" + name + ">: " + msg );
238
+ }
239
+
217
240
if (response .getContents ().equals ("sv_exit_successful" )) {
218
241
synchronized (this ){
219
242
this .wait ();
@@ -226,12 +249,12 @@ else if(response.getId() == Response.Type.LOGOUT.ordinal()) {
226
249
}
227
250
228
251
} catch (ClassNotFoundException | IOException e ) {
229
- e .printStackTrace (new PrintWriter (errors ));
230
- LogFileWriter .Log (errors .toString ());
252
+ e .printStackTrace (new PrintWriter (Config . errors ));
253
+ LogFileWriter .Log (Config . errors .toString ());
231
254
break ;
232
255
} catch (InterruptedException e ) {
233
- e .printStackTrace (new PrintWriter (errors ));
234
- LogFileWriter .Log (errors .toString ());
256
+ e .printStackTrace (new PrintWriter (Config . errors ));
257
+ LogFileWriter .Log (Config . errors .toString ());
235
258
}
236
259
}
237
260
}
@@ -249,6 +272,7 @@ public void mainFunc() {
249
272
char [] pwd = console .readPassword ("Enter password: " ); //take the password and separate it from user name by # delimiter
250
273
cont += Hash .getHash (new String (pwd ));
251
274
request = new Request (Request .Type .SIGN_UP .ordinal (),clientID ,roomId ,cont );
275
+ request .setIsConsole (true );
252
276
Message .println ("Signing Up ... " );
253
277
}
254
278
else if (choice == 2 ) {
@@ -258,6 +282,7 @@ else if(choice == 2) {
258
282
char [] pwd = console .readPassword ("Enter password: " ); //take the password and separate it from user name by # delimiter
259
283
cont += Hash .getHash (new String (pwd ));
260
284
request = new Request (Request .Type .LOGIN .ordinal (),clientID ,roomId ,cont );
285
+ request .setIsConsole (true );
261
286
Message .println ("Logging In ... " );
262
287
}
263
288
else {
@@ -297,8 +322,8 @@ else if(response.getId() == Request.Type.LOGIN.ordinal()) {
297
322
298
323
}
299
324
catch (Exception e ) {
300
- e .printStackTrace (new PrintWriter (errors ));
301
- LogFileWriter .Log (errors .toString ());
325
+ e .printStackTrace (new PrintWriter (Config . errors ));
326
+ LogFileWriter .Log (Config . errors .toString ());
302
327
}
303
328
}
304
329
}
0 commit comments