From 4b64e500799da46fc406c3d0217eb89afc8e07b1 Mon Sep 17 00:00:00 2001 From: Dor Bivas Date: Sat, 29 Mar 2014 15:43:49 +0300 Subject: ChatServer: dict of nicks. * add \ remove nicks. --- src/Server/ChatServer.java | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Server/ChatServer.java b/src/Server/ChatServer.java index 4705730..363a747 100644 --- a/src/Server/ChatServer.java +++ b/src/Server/ChatServer.java @@ -1,6 +1,7 @@ package Server; import java.net.*; import java.io.*; +import java.util.Hashtable; import java.util.LinkedList; import java.util.Iterator; @@ -8,6 +9,7 @@ public class ChatServer { private ServerSocket service; private LinkedList connections; + private Hashtable nicks; public static final int portNum = 6667; public static ChatServer server; @@ -25,7 +27,29 @@ public class ChatServer ChatServer() throws IOException { this.service = new ServerSocket(portNum); this.connections = new LinkedList(); + this.nicks = new Hashtable (); } + + /** check if nick already used. if not add it. + * @param nick */ + public boolean addNick (String nick) + { + if (this.nicks.get(nick) != null) + { + return false; + } + + this.nicks.put(nick, true); // this value (true) has no meaning. + return true; + } + + /** remove nick which is no longer in use. + * @param nick */ + public void removeNick (String nick) + { + this.nicks.remove(nick); + } + /** Listening to port and opening a socket */ public static void main(String[] args) throws IOException { @@ -55,17 +79,3 @@ public class ChatServer } } - -/** Tasks - * - * * Allow listening on a different port (from command-line?) - * * Have a test server running on cohens.org.il - * * Send requests to all clients (use list) - * - When a client disconnects - remove it from connections list. - * - temporary: When we get a PRIVMSG, iterate over connections and write message and every client - * - When we get a PRIVMSG, iterate over connections and write the message to each client - * * Don't allow two clients with the same nick - * * When passing a message (PRIVMSG) - include the sender's nick. - * * Keep channels up: PING occasionally - * * Graphical interface for server - */ -- cgit v1.2.3