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