Browse Source

Slight optimisations and TODO-list

Tankernn 8 years ago
parent
commit
170d0d040c

+ 5 - 0
TODO.txt

@@ -0,0 +1,5 @@
+1. HTML
+2. File transfer
+3. Better protocol (non-POJO-based)
+4. Channel permissions
+5. Proper accounts, using a MySQL-database (optional)

+ 5 - 4
src/main/java/eu/tankernn/chat/client/ChatClientHandler.java

@@ -1,10 +1,10 @@
 package eu.tankernn.chat.client;
 
 import javax.swing.DefaultListModel;
+import javax.swing.JScrollBar;
 
 import eu.tankernn.chat.common.InfoPacket;
 import eu.tankernn.chat.common.MessagePacket;
-import eu.tankernn.chat.common.MessagePacket.MessageType;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
 import io.netty.handler.timeout.IdleStateEvent;
@@ -22,6 +22,9 @@ public class ChatClientHandler extends ChannelInboundHandlerAdapter {
 	    if (fromServer instanceof MessagePacket) {
 			MessagePacket mess = ((MessagePacket) fromServer);
 			chatWindow.chat.log(mess);
+			// Scroll down
+			JScrollBar s = chatWindow.scroll.getVerticalScrollBar();
+			s.setValue(s.getMaximum());
 		} else if (fromServer instanceof InfoPacket) {
 			InfoPacket info = (InfoPacket) fromServer;
 
@@ -31,9 +34,7 @@ public class ChatClientHandler extends ChannelInboundHandlerAdapter {
 			for (String user : info.usersOnline)
 				model.addElement(user);
 
-			chatWindow.userList.setModel(chatWindow.model);
-		} else if (fromServer instanceof String) {
-			chatWindow.chat.log(new MessagePacket((String) fromServer, MessageType.NORMAL));
+			chatWindow.userList.setModel(model);
 		}
 	}
 	

+ 15 - 7
src/main/java/eu/tankernn/chat/common/MessagePacket.java

@@ -20,19 +20,19 @@ public class MessagePacket implements Packet {
 	}
 
 	public MessageType messType = MessageType.NORMAL;
-	public String content = "", channel = "", sender = "";
-	public SimpleAttributeSet style = new SimpleAttributeSet();
+	public final String content, sender;
+	private String channel;
+	public final SimpleAttributeSet style = new SimpleAttributeSet();
 
 	public MessagePacket(String channel, String send, String con, MessageType messType) {
 		this.sender = send;
-		this.channel = channel;
+		this.setChannel(channel);
 		this.content = con;
 		this.messType = messType;
 	}
 
 	public MessagePacket(String con, MessageType messType) {
-		this.content = con;
-		this.messType = messType;
+		this("", "", con, messType);
 	}
 
 	public MessagePacket(String sender, String con) {
@@ -44,7 +44,7 @@ public class MessagePacket implements Packet {
 	}
 
 	public boolean validate() {
-		return (!content.equals("")) && content != null;
+		return content != null && !content.isEmpty();
 	}
 
 	@Override
@@ -87,9 +87,17 @@ public class MessagePacket implements Packet {
 				Date time = new Date();
 				timestamp = dateFormat.format(time);
 			}
-			String preInfoStr = timestamp + "<" + channel + ">" + sender + ": ";
+			String preInfoStr = timestamp + "<" + getChannel() + ">" + sender + ": ";
 			return preInfoStr + content;
 		} else
 			return content;
 	}
+
+	public String getChannel() {
+		return channel;
+	}
+
+	public void setChannel(String channel) {
+		this.channel = channel;
+	}
 }

+ 2 - 2
src/main/java/eu/tankernn/chat/server/Channel.java

@@ -3,7 +3,7 @@ package eu.tankernn.chat.server;
 import eu.tankernn.chat.common.MessagePacket;
 
 public class Channel extends ClientCollection {
-	public String name;
+	public final String name;
 	
 	public Channel(String name) {
 		this.name = name;
@@ -11,7 +11,7 @@ public class Channel extends ClientCollection {
 	
 	@Override
 	public synchronized void broadcast(MessagePacket mess) {
-		mess.channel = name;
+		mess.setChannel(name);
 		super.broadcast(mess);
 	}
 }

+ 4 - 5
src/main/java/eu/tankernn/chat/server/ClientCollection.java

@@ -30,7 +30,7 @@ public class ClientCollection {
 	 * @param mess
 	 *            The message object to send.
 	 */
-	public synchronized void broadcast(MessagePacket mess) {
+	public void broadcast(MessagePacket mess) {
 		if (mess.validate()) {
 			clients.forEach(c -> c.send(mess));
 			Server.getLocalClient().send(mess);
@@ -39,11 +39,10 @@ public class ClientCollection {
 
 	/**
 	 * Adds a user to the collection, checking that the user has not already
-	 * been added and that the the collection isn't "full".
+	 * been added.
 	 * 
 	 * @param user
 	 *            User to be added to collection.
-	 * @return
 	 */
 	public void add(Client user) {
 		if (clients.contains(user))
@@ -58,7 +57,7 @@ public class ClientCollection {
 	 * @param user
 	 *            User to remove.
 	 */
-	public synchronized void remove(Client user) {
+	public void remove(Client user) {
 		remove(user, false);
 	}
 
@@ -70,7 +69,7 @@ public class ClientCollection {
 	 * @param disconnect
 	 *            Should the user also be disconnected?
 	 */
-	public synchronized void remove(Client user, boolean disconnect) {
+	public void remove(Client user, boolean disconnect) {
 		if (disconnect)
 			user.disconnect();
 		clients.remove(user);

+ 6 - 5
src/main/java/eu/tankernn/chat/server/Server.java

@@ -10,6 +10,7 @@ import java.util.List;
 import java.util.Optional;
 import java.util.Properties;
 import java.util.logging.Level;
+import java.util.logging.LogManager;
 import java.util.logging.Logger;
 
 import eu.tankernn.chat.common.MessagePacket;
@@ -43,11 +44,11 @@ public class Server {
 	private static CommandRegistry commandRegistry;
 	
 	public static void main(String[] arg) {
-		//		try {
-		//			LogManager.getLogManager().readConfiguration(Server.class.getResourceAsStream("/logger.properties"));
-		//		} catch (SecurityException | IOException e2) {
-		//			log.log(Level.SEVERE, e2.getMessage(), e2);
-		//		}
+		try {
+			LogManager.getLogManager().readConfiguration(Server.class.getResourceAsStream("/logger.properties"));
+		} catch (SecurityException | IOException e2) {
+			log.log(Level.SEVERE, e2.getMessage(), e2);
+		}
 		log.info("Starting ChatServer version " + version + "...");
 		
 		log.fine("Loadning properties file...");

+ 1 - 1
src/main/resources/logger.properties

@@ -8,6 +8,6 @@ java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
 java.util.logging.FileHandler.level = INFO
 
 # Console Logging
-java.util.logging.ConsoleHandler.level = FINER
+java.util.logging.ConsoleHandler.level = INFO
 
 java.util.logging.SimpleFormatter.format = %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %5$s %6$s %n