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

📄 kapschprovider.java

📁 tapi的java封装
💻 JAVA
字号:
/*
*	JTAPI library copyright 1998 by Web4Groups consortium (http://Web4Groups.at)
*/

import java.telephony.*;
import java.telephony.capabilities.*;
import java.telephony.events.*;
import java.telephony.media.*;
import java.telephony.media.events.*;
import java.util.*;



public class KapschProvider implements Provider { 

	private static final String name = "KapschProvider";

	int state = OUT_OF_SERVICE;
	int numLines;
	ArrayVector observers;
	ArrayVector calls;
	Hashtable lines;
	Hashtable addresses;
	Hashtable terminals;
	Hashtable appTerminals;
	Hashtable container;
	Hashtable configuration;

	/* native methods */
	private native int TAPIInitialize();
	private native void TAPIShutdown();
	private native int TAPICreateCall();



	public KapschProvider() { 

		observers = new ArrayVector();
		calls = new ArrayVector();
		lines = new Hashtable();
		addresses = new Hashtable();
		terminals = new Hashtable();
		container = new Hashtable();
		TAPIConfig config = new TAPIConfig();
		numLines = TAPIInitialize();
		configuration = config.read();

		Enumeration lineVect;
		Enumeration addrTerm;
		String addrStr;
		String termStr;
		TAPIAddress address;
		TAPITerminal terminal;
		Vector vec;
		lineVect = configuration.elements();
		while (lineVect.hasMoreElements()) {
			vec = (Vector)lineVect.nextElement();
			addrTerm = vec.elements();
			addrStr = (String)addrTerm.nextElement();
			address = new TAPIAddress(this, addrStr);
			addresses.put(addrStr, address);
			while (addrTerm.hasMoreElements()) {
				termStr = (String)addrTerm.nextElement();
				terminal = new TAPITerminal(this, termStr);
				terminals.put(termStr, terminal);
				terminal.addAddress(address);
				address.addTerminal(terminal);
			}
		}
		for (int i = 0; i < numLines; i++) {
			TAPIThread thread = new TAPIThread(this, i);
			thread.start();
		}
		state = IN_SERVICE;
		System.out.println("\nTAPI now running with " + numLines + " lines opened\n");
	}



	public void finalize() {
		TAPIShutdown();
		state = SHUTDOWN;
		ProviderObserver[] obs = getObservers();
		if (obs != null) {
			ProvEv[] evList = new TAPIProvEv[1];
			evList[0] = new TAPIProvShutdownEv(this, this);
			RunProvObservers po = new RunProvObservers(obs, evList);
			po.start();
		}
	}



	public int getState() {
		return state;
	}



	public String getName() {
		return name;
	}



	public Call[] getCalls() {

		Call ret[]=null;
		Object obj[]=calls.getArray();
		if(obj!=null){
			ret=new Call[obj.length];
			System.arraycopy(obj,0,ret,0,obj.length);
		}
		return ret;
	}



	public Address getAddress(String name){
		return (Address)addresses.get(name);
	}



	public Address[] getAddresses() {

		TAPIAddress address[] = new TAPIAddress[addresses.size()];
		Enumeration e = addresses.elements();

		int i = 0;
		while (e.hasMoreElements()) {
			address[i] = (TAPIAddress)e.nextElement();
			i++;
		}
		return address;
	}



	public Terminal[] getTerminals() {

		TAPITerminal terminal[] = new TAPITerminal[terminals.size()];
		Enumeration e = terminals.elements();

		int i = 0;
		while(e.hasMoreElements()) {
			terminal[i] = (TAPITerminal)e.nextElement();
			i++;
		}
		return terminal;
	}



	public Terminal getTerminal(String name) {
		Terminal terminal = (Terminal)terminals.get(name);
		return terminal;
	}



	public void shutdown() {
		TAPIShutdown();
		state = SHUTDOWN;
		ProviderObserver[] obs = getObservers();
		if (obs != null) {
			ProvEv[] evList = new TAPIProvEv[1];
			evList[0] = new TAPIProvShutdownEv(this, this);
			RunProvObservers po = new RunProvObservers(obs, evList);
			po.start();
		}
	}



	public Call createCall() throws InvalidStateException {
		if(getState() != IN_SERVICE)
			throw new InvalidStateException(this, InvalidStateException.PROVIDER_OBJECT, getState());
		Call call = new TAPICall(this);
		calls.addElement(call);
		return call;
	}



	public synchronized void addObserver(ProviderObserver observer) {
		if(observer!=null)
			observers.addElement(observer);
	}



	public synchronized ProviderObserver[] getObservers() {
		ProviderObserver ret[]=null;
		Object obj[]=observers.getArray();
		if(obj!=null){
			ret=new ProviderObserver[obj.length];
			System.arraycopy(obj,0,ret,0,obj.length);
		}
		return ret;
	}



	public synchronized void removeObserver(ProviderObserver observer) {
		if(observer!=null)
			observers.removeElement(observer);
	}



	public ProviderCapabilities getProviderCapabilities(Terminal term) {
		return new TAPIProviderCapabilities();
	}



	public CallCapabilities getCallCapabilities(Terminal terminal,Address address) {
		return new TAPICallCapabilities();
	}



	public ConnectionCapabilities getConnectionCapabilities(Terminal terminal, Address address) {
		TerminalConnection tc[] = null;
		try {
			tc = terminal.getTerminalConnections();
		}
		catch (Exception e) {
			System.out.println("Exception in Provider.getConnectionCapabilities: " + e.toString());
		}
		TAPITerminalConnection ttc = (TAPITerminalConnection)tc[0];
		return new TAPIMediaTerminalConnectionCapabilities(ttc.line);
	}



	public AddressCapabilities getAddressCapabilities(Terminal terminal) {
		return new TAPIAddressCapabilities();
	}



	public TerminalConnectionCapabilities getTerminalConnectionCapabilities(Terminal terminal) {
		return new TAPITerminalConnectionCapabilities();
	}
	


	public TerminalCapabilities getTerminalCapabilities(Terminal terminal) {
		return new TAPITerminalCapabilities();
	}



	static{
		System.loadLibrary(name);
	}
}






class TAPIThread extends Thread {

	private native void TAPILineHandler(int line);
	private int line_nr;

	KapschProvider provider;
	Call call = null;
	Connection[] c = null;
	Terminal t = null;
	TerminalConnection tc1 = null;
	TerminalConnection tc2 = null;

	final static int NEW_CALL = 0;
	final static int DISCONN = 1;
	final static int DIGIT_0 = 0x50;
	final static int DIGIT_1 = 0x51;
	final static int DIGIT_2 = 0x52;
	final static int DIGIT_3 = 0x53;
	final static int DIGIT_4 = 0x54;
	final static int DIGIT_5 = 0x55;
	final static int DIGIT_6 = 0x56;
	final static int DIGIT_7 = 0x57;
	final static int DIGIT_8 = 0x58;
	final static int DIGIT_9 = 0x59;
	final static int DIGIT_A = 0x5a;
	final static int DIGIT_B = 0x5b;
	final static int DIGIT_C = 0x5c;
	final static int DIGIT_D = 0x5d;
	final static int DIGIT_N = 0x5e;
	final static int DIGIT_S = 0x5f;



	TAPIThread(KapschProvider prov, int i) {
		super();
		provider = prov;
		line_nr = i;
	}


	public void run() {
		TAPILineHandler(line_nr);
	}



	protected void TAPIUpdate(int event, int line) {

		
		try {
			call = (Call)provider.container.get(new Integer(line));
			if (call != null) {
				c = call.getConnections();
				if (c != null) {
					TerminalConnection[] tcs1 = c[0].getTerminalConnections();
					TerminalConnection[] tcs2 = c[1].getTerminalConnections();
					if (tcs1 != null) {
						tc1 = tcs1[0];
						t = tcs1[0].getTerminal();
					}
					if (tcs2 != null) {
						tc2 = tcs2[0];
					}	
				}
			}

		switch (event) {
			case NEW_CALL:	new_call(line); 
							break;
			case DISCONN:	disconnect(line);
							break;
			case DIGIT_0:	digit('0'); 
							break;
			case DIGIT_1:	digit('1'); 
							break;
			case DIGIT_2:	digit('2'); 
							break;
			case DIGIT_3:	digit('3'); 
							break;
			case DIGIT_4:	digit('4'); 
							break;
			case DIGIT_5:	digit('5'); 
							break;
			case DIGIT_6:	digit('6'); 
							break;
			case DIGIT_7:	digit('7'); 
							break;
			case DIGIT_8:	digit('8'); 
							break;
			case DIGIT_9:	digit('9'); 
							break;
			case DIGIT_A:	digit('A'); 
							break;
			case DIGIT_B:	digit('B'); 
							break;
			case DIGIT_C:	digit('C'); 
							break;
			case DIGIT_D:	digit('D'); 
							break;
			case DIGIT_N:	digit('#'); 
							break;
			case DIGIT_S:	digit('*'); 
							break;
			default:	break;
		} //switch
		} //try
		catch (Exception e) { System.out.println(e.toString()); }

	}

	private void new_call(int line) throws Exception {

		//find out the terminal which wants to serve this call
		Vector addrTermVec = (Vector)provider.configuration.get(new Integer(line));
		Enumeration addrTerm = addrTermVec.elements();
		if (!addrTerm.hasMoreElements()) {
			System.out.println("addrTerm is null");
			return;
		}
		String addrStr = (String)addrTerm.nextElement();	//the first element is the address name
		TAPIAddress address = (TAPIAddress)provider.getAddress(addrStr);

		String termStr;
		TAPITerminal terminal = null;
		CallObserver[] callObservers = null;
		TerminalConnection[] tc;
		while (addrTerm.hasMoreElements()) {
			termStr = (String)addrTerm.nextElement();
			terminal = (TAPITerminal)provider.getTerminal(termStr);
			callObservers = terminal.getCallObservers();
			if (callObservers != null) {
				tc = terminal.getTerminalConnections();
				if (tc == null)	{	//bonga, we need this one
									
					//first stage of incoming call:
					TAPICall call = (TAPICall)provider.createCall();
					provider.container.put(new Integer(line), call);
					call.state = Call.ACTIVE;
					TAPIConnection connectionHome = new TAPIConnection(call, address, line);
					TAPIConnection connectionCaller = new TAPIConnection(call, address, line);
					call.addConnection(connectionHome);
					call.addConnection(connectionCaller);
					address.addConnection(connectionHome);
					connectionCaller.state = Connection.CONNECTED;
					TAPIMediaTerminalConnection mtcCaller = new TAPIMediaTerminalConnection(call, terminal, connectionCaller, line);
					mtcCaller.state = TerminalConnection.ACTIVE;
					connectionCaller.addTerminalConnection(mtcCaller);
					CallEv[] evList = new TAPICallEv[6];
					evList[0] = new TAPICallActiveEv(terminal, call);
					evList[1] = new TAPIConnCreatedEv(terminal, call, connectionHome);
					evList[2] = new TAPIConnCreatedEv(terminal, call, connectionCaller);
					evList[3] = new TAPIConnConnectedEv(terminal, call, connectionCaller);
					evList[4] = new TAPITermConnCreatedEv(null, call, mtcCaller);
					evList[5] = new TAPITermConnActiveEv(null, call, mtcCaller);
					RunCallObservers ro = new RunCallObservers(callObservers, evList);
					ro.start();

					//second stage of incoming call:
					evList = new TAPICallEv[1];
					evList[0] = new TAPIConnInProgressEv(terminal, call, connectionHome);
					ro = new RunCallObservers(callObservers, evList);
					ro.start();

					//third stage of incoming call:
					TAPIMediaTerminalConnection mtcHome = new TAPIMediaTerminalConnection(call, terminal, connectionHome, line);
					connectionHome.addTerminalConnection(mtcHome);
					terminal.addTerminalConnection(mtcHome);
					connectionHome.state = Connection.ALERTING;
					mtcHome.state = TerminalConnection.RINGING;
					evList = new TAPICallEv[3];
					evList[0] = new TAPIConnAlertingEv(terminal, call, connectionHome);
					evList[1] = new TAPITermConnCreatedEv(terminal, call, mtcHome);
					evList[2] = new TAPITermConnRingingEv(terminal, call, mtcHome);
					ro = new RunCallObservers(callObservers, evList);
					ro.start();

					break;
				}
			}
		}

	}



	private void disconnect(int line) throws Exception {
		TAPIAddress addr;
		TAPIConnection tc = (TAPIConnection)c[0];
		tc.state = Connection.DISCONNECTED;
		tc = (TAPIConnection)c[1];
		tc.state = Connection.DISCONNECTED;
		TAPITerminalConnection ttc = (TAPITerminalConnection)tc1;
		ttc.state = TerminalConnection.DROPPED;
		ttc = (TAPITerminalConnection)tc2;
		ttc.state = TerminalConnection.DROPPED;
		addr = (TAPIAddress)c[0].getAddress();
		addr.removeConnection(c[0]);
		TAPICall tcl = (TAPICall)call;
		tcl.removeConnection(c[0]);
		tcl.removeConnection(c[1]);
		TAPITerminal tt = (TAPITerminal)t;
		tt.removeTerminalConnection(tc1);
		CallEv[] evList = new TAPICallEv[4];
		evList[0] = new TAPIConnDisconnectedEv(t, call, c[0]);
		evList[1] = new TAPIConnDisconnectedEv(t, call, c[1]);
		evList[2] = new TAPITermConnDroppedEv(t, call, tc1);
		evList[3] = new TAPITermConnDroppedEv(t, call, tc2);
		CallObserver[] obs = t.getCallObservers();
		RunCallObservers ro = new RunCallObservers(obs, evList);
		ro.start();
	}


	private void digit(char digit) throws Exception {

		MediaTermConnEv[] evList = null;
		TAPIMediaTermConnDtmfEv ev = new TAPIMediaTermConnDtmfEv(t, call, tc1, digit);
		evList = new TAPIMediaTermConnEv[1];
		evList[0] = ev;
		CallObserver[] callObservers = t.getCallObservers();
		RunCallObservers ro = new RunCallObservers(callObservers, evList);
		ro.start();
	}


	static {
		System.loadLibrary("KapschProvider");
	}

}


⌨️ 快捷键说明

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