【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-VirtualClientSocketFactory.java,希望本篇文章对您学习有所帮助。
package rmiconnection;
import java.io.*;
import java.net.*;
import java.rmi.server.*;
import java.util.*;
import multiplexer.*;
/**
* This socket factory is used to create <code></code> objects that
* participate in the Connection Multiplexing pattern.
*/
public class VirtualClientSocketFactory
implements RMIClientSocketFactory {
private Hashtable muxTable;
/**
* Constructor
*/
public VirtualClientSocketFactory() {
muxTable = new Hashtable();
} // 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.getRemoteAddress(), 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) {
muxTable.remove(mux.getRemoteAddress());
} // unregisterMultiplexer(Multiplexer)
/**
* Create a client socket connected to the specified host
* and port.
*<p>
* If the port number is -1 and host name resolves to a
* network address that matches one of the registered
* <code>Multiplexer</code> object, then this method ignores the
* port number passed to it and uses the
* <code>Multiplexer</code> to create a virtual connection.
* Otherwise it create a real connection using rmi's
* default mechanism.
*/
public Socket createSocket(String host, int port)
throws IOException {
if (port==-1) {
InetAddress remoteAddress;
remoteAddress = InetAddress.getByName(host);
Multiplexer mux;
mux = (Multiplexer)muxTable.get(remoteAddress);
if (mux !=null) {
return mux.createConnection();
} else {
String msg;
msg = "No Multiplexer registrered for " + host;
throw new IOException();
} // if mux
} // if port
RMISocketFactory factory;
factory = RMISocketFactory.getDefaultSocketFactory();
return factory.createSocket(host, port);
} // createSocket(String, int)
} // class VirtualClientSocketFactory
上一篇:
ViolatedConstraintNameExtracter.java
下一篇:
下鼻甲不同术式对鼻黏膜纤毛功能的影响