【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了VirtualServerSocketFactory.java相关资料,希望对各位网友有所帮助!
package rmiconnection;
import java.io.*;
import java.net.*;
import java.rmi.server.*;
import java.util.*;
import multiplexer.*;
import util.Queue;
/**
* This socket factory is used to create socket and
* socketServer objects that participate in the Virtual
* pattern.
*/
public class VirtualServerSocketFactory
implements RMIServerSocketFactory {
// <code>ConnectionAcceptor</code> objects accept connections from each
// registered <code>Multiplexer</code> object and put them
// in this queue. <code>RMIVirtualServerSocket</code>
// objects remove them from this queue.
private Queue acceptQueue;
private Hashtable muxTable;
private RMIVirtualServerSocket serverSocketInstance;
/**
* Constructor
*/
public VirtualServerSocketFactory() {
muxTable = new Hashtable();
acceptQueue = new Queue();
} // constructor(int)
/**
* Register a <code>Multiplexer</code> object so that this
* Factory object will use it.
* @param mux The <code>Multiplexer</code> object to
* register.
*/
public void registerMultiplexer(Multiplexer mux) {
muxTable.put(mux, new ConnectionAcceptor(mux));
} // registerMultiplexer(Multiplexer)
/**
* Unregister a <code>Multiplexer</code> object so that this
* Factory object will stop using it.
* @param mux The <code>Multiplexer</code> object to
* unregister.
*/
public void unregisterMultiplexer(Multiplexer mux) {
ConnectionAcceptor acceptor;
acceptor = (ConnectionAcceptor)muxTable.remove(mux);
if (acceptor!=null) {
acceptor.interrupt();
} // if
} // unregisterMultiplexer(Multiplexer)
/**
* Create a server socket. If the given port number is -1,
* then it creats a server socket that accepts connections
* from registered <code>Multiplexer</code> objects.
* Otherwise, it creates a standard
* <code>ServerSocket</code> that accepts actual
* connections.
*/
public ServerSocket createServerSocket(int port)
throws IOException {
if (port==-1) {
if (serverSocketInstance==null) {
serverSocketInstance
= new RMIVirtualServerSocket();
} // if
return serverSocketInstance;
} // if
RMISocketFactory defaultFactory;
defaultFactory
= RMISocketFactory.getDefaultSocketFactory();
return defaultFactory.createServerSocket(port);
} // createServerSocket(int)
/**
* Use the given name to a string of the form
*<br>
* //host:-1/name
*<br>
* This string can be used to register an object with a
* name registry.
*/
public String makeRegistryString(String name)
throws UnknownHostException {
InetAddress host = InetAddress.getLocalHost();
return "//" + host.getHostAddress() + "-1/" + name;
} // makeRegistryString(String)
/**
* Instances of this class are responsible for accepting
* connections from the <code>Multiplexer</code> object passed
* to their constructor.
*/
private class ConnectionAcceptor extends Thread {
private Multiplexer mux;
/**
* Constructor
* @param mux The <code>Multiplexer</code> object that
* this object accepts connections from.
*/
private ConnectionAcceptor(Multiplexer mux) {
this.mux = mux;
start();
} // constructor(Multiplexer)
public void run() {
try {
while (!isInterrupted()) {
acceptQueue.put(mux.accept());
} // while
} catch (InterruptedException e) {
} // try
} // run()
} // class ConnectionAcceptor
/**
* This subclass of ServerSocket accepts virtual
* connections through a multiplexer object
*/
private class RMIVirtualServerSocket extends ServerSocket {
private int soTimeout = 0;
/**
* Constructor.
* @param mux The multiplexer object this object will work
* with.
* @exception IOException if an I/O error occurs when
* opening the socket.
* @exception SecurityException
* If a security manager exists and its
* <code>checkListen</code> method doesn't allow the
* operation.
*/
RMIVirtualServerSocket() throws IOException {
// The superclass has no constructor that does not bind
// a port. So we call the simplest constructor and then
// immediately call the superclass's close.
super(0);
super.close();
} // constructor
/**
* Returns the local address of this server socket.
* @return the address to which this socket is connected,
* or <code>null</code> if the socket is not yet connected.
*/
public InetAddress getInetAddress() {
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
return null;
} // try
} // getInetAddress()
/**
* Returns the port on which this socket is listening.
* @return -1
*/
public int getLocalPort() {
return -1;
} // getLocalPort()
/**
* Listen for a new virtual connection and accept it. If
* there are no virtual connections waiting to be accepted,
* this method does not return until there is a virtual
* connection to be accepted.
* @exception IOException if an I/O error occurs when
* waiting for a connection.
*/
public Socket accept() throws IOException {
try {
return (Socket)acceptQueue.get(soTimeout);
} catch (InterruptedException e) {
throw new InterruptedIOException();
} // try
} // accept()
/**
* Closes this socket.
*/
public void close() {
// Nothing to do.
} // close()
/**
* Set SO_TIMEOUT to the specified timeout in
* milliseconds. With this option set to a non-zero value,
* a call to accept() will block for only this number of
* milliseconds. If the timeout expires, a call to accept
* throws a <code>java.io.InterruptedIOException</code>.
* The <B>must</B> be set prior to a call to accept to have
* an effect. The value must be > 0.
* A timeout of zero is interpreted as an infinite timeout.
*/
public void setSoTimeout(int timeout) throws SocketException {
soTimeout = timeout;
} // setSoTimeout(int)
/**
* Return current setting for SO_TIMEOUT.
*/
public int getSoTimeout() throws IOException {
return soTimeout;
} // getSoTimeout
} // class RMIVirtualServerSocket
} // class VirtualServerSocketFactory
上一篇:
VirtualClientSocketFactory.java
下一篇:
下鼻甲不同术式对鼻黏膜纤毛功能的影响