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 java.io.IOException;
022import java.net.ServerSocket;
023
024import org.jpos.iso.BaseChannel;
025import org.jpos.iso.ISOException;
026import org.jpos.iso.ISOMsg;
027import org.jpos.iso.ISOPackager;
028
029/**
030 * four-byte header in nbp
031 */
032public class GICCChannel extends BaseChannel {
033    /**
034     * Public constructor (used by Class.forName("...").newInstance())
035     */
036    public GICCChannel () {
037        super();
038    }
039    /**
040     * Construct client ISOChannel
041     * @param host  server TCP Address
042     * @param port  server port number
043     * @param p     an ISOPackager
044     * @see ISOPackager
045     */
046    public GICCChannel (String host, int port, ISOPackager p) {
047        super(host, port, p);
048    }
049    /**
050     * Construct server ISOChannel
051     * @param p     an ISOPackager
052     * @exception IOException on I/O error
053     * @see ISOPackager
054     */
055    public GICCChannel (ISOPackager p) throws IOException {
056        super(p);
057    }
058    /**
059     * constructs a server ISOChannel associated with a Server Socket
060     * @param p     an ISOPackager
061     * @param serverSocket where to accept a connection
062     * @exception IOException on I/O error
063     * @see ISOPackager
064     */
065    public GICCChannel (ISOPackager p, ServerSocket serverSocket) 
066        throws IOException
067    {
068        super(p, serverSocket);
069    }
070    /**
071     * Constructs a packager with the given length and description.
072     * @param len the packed Message len
073     * @exception IOException on I/O error
074     */
075    protected void sendMessageLength(int len) throws IOException {
076        serverOut.write (0);
077        serverOut.write (0);
078        serverOut.write (len >> 8);
079        serverOut.write (len);
080    }
081    /**
082     * @return the Message len
083     * @exception IOException on I/O error
084     * @exception ISOException on ISO processing error
085     */
086    protected int getMessageLength() throws IOException, ISOException {
087        int l = 0;
088        byte[] b = new byte[4];
089        while (l == 0) {
090            serverIn.readFully(b,0,4);
091            l = ((int)b[2] &0xFF) << 8 | (int)b[3] &0xFF;
092            if (l == 0) {
093                serverOut.write(b);
094                serverOut.flush();
095            }
096        }
097        return l;
098    }
099    protected int getHeaderLength() { 
100        // GICC Channel does not support header
101        return 0; 
102    }
103    protected void sendMessageHeader(ISOMsg m, int len) {
104        // GICC Channel does not support header
105    }
106}
107