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 - Postilion Channel
028 * Send packet len (2 bytes network byte order MSB/LSB) followed by
029 * raw data. 
030 *
031 * @author salaman@teknos.com
032 * @version Id: PostChannel.java,v 1.0 1999/05/14 19:00:00 may Exp 
033 * @see ISOMsg
034 * @see ISOException
035 * @see ISOChannel
036 */
037public class PostChannel extends BaseChannel {
038    /**
039     * Public constructor (used by Class.forName("...").newInstance())
040     */
041    public PostChannel () {
042        super();
043    }
044    /**
045     * Construct client ISOChannel
046     * @param host  server TCP Address
047     * @param port  server port number
048     * @param p     an ISOPackager
049     * @see ISOPackager
050     */
051    public PostChannel (String host, int port, ISOPackager p) {
052        super(host, port, p);
053    }
054    /**
055     * Construct server ISOChannel
056     * @param p     an ISOPackager
057     * @exception IOException
058     * @see ISOPackager
059     */
060    public PostChannel (ISOPackager p) throws IOException {
061        super(p);
062    }
063    /**
064     * constructs a server ISOChannel associated with a Server Socket
065     * @param p     an ISOPackager
066     * @param serverSocket where to accept a connection
067     * @exception IOException
068     * @see ISOPackager
069     */
070    public PostChannel (ISOPackager p, ServerSocket serverSocket) 
071        throws IOException
072    {
073        super(p, serverSocket);
074    }
075    protected void sendMessageLength(int len) throws IOException {
076        serverOut.write (len >> 8);
077        serverOut.write (len);
078    }
079    protected int getMessageLength() throws IOException, ISOException {
080        byte[] b = new byte[2];
081        serverIn.readFully(b,0,2);
082        return ((int)b[0] &0xFF) << 8 |
083                (int)b[1] &0xFF;
084    }
085   /**
086    *      * @param header Hex representation of header
087    */
088    public void setHeader (String header) {
089        super.setHeader (
090            ISOUtil.hex2byte (header.getBytes(), 0, header.length() / 2)
091        );
092    }
093}
094