├── Server.class ├── IndexHandler.java ├── HomeOldHandler.java ├── SecretHandler.java ├── BlockingQueue.java ├── PoolThread.java ├── ThreadPool.java ├── server.java └── Server.java /Server.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gonzalezf/TareaRedes/master/Server.class -------------------------------------------------------------------------------- /IndexHandler.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | public class IndexHandler { 7 | 8 | IndexHandler (String name) { 9 | } 10 | 11 | public void run() { 12 | out.write("HTTP/1.1 200 OK\r\n"); 13 | out.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"); 14 | out.write("Server: Apache/0.8.4\r\n"); 15 | out.write("Content-Type: text/html\r\n"); 16 | out.write("Content-Length: 59\r\n"); 17 | out.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n"); 18 | out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n"); 19 | out.write("\r\n"); 20 | out.write("
Pagina bienvenida!
"); 22 | out.write("\n\n"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /HomeOldHandler.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | public class HomeOldHandler { 7 | 8 | IndexHandler (String name) { 9 | } 10 | 11 | public void run() { 12 | out.write("HTTP/1.1 301 Moved Permanently\r\n"); 13 | out.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"); 14 | out.write('Location: http://www.example.com/'); 15 | out.write("Server: Apache/0.8.4\r\n"); 16 | out.write("Content-Type: text/html\r\n"); 17 | out.write("Content-Length: 59\r\n"); 18 | out.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n"); 19 | out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n"); 20 | out.write("\r\n"); 21 | out.write("Pagina bienvenida!
"); 23 | out.write("\n\n"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /SecretHandler.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | public class SecretHandler { 7 | 8 | SecretHandler (String name) { 9 | } 10 | 11 | public void run() { 12 | out.write("HTTP/1.1 301 Moved Permanently\r\n"); 13 | out.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"); 14 | out.write('Location: http://www.example.com/'); 15 | out.write("Server: Apache/0.8.4\r\n"); 16 | out.write("Content-Type: text/html\r\n"); 17 | out.write("Content-Length: 59\r\n"); 18 | out.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n"); 19 | out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n"); 20 | out.write("\r\n"); 21 | out.write("Pagina bienvenida!
"); 23 | out.write("\n\n"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /BlockingQueue.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | public class BlockingQueue { 7 | 8 | private List queue = new LinkedList(); 9 | private int limit = 10; 10 | 11 | public BlockingQueue(int limit){ 12 | this.limit = limit; 13 | } 14 | 15 | 16 | public synchronized void enqueue(Object item) 17 | throws InterruptedException { 18 | while(this.queue.size() == this.limit) { 19 | wait(); 20 | } 21 | if(this.queue.size() == 0) { 22 | notifyAll(); 23 | } 24 | this.queue.add(item); 25 | } 26 | 27 | public synchronized Object dequeue() 28 | throws InterruptedException{ 29 | while(this.queue.size() == 0){ 30 | wait(); 31 | } 32 | if(this.queue.size() == this.limit){ 33 | notifyAll(); 34 | } 35 | return this.queue.remove(0); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /PoolThread.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | public class PoolThread extends Thread { 7 | 8 | private BlockingQueue taskQueue = null; 9 | private boolean isStopped = false; 10 | 11 | public PoolThread(BlockingQueue queue){ 12 | taskQueue = queue; 13 | } 14 | 15 | public void run(){ 16 | while(!isStopped()){ 17 | try{ 18 | Runnable runnable = (Runnable) taskQueue.dequeue(); 19 | runnable.run(); 20 | } catch(Exception e){ 21 | //log or otherwise report exception, 22 | //but keep pool thread alive. 23 | } 24 | } 25 | } 26 | 27 | public synchronized void doStop(){ 28 | isStopped = true; 29 | this.interrupt(); //break pool thread out of dequeue() call. 30 | } 31 | 32 | public synchronized boolean isStopped(){ 33 | return isStopped; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /ThreadPool.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | public class ThreadPool { 7 | 8 | private BlockingQueue taskQueue = null; 9 | private List