Skip to content

Commit

Permalink
Merge pull request #9 from iamrohitsuthar/swingintegration
Browse files Browse the repository at this point in the history
Swingintegration
  • Loading branch information
iamrohitsuthar authored Aug 2, 2019
2 parents e5d926a + 652ee94 commit 521d27f
Showing 21 changed files with 859 additions and 232 deletions.
8 changes: 8 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -7,3 +7,11 @@
/bg.resized.png
/logo1.png
/background.png
/back_arrow.png
/back_arrow1.png
/53567(1).png
/exit(1).png
/exit.png
/logout(1).png
/logout.png
/logout(2).resized.png
Binary file added res/back_arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/exit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/logout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 6 additions & 10 deletions src/com/chatroom/Database/createdb.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.chatroom.Database;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.chatroom.configuration.Config;
import com.chatroom.others.LogFileWriter;
import com.chatroom.server.Server;


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

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
LogFileWriter.Log(e.getMessage());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
}
finally {
try {
connection.close(); //close the database connection
} catch (SQLException e) {
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
}
}
}
59 changes: 42 additions & 17 deletions src/com/chatroom/client/Client.java
Original file line number Diff line number Diff line change
@@ -9,14 +9,15 @@
import java.lang.Thread.State;
import java.net.Socket;
import java.util.Scanner;
import com.chatroom.configuration.Config;
import com.chatroom.models.Request;
import com.chatroom.models.Response;
import com.chatroom.others.Hash;
import com.chatroom.others.LogFileWriter;
import com.chatroom.others.Message;

public class Client {
private int clientID=-1;
private int clientID = -1;
private int roomId = -1;
private Scanner scanner = new Scanner(System.in);
private int choice;
@@ -29,13 +30,11 @@ public class Client {
private Request request = null;
private Response response = null;
private MessageListener messageListener;
public static StringWriter errors;

public Client(String host, int port) {
this.host = host;
this.port = port;
messageListener = new MessageListener();
errors = new StringWriter();
}

public void connect() {
@@ -45,8 +44,8 @@ public void connect() {
objectInputStream = new ObjectInputStream(socket.getInputStream());
mainFunc();
} catch (IOException e) {
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
}
}

@@ -78,14 +77,15 @@ private void mainOptions() {
}
}
catch (Exception e) {
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
}
}

private void logOut() throws Exception{
cont = "";
request = new Request(Request.Type.LOGOUT.ordinal(),clientID,roomId,cont);
request.setIsConsole(true);
objectOutputStream.writeObject(request);
objectOutputStream.flush();
response = (Response) objectInputStream.readObject();
@@ -105,6 +105,7 @@ private void viewRooms() throws Exception{
try {
cont = "";
request = new Request(Request.Type.VIEW_ROOMS.ordinal(),clientID,roomId,cont);
request.setIsConsole(true);
objectOutputStream.writeObject(request);
objectOutputStream.flush();
response = (Response) objectInputStream.readObject();
@@ -123,8 +124,8 @@ private void viewRooms() throws Exception{
}
}
catch(Exception e) {
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
}
}

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

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

request = new Request(Request.Type.MSG.ordinal(),clientID,roomId,"joined the chat");
request = new Request(Request.Type.STATUS_MSG.ordinal(),clientID,roomId,"joined the chat");
request.setIsConsole(true);

objectOutputStream.writeObject(request);
objectOutputStream.flush();

while(true) {
cont = scanner.nextLine();
request = new Request(Request.Type.MSG.ordinal(),clientID,roomId,cont);
request.setIsConsole(true);
objectOutputStream.writeObject(request);
objectOutputStream.flush();
if( cont.equals("sv_exit") || cont.equals("sv_logout"))
@@ -213,7 +217,26 @@ public void run()
while(true) {
try {
response = (Response) objectInputStream.readObject();
Message.println(response.getContents());

if(response.getId() == Response.Type.LOGOUT.ordinal()) {
Message.println(response.getContents());
}
else if(response.getId() == Response.Type.STATUS_MSG.ordinal() && response.getContents().equals("sv_exit_successful")) {
Message.println(response.getContents());
}
else if(response.getId() == Response.Type.STATUS_MSG.ordinal() && response.getContents().contains("Wrong username ")) {
Message.println(response.getContents());
}
else if(response.getId() == Response.Type.MSG.ordinal() || response.getId() == Response.Type.STATUS_MSG.ordinal() || response.getId() == Response.Type.P_MSG.ordinal()){
String msg = response.getContents();
String name = msg.substring(0, msg.indexOf(" "));
msg = msg.substring(msg.indexOf(" ")+1);
if(response.getId() == Response.Type.P_MSG.ordinal())
Message.println("\n<" + name + "> (Personal Message): " + msg);
else
Message.println("\n<" + name + ">: " + msg);
}

if(response.getContents().equals("sv_exit_successful")) {
synchronized(this){
this.wait();
@@ -226,12 +249,12 @@ else if(response.getId() == Response.Type.LOGOUT.ordinal()) {
}

} catch (ClassNotFoundException | IOException e) {
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
break;
} catch (InterruptedException e) {
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
}
}
}
@@ -249,6 +272,7 @@ public void mainFunc() {
char[] pwd = console.readPassword("Enter password: "); //take the password and separate it from user name by # delimiter
cont += Hash.getHash(new String(pwd));
request = new Request(Request.Type.SIGN_UP.ordinal(),clientID,roomId,cont);
request.setIsConsole(true);
Message.println("Signing Up ... ");
}
else if(choice == 2) {
@@ -258,6 +282,7 @@ else if(choice == 2) {
char[] pwd = console.readPassword("Enter password: "); //take the password and separate it from user name by # delimiter
cont += Hash.getHash(new String(pwd));
request = new Request(Request.Type.LOGIN.ordinal(),clientID,roomId,cont);
request.setIsConsole(true);
Message.println("Logging In ... ");
}
else {
@@ -297,8 +322,8 @@ else if(response.getId() == Request.Type.LOGIN.ordinal()) {

}
catch(Exception e) {
e.printStackTrace(new PrintWriter(errors));
LogFileWriter.Log(errors.toString());
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
}
}
}
26 changes: 20 additions & 6 deletions src/com/chatroom/client/ClientExec.java
Original file line number Diff line number Diff line change
@@ -3,11 +3,12 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import com.chatroom.configuration.Config;
import com.chatroom.others.LogFileWriter;
import com.chatroom.ui.MainSplash;

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

Client client = new Client(args[0],Integer.parseInt(args[1]));
client.connect();
if(args.length == 3 && args[2].equals("--console")) {
Client client = new Client(args[0],Integer.parseInt(args[1]));
client.connect();
}
else if(args.length == 2) {
try {
new MainSplash(new ClientModel(args[0], Integer.parseInt(args[1])));
} catch (NumberFormatException e) {
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
} catch (IOException e) {
e.printStackTrace(new PrintWriter(Config.errors));
LogFileWriter.Log(Config.errors.toString());
}
}
}
}
51 changes: 51 additions & 0 deletions src/com/chatroom/client/ClientModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.chatroom.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import com.chatroom.others.LogFileWriter;

public class ClientModel {
private int clientID=-1;
private int roomId = -1;
private String host = "";
private int port = -1;
public static ObjectOutputStream objectOutputStream;
public static ObjectInputStream objectInputStream;
private Socket socket;

public ClientModel(String host, int port) {
super();
this.host = host;
this.port = port;

// connecting
try {
socket = new Socket(host,port);
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}

public int getClientID() {
return clientID;
}

public void setClientID(int clientID) {
this.clientID = clientID;
}

public int getRoomId() {
return roomId;
}

public void setRoomId(int roomId) {
this.roomId = roomId;
}

}
4 changes: 3 additions & 1 deletion src/com/chatroom/configuration/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chatroom.configuration;

import java.awt.Color;
import java.io.StringWriter;

public class Config {
public static String USER_NAME = "";
@@ -12,5 +13,6 @@ public class Config {
public static final String CLIENT_ID = "client_id";
public static final String CLIENT_NAME = "client_name";
public static final String CLIENT_PWD = "client_pwd";
public static final Color colorPrimary = new Color(108, 99, 255);
public static final Color colorPrimary = new Color(108, 99, 255);;
public static StringWriter errors = new StringWriter();
}
11 changes: 10 additions & 1 deletion src/com/chatroom/models/Request.java
Original file line number Diff line number Diff line change
@@ -2,13 +2,14 @@
import java.io.Serializable;
public class Request implements Serializable{
public enum Type {
ACK,SIGN_UP, LOGIN, LOGOUT, CREATE_ROOM, JOIN_ROOM, VIEW_ROOMS, MSG;
ACK,SIGN_UP, LOGIN, LOGOUT, CREATE_ROOM, JOIN_ROOM, VIEW_ROOMS, MSG, STATUS_MSG;
}

int id;
int clientId;
int roomId;
String contents = "";
boolean isConsoleRequest = false;

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

public boolean getIsConsole() {
return isConsoleRequest;
}

//setter methods
public void setIsConsole(boolean consoleRequest) {
this.isConsoleRequest = consoleRequest;
}

public void setId(int id) {
this.id = id;
}
2 changes: 1 addition & 1 deletion src/com/chatroom/models/Response.java
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

public class Response implements Serializable{
public enum Type {
ACK,SIGN_UP, LOGIN, LOGOUT, CREATE_ROOM, JOIN_ROOM, VIEW_ROOMS,MSG;
ACK,SIGN_UP, LOGIN, LOGOUT, CREATE_ROOM, JOIN_ROOM, VIEW_ROOMS,MSG, STATUS_MSG, P_MSG;
}
int id;
String content;
Loading

0 comments on commit 521d27f

Please sign in to comment.