Update of /cvsroot/mrpostman/mrpostman/src/org/mrbook/mrpostman/pop
In directory sc8-pr-cvs1:/tmp/cvs-serv671/src/org/mrbook/mrpostman/pop
Modified Files:
PopConnection.java PopServer.java
Log Message:
Threadpool patch applied.
This feature allows the PopServer to use thread pooling to increase efficiency.
( Instanciation of thread is a costly business)
Classes borrowed from public domain :
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html
Index: PopConnection.java
===================================================================
RCS file: /cvsroot/mrpostman/mrpostman/src/org/mrbook/mrpostman/pop/PopConnection.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** PopConnection.java 7 Apr 2003 01:19:35 -0000 1.13
--- PopConnection.java 24 Apr 2003 22:30:33 -0000 1.14
***************
*** 48,52 ****
! public class PopConnection extends Thread {
public static final String CVSID = "$Id$";
private static Logger logger = Logger.getLogger("org.mrbook.mrpostman.pop.PopConnection");
--- 48,52 ----
! public class PopConnection implements Runnable {
public static final String CVSID = "$Id$";
private static Logger logger = Logger.getLogger("org.mrbook.mrpostman.pop.PopConnection");
***************
*** 66,69 ****
--- 66,70 ----
private static Pattern guiPat = Pattern.compile("\\AGUI");
private static Pattern userExtensionPat = Pattern.compile("(@.+)");
+ private static final byte RESET = 0;
private static final byte AUTHORIZATION = 1;
private static final byte TRANSACTION = 2;
***************
*** 79,82 ****
--- 80,86 ----
public PopConnection(Socket clientSocket) {
+ setSocket(clientSocket);
+ }
+ public void setSocket(Socket clientSocket) {
client = clientSocket;
***************
*** 88,92 ****
}
state = AUTHORIZATION;
! this.start();
}
--- 92,104 ----
}
state = AUTHORIZATION;
! }
! public void resetSocket() throws IOException {
! if (client != null) {
! client.close();
! }
! client = null;
! in = null;
! out = null;
! state = RESET;
}
***************
*** 103,107 ****
done = parseLine(line);
}
! sleep(100);
}
} catch (Exception e) {
--- 115,119 ----
done = parseLine(line);
}
! Thread.sleep(100);
}
} catch (Exception e) {
***************
*** 109,113 ****
} finally {
try {
! client.close();
} catch (Exception e) {
logger.log(Level.SEVERE, "should not happen", e);
--- 121,125 ----
} finally {
try {
! resetSocket();
} catch (Exception e) {
logger.log(Level.SEVERE, "should not happen", e);
Index: PopServer.java
===================================================================
RCS file: /cvsroot/mrpostman/mrpostman/src/org/mrbook/mrpostman/pop/PopServer.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** PopServer.java 9 Feb 2003 23:38:16 -0000 1.7
--- PopServer.java 24 Apr 2003 22:30:36 -0000 1.8
***************
*** 30,33 ****
--- 30,37 ----
package org.mrbook.mrpostman.pop;
+ import EDU.oswego.cs.dl.util.concurrent.Executor;
+ import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
+ import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
+
import org.mrbook.mrpostman.MrPostman;
***************
*** 36,39 ****
--- 40,44 ----
import java.net.ServerSocket;
import java.net.Socket;
+ import java.net.SocketException;
import java.util.logging.Level;
***************
*** 45,50 ****
--- 50,60 ----
private static Logger logger = Logger.getLogger("org.mrbook.mrpostman.pop.PopServer");
public static final int DEFAULT_PORT = 11110;
+ public static final int DEFAULT_BUFFERLENGTH = 10;
+ public static final int DEFAULT_MAXIMUM_THREADS = 10;
+ public static final int DEFAULT_MINIMUM_THREADS = 5;
+ public static final int DEFAULT_KEEPALIVE = 1000 * 60 * 5;
protected int port;
protected ServerSocket listenSocket;
+ private Executor executor;
public PopServer(int port) throws IOException {
***************
*** 53,56 ****
--- 63,74 ----
}
this.port = port;
+ logger.info("Starting executor");
+
+ executor = new PooledExecutor(new BoundedBuffer(DEFAULT_BUFFERLENGTH), DEFAULT_MAXIMUM_THREADS);
+ ((PooledExecutor) executor).setMinimumPoolSize(DEFAULT_MINIMUM_THREADS);
+ ((PooledExecutor) executor).setKeepAliveTime(DEFAULT_KEEPALIVE);
+ ((PooledExecutor) executor).abortWhenBlocked();
+ ((PooledExecutor) executor).createThreads(DEFAULT_MINIMUM_THREADS);
+
logger.info("Starting PopServer on port " + port);
listenSocket = new ServerSocket(port);
***************
*** 58,79 ****
logger.info(" PopServer started ");
}
!
public void run() {
! try {
! while (true) {
! Socket clientSocket = listenSocket.accept();
! logger.info("Someone connected");
!
if (isSocketAllowed(clientSocket)) {
logger.info("Connection accepted for IP " + clientSocket.getInetAddress());
! PopConnection pc = new PopConnection(clientSocket);
} else {
! logger.info("Connection refused for IP " + clientSocket.getInetAddress());
clientSocket.close();
}
}
- } catch (IOException e) {
- logger.log(Level.SEVERE, "Should not happen! ", e);
}
}
--- 76,116 ----
logger.info(" PopServer started ");
}
! public boolean carryon = true;
public void run() {
! Socket clientSocket;
! while (carryon) {
! try {
! clientSocket = listenSocket.accept();
! } catch (SocketException ex) {
! if (carryon) {
! logger.log(Level.SEVERE, "Should not happen! Exiting from server. ", ex);
! carryon = false;
! }
! break;
! } catch (IOException ex) {
! logger.log(Level.SEVERE, "Should not happen! Exiting from server. ", ex);
! break;
! }
! logger.info("Someone connected");
! try {
if (isSocketAllowed(clientSocket)) {
logger.info("Connection accepted for IP " + clientSocket.getInetAddress());
! try {
! executor.execute(new PopConnection(clientSocket));
! } catch (PooledExecutor.AbortException ex) {
! logger.warning("Too many connections! refused for IP " + clientSocket.getInetAddress());
! clientSocket.close();
! } catch (InterruptedException ex) {
! logger.warning("Interrupted exception for IP: " + clientSocket.getInetAddress());
! }
} else {
! logger.warning("Connection not allowed for IP " + clientSocket.getInetAddress());
clientSocket.close();
}
+ } catch (IOException e) {
+ logger.log(Level.FINE, " client socket close with difficulty. ", e);
}
}
+
}
***************
*** 90,93 ****
--- 127,146 ----
}
return false;
+ }
+ public void shutdown() {
+ logger.info("Shutting down pop server");
+ logger.fine("Halt popserver thread");
+ carryon = false;
+ this.interrupt();
+ try {
+ if (listenSocket != null) {
+ listenSocket.close();
+ }
+ } catch (IOException e) {
+ logger.fine(" Not important: IOException while closing finally ListenSocket");
+ }
+ listenSocket = null;
+ ((PooledExecutor) executor).shutdownAfterProcessingCurrentlyQueuedTasks();
+ logger.info("pop server shut down");
}
}
|