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;
024import org.jpos.core.Configuration;
025import org.jpos.core.ConfigurationException;
026
027import java.io.IOException;
028import java.net.ServerSocket;
029
030/**
031 * Talks with TCP based NCCs
032 * Sends [LEN][TPDU][ISOMSG]
033 * (len=2 bytes BCD)
034 *
035 * @author Alejandro P. Revilla
036 * @version $Revision$ $Date$
037 * @see ISOMsg
038 * @see ISOException
039 * @see ISOChannel
040 */
041public class NCCChannel extends BaseChannel {
042    /**
043     * Public constructor 
044     */
045    boolean tpduSwap = true;
046    public NCCChannel () {
047        super();
048    }
049    /**
050     * Construct client ISOChannel
051     * @param host  server TCP Address
052     * @param port  server port number
053     * @param p     an ISOPackager
054     * @param TPDU  an optional raw header (i.e. TPDU)
055     * @see ISOPackager
056     */
057    public NCCChannel (String host, int port, ISOPackager p, byte[] TPDU) {
058        super(host, port, p);
059        this.header = TPDU;
060    }
061    /**
062     * Construct server ISOChannel
063     * @param p     an ISOPackager
064     * @param TPDU  an optional raw header (i.e. TPDU)
065     * @exception IOException
066     * @see ISOPackager
067     */
068    public NCCChannel (ISOPackager p, byte[] TPDU) throws IOException {
069        super(p);
070        this.header = TPDU;
071    }
072    /**
073     * constructs server ISOChannel associated with a Server Socket
074     * @param p     an ISOPackager
075     * @param TPDU  an optional raw header (i.e. TPDU)
076     * @param serverSocket where to accept a connection
077     * @exception IOException
078     * @see ISOPackager
079     */
080    public NCCChannel (ISOPackager p, byte[] TPDU, ServerSocket serverSocket) 
081        throws IOException
082    {
083        super(p, serverSocket);
084        this.header = TPDU;
085    }
086    protected void sendMessageLength(int len) throws IOException {
087        try {
088            serverOut.write (
089                ISOUtil.str2bcd (
090                    ISOUtil.zeropad (Integer.toString (len % 10000), 4), true
091                )
092            );
093        } 
094        catch (ISOException e) {
095            Logger.log (new LogEvent (this, "send-message-length", e));
096        }
097    }
098    protected int getMessageLength() throws IOException, ISOException {
099        byte[] b = new byte[2];
100        serverIn.readFully(b,0,2);
101        return Integer.parseInt (
102            ISOUtil.bcd2str (b, 0, 4, true)
103        );
104    }
105    protected void sendMessageHeader(ISOMsg m, int len) throws IOException { 
106        byte[] h = m.getHeader();
107        if (h != null) {
108            if (tpduSwap && h.length == 5) {
109                // swap src/dest address
110                byte[] tmp = new byte[2];
111                System.arraycopy (h,   1, tmp, 0, 2);
112                System.arraycopy (h,   3,   h, 1, 2);
113                System.arraycopy (tmp, 0,   h, 3, 2);
114            }
115        }
116        else
117            h = header ;
118        if (h != null) 
119            serverOut.write(h);
120    }
121    /**
122     * New QSP compatible signature (see QSP's ConfigChannel)
123     * @param header String as seen by QSP
124     */
125    public void setHeader (String header) {
126        super.setHeader (ISOUtil.str2bcd(header, false));
127    }
128    public void setConfiguration (Configuration cfg) 
129        throws ConfigurationException
130    {
131        super.setConfiguration (cfg);
132        tpduSwap = cfg.getBoolean ("tpdu-swap", true);
133    }
134}
135