📄 basechannel.java
字号:
/* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2008 Alejandro P. Revilla * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */package org.jpos.iso;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.IOException;import java.io.InterruptedIOException;import java.net.ConnectException;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketException;import java.util.Collection;import java.util.Iterator;import java.util.Observable;import java.util.Vector;import org.jpos.core.Configurable;import org.jpos.core.Configuration;import org.jpos.core.ConfigurationException;import org.jpos.core.ReConfigurable;import org.jpos.iso.ISOFilter.VetoException;import org.jpos.iso.header.BaseHeader;import org.jpos.util.LogEvent;import org.jpos.util.LogSource;import org.jpos.util.Logger;import org.jpos.util.NameRegistrar;/* * 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: 2609 $ $Date: 2008-03-01 21:41:39 -0200 (Sat, 01 Mar 2008) $ * @see ISOMsg * @see ISOMUX * @see ISOException * @see org.jpos.iso.channel.CSChannel * @see Logger * */public abstract class BaseChannel extends Observable implements FilteredChannel, ClientChannel, ServerChannel, FactoryChannel, LogSource, ReConfigurable, BaseChannelMBean, Cloneable{ private Socket socket; private String host, localIface; private String[] hosts; int[] ports; private int port, timeout, connectTimeout, localPort; private int maxPacketLength = 100000; private boolean keepAlive; private Configuration cfg; protected boolean usable; protected boolean overrideHeader; 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 String originalRealm = 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; this.hosts = new String[] { host }; this.ports = new int[] { port }; } /** * initialize an ISOChannel * @param host server TCP Address * @param port server port number */ public void setLocalAddress (String iface, int port) { this.localIface = iface; this.localPort = port; } /** * @param host to connect (client ISOChannel) */ public void setHost (String host) { this.host = host; this.hosts = new String[] { host }; } /** * @param port to connect (client ISOChannel) */ public void setPort (int port) { this.port = port; this.ports = new int[] { 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 sock 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(); setLogger(getLogger(), getOriginalRealm() + "/" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort() ); serverIn = new DataInputStream ( new BufferedInputStream (socket.getInputStream ()) ); serverOut = new DataOutputStream( new BufferedOutputStream(socket.getOutputStream(), 2048) ); 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(String host, int port) throws IOException { try { if (socketFactory != null) return socketFactory.createSocket (host, port); else { if (connectTimeout > 0) { Socket s = new Socket(); s.connect ( new InetSocketAddress (host, port), connectTimeout ); return s; } else if (localIface == null && localPort == 0){ return new Socket(host,port); } else { InetAddress addr = (localIface == null) ? InetAddress.getLocalHost() : InetAddress.getByName(localIface); return new Socket(host, port, addr, localPort); } } } catch (ISOException e) { throw new IOException (e.getMessage()); } } protected Socket newSocket (String[] hosts, int[] ports, LogEvent evt) throws IOException { Socket s = null; for (int i=0; i<hosts.length; i++) { try { evt.addMessage (hosts[i]+":"+ports[i]); s = newSocket (hosts[i], ports[i]); break; } catch (IOException e) { evt.addMessage (" " + e.getMessage()); } } if (s == null) throw new IOException ("Unable to connect"); return s; } /** * @return current socket
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -