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    public HEXChannel () {
040        super();
041    }
042    /**
043     * Construct client ISOChannel
044     * @param host  server TCP Address
045     * @param port  server port number
046     * @param p     an ISOPackager
047     * @param TPDU  an optional raw header (i.e. TPDU)
048     * @see ISOPackager
049     */
050    public HEXChannel (String host, int port, ISOPackager p, byte[] TPDU) {
051        super(host, port, p);
052        this.header = TPDU;
053    }
054    /**
055     * Construct server ISOChannel
056     * @param p     an ISOPackager
057     * @param TPDU  an optional raw header (i.e. TPDU)
058     * @exception IOException
059     * @see ISOPackager
060     */
061    public HEXChannel (ISOPackager p, byte[] TPDU) throws IOException {
062        super(p);
063        this.header = TPDU;
064    }
065    /**
066     * constructs server ISOChannel associated with a Server Socket
067     * @param p     an ISOPackager
068     * @param TPDU  an optional raw header (i.e. TPDU)
069     * @param serverSocket where to accept a connection
070     * @exception IOException
071     * @see ISOPackager
072     */
073    public HEXChannel (ISOPackager p, byte[] TPDU, ServerSocket serverSocket) 
074        throws IOException
075    {
076        super(p, serverSocket);
077        this.header = TPDU;
078    }
079    protected void sendMessageLength(int len) throws IOException {
080        if (len > 0xFFFF)
081            throw new IOException (len + " exceeds maximum length");
082        try {
083            serverOut.write (
084                ISOUtil.zeropad (Integer.toString (len % 0xFFFF,16), 4).getBytes()
085            );
086        } 
087        catch (ISOException e) {
088            Logger.log (new LogEvent (this, "send-message-length", e));
089        }
090    }
091    protected int getMessageLength() throws IOException, ISOException {
092        byte[] b = new byte[4];
093        serverIn.readFully(b,0,4);
094        return Integer.parseInt (new String(b),16);
095    }
096}
097