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 * Sends a four ASCII hex characters indicating message length (up to 0xffff)
030 *
031 * @author Mladen Mrkic (mmrkic@arius.co.yu)
032 * @author apr
033 * @version $Revision$ $Date$
034 * @see ISOMsg
035 * @see ISOException
036 * @see ISOChannel
037 */
038public class HEXChannel extends BaseChannel {
039    /** Default constructor. */
040    public HEXChannel () {
041        super();
042    }
043    /**
044     * Construct client ISOChannel
045     * @param host  server TCP Address
046     * @param port  server port number
047     * @param p     an ISOPackager
048     * @param TPDU  an optional raw header (i.e. TPDU)
049     * @see ISOPackager
050     */
051    public HEXChannel (String host, int port, ISOPackager p, byte[] TPDU) {
052        super(host, port, p);
053        this.header = TPDU;
054    }
055    /**
056     * Construct server ISOChannel
057     * @param p     an ISOPackager
058     * @param TPDU  an optional raw header (i.e. TPDU)
059     * @exception IOException on I/O error
060     * @see ISOPackager
061     */
062    public HEXChannel (ISOPackager p, byte[] TPDU) throws IOException {
063        super(p);
064        this.header = TPDU;
065    }
066    /**
067     * constructs server ISOChannel associated with a Server Socket
068     * @param p     an ISOPackager
069     * @param TPDU  an optional raw header (i.e. TPDU)
070     * @param serverSocket where to accept a connection
071     * @exception IOException on I/O error
072     * @see ISOPackager
073     */
074    public HEXChannel (ISOPackager p, byte[] TPDU, ServerSocket serverSocket) 
075        throws IOException
076    {
077        super(p, serverSocket);
078        this.header = TPDU;
079    }
080    protected void sendMessageLength(int len) throws IOException {
081        if (len > 0xFFFF)
082            throw new IOException (len + " exceeds maximum length");
083        try {
084            serverOut.write (
085                ISOUtil.zeropad (Integer.toString (len % 0xFFFF,16), 4).getBytes()
086            );
087        } 
088        catch (ISOException e) {
089            Logger.log (new LogEvent (this, "send-message-length", e));
090        }
091    }
092    protected int getMessageLength() throws IOException, ISOException {
093        byte[] b = new byte[4];
094        serverIn.readFully(b,0,4);
095        return Integer.parseInt (new String(b),16);
096    }
097}
098