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 * ISOChannel implementation - American Express
030 *
031 * @author marksalter@dsl.pipex.com
032 * @version $Id: AmexChannel.java,v 1.5 2006/01/27 10:36:18 mark Exp $
033 * @see ISOMsg
034 * @see ISOException
035 * @see ISOChannel
036 */
037public class AmexChannel extends BaseChannel {
038
039    /**
040     * Public constructor (used by Class.forName("...").newInstance())
041     */
042    public AmexChannel() {
043        super();
044    }
045
046    /**
047     * Construct client ISOChannel
048     *
049     * @param host
050     *            server TCP Address
051     * @param port
052     *            server port number
053     * @param p
054     *            an ISOPackager (should be ISO87BPackager)
055     * @see org.jpos.iso.packager.ISO87BPackager
056     */
057    public AmexChannel(String host, int port, ISOPackager p) {
058        super(host, port, p);
059    }
060
061    /**
062     * Construct server ISOChannel
063     *
064     * @param p
065     *            an ISOPackager (should be ISO87BPackager)
066     * @exception IOException
067     * @see org.jpos.iso.packager.ISO87BPackager
068     */
069    public AmexChannel(ISOPackager p) throws IOException {
070        super(p);
071    }
072
073    /**
074     * constructs a server ISOChannel associated with a Server Socket
075     *
076     * @param p
077     *            an ISOPackager
078     * @param serverSocket
079     *            where to accept a connection
080     * @exception IOException
081     * @see ISOPackager
082     */
083    public AmexChannel(ISOPackager p, ServerSocket serverSocket)
084        throws IOException {
085        super(p, serverSocket);
086    }
087
088    protected void sendMessageLength(int len) throws IOException {
089        serverOut.write(len+2 >> 8);
090        serverOut.write(len+2);
091    }
092
093    protected int getMessageLength() throws IOException, ISOException {
094        int l = 0;
095        byte[] b = new byte[2];
096        // ignore polls (0 message length)
097        while (l == 0) {
098            serverIn.readFully(b, 0, 2);
099            l = ((int) b[0] & 0xFF) << 8 | (int) b[1] & 0xFF;
100            if (l == 0 && isExpectKeepAlive()) {
101                serverOutLock.lock();
102                try {
103                    serverOut.write(b);
104                    serverOut.flush();
105                } finally {
106                    serverOutLock.unlock();
107                }
108                Logger.log(new LogEvent(this, "poll"));
109            }
110        }
111        // Message length includes length itself, so adjust the message total down by 2
112        l = l - 2;
113
114        return l;
115    }
116
117}