📄 basechannel.java
字号:
/* * Copyright (c) 2000 jPOS.org. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the jPOS project * (http://www.jpos.org/)". Alternately, this acknowledgment may * appear in the software itself, if and wherever such third-party * acknowledgments normally appear. * * 4. The names "jPOS" and "jPOS.org" must not be used to endorse * or promote products derived from this software without prior * written permission. For written permission, please contact * license@jpos.org. * * 5. Products derived from this software may not be called "jPOS", * nor may "jPOS" appear in their name, without prior written * permission of the jPOS project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE JPOS PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the jPOS Project. For more * information please see <http://www.jpos.org/>. */package org.jpos.iso;import java.io.*;import java.util.*;import java.net.ConnectException;import java.net.UnknownHostException;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketException;import org.jpos.util.Logger;import org.jpos.util.LogEvent;import org.jpos.util.LogSource;import org.jpos.util.NameRegistrar;import org.jpos.iso.ISOFilter.VetoException;import org.jpos.core.Configurable;import org.jpos.core.ReConfigurable;import org.jpos.core.Configuration;import org.jpos.core.ConfigurationException;import org.jpos.iso.header.BaseHeader;/* * BaseChannel was ISOChannel. Now ISOChannel is an interface * Revision: 1.34 Date: 2000/04/08 23:54:55 *//** * ISOChannel is an abstract class that provides functionality that * allows the transmision and reception of ISO 8583 Messages * over a TCP/IP session. * <p> * It is not thread-safe, ISOMUX takes care of the * synchronization details * <p> * ISOChannel is Observable in order to suport GUI components * such as ISOChannelPanel. * <br> * It now support the new Logger architecture so we will * probably setup ISOChannelPanel to be a LogListener insteado * of being an Observer in future releases. * * @author Alejandro P. Revilla * @author Bharavi Gade * @version $Revision: 1.37 $ $Date: 2002/08/06 14:26:29 $ * @see ISOMsg * @see ISOMUX * @see ISOException * @see org.jpos.iso.channel.CSChannel * @see Logger * */public abstract class BaseChannel extends Observable implements FilteredChannel, ClientChannel, ServerChannel, LogSource, ReConfigurable, BaseChannelMBean{ private Socket socket; private String host; private int port, timeout; protected boolean usable; private String name; // private int serverPort = -1; protected DataInputStream serverIn; protected DataOutputStream serverOut; protected ISOPackager packager; protected ServerSocket serverSocket = null; protected Vector incomingFilters, outgoingFilters; protected ISOClientSocketFactory socketFactory = null; protected int[] cnt; protected Logger logger = null; protected String realm = null; protected byte[] header = null; /** * constructor shared by server and client * ISOChannels (which have different signatures) */ public BaseChannel () { super(); cnt = new int[SIZEOF_CNT]; name = ""; incomingFilters = new Vector(); outgoingFilters = new Vector(); setHost (null, 0); } /** * constructs a client ISOChannel * @param host server TCP Address * @param port server port number * @param p an ISOPackager * @see ISOPackager */ public BaseChannel (String host, int port, ISOPackager p) { this(); setHost(host, port); setPackager(p); } /** * constructs a server ISOChannel * @param p an ISOPackager * @exception IOException * @see ISOPackager */ public BaseChannel (ISOPackager p) throws IOException { this(); setPackager (p); } /** * constructs a server ISOChannel associated with a Server Socket * @param p an ISOPackager * @param serverSocket where to accept a connection * @exception IOException * @see ISOPackager */ public BaseChannel (ISOPackager p, ServerSocket serverSocket) throws IOException { this(); setPackager (p); setServerSocket (serverSocket); } /** * initialize an ISOChannel * @param host server TCP Address * @param port server port number */ public void setHost(String host, int port) { this.host = host; this.port = port; } /** * @param host to connect (client ISOChannel) */ public void setHost (String host) { this.host = host; } /** * @param port to connect (client ISOChannel) */ public void setPort (int port) { this.port = port; } /** * @return hostname (may be null) */ public String getHost() { return host; } /** * @return port number */ public int getPort() { return port; } /** * set Packager for channel * @param p an ISOPackager * @see ISOPackager */ public void setPackager(ISOPackager p) { this.packager = p; } /** * @return current packager */ public ISOPackager getPackager() { return packager; } /** * Associates this ISOChannel with a server socket * @param serverSocket where to accept a connection */ public void setServerSocket (ServerSocket sock) { setHost (null, 0); this.serverSocket = sock; name = ""; } /** * reset stat info */ public void resetCounters() { for (int i=0; i<SIZEOF_CNT; i++) cnt[i] = 0; } /** * @return counters */ public int[] getCounters() { return cnt; } /** * @return the connection state */ public boolean isConnected() { return socket != null && usable; } /** * setup I/O Streams from socket * @param socket a Socket (client or server) * @exception IOException */ protected void connect (Socket socket) throws IOException, SocketException { this.socket = socket; applyTimeout(); serverIn = new DataInputStream ( new BufferedInputStream (socket.getInputStream ()) ); serverOut = new DataOutputStream( new BufferedOutputStream(socket.getOutputStream()) ); usable = true; cnt[CONNECT]++; setChanged(); notifyObservers(); } /** * factory method pattern (as suggested by Vincent.Greene@amo.com) * @throws IOException * Use Socket factory if exists. If it is missing create a normal socket * @see ISOClientSocketFactory */ protected Socket newSocket() throws IOException { try { if (socketFactory != null) return socketFactory.createSocket (host, port); else return new Socket(host,port); } catch (ISOException e) { throw new IOException (e.getMessage()); } } /** * @return current socket */ public Socket getSocket() { return socket; } /** * @return current serverSocket */ public ServerSocket getServerSocket() { return serverSocket; } /** * sets socket timeout (as suggested by * Leonard Thomas <leonard@rhinosystemsinc.com>) * @param timeout in milliseconds * @throws SocketException */ public void setTimeout (int timeout) throws SocketException { this.timeout = timeout; applyTimeout(); } public int getTimeout () { return timeout; } protected void applyTimeout () throws SocketException { if (timeout != 0 && socket != null) socket.setSoTimeout (timeout); } /** * Connects client ISOChannel to server * @exception IOException */ public void connect () throws IOException { LogEvent evt = new LogEvent (this, "connect"); try { if (serverSocket != null) { accept(serverSocket); evt.addMessage ("local port "+serverSocket.getLocalPort() +" remote host "+socket.getInetAddress()); } else { evt.addMessage (host+":"+port); connect(newSocket ()); } applyTimeout(); Logger.log (evt); } catch (ConnectException e) { Logger.log (new LogEvent (this, "connection-refused", getHost()+":"+getPort()) ); } catch (IOException e) { evt.addMessage (e); Logger.log (evt); throw e; } } /** * Accepts connection * @exception IOException */ public void accept(ServerSocket s) throws IOException { // if (serverPort > 0) // s = new ServerSocket (serverPort); // else // serverPort = s.getLocalPort(); connect(s.accept()); // Warning - closing here breaks ISOServer, we need an // accept that keep ServerSocket open. // s.close(); } /** * @param b - new Usable state (used by ISOMUX internals to * flag as unusable in order to force a reconnection) */ public void setUsable(boolean b) { Logger.log (new LogEvent (this, "usable", new Boolean (b))); usable = b; } /** * allow subclasses to override default packager * on outgoing messages * @param m outgoing ISOMsg * @return ISOPackager */ protected ISOPackager getDynamicPackager (ISOMsg m) { return packager; } /** * allow subclasses to override default packager * on outgoing messages * @param image incoming message image * @return ISOPackager */ protected ISOPackager getDynamicPackager (byte[] image) { return packager; } /** * Allow subclasses to override the Default header on * incoming messages. */ protected ISOHeader getDynamicHeader (byte[] image) { return image != null ? new BaseHeader (image) : null; } protected void sendMessageLength(int len) throws IOException { } protected void sendMessageHeader(ISOMsg m, int len) throws IOException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -