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

📄 modemprovider.java

📁 jtapi for telephone
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.sourceforge.gjtapi.raw.modem;

// NAME
//      $RCSfile: ModemProvider.java,v $
// DESCRIPTION
//      [given below in javadoc format]
// DELTA
//      $Revision: 1.5 $
// CREATED
//      $Date: 2003/11/04 17:15:35 $
// COPYRIGHT
//      Westhawk Ltd
// TO DO
//

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URL;
import java.util.*;
import javax.telephony.*;
import javax.telephony.media.*;

import net.sourceforge.gjtapi.*;
import net.sourceforge.gjtapi.media.SymbolConvertor;
import net.sourceforge.gjtapi.raw.MediaTpi;

/**
 * An implementation of a Jtapi provider which uses a voice capable
 * modem.
 *
 * @author <a href="mailto:ray@westhawk.co.uk">Ray Tran</a>
 * @version $Revision: 1.5 $ $Date: 2003/11/04 17:15:35 $
 */

public class ModemProvider implements MediaTpi, ModemListener {
    private static final String     version_id =
        "@(#)$Id: ModemProvider.java,v 1.5 2003/11/04 17:15:35 rdeadman Exp $ Copyright Westhawk Ltd";

    private final static String RESOURCE_NAME = "Modem.props";
    private final static String ADDRESS_PREFIX = "Address";
    private final static String SERIAL = "Serial";//used as a key to select the
                                                //serial port used by the modem
	// The plugged in Modem interface implementation lookup key
	private final static String MODEM_PROVIDER_CLASS = "ModemClass";
	private final static String DEFAULT_MODEM_PROVIDER = "net.sourceforge.gjtapi.raw.modem.AccuraV92";

    private Properties provProps;
    private List addresses;
    private TermData terminal;
    private TelephonyListener listener; //According to implementors guide we only need one!
    private Modem modem;

    /**
     * Raw constructor used by the GenericJtapiPeer factory
     */
    public ModemProvider() {
        // read provider details and load the resources, if available
        Properties props = new Properties();
        try {
            props.load(this.getClass().getResourceAsStream('/' + RESOURCE_NAME));
        } catch (IOException ioe) {
            // ignore and hope that the initialize method sets my required properties
        }
        provProps = props;
    }

    //ModemListener implementation
    
    /**
     * The modem is ringing
     * 
     * @return CallId - the id of the new call
     */
    public CallId modemRinging(){
        CallId id = null;
        if (listener != null){
            String address = (String)addresses.get(0);
            try{
                id = reserveCallId(address);
                listener.connectionAlerting(id, address,
                        ConnectionEvent.CAUSE_NEW_CALL);
                listener.terminalConnectionRinging(id, address,
                        terminal.terminal, TerminalConnectionEvent.CAUSE_NORMAL);
            }catch (InvalidArgumentException ex){
                System.err.println("Invalid argument");
            }
        }
        return id;
    }
    
    /**
     * The modem was ringing but the caller has hung up before we answered
     * 
     * @param id - the id of the call which has just stopped
     */
    public void ringingStopped(CallId id){
        if (listener != null){
            String address = (String)addresses.get(0);
            listener.terminalConnectionDropped(id, address,
                    terminal.terminal, ConnectionEvent.CAUSE_CALL_CANCELLED);
        }
        releaseCallId(id);
    }

    public void modemConnected(CallId id){
        if (listener != null){
            String address = (String)addresses.get(0);
            listener.connectionConnected(id, address, ConnectionEvent.CAUSE_NORMAL);
            listener.terminalConnectionTalking(id, address, this.terminal.terminal, Event.CAUSE_NORMAL);
        }
    }

    public void modemDisconnected(CallId id){
        if (listener != null){
            String address = (String)addresses.get(0);
            listener.connectionDisconnected(id, address, ConnectionEvent.CAUSE_NORMAL);
        }
    }

    public void modemFailed(CallId id){
        if (listener != null){
            String address = (String)addresses.get(0);
            listener.connectionFailed(id, address, ConnectionEvent.CAUSE_DEST_NOT_OBTAINABLE);
        }
    }

    //BasicJtapiTpi implementation
    /**
     * Initialize the provider.
     *
     * The main task is setting the serial port and modem up.
     *
     * @param props Map describing how to initialise
     * @throws ProviderUnavailableException
     */
    public void initialize(Map props) throws ProviderUnavailableException {
        if(provProps != null){
        	// first allow passed in props to override my defaults
        	if (props != null)	
        		provProps.putAll(props);
        		
            Object obj = provProps.get(SERIAL);
            if((obj != null) && (obj instanceof String)){
                String portname = (String) obj;
                // get the class name for the plugged in Modem adapter
                String modemClassName = (String)provProps.get(ModemProvider.MODEM_PROVIDER_CLASS);
                if (modemClassName == null)
                	modemClassName = ModemProvider.DEFAULT_MODEM_PROVIDER;
                // now try to instantiate
               
                //TODO: need to get the modem by reflection or similar
                try {
                	// set the parameter type
                	Class[] paramTypes = {ModemListener.class};
                	Constructor constructor = Class.forName(modemClassName).getConstructor(paramTypes);
                	Object[] params = {this};
                	modem = (Modem)constructor.newInstance(params);
                } catch (ClassNotFoundException cnfe) {
                	throw new ProviderUnavailableException("Modem implementation not found: " + modemClassName);
                } catch (NoSuchMethodException e) {
                	throw new ProviderUnavailableException("Incorrect Constructor: " + modemClassName);
				} catch (InvocationTargetException e) {
                	throw new ProviderUnavailableException("Incorrect target: " + modemClassName);
				} catch (IllegalAccessException iae) {
                	throw new ProviderUnavailableException("Could not access: " + modemClassName);					
                } catch (InstantiationException e) {
                	throw new ProviderUnavailableException("Could not instantiate: " + modemClassName);
				}
                if (modem.initialize(portname) == false){
                    modem = null;
                    throw new ProviderUnavailableException(
                        ProviderUnavailableException.CAUSE_NOT_IN_SERVICE,
                        "The modem on " + portname + " could not be initialized"
                    );
                }
            }else{
                throw new ProviderUnavailableException(
                    ProviderUnavailableException.CAUSE_INVALID_ARGUMENT,
                    "The passed in serial port name was \"null\" or not a String"
                );
            }
        }else{
            throw new ProviderUnavailableException(
                ProviderUnavailableException.CAUSE_INVALID_ARGUMENT,
                "Parameter \"props\" was null"
            );
        }

        //Now get all of the addresses
        addresses = new Vector();
        Iterator iter = provProps.keySet().iterator();
        while (iter.hasNext()){
            String key = (String)iter.next();
            if (key.startsWith(ADDRESS_PREFIX)){
                addresses.add(provProps.get(key));
            }
        }

        //Finally set up the terminal
        terminal = new TermData((String)provProps.get(SERIAL), true);
    }

    /**
     * Add an observer for RawEvents.
     *
     * We can only store a single listener because that is all that is required
     * by the architecture.
     *
     * @param ro TelephonyListener to register
     */
    public void addListener(TelephonyListener ro) {
        if (listener == null){
            listener = ro;
        }else{
            System.err.println("Request to add a TelephonyListener to "
                + this.getClass().getName() + ", but one is already registered");
        }
    }

    /**
     * Remove the observer for RawEvents.
     *
     * If the listener isn't the one registered nothing is done
     *
     * @param ro TelephonyListener to de-register
     */
    public void removeListener(TelephonyListener ro) {
        if (ro == listener){
            listener = null;
        }else{
            System.err.println("Request to remove a TelephonyListener from "
                + this.getClass().getName() + ", but it wasn't registered");
        }
    }

    public String[] getAddresses() throws ResourceUnavailableException {
        Iterator iter = addresses.iterator();
        /*debug
         * while (iter.hasNext()){
         *     System.err.println("    Address: " + iter.next());
         * }
         */
        return (String[]) addresses.toArray(new String[0]);
    }

⌨️ 快捷键说明

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