summaryrefslogtreecommitdiff
path: root/src/Server/ChatServer.java
blob: 470573031d662335cf93d1bdacb0441b1c7c983a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package Server;
import java.net.*;
import java.io.*;
import java.util.LinkedList;
import java.util.Iterator;

public class ChatServer
{
	private ServerSocket service;
	private LinkedList<Connection> connections;
	
	public static final int portNum = 6667;
	public static ChatServer server;
	
	public void connectionRemove(Connection con)
	{
		this.connections.remove(con);
	}
	
	public Iterator<Connection> getConnectionIterator()
	{
		return this.connections.iterator();
	}
	
	ChatServer() throws IOException {
		this.service = new ServerSocket(portNum);
		this.connections = new LinkedList<Connection>();
	}
	/** Listening to port and opening a socket */
	public static void main(String[] args) throws IOException
	{
		try
		{
			server = new ChatServer();
		} catch (IOException e)
		{
			System.err.println("Failed listening on port " + portNum + " (" + e + ")");
			return;
		}

		Socket soc = null;
		try
		{
			while (true)
			{
				soc = server.service.accept();
				Connection con = new Connection(soc);
				server.connections.add(con);
				con.start();
			}
		} catch (IOException e)
		{
			System.out.println(e);
		}

	}
}

/** 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
 */