⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tapi3provider.java

📁 jtapi for telephone
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        logger.debug("Trying to instantiate " + tapi3ImplClass);
        try {
            Class cls = Class.forName(tapi3ImplClass);
            Method m = cls.getMethod("getInstance", null);
            tapi3Native = (Tapi3Native)m.invoke(null, null);
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
        logger.debug("tapi3Native successfully created.");
    }

    /**
     * Return the friendly name of the parameter methodID  
     * @param methodID The identifier of the method parameter (as used in the {@link #callback} method)
     * @return The friendly name of methodID
     */
    private static String getMethodName(int methodID) {
        String name = "<UNKNOWN>";
        if(methodID >= 0 && methodID < METHOD_NAMES.length) {
            name = METHOD_NAMES[methodID];
        }
        return name;
    }

    /**
     * Return the friendly name of the parameter jniCause 
     * @param jniCause The identifier of the cause parameter (as used in the {@link #callback} method)
     * @return The friendly name of jniCause
     */
    private static int getEventCause(int jniCause) {
        switch(jniCause) {
            case JNI_CAUSE_NORMAL: return Event.CAUSE_NORMAL;
            case JNI_CAUSE_NEW_CALL: return Event.CAUSE_NEW_CALL;
            case JNI_CAUSE_SNAPSHOT: return Event.CAUSE_SNAPSHOT;
            case JNI_CAUSE_DEST_NOT_OBTAINABLE: return Event.CAUSE_DEST_NOT_OBTAINABLE;
        }
        return Event.CAUSE_UNKNOWN;
    }
    
    /**
     * Callback method typically called by the concrete implementation of the {@link Tapi3Native} interface
     * @param methodID The identifier of the method (event) that should be notified. Must be one of the <i>METHOD_XXX</i> values.
     * @param callID The identifier for the call
     * @param address The address that defines the call
     * @param jniCause The event cause. Must be one of the <i>JNI_CAUSE_XXX</i> values.
     * @param callInfo Array of 4 elements used to initialize a {@link Tapi3PrivateData}
     */
    public void callback(int methodID, int callID, String address, int jniCause, String[] callInfo) {
        Tapi3CallID tapi3CallID = new Tapi3CallID(callID);
        Iterator it = listenerList.iterator();
        String terminal = address;  // !!!
//        String terminal = getTerminals(address)[0].terminal;
        Tapi3PrivateData privateData = null;
        if(callInfo != null && callInfo.length == 4) {
            privateData = new Tapi3PrivateData(callInfo[0], callInfo[1], callInfo[2], callInfo[3]);
        }
        int eventCause = getEventCause(jniCause);
        String methodName = getMethodName(methodID);
        logger.info("CALLBACK: " + methodID + " (" + methodName + ") on " + address +
                ": callID=" + tapi3CallID.getCallID() + ", privateData: " + privateData);
        while(it.hasNext()) {
            TelephonyListener listener = (TelephonyListener)it.next();
            switch(methodID) {
                case METHOD_ADDRESS_PRIVATE_DATA:
                    listener.addressPrivateData(address, privateData, eventCause);
                    break;
                case METHOD_CALL_ACTIVE:
                    listener.callActive(tapi3CallID, eventCause);
                    break;
                case METHOD_CALL_INVALID:
                    listener.callInvalid(tapi3CallID, eventCause);
                    break;
                case METHOD_CALL_PRIVATE_DATA:
                	// Called at exit
                    // listener.callPrivateData(tapi3CallID, privateData, eventCause);
                    break;
                case METHOD_CONNECTION_ALERTING:
                    listener.connectionAlerting(tapi3CallID, address, eventCause);
                    break;
                case METHOD_CONNECTION_CONNECTED:
                    listener.connectionConnected(tapi3CallID, address, eventCause);
                    break;
                case METHOD_CONNECTION_DISCONNECTED:
                    listener.connectionDisconnected(tapi3CallID, address, eventCause);
                    break;
                case METHOD_CONNECTION_FAILED:
                    listener.connectionFailed(tapi3CallID, address, eventCause);
                    break;
                case METHOD_CONNECTION_IN_PROGRESS:
                    listener.connectionInProgress(tapi3CallID, address, eventCause);
                    break;
                case METHOD_PROVIDER_PRIVATE_DATA:
                    listener.providerPrivateData(privateData, eventCause);
                    break;
                case METHOD_TERMINAL_CONNECTION_CREATED:
                    listener.terminalConnectionCreated(tapi3CallID, address, terminal, eventCause);
                    break;
                case METHOD_TERMINAL_CONNECTION_DROPPED:
                    listener.terminalConnectionDropped(tapi3CallID, address, terminal, eventCause);
                    break;
                case METHOD_TERMINAL_CONNECTION_HELD:
                    listener.terminalConnectionHeld(tapi3CallID, address, terminal, eventCause);
                    break;
                case METHOD_TERMINAL_CONNECTION_RINGING:
                    listener.terminalConnectionRinging(tapi3CallID, address, terminal, eventCause);
                    break;
                case METHOD_TERMINAL_CONNECTION_TALKING:
                    listener.terminalConnectionTalking(tapi3CallID, address, terminal, eventCause);
                    break;
                case METHOD_TERMINAL_PRIVATE_DATA:
                    listener.terminalPrivateData(terminal, privateData, eventCause);
                    break;
                default:
                    logger.error("CALLBACK: Unknown method: " + methodID + ", callID=" + callID +
                            ", address=" + address + ", jniCause=" + jniCause + ", callInfo=" +
                            (callInfo != null ? Arrays.asList(callInfo).toString() : "null"));
                    break;
            }
            if(privateData != null) {
                listener.callPrivateData(tapi3CallID, privateData, eventCause);
            }
        }
    }

    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.CoreTpi#initialize(java.util.Map)
     */
    public void initialize(Map props) throws ProviderUnavailableException {
    	configureProperties(props);
		configureLogger(props);
        logger.debug("Tapi3 properties: " + props + " ...");
        configureNative(props);        
        logger.debug("Initializing Tapi3 provider...");
        addresses = tapi3Native.tapi3Init(props);
        logger.debug("Registering Tapi3 provider...");
        tapi3Native.registerProvider(this);

        logger.debug("Retrieving addresses...");
        if(addresses == null) {
            addresses = new String[0];
        }
        terminals = new TermData[addresses.length];
        for(int i=0; i<terminals.length; i++) {
            logger.info("Address #" + (i+1) + ": " + addresses[i]);
            terminals[i] = new TermData(addresses[i], false);
        }
        logger.debug("Initialized");
    }

    /**
     * Replace ${<i>propertyName</i>} occurences with the actual value of the system property identified by <i>propertyName</i>  
     * @param props The properties map whose values will be modified
     */
    public static void configureProperties(Map props) {
    	Iterator it = props.entrySet().iterator();
    	while(it.hasNext()) {
        	Map.Entry entry = (Map.Entry) it.next();
			StringBuffer sbufVal = new StringBuffer((String)entry.getValue());
        	while(true) {
        		int startPos = sbufVal.indexOf("${");
        		if(startPos < 0) break;
        		int endPos = sbufVal.indexOf("}", startPos+3);
        		if(endPos < 0) break;
        		String sysProp = sbufVal.substring(startPos+2, endPos);
        		String replaceVal = System.getProperty(sysProp);
        		if(replaceVal != null) {
        			sbufVal.replace(startPos, endPos+1, replaceVal); 
        		}
        	}
        	entry.setValue(sbufVal.toString());
    	}
    }
    
    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.CoreTpi#shutdown()
     */
    public void shutdown() {
        logger.debug("Shutting down...");
        int retCode = tapi3Native.tapi3Shutdown();
        logger.debug("Shut down: retCode=" + retCode);
    }

    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.CoreTpi#addListener(net.sourceforge.gjtapi.TelephonyListener)
     */
    public void addListener(TelephonyListener ro) {
        logger.debug("addListener(" + ro.getClass().getName() + ")");
        listenerList.add(ro);
    }

    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.CoreTpi#removeListener(net.sourceforge.gjtapi.TelephonyListener)
     */
    public void removeListener(TelephonyListener ro) {
        logger.debug("removeListener()");
        listenerList.remove(ro);
    }

    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.CoreTpi#getCapabilities()
     */
    public Properties getCapabilities() {
        logger.debug("getCapabilities()");
        Properties caps = new Properties();
        // mark my differences from the default
//      caps.put(Capabilities.HOLD, "f");
//      caps.put(Capabilities.JOIN, "f");
        caps.put(Capabilities.THROTTLE, "f");
        caps.put(Capabilities.MEDIA, "t");
        caps.put(Capabilities.ALL_MEDIA_TERMINALS, "t");
        caps.put(Capabilities.ALLOCATE_MEDIA, "f");

        return caps;
    }

    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.BasicJtapiTpi#getAddresses()
     */
    public String[] getAddresses() throws ResourceUnavailableException {
        logger.debug("getAddresses()");
        return addresses;
    }

    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.BasicJtapiTpi#getAddresses(java.lang.String)
     */
    public String[] getAddresses(String terminal) throws InvalidArgumentException {
        logger.debug("getAddresses(" + terminal + ")");
        for(int i=0; i<terminals.length; i++) {
            if(terminals[i].terminal.equals(terminal)) {
                return new String[] { addresses[i] };
            }
        }
        return new String[0];
    }

    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.BasicJtapiTpi#getTerminals()
     */
    public TermData[] getTerminals() throws ResourceUnavailableException {
        logger.debug("getTerminals()");
        return terminals;
    }

    /* (non-Javadoc)
     * @see net.sourceforge.gjtapi.raw.BasicJtapiTpi#getTerminals(java.lang.String)
     */
    public TermData[] getTerminals(String address) throws InvalidArgumentException {
        logger.debug("getTerminals(" + address + ")");
        for(int i=0; i<addresses.length; i++) {
            if(addresses[i].equals(address)) {
                return new TermData[] { terminals[i] };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -