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

📄 xtapiprovider.java

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

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.telephony.Event;
import javax.telephony.InvalidArgumentException;
import javax.telephony.InvalidPartyException;
import javax.telephony.InvalidStateException;
import javax.telephony.MethodNotSupportedException;
import javax.telephony.PrivilegeViolationException;
import javax.telephony.ProviderUnavailableException;
import javax.telephony.ResourceUnavailableException;
import javax.telephony.media.MediaResourceException;
import javax.telephony.media.PlayerConstants;
import javax.telephony.media.RTC;
import javax.telephony.media.RecorderConstants;
import javax.telephony.media.SignalDetectorConstants;
import javax.telephony.media.Symbol;
import net.sourceforge.gjtapi.CallId;
import net.sourceforge.gjtapi.RawSigDetectEvent;
import net.sourceforge.gjtapi.RawStateException;
import net.sourceforge.gjtapi.TelephonyListener;
import net.sourceforge.gjtapi.TermData;
import net.sourceforge.gjtapi.capabilities.Capabilities;
import net.sourceforge.gjtapi.media.SymbolConvertor;
import net.sourceforge.gjtapi.raw.MediaTpi;

import net.xtapi.serviceProvider.*;

/*
*  GJTAPI - XTAPI Bridge
*  Copyright (C) 2002 Richard Deadman
* 
	All rights reserved. 
	
	Permission is hereby granted, free of charge, to any person obtaining a 
	copy of this software and associated documentation files (the 
	"Software"), to deal in the Software without restriction, including 
	without limitation the rights to use, copy, modify, merge, publish, 
	distribute, and/or sell copies of the Software, and to permit persons 
	to whom the Software is furnished to do so, provided that the above 
	copyright notice(s) and this permission notice appear in all copies of 
	the Software and that both the above copyright notice(s) and this 
	permission notice appear in supporting documentation. 
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 
	OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
	HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 
	INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 
	FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
	NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
	WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
	
	Except as contained in this notice, the name of a copyright holder 
	shall not be used in advertising or otherwise to promote the sale, use 
	or other dealings in this Software without prior written authorization 
	of the copyright holder.
	
	---
	Licenced changed from GPL to the common GJTAPI licence since the
	Free Software Foundation acknowledges that the X11 licence is compatable
	with the GPL and may link to GPL (i.e. XTAPI) code. - 23May2005 Richard Deadman
 *
 * @author  Richard Deadman
 * @version .01
 */

/**
 * This is a bridge GJTAPI service provider that allows XTAPI service providers
 * to be plugged in underneath.
 */
public class XtapiProvider implements MediaTpi, IXTapiCallBack {
	
	/**
	 * This is identifier for calls known by GJTAPI. As such, it has existance
	 * before an XTAPI id is created for it.
	 * <P>Note that this uses identity for equality, since otherwise we have
	 * to keep track of the disposal of a pool of identity tokens.
	 */
	public class XtapiCallId implements CallId {
		// initially not set.
		private int callNum = -1;
		
		// the local address of the call
		private String localAddress = null;
		
		// the remote address of the call, if known
		private String remoteAddress = null;
		public XtapiCallId() {
			super();
		}
		
		/**
		 * Set the XTAPI id for the call.
		 */
		public void setCallNumber(int num) {
			this.callNum = num;
		}
		
		int getCallNum() { return callNum; }
		
		/**
		 * Ensure equality works
		 * Note that if the callNum's are not set, we default to identity
		 */
		public boolean equals(Object other) {
			int myCallId = this.getCallNum();
			if (myCallId == -1)
				return super.equals(other);

			if (other instanceof XtapiCallId) {
				if (myCallId == ((XtapiCallId)other).getCallNum())
					return true;
			}
			return false;
		}
		
		/**
		 * Ensure hashing works
		 */
		/* Removed since the Call Num changes and this will screw up
		 * Hash Tables.
		 * Removed by: Steve Frare, June 9, 2002
		public int hashCode() {
			return this.getCallNum();
		}
		*/
		
		/**
		 * @return
		 */
		public String getLocalAddress() {
			return localAddress;
		}

		/**
		 * @return
		 */
		public String getRemoteAddress() {
			return remoteAddress;
		}

		/**
		 * @param string
		 */
		public void setLocalAddress(String string) {
			localAddress = string;
		}

		/**
		 * @param string
		 */
		public void setRemoteAddress(String string) {
			remoteAddress = string;
		}

	}
	
	/**
	 * Simple holder for Address information.
	 * XTAPI associates one terminal with each address and also holds
	 * a raw handle for each address.
	 */
	class AddressInfo {
		int line = -1;
		String terminal = null;
		int rawHandle = -1;
		
		AddressInfo(int lineNo, String termName, int handle) {
			super();
			
			this.line = lineNo;
			this.terminal = termName;
			this.rawHandle = handle;
		}
		
		String getName() { return ADDR_PREFIX + line; }

		/**
		 * Ensure equality works
		 */
		public boolean equals(Object other) {
			if (other instanceof AddressInfo) {
				AddressInfo ai = (AddressInfo)other;
				if ((this.line == ai.line) &&
					(this.terminal.equals(ai.terminal)) &&
					(this.rawHandle == ai.rawHandle))
						return true;
			}
			return false;
		}
		
		/**
		 * Ensure hashing works
		 */
		public int hashCode() {
			return this.line + this.terminal.hashCode() + this.rawHandle;
		}
		
	}

	// The initialization map key	
	private final static String IXTAPI_KEY = "XtapiSp";
	
	// The TAPI service provider
	private final static String TAPI_SP = "net.xtapi.serviceProvider.MSTAPI";
	
	// The Comm service provider
	private final static String COMM_SP = "net.xtapi.serviceProvider.Serial";
	
	// The address prefix
	private final static String ADDR_PREFIX = "addr_";
	
	// The XTAPI spervice provider
	private IXTapi realProvider = null;
	
	// The GJTAPI Listener I delegate callbacks to
	private TelephonyListener gjListener = null;
	
	// The number of lines my real provider manages
	private int numLines = 0;
	
	// Map of address name to AddressInfo for XTAPI
	private Map addInfoMap = new HashMap();
	
	// Map of Terminal name to AddressInfo
	private Map termToAddr = new HashMap();
	
	// Map of line name (new Integer(rawHandle)) to AddressInfo
	private Map lineToAddr = new HashMap();
	
	/**
	 * Map of terminals to CallHandles
	 * This is used by the media methods to map media terminal to calls
	 */
	private Map termToCalls = new HashMap();
	
	/**
	 * Map of line ids to CallHandles
	 */
	private Map lineToCalls = new HashMap();
	
	/**
	 * map call handles to calls
	 */
	private Map callNumToCalls = new HashMap();
	
	// now hold onto the last remote number and name reported
	private String remoteName = null;
	private String remoteNumber = null;
	
	// The digit buckets to record digits for various terminals
	// This contains a Map of Terminal names to digit StringBuffers.
	private Map termToBuckets = new HashMap();

	/**
	 * Raw constructor used by the GenericJtapiPeer factory
	 * Creation date: (2002-04-12 10:28:55)
	 * @author: Richard Deadman
	 */
	public XtapiProvider() {
		super();
	}

	/**
	 * @see BasicJtapiTpi#getAddresses()
	 */
	public String[] getAddresses() throws ResourceUnavailableException {
		if (this.numLines > 0) {
			String addresses[] = new String[numLines];
			for (int i = 0; i < numLines; i++) {
				addresses[i] = ADDR_PREFIX + i;
			}
			return addresses;
		} else
			return null;
	}

	/**
	 * @see BasicJtapiTpi#getAddresses(String)
	 */
	public String[] getAddresses(String terminal) throws InvalidArgumentException {
		if (terminal != null) {
			AddressInfo addInfo = (AddressInfo)this.termToAddr.get(terminal);
			if (addInfo == null)
				return null;
			String result[] = { addInfo.getName() };
			return result;
		} else
			return null;
	}

	/**
	 * @see BasicJtapiTpi#getTerminals()
	 */
	public TermData[] getTerminals() throws ResourceUnavailableException {
		int size = this.termToAddr.size();
		if (size > 0) {
			TermData terminals[] = new TermData[size];
			Iterator it = this.termToAddr.keySet().iterator();
			int i = 0;
			while (it.hasNext()) {
				String termName = (String)it.next();
					// all terminals support media
				terminals[i] = new TermData(termName, true);
				i++;
			}
			return terminals;
		} else
			return null;
	}

	/**
	 * @see BasicJtapiTpi#getTerminals(String)
	 */
	public TermData[] getTerminals(String address)
		throws InvalidArgumentException {
		if (address != null) {
			AddressInfo addInfo = (AddressInfo)addInfoMap.get(address);
			if (addInfo == null)
				return null;
			TermData result[] = { new TermData(addInfo.terminal, true) };
			return result;
		} else
			return null;
	}

	/**
	 * @see CoreTpi#addListener(TelephonyListener)
	 */
	public void addListener(TelephonyListener ro) {
		this.gjListener = ro;
	}

	/**
	 * @see CoreTpi#answerCall(CallId, String, String)
	 */
	public void answerCall(CallId call, String address, String terminal)
		throws
			PrivilegeViolationException,
			ResourceUnavailableException,
			MethodNotSupportedException,
			RawStateException {
		try {
			this.realProvider.XTAnswerCall(((XtapiCallId)call).getCallNum());
		} catch (InvalidStateException ise) {
			throw new RawStateException(call, ise.getState());
		}
	}

	/**
	 * @see CoreTpi#createCall(CallId, String, String, String)
	 */
	public CallId createCall(CallId id, String address, String term, String dest)
		throws
			ResourceUnavailableException,
			PrivilegeViolationException,
			InvalidPartyException,
			InvalidArgumentException,
			RawStateException,
			MethodNotSupportedException {
		// translate the address into a line
		AddressInfo addInfo = (AddressInfo)this.addInfoMap.get(address);
		if (addInfo == null)
			throw new InvalidArgumentException("Address not known: " + address);

                //sf
		// register the call with the line
		this.lineToCalls.put(new Integer(addInfo.line), id);
                //end sf
                
		// Get the call handle
		int callNum = 0;
		try {
			callNum = this.realProvider.XTConnectCall(addInfo.line,
											dest,
											addInfo.rawHandle);
		} catch (InvalidStateException ise) {
			throw new RawStateException(id, ise.getState());
		}

		// update the call structure
		XtapiCallId xid = (XtapiCallId)id;
		xid.setCallNumber(callNum);
		this.callNumToCalls.put(new Integer(callNum), xid);
		xid.setLocalAddress(address);
		xid.setRemoteAddress(dest);
		
		// register the call with the terminal
		this.termToCalls.put(term, id);
                
                //sf moved up to avoid race condition.
		// register the call with the line
		//this.lineToCalls.put(new Integer(addInfo.line), id);
                //end sf

		// now note that the local leg is connected
		this.gjListener.connectionConnected(id, address, Event.CAUSE_NORMAL);
		this.gjListener.connectionAlerting(id, dest, Event.CAUSE_NORMAL);
		
		return id;
	}

	/**
	 * I support the default capabilities associated with the MediaTpi
	 * @see CoreTpi#getCapabilities()
	 */
	public Properties 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.ALLOCATE_MEDIA, "f");
		
		return caps;
	}

	/**
	 * Initialize the Xtapi Bricdge Provider with a Map that defines which xTapi service provider to load beneath me.
	 * This consists of one known property:
	 * <ul>

⌨️ 快捷键说明

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