⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basechannel.java

📁 pos机交易实现源代码 含金融卡交易8583协议实现 开发环境:linux 典型应用:嵌入系统开发 仅供参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	if (m.getHeader() != null)            serverOut.write(m.getHeader());        else if (header != null)             serverOut.write(header);    }    protected void sendMessageTrailler(ISOMsg m, int len) throws IOException { }    protected void getMessageTrailler() throws IOException { }    protected int getMessageLength() throws IOException, ISOException {        return -1;    }    protected int getHeaderLength() {         return header != null ? header.length : 0;    }    protected int getHeaderLength(byte[] b) { return 0; }    protected byte[] streamReceive() throws IOException {        return new byte[0];    }    protected void sendMessage (byte[] b, int offset, int len)         throws IOException    {        serverOut.write(b, 0, b.length);    }    /**     * sends an ISOMsg over the TCP/IP session     * @param m the Message to be sent     * @exception IOException     * @exception ISOException     * @exception ISOFilter.VetoException;     */    public void send (ISOMsg m) 	throws IOException, ISOException, VetoException    {	LogEvent evt = new LogEvent (this, "send");	evt.addMessage (m);	try {	    if (!isConnected())		throw new ISOException ("unconnected ISOChannel");	    m.setDirection(ISOMsg.OUTGOING);	    m = applyOutgoingFilters (m, evt);	    m.setDirection(ISOMsg.OUTGOING); // filter may have drop this info	    m.setPackager (getDynamicPackager(m));	    byte[] b = m.pack();	    synchronized (serverOut) {		sendMessageLength(b.length + getHeaderLength());		sendMessageHeader(m, b.length);                sendMessage (b, 0, b.length);		sendMessageTrailler(m, b.length);		serverOut.flush ();	    }	    cnt[TX]++;	    setChanged();	    notifyObservers(m);	} catch (VetoException e) {	    evt.addMessage (e);	    throw e;	} catch (ISOException e) {	    evt.addMessage (e);	    throw e;	} catch (IOException e) {	    evt.addMessage (e);	    throw e;	} catch (Exception e) {	    evt.addMessage (e);	    throw new ISOException ("unexpected exception", e);	} finally {	    Logger.log (evt);	}    }    protected boolean isRejected(byte[] b) {        // VAP Header support - see VAPChannel        return false;    }    /**     * Waits and receive an ISOMsg over the TCP/IP session     * @return the Message received     * @exception IOException     * @exception ISOException     */    public ISOMsg receive() throws IOException, ISOException {        byte[] b, header=null;	LogEvent evt = new LogEvent (this, "receive");	ISOMsg m = new ISOMsg();	try {	    if (!isConnected())		throw new ISOException ("unconnected ISOChannel");	    synchronized (serverIn) {		int len  = getMessageLength();		int hLen = getHeaderLength();		if (len == -1) {                    if (hLen > 0) {		        header = new byte [hLen];		        serverIn.readFully(header,0,hLen);                    }		    b = streamReceive();		}		else if (len > 10 && len <= 4096) {		    int l;		    if (hLen > 0) {			// ignore message header (TPDU)			header = new byte [hLen];			serverIn.readFully(header,0,hLen);			if (isRejected(header))			    throw new ISOException 				("Unhandled Rejected Message");			len -= hLen;		    }		    b = new byte[len];		    serverIn.readFully(b,0,len);                    getMessageTrailler();		}		else		    throw new ISOException(			"receive length " +len + " seems extrange");	    }	    m.setPackager (getDynamicPackager(b));	    m.setHeader (getDynamicHeader(header));	    if (b.length > 0)  // Ignore NULL messages		m.unpack (b);	    m.setDirection(ISOMsg.INCOMING);	    m = applyIncomingFilters (m, evt);	    m.setDirection(ISOMsg.INCOMING);	    evt.addMessage (m);	    cnt[RX]++;	    setChanged();	    notifyObservers(m);	} catch (ISOException e) {	    evt.addMessage (e);	    throw e;	} catch (EOFException e) {            if (socket != null)                socket.close ();	    evt.addMessage ("<peer-disconnect/>");	    throw e;	} catch (InterruptedIOException e) {            if (socket != null)                socket.close ();	    evt.addMessage ("<io-timeout/>");	    throw e;	} catch (IOException e) {             if (socket != null)                socket.close ();	    if (usable) 		evt.addMessage (e);	    throw e;	} catch (Exception e) {             evt.addMessage (e);	    throw new ISOException ("unexpected exception", e);	} finally {	    Logger.log (evt);	}	return m;    }    /**     * Low level receive     * @param b byte array     * @exception IOException     */    public int getBytes (byte[] b) throws IOException {        return serverIn.read (b);    }    /**     * disconnects the TCP/IP session. The instance is ready for     * a reconnection. There is no need to create a new ISOChannel<br>     * @exception IOException     */    public void disconnect () throws IOException {	LogEvent evt = new LogEvent (this, "disconnect");        if (serverSocket != null) 	    evt.addMessage ("local port "+serverSocket.getLocalPort()		+" remote host "+serverSocket.getInetAddress());	else	    evt.addMessage (host+":"+port);	try {	    usable = false;	    setChanged();	    notifyObservers();            if (serverIn != null) {                try {                    serverIn.close();                } catch (IOException ex) { evt.addMessage (ex); }	        serverIn  = null;            }            if (serverOut != null) {                try {                    serverOut.close();                } catch (IOException ex) { evt.addMessage (ex); }                serverOut = null;            }	    if (socket != null)		socket.close ();	    Logger.log (evt);	} catch (IOException e) {	    evt.addMessage (e);	    Logger.log (evt);	    throw e;	}        socket = null;    }       /**     * Issues a disconnect followed by a connect     * @exception IOException     */    public void reconnect() throws IOException {        disconnect();        connect();    }    public void setLogger (Logger logger, String realm) {	this.logger = logger;	this.realm  = realm;    }    public String getRealm () {	return realm;    }    public Logger getLogger() {	return logger;    }    /**     * associates this ISOChannel with a name using NameRegistrar     * @param name name to register     * @see NameRegistrar     */    public void setName (String name) {	this.name = name;	NameRegistrar.register ("channel."+name, this);    }    /**     * @return this ISOChannel's name ("" if no name was set)     */    public String getName() {	return this.name;    }    /**     * @param filter filter to add     * @param direction ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both     */    public void addFilter (ISOFilter filter, int direction) {	switch (direction) {	    case ISOMsg.INCOMING :		incomingFilters.add (filter);		break;	    case ISOMsg.OUTGOING :		outgoingFilters.add (filter);		break;	    case 0 :		incomingFilters.add (filter);		outgoingFilters.add (filter);		break;	}    }    /**     * @param filter incoming filter to add     */    public void addIncomingFilter (ISOFilter filter) {	addFilter (filter, ISOMsg.INCOMING);    }    /**     * @param filter outgoing filter to add     */    public void addOutgoingFilter (ISOFilter filter) {	addFilter (filter, ISOMsg.OUTGOING);    }    /**     * @param filter filter to add (both directions, incoming/outgoing)     */    public void addFilter (ISOFilter filter) {	addFilter (filter, 0);    }    /**     * @param filter filter to remove     * @param direction ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both     */    public void removeFilter (ISOFilter filter, int direction) {	switch (direction) {	    case ISOMsg.INCOMING :		incomingFilters.remove (filter);		break;	    case ISOMsg.OUTGOING :		outgoingFilters.remove (filter);		break;	    case 0 :		incomingFilters.remove (filter);		outgoingFilters.remove (filter);		break;	}    }    /**     * @param filter filter to remove (both directions)     */    public void removeFilter (ISOFilter filter) {	removeFilter (filter, 0);    }    /**     * @param filter incoming filter to remove     */    public void removeIncomingFilter (ISOFilter filter) {	removeFilter (filter, ISOMsg.INCOMING);    }    /**     * @param filter outgoing filter to remove     */    public void removeOutgoingFilter (ISOFilter filter) {	removeFilter (filter, ISOMsg.OUTGOING);    }    protected ISOMsg applyOutgoingFilters (ISOMsg m, LogEvent evt) 	throws VetoException    {	Iterator iter  = outgoingFilters.iterator();	while (iter.hasNext())	    m = ((ISOFilter) iter.next()).filter (this, m, evt);	return m;    }    protected ISOMsg applyIncomingFilters (ISOMsg m, LogEvent evt) 	throws VetoException    {	Iterator iter  = incomingFilters.iterator();	while (iter.hasNext())	    m = ((ISOFilter) iter.next()).filter (this, m, evt);	return m;    }   /**    * Implements Configurable<br>    * Properties:<br>    * <ul>    * <li>host - destination host (if ClientChannel)    * <li>port - port number      (if ClientChannel)    * </ul>    * (host not present indicates a ServerChannel)    *    * @param cfg Configuration    * @throws ConfigurationException    */    public void setConfiguration (Configuration cfg)	throws ConfigurationException     {	String h    = cfg.get    ("host");	int port    = cfg.getInt ("port");	if (h != null && h.length() > 0) {	    if (port == 0)		throw new ConfigurationException 		    ("invalid port for host '"+h+"'");	    setHost (h, port);        }        if (socketFactory != this && socketFactory instanceof Configurable)            ((Configurable)socketFactory).setConfiguration (cfg);        try {            setTimeout (cfg.getInt ("timeout"));        } catch (SocketException e) {            throw new ConfigurationException (e);        }    }    public Collection getIncomingFilters() {	return incomingFilters;    }    public Collection getOutgoingFilters() {	return outgoingFilters;    }    public void setIncomingFilters (Collection filters) {	incomingFilters = new Vector (filters);    }    public void setOutgoingFilters (Collection filters) {	outgoingFilters = new Vector (filters);    }    public void setHeader (byte[] header) {	this.header = header;    }    public void setHeader (String header) {	setHeader (header.getBytes());    }    public byte[] getHeader () {	return header;    }    /**     * @return ISOChannel instance with given name.     * @throws NameRegistrar.NotFoundException;     * @see NameRegistrar     */    public static ISOChannel getChannel (String name)	throws NameRegistrar.NotFoundException    {	return (ISOChannel) NameRegistrar.get ("channel."+name);    }   /**    * Gets the ISOClientSocketFactory (may be null)    * @see     ISOClientSocketFactory    * @since 1.3.3    */    public ISOClientSocketFactory getSocketFactory() {        return socketFactory;    }   /**    * Sets the specified Socket Factory to create sockets    * @param         socketFactory the ISOClientSocketFactory    * @see           ISOClientSocketFactory    * @since 1.3.3    */    public void setSocketFactory(ISOClientSocketFactory socketFactory) {        this.socketFactory = socketFactory;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -