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.*;
022
023import java.io.IOException;
024import java.net.ServerSocket;
025
026/**
027 * ISOChannel implementation - RAW Channel
028 * Send packet len (4 bytes network byte order) followed by
029 * raw data. Usefull when you need to send propietary headers
030 * with ISOMsgs (such as NAC's TPDUs)
031 *
032 * @author apr@cs.com.uy
033 * @version $Id$
034 * @see ISOMsg
035 * @see ISOException
036 * @see ISOChannel
037 */
038public class RawChannel extends BaseChannel {
039    /**
040     * Public constructor (used by Class.forName("...").newInstance())
041     */
042    public RawChannel () {
043        super();
044    }
045    /**
046     * Construct client ISOChannel
047     * @param host   server TCP Address
048     * @param port   server port number
049     * @param p      an ISOPackager
050     * @param header an optional raw header (i.e. TPDU)
051     * @see ISOPackager
052     */
053    public RawChannel (String host, int port, ISOPackager p, byte[] header) {
054        super(host, port, p);
055        this.header = header;
056    }
057    /**
058     * Construct server ISOChannel
059     * @param p      an ISOPackager
060     * @param header an optional raw header (i.e. TPDU)
061     * @exception IOException
062     * @see ISOPackager
063     */
064    public RawChannel (ISOPackager p, byte[] header) throws IOException {
065        super(p);
066        this.header = header;
067    }
068    /**
069     * constructs a server ISOChannel associated with a Server Socket
070     * @param p      an ISOPackager
071     * @param header an optional raw header (i.e. TPDU)
072     * @param serverSocket where to accept a connection
073     * @exception IOException
074     * @see ISOPackager
075     */
076    public RawChannel (ISOPackager p, byte[] header, ServerSocket serverSocket) 
077        throws IOException
078    {
079        super(p, serverSocket);
080        this.header = header;
081    }
082    protected void sendMessageLength(int len) throws IOException {
083        serverOut.write (len >> 24);
084        serverOut.write (len >> 16);
085        serverOut.write (len >> 8);
086        serverOut.write (len);
087    }
088    protected int getMessageLength() throws IOException, ISOException {
089        byte[] b = new byte[4];
090        serverIn.readFully(b,0,4);
091        return ((int)b[0] &0xFF) << 24 |
092                ((int)b[1] &0xFF) << 16 |
093                ((int)b[2] &0xFF) << 8 |
094                (int)b[3] &0xFF;
095    }
096    /**
097     * New QSP compatible signature (see QSP's ConfigChannel)
098     * @param header String as seen by QSP
099     */
100    public void setHeader (String header) {
101        super.setHeader (ISOUtil.str2bcd(header, false));
102    }
103}