www.pudn.com > d4j.zip > DTI.java


// DTI: Dialogic DTI devices 
// $Id: DTI.java,v 1.3 2003/11/13 11:42:30 cgm8 Exp $ 
/*  
 * Copyright (c) 1999 Carlos G Mendioroz. 
 * 
 *  This file is part of D4J. 
 * 
 *  D4J is free software; you can redistribute it and/or 
 *  modify it under the terms of the GNU Lesser General Public 
 *  License as published by the Free Software Foundation; either 
 *  version 2 of the License, or (at your option) any later version. 
 *   
 *  D4J is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 *  Lesser General Public License for more details. 
 *   
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with this library; if not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 *  Boston, MA  02111-1307, USA. 
 * 
 * Report problems and direct all questions to: 
 * 
 *	tron@acm.org 
 */ 
 package local.dialogic; 
 
public class DTI extends Device implements ResourceServer 
{ 
    // Handy constants 
    private static final int A = Dialogic.DTB_ABIT; 
    private static final int B = Dialogic.DTB_BBIT; 
    private static final int C = Dialogic.DTB_CBIT; 
    private static final int D = Dialogic.DTB_DBIT; 
    private static final int CA = Dialogic.DTC_ABIT; 
    private static final int CB = Dialogic.DTC_BBIT; 
    private static final int CC = Dialogic.DTC_CBIT; 
    private static final int CD = Dialogic.DTC_DBIT; 
    private static final int WINK = Dialogic.DTMM_WINK; 
    private static final int FORCE = Dialogic.DTA_SETMSK; 
    private static final int SET = Dialogic.DTA_ADDMSK; 
    private static final int CLEAR = Dialogic.DTA_SUBMSK; 
     
    // Class variables 
    private static java.util.Hashtable boards; 
    private static AlarmHandler alarms = null; 
     
    static { 
        boards = new java.util.Hashtable(); 
    } 
     
    public static void setAlarmHandler(AlarmHandler handler) { 
        alarms = handler; 
    } 
     
    // Instance variables 
    int ts; 
    DTI tss[]; // reference to boards'ts devices 
 
    /** Open a dt device */ 
    public DTI(Channel ch, String name) 
    { 
        super(ch, name); 
        // dtiBnTm 
        this.name = name; 
        if (! name.substring(0, 4).equals("dtiB")) 
            throw new RuntimeException("DTI: bad name"); 
         
 
        int ti = name.indexOf('T'); 
        if (ti < 0) { 
            device = Dialogic.dt_open(name, 0); 
            // opening a board, should be another class ? 
            ts = -1; 
            tss = new DTI[31]; 
            Channel.register(this, device); 
            try { 
            // Let me know of alarms to clear channels, etc 
            Dialogic.dt_setevtmsk(device, Dialogic.DTG_E1ERREVT,  
                Dialogic.DEEC_RLOS|Dialogic.DEEC_RUA1|Dialogic.DEEC_FSERR| 
                Dialogic.DEEC_RRA|Dialogic.DEEC_BPVS|Dialogic.DEEC_CECS| 
                Dialogic.DEEC_ECS|Dialogic.DEEC_LOS|Dialogic.DEEC_DPM| 
                Dialogic.DEEC_MFSERR|Dialogic.DEEC_RSA1|Dialogic.DEEC_RDMA          
                , Dialogic.DTA_SETMSK); 
            // ? 
            // No loopback (DTMD_NORMAL) loopbacks DTMD_XCVRLB, _LIULLB, _LIURLB 
            Dialogic.dt_setparm(device, Dialogic.DTG_SETBDMD,Dialogic.DTMD_NORMAL); 
            // Clock: DTC_LOOP, _IND, _EXT, _NOCLK 
            Dialogic.dt_setparm(device, Dialogic.DTG_SETCLK,Dialogic.DTC_LOOP); 
            } catch (RuntimeException re) {} //assume restart after crash 
            // XXX We should take care of alarms... 
        } else { 
            String boardname = name.substring(0, ti); 
            int boardts; 
            try { 
                boardts = Integer.parseInt(name.substring(ti+1)); 
            } catch (NumberFormatException nfe) { 
                throw new RuntimeException("DTI: bad name"); 
            } 
            DTI board; 
            synchronized(boards) { 
                if ((board = (DTI)boards.get(boardname)) == null) { 
                    board = new DTI(null, boardname); 
                    boards.put(boardname, board); 
                } 
            } 
            device = Dialogic.dt_open(name, 0); 
            register(); 
            Dialogic.dt_setevtmsk(device, Dialogic.DTG_SIGEVT,  
                Dialogic.DTMM_AON|Dialogic.DTMM_BON|Dialogic.DTMM_AOFF| 
                Dialogic.DTMM_BOFF|Dialogic.DTMM_WINK, Dialogic.DTA_SETMSK); 
            Dialogic.dt_settssig(device, A|B|D, FORCE); 
            ts = Dialogic.dt_getxmitslot(device); 
            board.tss[boardts] = this; 
        } 
    } 
 
    public void finalize() throws Throwable  
    { 
        close(); 
        super.finalize(); 
    } 
     
    public void close() 
    { 
        if (device != 0) { 
            Dialogic.dt_setevtmsk(device, Dialogic.DTG_SIGEVT, 0, Dialogic.DTA_SETMSK); 
            Dialogic.dt_settssig(device, A|B|D, FORCE); 
            Dialogic.dt_close(device); 
            super.close(); 
            device = 0;     // just in case 
        } 
    } 
 
    public boolean getA() { 
        int sigbits = Dialogic.ATDT_TSSGBIT(device); 
         return ((sigbits & CA) != 0); 
    } 
 
    public boolean getB() { 
        int sigbits = Dialogic.ATDT_TSSGBIT(device); 
         return ((sigbits & CB) != 0); 
    } 
     
    public int getTs() { 
        return ts; 
    } 
 
    public void listen(Device source) { 
        if (source != null)  
            Dialogic.dt_listen(device, source.getTs()); 
        else 
            Dialogic.dt_unlisten(device); 
    } 
     
    public void listen(Channel ch, Resource res) { 
        int ts = res.getTs(ch); 
        if (ts < 0) 
            Dialogic.dt_unlisten(device); 
        else  
            Dialogic.dt_listen(device, ts); 
    } 
  
    public void setsig(boolean a, boolean b) { 
        int mask = D; 
        if (a) mask |= A; 
        if (b) mask |= B; 
        Dialogic.dt_settssig(device, mask, FORCE); 
    } 
     
    public boolean service(EVT evt) { 
        if (Dialogic.debug != 0) 
            System.out.println("DTI board event: " + evt); 
        if (alarms != null) 
            alarms.handleAlarm("DTI board " + name + ": " + evt.toString()); 
        // We should copy to all interested parties... 
        for (int i = 1; i <= 30; i++) { 
            if (tss[i] == null) 
                continue; 
            Channel.dispatch(evt.dup(tss[i].device)); 
        } 
        return true; 
    } 
}