Skip to content

Commit 521d27f

Browse files
Merge pull request #9 from iamrohitsuthar/swingintegration
Swingintegration
2 parents e5d926a + 652ee94 commit 521d27f

21 files changed

+859
-232
lines changed

bin/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@
77
/bg.resized.png
88
/logo1.png
99
/background.png
10+
/back_arrow.png
11+
/back_arrow1.png
12+
/53567(1).png
13+
/exit(1).png
14+
/exit.png
15+
/logout(1).png
16+
/logout.png
17+
/logout(2).resized.png

res/back_arrow.png

1.6 KB
Loading

res/exit.png

1.09 KB
Loading

res/logout.png

1.29 KB
Loading

src/com/chatroom/Database/createdb.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.chatroom.Database;
22

33
import java.io.PrintWriter;
4-
import java.io.StringWriter;
54
import java.sql.Connection;
65
import java.sql.DriverManager;
76
import java.sql.SQLException;
8-
97
import com.chatroom.configuration.Config;
108
import com.chatroom.others.LogFileWriter;
11-
import com.chatroom.server.Server;
129

1310

1411
public class createdb {
15-
static StringWriter errors = new StringWriter();
1612
public static void main(String[] args) {
1713
Connection connection = null;
1814
java.sql.Statement statement= null;
@@ -35,18 +31,18 @@ public static void main(String[] args) {
3531
statement.executeUpdate(Queries);
3632

3733
} catch (ClassNotFoundException e) {
38-
// TODO Auto-generated catch block
39-
LogFileWriter.Log(e.getMessage());
34+
e.printStackTrace(new PrintWriter(Config.errors));
35+
LogFileWriter.Log(Config.errors.toString());
4036
} catch (SQLException e) {
41-
// TODO Auto-generated catch block
42-
LogFileWriter.Log(e.getMessage());
37+
e.printStackTrace(new PrintWriter(Config.errors));
38+
LogFileWriter.Log(Config.errors.toString());
4339
}
4440
finally {
4541
try {
4642
connection.close(); //close the database connection
4743
} catch (SQLException e) {
48-
e.printStackTrace(new PrintWriter(errors));
49-
LogFileWriter.Log(errors.toString());
44+
e.printStackTrace(new PrintWriter(Config.errors));
45+
LogFileWriter.Log(Config.errors.toString());
5046
}
5147
}
5248
}

src/com/chatroom/client/Client.java

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import java.lang.Thread.State;
1010
import java.net.Socket;
1111
import java.util.Scanner;
12+
import com.chatroom.configuration.Config;
1213
import com.chatroom.models.Request;
1314
import com.chatroom.models.Response;
1415
import com.chatroom.others.Hash;
1516
import com.chatroom.others.LogFileWriter;
1617
import com.chatroom.others.Message;
1718

1819
public class Client {
19-
private int clientID=-1;
20+
private int clientID = -1;
2021
private int roomId = -1;
2122
private Scanner scanner = new Scanner(System.in);
2223
private int choice;
@@ -29,13 +30,11 @@ public class Client {
2930
private Request request = null;
3031
private Response response = null;
3132
private MessageListener messageListener;
32-
public static StringWriter errors;
3333

3434
public Client(String host, int port) {
3535
this.host = host;
3636
this.port = port;
3737
messageListener = new MessageListener();
38-
errors = new StringWriter();
3938
}
4039

4140
public void connect() {
@@ -45,8 +44,8 @@ public void connect() {
4544
objectInputStream = new ObjectInputStream(socket.getInputStream());
4645
mainFunc();
4746
} 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());
5049
}
5150
}
5251

@@ -78,14 +77,15 @@ private void mainOptions() {
7877
}
7978
}
8079
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());
8382
}
8483
}
8584

8685
private void logOut() throws Exception{
8786
cont = "";
8887
request = new Request(Request.Type.LOGOUT.ordinal(),clientID,roomId,cont);
88+
request.setIsConsole(true);
8989
objectOutputStream.writeObject(request);
9090
objectOutputStream.flush();
9191
response = (Response) objectInputStream.readObject();
@@ -105,6 +105,7 @@ private void viewRooms() throws Exception{
105105
try {
106106
cont = "";
107107
request = new Request(Request.Type.VIEW_ROOMS.ordinal(),clientID,roomId,cont);
108+
request.setIsConsole(true);
108109
objectOutputStream.writeObject(request);
109110
objectOutputStream.flush();
110111
response = (Response) objectInputStream.readObject();
@@ -123,8 +124,8 @@ private void viewRooms() throws Exception{
123124
}
124125
}
125126
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());
128129
}
129130
}
130131

@@ -135,6 +136,7 @@ private void createAndJoinRoom(String rName, boolean create) throws Exception
135136
else
136137
request = new Request(Request.Type.JOIN_ROOM.ordinal(),clientID,roomId,cont);
137138

139+
request.setIsConsole(true);
138140
objectOutputStream.writeObject(request);
139141
objectOutputStream.flush();
140142
Object obj = objectInputStream.readObject();
@@ -179,14 +181,16 @@ private void conversation() throws Exception{
179181
+ "For exiting the room type 'sv_exit' without quotes\n"
180182
+ "For logging out type 'sv_logout' without quotes");
181183

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);
183186

184187
objectOutputStream.writeObject(request);
185188
objectOutputStream.flush();
186189

187190
while(true) {
188191
cont = scanner.nextLine();
189192
request = new Request(Request.Type.MSG.ordinal(),clientID,roomId,cont);
193+
request.setIsConsole(true);
190194
objectOutputStream.writeObject(request);
191195
objectOutputStream.flush();
192196
if( cont.equals("sv_exit") || cont.equals("sv_logout"))
@@ -213,7 +217,26 @@ public void run()
213217
while(true) {
214218
try {
215219
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+
217240
if(response.getContents().equals("sv_exit_successful")) {
218241
synchronized(this){
219242
this.wait();
@@ -226,12 +249,12 @@ else if(response.getId() == Response.Type.LOGOUT.ordinal()) {
226249
}
227250

228251
} 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());
231254
break;
232255
} 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());
235258
}
236259
}
237260
}
@@ -249,6 +272,7 @@ public void mainFunc() {
249272
char[] pwd = console.readPassword("Enter password: "); //take the password and separate it from user name by # delimiter
250273
cont += Hash.getHash(new String(pwd));
251274
request = new Request(Request.Type.SIGN_UP.ordinal(),clientID,roomId,cont);
275+
request.setIsConsole(true);
252276
Message.println("Signing Up ... ");
253277
}
254278
else if(choice == 2) {
@@ -258,6 +282,7 @@ else if(choice == 2) {
258282
char[] pwd = console.readPassword("Enter password: "); //take the password and separate it from user name by # delimiter
259283
cont += Hash.getHash(new String(pwd));
260284
request = new Request(Request.Type.LOGIN.ordinal(),clientID,roomId,cont);
285+
request.setIsConsole(true);
261286
Message.println("Logging In ... ");
262287
}
263288
else {
@@ -297,8 +322,8 @@ else if(response.getId() == Request.Type.LOGIN.ordinal()) {
297322

298323
}
299324
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());
302327
}
303328
}
304329
}

src/com/chatroom/client/ClientExec.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.PrintWriter;
6-
6+
import com.chatroom.configuration.Config;
77
import com.chatroom.others.LogFileWriter;
8+
import com.chatroom.ui.MainSplash;
89

910
public class ClientExec {
10-
public static void main(String[] args) {
11+
public static void main(String[] args) {
1112
//create the log file if it is not present
1213
String path = System.getProperty("user.home");
1314
path += "/CHATROOM";
@@ -20,12 +21,25 @@ public static void main(String[] args) {
2021
try {
2122
f.createNewFile();
2223
} catch (IOException e) {
23-
e.printStackTrace(new PrintWriter(Client.errors));
24-
LogFileWriter.Log(Client.errors.toString());
24+
e.printStackTrace(new PrintWriter(Config.errors));
25+
LogFileWriter.Log(Config.errors.toString());
2526
}
2627
}
2728

28-
Client client = new Client(args[0],Integer.parseInt(args[1]));
29-
client.connect();
29+
if(args.length == 3 && args[2].equals("--console")) {
30+
Client client = new Client(args[0],Integer.parseInt(args[1]));
31+
client.connect();
32+
}
33+
else if(args.length == 2) {
34+
try {
35+
new MainSplash(new ClientModel(args[0], Integer.parseInt(args[1])));
36+
} catch (NumberFormatException e) {
37+
e.printStackTrace(new PrintWriter(Config.errors));
38+
LogFileWriter.Log(Config.errors.toString());
39+
} catch (IOException e) {
40+
e.printStackTrace(new PrintWriter(Config.errors));
41+
LogFileWriter.Log(Config.errors.toString());
42+
}
43+
}
3044
}
3145
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.chatroom.client;
2+
3+
import java.io.IOException;
4+
import java.io.ObjectInputStream;
5+
import java.io.ObjectOutputStream;
6+
import java.io.PrintWriter;
7+
import java.net.Socket;
8+
9+
import com.chatroom.others.LogFileWriter;
10+
11+
public class ClientModel {
12+
private int clientID=-1;
13+
private int roomId = -1;
14+
private String host = "";
15+
private int port = -1;
16+
public static ObjectOutputStream objectOutputStream;
17+
public static ObjectInputStream objectInputStream;
18+
private Socket socket;
19+
20+
public ClientModel(String host, int port) {
21+
super();
22+
this.host = host;
23+
this.port = port;
24+
25+
// connecting
26+
try {
27+
socket = new Socket(host,port);
28+
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
29+
objectInputStream = new ObjectInputStream(socket.getInputStream());
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
35+
public int getClientID() {
36+
return clientID;
37+
}
38+
39+
public void setClientID(int clientID) {
40+
this.clientID = clientID;
41+
}
42+
43+
public int getRoomId() {
44+
return roomId;
45+
}
46+
47+
public void setRoomId(int roomId) {
48+
this.roomId = roomId;
49+
}
50+
51+
}

src/com/chatroom/configuration/Config.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.chatroom.configuration;
22

33
import java.awt.Color;
4+
import java.io.StringWriter;
45

56
public class Config {
67
public static String USER_NAME = "";
@@ -12,5 +13,6 @@ public class Config {
1213
public static final String CLIENT_ID = "client_id";
1314
public static final String CLIENT_NAME = "client_name";
1415
public static final String CLIENT_PWD = "client_pwd";
15-
public static final Color colorPrimary = new Color(108, 99, 255);
16+
public static final Color colorPrimary = new Color(108, 99, 255);;
17+
public static StringWriter errors = new StringWriter();
1618
}

src/com/chatroom/models/Request.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import java.io.Serializable;
33
public class Request implements Serializable{
44
public enum Type {
5-
ACK,SIGN_UP, LOGIN, LOGOUT, CREATE_ROOM, JOIN_ROOM, VIEW_ROOMS, MSG;
5+
ACK,SIGN_UP, LOGIN, LOGOUT, CREATE_ROOM, JOIN_ROOM, VIEW_ROOMS, MSG, STATUS_MSG;
66
}
77

88
int id;
99
int clientId;
1010
int roomId;
1111
String contents = "";
12+
boolean isConsoleRequest = false;
1213

1314
public Request(int id, int clientId, int roomId, String contents) {
1415
this.id = id;
@@ -34,7 +35,15 @@ public String getContents() {
3435
return contents;
3536
}
3637

38+
public boolean getIsConsole() {
39+
return isConsoleRequest;
40+
}
41+
3742
//setter methods
43+
public void setIsConsole(boolean consoleRequest) {
44+
this.isConsoleRequest = consoleRequest;
45+
}
46+
3847
public void setId(int id) {
3948
this.id = id;
4049
}

src/com/chatroom/models/Response.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class Response implements Serializable{
66
public enum Type {
7-
ACK,SIGN_UP, LOGIN, LOGOUT, CREATE_ROOM, JOIN_ROOM, VIEW_ROOMS,MSG;
7+
ACK,SIGN_UP, LOGIN, LOGOUT, CREATE_ROOM, JOIN_ROOM, VIEW_ROOMS,MSG, STATUS_MSG, P_MSG;
88
}
99
int id;
1010
String content;

0 commit comments

Comments
 (0)