📄 socketconnection.java
字号:
/*------------------------------------------------------------------------------Name: SocketConnection.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: Handles connection to xmlBlaster with plain socketsAuthor: xmlBlaster@marcelruff.info------------------------------------------------------------------------------*/package org.xmlBlaster.client.protocol.socket;import java.io.IOException;import java.net.BindException;import java.net.Socket;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.util.Global;import org.xmlBlaster.client.qos.ConnectReturnQos;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.def.ErrorCode;import org.xmlBlaster.util.def.MethodName;import org.xmlBlaster.util.def.Constants;import org.xmlBlaster.util.plugin.PluginInfo;import org.xmlBlaster.util.protocol.socket.SocketExecutor;import org.xmlBlaster.util.protocol.socket.SocketUrl;import org.xmlBlaster.util.MsgUnitRaw;import org.xmlBlaster.util.qos.address.Address;import org.xmlBlaster.util.xbformat.I_ProgressListener;import org.xmlBlaster.util.xbformat.MsgInfo;import org.xmlBlaster.client.protocol.I_XmlBlasterConnection;import org.xmlBlaster.client.protocol.I_CallbackServer;import org.xmlBlaster.client.protocol.I_CallbackExtended;/** * This driver establishes exactly one connection to xmlBlaster-Server and * uses this socket for asynchronous callbacks as well. This way we don't need * to setup a callbackserver. * <p /> * This "SOCKET:" driver needs to be registered in xmlBlaster.properties * and will be started on xmlBlaster startup: * <pre> * ClientProtocolPlugin[SOCKET][1.0]=org.xmlBlaster.client.protocol.socket.SocketConnection * </pre> * <p /> * All adjustable parameters are explained in {@link org.xmlBlaster.client.protocol.socket.SocketConnection#usage()} * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/protocol.socket.html">The protocol.socket requirement</a> * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>. */public class SocketConnection implements I_XmlBlasterConnection{ private String ME = "SocketConnection"; private Global glob; private static Logger log = Logger.getLogger(SocketConnection.class.getName()); /** The info object holding hostname and port on the other side */ private SocketUrl socketUrl; /** The info object holding hostname and port on this side */ private SocketUrl localSocketUrl; /** The socket connection to/from one client */ protected Socket sock; /** SocketCallbackImpl listens on socket to receive callbacks */ protected SocketCallbackImpl cbReceiver; /** The unique client sessionId */ protected String sessionId; protected String loginName = "dummyLoginName"; protected Address clientAddress; private I_CallbackExtended cbClient; private PluginInfo pluginInfo; /** * Setting by plugin configuration, see xmlBlaster.properties, for example * <br /> * ClientProtocolPlugin[SOCKET_UDP][1.0]=org.xmlBlaster.client.protocol.socket.SocketConnection,useUdpForOneway=true */ private boolean useUdpForOneway = false; /** Placeholder for the progess listener in case the registration happens before the cbReceiver has been registered */ private I_ProgressListener tmpProgressListener; /** * Called by plugin loader which calls init(Global, PluginInfo) thereafter. */ public SocketConnection() { } /** * Connect to xmlBlaster using plain socket with native message format. */ public SocketConnection(Global glob) throws XmlBlasterException { init(glob, null); } /** * Connect to xmlBlaster using plain socket messaging. */ public SocketConnection(Global glob, java.applet.Applet ap) throws XmlBlasterException { init(glob, null); } /** */ public String getLoginName() { return this.loginName; } /** Enforced by I_Plugin */ public String getType() { return getProtocol(); } /** Enforced by I_Plugin */ public String getVersion() { return (this.pluginInfo == null) ? "1.0" : this.pluginInfo.getVersion(); } /** * This method is called by the PluginManager (enforced by I_Plugin). * @see org.xmlBlaster.util.plugin.I_Plugin#init(org.xmlBlaster.util.Global,org.xmlBlaster.util.plugin.PluginInfo) */ public void init(org.xmlBlaster.util.Global glob, PluginInfo pluginInfo) throws XmlBlasterException { this.glob = (glob == null) ? Global.instance() : glob; this.pluginInfo = pluginInfo; // String tmp = pluginInfo.getParameters().getProperty("useUdpForOneway", ""+this.useUdpForOneway); // this.useUdpForOneway = Boolean.valueOf(tmp).booleanValue(); this.useUdpForOneway = this.glob.get("useUdpForOneWay", this.useUdpForOneway, null, pluginInfo); if (log.isLoggable(Level.FINER)) log.finer("Entering init(useUdpForOneway=" + this.useUdpForOneway + ")"); // Put this instance in the NameService, will be looked up by SocketCallbackImpl this.glob.addObjectEntry("org.xmlBlaster.client.protocol.socket.SocketConnection", this); } /** * Get the raw socket handle */ public Socket getSocket() throws XmlBlasterException { if (this.sock == null) { if (log.isLoggable(Level.FINE)) log.fine("No socket connection available."); //Thread.currentThread().dumpStack(); throw new XmlBlasterException(glob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, "No plain SOCKET connection available."); } return this.sock; } final Global getGlobal() { return this.glob; } /** * Connects to xmlBlaster with one socket connection. * @see I_XmlBlasterConnection#connectLowlevel(Address) */ public void connectLowlevel(Address address) throws XmlBlasterException { if (isConnected()) return; // TODO: USE address for configurtation this.clientAddress = address; if (this.pluginInfo != null) this.clientAddress.setPluginInfoParameters(this.pluginInfo.getParameters()); if (log.isLoggable(Level.FINER)) log.finer("Entering connectLowlevel(), connection with raw socket to server, plugin setting is: " + this.pluginInfo.dumpPluginParameters()); this.socketUrl = new SocketUrl(glob, this.clientAddress); if (this.socketUrl.getPort() < 1) { String str = "Option dispatch/connection/plugin/socket/port set to " + this.socketUrl.getPort() + ", socket client not started"; log.info(str); throw new XmlBlasterException(glob, ErrorCode.RESOURCE_CONFIGURATION_ADDRESS, ME, str); } this.localSocketUrl = new SocketUrl(glob, this.clientAddress, true, -1); // SSL support boolean ssl = this.clientAddress.getEnv("SSL", false).getValue(); if (log.isLoggable(Level.FINE)) log.fine(clientAddress.getEnvLookupKey("SSL") + "=" + ssl); try { if (ssl) { this.sock = this.socketUrl.createSocketSSL(this.localSocketUrl, this.clientAddress); } else { if (this.localSocketUrl.isEnforced()) { this.sock = new Socket(this.socketUrl.getInetAddress(), this.socketUrl.getPort(), this.localSocketUrl.getInetAddress(), this.localSocketUrl.getPort()); } else { if (log.isLoggable(Level.FINE)) log.fine("Trying socket connection to " + socketUrl.getUrl() + " ..."); this.sock = new Socket(this.socketUrl.getInetAddress(), this.socketUrl.getPort()); } } if (this.localSocketUrl.isEnforced()) { log.info(getType() + (ssl ? " SSL" : "") + " client connected to '" + this.socketUrl.getUrl() + "', your configured local parameters are localHostname=" + this.localSocketUrl.getHostname() + " on localPort=" + this.localSocketUrl.getPort() + " useUdpForOneway=" + this.useUdpForOneway + "', callback address is '" + this.sock.getLocalAddress().getHostAddress() + ":" + this.sock.getLocalPort() + "'"); } else { // SocketUrl constructor updates client address this.localSocketUrl = new SocketUrl(glob, this.sock.getLocalAddress().getHostAddress(), this.sock.getLocalPort()); this.clientAddress.setRawAddress(this.socketUrl.getUrl()); log.info(getType() + (ssl ? " SSL" : "") + " client connected to '" + socketUrl.getUrl() + "', callback address is '" + this.localSocketUrl.getUrl() + "' useUdpForOneway=" + this.useUdpForOneway + " clientAddress='" + this.clientAddress.getRawAddress() + "'"); } // start the socket sender and callback thread here if (this.cbReceiver != null) { // only the first time, not on reconnect // NOTE: This address should come from the client !!! org.xmlBlaster.util.qos.address.CallbackAddress cba = new org.xmlBlaster.util.qos.address.CallbackAddress(glob); this.cbReceiver.initialize(glob, getLoginName(), cba, this.cbClient); } } catch (XmlBlasterException e) { throw e; } catch (java.net.UnknownHostException e) { String str = "XmlBlaster server host is unknown, '-dispatch/connection/plugin/socket/hostname=<ip>': " + e.toString(); if (log.isLoggable(Level.FINE)) log.fine(str); //e.printStackTrace(); throw new XmlBlasterException(glob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, "XmlBlaster server is unknown, '-dispatch/connection/plugin/socket/hostname=<ip>'", e); } catch (BindException e) { // If client host changed address: Cannot assign requested address String str = "Connection to xmlBlaster server failed local=" + this.localSocketUrl + " remote=" + this.socketUrl + ": " + e.toString(); log.warning(str); //e.printStackTrace(); throw new XmlBlasterException(glob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, str); } catch (java.net.SocketException e) { // ifconfig eth0 down: Network is unreachable String str = "Connection to xmlBlaster server failed local=" + this.localSocketUrl + " remote=" + this.socketUrl + ": " + e.toString(); if (log.isLoggable(Level.FINE)) log.fine(str); //e.printStackTrace(); throw new XmlBlasterException(glob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, str); } catch (java.io.IOException e) { String str = "Connection to xmlBlaster server failed local=" + this.localSocketUrl + " remote=" + this.socketUrl + ": " + e.toString(); if (log.isLoggable(Level.FINE)) log.fine(str); //e.printStackTrace(); throw new XmlBlasterException(glob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, str); } catch (Throwable e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -