From 7a495aa06fb1c48148d5c2c4b139f5a477f222d6 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sun, 8 Dec 2013 04:15:48 +0000 Subject: [PATCH] git-svn-id: svn://imanolbarba.net/PAD@23 c2ee353e-ed0d-4329-bf56-03aec153487f --- JChatServer/bin/pad/prac2/JChat.class | Bin 980 -> 0 bytes JChatServer/bin/pad/prac2/MyServerSocket.class | Bin 1596 -> 0 bytes JChatServer/bin/pad/prac2/MySocket.class | Bin 2247 -> 0 bytes JChatServer/bin/pad/prac2/Server.class | Bin 1103 -> 0 bytes JChatServer/bin/pad/prac2/Worker.class | Bin 658 -> 0 bytes JChatServer/src/pad/prac2/JChat.java | 13 +++++++++++-- JChatServer/src/pad/prac2/MyServerSocket.java | 21 +++++++++++---------- JChatServer/src/pad/prac2/MySocket.java | 15 ++++++++++++++- JChatServer/src/pad/prac2/Server.java | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- JChatServer/src/pad/prac2/Worker.java | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 10 files changed, 163 insertions(+), 24 deletions(-) diff --git a/JChatServer/bin/pad/prac2/JChat.class b/JChatServer/bin/pad/prac2/JChat.class index 6721607..698da9f 100644 Binary files a/JChatServer/bin/pad/prac2/JChat.class and b/JChatServer/bin/pad/prac2/JChat.class differ diff --git a/JChatServer/bin/pad/prac2/MyServerSocket.class b/JChatServer/bin/pad/prac2/MyServerSocket.class index 0fbc3e6..a540016 100644 Binary files a/JChatServer/bin/pad/prac2/MyServerSocket.class and b/JChatServer/bin/pad/prac2/MyServerSocket.class differ diff --git a/JChatServer/bin/pad/prac2/MySocket.class b/JChatServer/bin/pad/prac2/MySocket.class index 9752fbc..5550f1f 100644 Binary files a/JChatServer/bin/pad/prac2/MySocket.class and b/JChatServer/bin/pad/prac2/MySocket.class differ diff --git a/JChatServer/bin/pad/prac2/Server.class b/JChatServer/bin/pad/prac2/Server.class index e525627..64c6e2e 100644 Binary files a/JChatServer/bin/pad/prac2/Server.class and b/JChatServer/bin/pad/prac2/Server.class differ diff --git a/JChatServer/bin/pad/prac2/Worker.class b/JChatServer/bin/pad/prac2/Worker.class index f53ae7d..3cfaa44 100644 Binary files a/JChatServer/bin/pad/prac2/Worker.class and b/JChatServer/bin/pad/prac2/Worker.class differ diff --git a/JChatServer/src/pad/prac2/JChat.java b/JChatServer/src/pad/prac2/JChat.java index 762be1f..2cf847a 100644 --- a/JChatServer/src/pad/prac2/JChat.java +++ b/JChatServer/src/pad/prac2/JChat.java @@ -7,13 +7,22 @@ public class JChat public static void main(String[] args) { String ip; - int port; + int port, roomSize; Scanner in = new Scanner(System.in); System.out.print("IP: "); ip = in.nextLine(); System.out.print("Port: "); port = in.nextInt(); + System.out.print("Size of chatroom: "); + roomSize = in.nextInt(); in.close(); - Server serv = new Server(ip,port); + Server serv = new Server(ip,port,roomSize); + serv.startServer(); } + /* + * TODO + * + * capture SIGINT + * + */ } diff --git a/JChatServer/src/pad/prac2/MyServerSocket.java b/JChatServer/src/pad/prac2/MyServerSocket.java index 0152598..05b455c 100644 --- a/JChatServer/src/pad/prac2/MyServerSocket.java +++ b/JChatServer/src/pad/prac2/MyServerSocket.java @@ -1,28 +1,29 @@ package pad.prac2; -import java.io.BufferedReader; import java.io.IOException; -import java.io.PrintWriter; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.SocketAddress; public class MyServerSocket extends ServerSocket { - private PrintWriter output; - private BufferedReader input; - - public MyServerSocket(PrintWriter o, BufferedReader i) throws IOException + public MyServerSocket() throws IOException { super(); - output = o; - input = i; } public MySocket accept() { - MySocket incoming = accept(); - return incoming; + try + { + MySocket incoming = (MySocket)super.accept(); + return incoming; + } + catch(IOException ioExc) + { + System.out.println("TCP: Error accepting connection"); + return null; + } } public void bind(String ip, int port) diff --git a/JChatServer/src/pad/prac2/MySocket.java b/JChatServer/src/pad/prac2/MySocket.java index 59a86b2..3f317d6 100644 --- a/JChatServer/src/pad/prac2/MySocket.java +++ b/JChatServer/src/pad/prac2/MySocket.java @@ -70,7 +70,20 @@ public class MySocket extends Socket catch(IOException ioExc) { System.out.println("TCP: Error retrieving data from remote endpoint"); - return ""; + return null; + } + } + + public String readLine() + { + try + { + return input.readLine(); + } + catch(IOException ioExc) + { + System.out.println("TCP: Error retrieving data from remote endpoint"); + return null; } } diff --git a/JChatServer/src/pad/prac2/Server.java b/JChatServer/src/pad/prac2/Server.java index cbaf58a..e577123 100644 --- a/JChatServer/src/pad/prac2/Server.java +++ b/JChatServer/src/pad/prac2/Server.java @@ -4,21 +4,73 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; +import java.util.Collection; +import java.util.Iterator; +import java.util.concurrent.ConcurrentHashMap; public class Server { private MyServerSocket ss; + private BufferedReader stdin; + private PrintWriter stdout; + private int roomSize; + private boolean run; - public Server(String ip, int port) + private Worker[] rWorkerPool; + private Worker[] wWorkerPool; + + private ConcurrentHashMap activeConnections; + + public Server(String ip, int port, int rS) { try { - ss = new MyServerSocket(new PrintWriter(System.out),new BufferedReader(new InputStreamReader(System.in))); + ss = new MyServerSocket(); + stdout = new PrintWriter(System.out); + stdin = new BufferedReader(new InputStreamReader(System.in)); + roomSize = rS; + rWorkerPool = new Worker[roomSize]; + wWorkerPool = new Worker[roomSize]; } catch(IOException ioExc) { System.out.println("TCP: Error initializating server socket"); } } - + + public void startServer() + { + + } + + public void startWorker(Worker r, Worker w, MySocket s) + { + if(r == null) + { + r = new Worker(s,null,stdout); + } + r.awake(); + if(w == null) + { + w = new Worker(s,stdin,null); + } + w.awake(); + } + + public void finishWorkers() + { + Collection worerke= activeConnections.values(); + Iterator it = sockets.iterator(); + while(it.hasNext()) + { + it.next().close(); + } + } + + public void killServer() + { + run = false; + ss.close(); + finishWorkers(); + } } diff --git a/JChatServer/src/pad/prac2/Worker.java b/JChatServer/src/pad/prac2/Worker.java index 3e93669..47daab0 100644 --- a/JChatServer/src/pad/prac2/Worker.java +++ b/JChatServer/src/pad/prac2/Worker.java @@ -1,6 +1,7 @@ package pad.prac2; import java.io.BufferedReader; +import java.io.IOException; import java.io.PrintWriter; public class Worker extends Thread @@ -8,24 +9,87 @@ public class Worker extends Thread private PrintWriter output; private BufferedReader input; private MySocket sock; + private boolean run; + private boolean kill; + private boolean sleep; + private Server serv; - public Worker(MySocket s, PrintWriter o, BufferedReader i) + public Worker(Server srv, MySocket s, BufferedReader i, PrintWriter o) { sock = s; output = o; input = i; + run = false; + kill = false; + sleep = true; + serv = srv; + } + + public void finish() + { + kill = true; + run = false; + sleep = false; + sock.close(); } public void run() { - if(output == null) - { - - } - else if(input == null) + while(!kill) { - + while(sleep) + { + try + { + wait(); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + while(run) + { + if(output == null && input != null) + { + String str, length; + while(true) + { + try + { + str = input.readLine(); + length = new Integer(str.length()).toString(); + sock.write(length, length.length()); + sock.write(str, str.length()); + } + catch(IOException ioExc) + { + run = false; + sleep = true; + break; + } + } + } + else if(input == null && output != null) + { + String len, str; + while((len = sock.readLine()) != null) + { + int length = Integer.parseInt(len); + str = sock.read(length); + output.println(str); + } + run = false; + sleep = true; + } + } } } - + + public void awake() + { + run = true; + sleep = false; + this.notify(); + } } -- libgit2 0.22.2