001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso.channel;
020
021import org.jpos.iso.*;
022import org.jpos.util.LogEvent;
023import org.jpos.util.Logger;
024
025import java.io.IOException;
026import java.net.ServerSocket;
027
028/**
029 * Implements an ISOChannel able to exchange messages with
030 * ACI's BASE24 over a TCP link, modified from BASE24ISOChannel 
031 * by Victor A. Salaman (salaman@teknos.com) .<br>
032 * An instance of this class exchanges messages by means of an
033 * intermediate 'port server' as described in the
034 * <a href="/doc/javadoc/overview-summary.html">Overview</a> page.
035 * @author apr@cs.com.uy
036 * @author salaman@teknos.com
037 *
038 * @version $Id$
039 *
040 * @see ISOMsg
041 * @see ISOException
042 * @see ISOChannel
043 */
044
045@SuppressWarnings("deprecation")
046public class BASE24TCPChannel extends BaseChannel {
047    /**
048     * Public constructor (used by Class.forName("...").newInstance())
049     */
050    public BASE24TCPChannel () {
051        super();
052    }
053    /**
054     * Construct client ISOChannel
055     * @param host  server TCP Address
056     * @param port  server port number
057     * @param p     an ISOPackager
058     * @see ISOPackager
059     */
060    public BASE24TCPChannel (String host, int port, ISOPackager p) {
061        super(host, port, p);
062    }
063    /**
064     * Construct server ISOChannel
065     * @param p     an ISOPackager
066     * @see ISOPackager
067     * @exception IOException
068     */
069    public BASE24TCPChannel (ISOPackager p) throws IOException {
070        super(p);
071    }
072    /**
073     * constructs a server ISOChannel associated with a Server Socket
074     * @param p     an ISOPackager
075     * @param serverSocket where to accept a connection
076     * @exception IOException
077     * @see ISOPackager
078     */
079    public BASE24TCPChannel (ISOPackager p, ServerSocket serverSocket) 
080        throws IOException
081    {
082        super(p, serverSocket);
083    }
084    /**
085     * @param m the Message to send (in this case it is unused)
086     * @param len   message len (ignored)
087     * @exception IOException
088     */
089    protected void sendMessageTrailler(ISOMsg m, int len) throws IOException {
090        serverOut.write (3);
091    }
092    protected void sendMessageLength(int len) throws IOException {
093        len++;  // one byte trailler
094        serverOut.write (len >> 8);
095        serverOut.write (len);
096    }
097    protected int getMessageLength() throws IOException, ISOException {
098        int l = 0;
099        byte[] b = new byte[2];
100        Logger.log (new LogEvent (this, "get-message-length"));
101        while (l == 0) {
102            serverIn.readFully(b,0,2);
103            l = ((int)b[0] &0xFF) << 8 | (int)b[1] &0xFF;
104            if (l == 0) {
105                serverOut.write(b);
106                serverOut.flush();
107            }
108        }
109        Logger.log (new LogEvent (this, "got-message-length", Integer.toString(l)));
110        return l - 1;   // trailler length
111    }
112    protected void getMessageTrailler() throws IOException {
113        Logger.log (new LogEvent (this, "get-message-trailler"));
114        byte[] b = new byte[1];
115        serverIn.readFully(b,0,1);
116        Logger.log (new LogEvent (this, "got-message-trailler", ISOUtil.hexString(b)));
117    }
118}
119