Commit 7a495aa06fb1c48148d5c2c4b139f5a477f222d6

Authored by Imanol-Mikel Barba Sabariego
1 parent bbe83807

git-svn-id: svn://imanolbarba.net/PAD@23 c2ee353e-ed0d-4329-bf56-03aec153487f

JChatServer/bin/pad/prac2/JChat.class
No preview for this file type
JChatServer/bin/pad/prac2/MyServerSocket.class
No preview for this file type
JChatServer/bin/pad/prac2/MySocket.class
No preview for this file type
JChatServer/bin/pad/prac2/Server.class
No preview for this file type
JChatServer/bin/pad/prac2/Worker.class
No preview for this file type
JChatServer/src/pad/prac2/JChat.java
... ... @@ -7,13 +7,22 @@ public class JChat
7 7 public static void main(String[] args)
8 8 {
9 9 String ip;
10   - int port;
  10 + int port, roomSize;
11 11 Scanner in = new Scanner(System.in);
12 12 System.out.print("IP: ");
13 13 ip = in.nextLine();
14 14 System.out.print("Port: ");
15 15 port = in.nextInt();
  16 + System.out.print("Size of chatroom: ");
  17 + roomSize = in.nextInt();
16 18 in.close();
17   - Server serv = new Server(ip,port);
  19 + Server serv = new Server(ip,port,roomSize);
  20 + serv.startServer();
18 21 }
  22 + /*
  23 + * TODO
  24 + *
  25 + * capture SIGINT
  26 + *
  27 + */
19 28 }
... ...
JChatServer/src/pad/prac2/MyServerSocket.java
1 1 package pad.prac2;
2 2  
3   -import java.io.BufferedReader;
4 3 import java.io.IOException;
5   -import java.io.PrintWriter;
6 4 import java.net.InetSocketAddress;
7 5 import java.net.ServerSocket;
8 6 import java.net.SocketAddress;
9 7  
10 8 public class MyServerSocket extends ServerSocket
11 9 {
12   - private PrintWriter output;
13   - private BufferedReader input;
14   -
15   - public MyServerSocket(PrintWriter o, BufferedReader i) throws IOException
  10 + public MyServerSocket() throws IOException
16 11 {
17 12 super();
18   - output = o;
19   - input = i;
20 13 }
21 14  
22 15 public MySocket accept()
23 16 {
24   - MySocket incoming = accept();
25   - return incoming;
  17 + try
  18 + {
  19 + MySocket incoming = (MySocket)super.accept();
  20 + return incoming;
  21 + }
  22 + catch(IOException ioExc)
  23 + {
  24 + System.out.println("TCP: Error accepting connection");
  25 + return null;
  26 + }
26 27 }
27 28  
28 29 public void bind(String ip, int port)
... ...
JChatServer/src/pad/prac2/MySocket.java
... ... @@ -70,7 +70,20 @@ public class MySocket extends Socket
70 70 catch(IOException ioExc)
71 71 {
72 72 System.out.println("TCP: Error retrieving data from remote endpoint");
73   - return "";
  73 + return null;
  74 + }
  75 + }
  76 +
  77 + public String readLine()
  78 + {
  79 + try
  80 + {
  81 + return input.readLine();
  82 + }
  83 + catch(IOException ioExc)
  84 + {
  85 + System.out.println("TCP: Error retrieving data from remote endpoint");
  86 + return null;
74 87 }
75 88 }
76 89  
... ...
JChatServer/src/pad/prac2/Server.java
... ... @@ -4,21 +4,73 @@ import java.io.BufferedReader;
4 4 import java.io.IOException;
5 5 import java.io.InputStreamReader;
6 6 import java.io.PrintWriter;
  7 +import java.util.Collection;
  8 +import java.util.Iterator;
  9 +import java.util.concurrent.ConcurrentHashMap;
7 10  
8 11 public class Server
9 12 {
10 13 private MyServerSocket ss;
  14 + private BufferedReader stdin;
  15 + private PrintWriter stdout;
  16 + private int roomSize;
  17 + private boolean run;
11 18  
12   - public Server(String ip, int port)
  19 + private Worker[] rWorkerPool;
  20 + private Worker[] wWorkerPool;
  21 +
  22 + private ConcurrentHashMap<String,MySocket> activeConnections;
  23 +
  24 + public Server(String ip, int port, int rS)
13 25 {
14 26 try
15 27 {
16   - ss = new MyServerSocket(new PrintWriter(System.out),new BufferedReader(new InputStreamReader(System.in)));
  28 + ss = new MyServerSocket();
  29 + stdout = new PrintWriter(System.out);
  30 + stdin = new BufferedReader(new InputStreamReader(System.in));
  31 + roomSize = rS;
  32 + rWorkerPool = new Worker[roomSize];
  33 + wWorkerPool = new Worker[roomSize];
17 34 }
18 35 catch(IOException ioExc)
19 36 {
20 37 System.out.println("TCP: Error initializating server socket");
21 38 }
22 39 }
23   -
  40 +
  41 + public void startServer()
  42 + {
  43 +
  44 + }
  45 +
  46 + public void startWorker(Worker r, Worker w, MySocket s)
  47 + {
  48 + if(r == null)
  49 + {
  50 + r = new Worker(s,null,stdout);
  51 + }
  52 + r.awake();
  53 + if(w == null)
  54 + {
  55 + w = new Worker(s,stdin,null);
  56 + }
  57 + w.awake();
  58 + }
  59 +
  60 + public void finishWorkers()
  61 + {
  62 + Collection<Worker> worerke= activeConnections.values();
  63 + Iterator<MySocket> it = sockets.iterator();
  64 + while(it.hasNext())
  65 + {
  66 + it.next().close();
  67 + }
  68 + }
  69 +
  70 + public void killServer()
  71 + {
  72 + run = false;
  73 + ss.close();
  74 + finishWorkers();
  75 + }
24 76 }
... ...
JChatServer/src/pad/prac2/Worker.java
1 1 package pad.prac2;
2 2  
3 3 import java.io.BufferedReader;
  4 +import java.io.IOException;
4 5 import java.io.PrintWriter;
5 6  
6 7 public class Worker extends Thread
... ... @@ -8,24 +9,87 @@ public class Worker extends Thread
8 9 private PrintWriter output;
9 10 private BufferedReader input;
10 11 private MySocket sock;
  12 + private boolean run;
  13 + private boolean kill;
  14 + private boolean sleep;
  15 + private Server serv;
11 16  
12   - public Worker(MySocket s, PrintWriter o, BufferedReader i)
  17 + public Worker(Server srv, MySocket s, BufferedReader i, PrintWriter o)
13 18 {
14 19 sock = s;
15 20 output = o;
16 21 input = i;
  22 + run = false;
  23 + kill = false;
  24 + sleep = true;
  25 + serv = srv;
  26 + }
  27 +
  28 + public void finish()
  29 + {
  30 + kill = true;
  31 + run = false;
  32 + sleep = false;
  33 + sock.close();
17 34 }
18 35  
19 36 public void run()
20 37 {
21   - if(output == null)
22   - {
23   -
24   - }
25   - else if(input == null)
  38 + while(!kill)
26 39 {
27   -
  40 + while(sleep)
  41 + {
  42 + try
  43 + {
  44 + wait();
  45 + }
  46 + catch (InterruptedException e)
  47 + {
  48 + e.printStackTrace();
  49 + }
  50 + }
  51 + while(run)
  52 + {
  53 + if(output == null && input != null)
  54 + {
  55 + String str, length;
  56 + while(true)
  57 + {
  58 + try
  59 + {
  60 + str = input.readLine();
  61 + length = new Integer(str.length()).toString();
  62 + sock.write(length, length.length());
  63 + sock.write(str, str.length());
  64 + }
  65 + catch(IOException ioExc)
  66 + {
  67 + run = false;
  68 + sleep = true;
  69 + break;
  70 + }
  71 + }
  72 + }
  73 + else if(input == null && output != null)
  74 + {
  75 + String len, str;
  76 + while((len = sock.readLine()) != null)
  77 + {
  78 + int length = Integer.parseInt(len);
  79 + str = sock.read(length);
  80 + output.println(str);
  81 + }
  82 + run = false;
  83 + sleep = true;
  84 + }
  85 + }
28 86 }
29 87 }
30   -
  88 +
  89 + public void awake()
  90 + {
  91 + run = true;
  92 + sleep = false;
  93 + this.notify();
  94 + }
31 95 }
... ...