diff --git a/src/com/chatroom/Database/createdb.java b/src/com/chatroom/Database/createdb.java index 3675d20..552565e 100644 --- a/src/com/chatroom/Database/createdb.java +++ b/src/com/chatroom/Database/createdb.java @@ -1,15 +1,18 @@ 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; @@ -42,8 +45,8 @@ public static void main(String[] args) { try { connection.close(); //close the database connection } catch (SQLException e) { - // TODO Auto-generated catch block - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); } } } diff --git a/src/com/chatroom/client/Client.java b/src/com/chatroom/client/Client.java index 4756691..d6dc9be 100755 --- a/src/com/chatroom/client/Client.java +++ b/src/com/chatroom/client/Client.java @@ -5,6 +5,8 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; import java.lang.Thread.State; import java.net.Socket; import java.util.Scanner; @@ -14,6 +16,7 @@ import com.chatroom.others.Hash; import com.chatroom.others.LogFileWriter; import com.chatroom.others.Message; +import com.chatroom.server.Server; public class Client { private int clientID=-1; @@ -29,11 +32,13 @@ public class Client { private Request request = null; private Response response = null; private MessageListener messageListener; + private static StringWriter errors; public Client(String host, int port) { this.host = host; this.port = port; messageListener = new MessageListener(); + errors = new StringWriter(); //create the log file if it is not present String path = System.getProperty("user.home"); @@ -47,8 +52,8 @@ public Client(String host, int port) { try { f.createNewFile(); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); } } } @@ -60,7 +65,8 @@ public void connect() { objectInputStream = new ObjectInputStream(socket.getInputStream()); mainFunc(); } catch (IOException e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); } } @@ -92,7 +98,8 @@ private void mainOptions() { } } catch (Exception e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); } } @@ -136,7 +143,8 @@ private void viewRooms() throws Exception{ } } catch(Exception e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); } } @@ -238,10 +246,12 @@ else if(response.getId() == Response.Type.LOGOUT.ordinal()) { } } catch (ClassNotFoundException | IOException e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); break; } catch (InterruptedException e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); } } } @@ -307,7 +317,8 @@ else if(response.getId() == Request.Type.LOGIN.ordinal()) { } catch(Exception e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); } } } diff --git a/src/com/chatroom/others/Hash.java b/src/com/chatroom/others/Hash.java index 6ccb22c..7c555fd 100644 --- a/src/com/chatroom/others/Hash.java +++ b/src/com/chatroom/others/Hash.java @@ -1,10 +1,15 @@ package com.chatroom.others; +import java.io.PrintWriter; +import java.io.StringWriter; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import com.chatroom.server.Server; + public class Hash { + static StringWriter errors = new StringWriter(); public static String getHash(String input) { String hash; try { @@ -17,8 +22,8 @@ public static String getHash(String input) { } return hash; } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + e.printStackTrace(new PrintWriter(errors)); + LogFileWriter.Log(errors.toString()); return null; } diff --git a/src/com/chatroom/server/ClientThread.java b/src/com/chatroom/server/ClientThread.java index 252ab48..f104f0a 100755 --- a/src/com/chatroom/server/ClientThread.java +++ b/src/com/chatroom/server/ClientThread.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.io.PrintWriter; import java.net.Socket; import com.chatroom.models.Request; @@ -39,7 +40,8 @@ public void run() } } catch (IOException | ClassNotFoundException e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); if( e.getClass() == java.io.EOFException.class ) { // if client exited the terminal itself diff --git a/src/com/chatroom/server/Server.java b/src/com/chatroom/server/Server.java index 6743a8f..f78db6c 100755 --- a/src/com/chatroom/server/Server.java +++ b/src/com/chatroom/server/Server.java @@ -2,6 +2,8 @@ import java.io.IOException; import java.io.ObjectOutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; import java.net.ServerSocket; import java.net.Socket; import java.sql.Connection; @@ -242,7 +244,8 @@ public void run() } catch( Exception e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); } } @@ -290,7 +293,8 @@ public void run() responseHolder.objectOutputStream.flush(); } catch (Exception e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); } } } @@ -403,7 +407,8 @@ public void run() } catch( Exception e ) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); } finally { @@ -431,12 +436,14 @@ public void run() } catch(Exception e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); } } } catch (Exception e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); } } } @@ -480,7 +487,7 @@ public class Server { static HashMap messagesTrackHashmap; static ServerOperations serverOperations; static int roomIdGenerator = 0; - + static StringWriter errors; public Server(int port) { this.port = port; @@ -501,12 +508,14 @@ public Server(int port) messagesTrackQueue = new PriorityQueue(100,new MessagesTrackComparator()); serverOperations = new ServerOperations(); serverOperations.start(); + errors = new StringWriter(); try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(Config.DATABASE_URL+"/"+Config.DATABASE_NAME,Config.USER_NAME,Config.USER_PWD); } catch (SQLException | ClassNotFoundException e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); } } @@ -538,7 +547,8 @@ public static String getClientNameFromId(int id) { } catch (SQLException e) { // TODO Auto-generated catch block - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); return null; } @@ -563,7 +573,8 @@ public void connect() { } } catch (IOException e) { - LogFileWriter.Log(e.getMessage()); + e.printStackTrace(new PrintWriter(Server.errors)); + LogFileWriter.Log(Server.errors.toString()); } } diff --git a/src/com/chatroom/server/ServerOperations.java b/src/com/chatroom/server/ServerOperations.java index b4d56af..e0d2e47 100755 --- a/src/com/chatroom/server/ServerOperations.java +++ b/src/com/chatroom/server/ServerOperations.java @@ -13,7 +13,6 @@ public void run() { int input; while(true) { Message.println("1. For Messages logs"); - //TODO : kitne user or rooms hai or queue ki size kitni hai Message.println("2. Server Shutdown"); input = scanner.nextInt(); if(input == 1) {