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

📄 phonemodel.java

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

/*
	Copyright (c) 2002 8x8 Inc. (www.8x8.com) 

	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.
*/
import net.sourceforge.gjtapi.*;
import net.sourceforge.gjtapi.CallId;
import javax.telephony.Event;
/**
 * This represents the state of a Phone.
 * Creation date: (2000-02-28 12:41:07)
 * @author: Richard Deadman
 */
public class PhoneModel implements PhoneListener, RawPhone {
	private int state = IDLE;
	private Leg activeCall;
	private Leg heldCall;
	private String address;
	private PhoneManager manager;
	private PhoneListener listener;
		// collector for DTMF signals
	private StringBuffer digitBucket = new StringBuffer();
	private boolean sendDtmf = false;	// do I cause events for DTMF signals?
		// hold on to any Play or Record threads I may have
		// This may be used to allow playing to stop by interrupting the Thread
	private Thread playThread = null;
	private Thread recordThread = null;
/**
 * Create a Phone with a known address
 * Creation date: (2000-02-28 12:46:19)
 * @author: Richard Deadman
 * @param addr The address of the phone
 * @param list The view listening to my changes
 * @param mgr The call manager used to create new calls
 */
public PhoneModel(String addr, PhoneListener list, PhoneManager mgr) {
	super();
	
	this.setAddress(addr);
	this.setListener(list);
	this.setManager(mgr);
}
/**
 * Add a link between this phone and a call
 * Creation date: (2000-02-29 13:47:35)
 * @author: Richard Deadman
 * @return boolean
 * @param leg The leg to add to the phone
 */
public boolean add(Leg leg) {
	this.setActiveLeg(leg);
	if (leg.getState() == Leg.ALERTING)
		this.getListener().ringing();
	return true;
}
/**
 * Answer a ringing call
 */
public boolean answer() {
	return this.getActiveLeg().answer();
}
/**
 * Answer a ringing call
 */
public boolean answer(CallId call) {
	Leg l = this.getActiveLeg();
	if (l.getCall().equals(call)) {
		return l.answer();
	}
	return false;
}
/**
 * bridged method comment.
 */
public void bridged() {
	this.setState(BRIDGED);
	this.getListener().bridged();
}
/**
 * I must have a Held and active call to conference.
 */
public boolean conference() {
	Leg active = this.getActiveLeg();
	Leg held = this.getHeldLeg();

	if (active != null && held != null) {
		held.getCall().join(active.getCall());
	
		this.setActiveLeg(held);
		this.setHeldLeg(null);
		this.connected();
		return true;
	}
	return false;
}
/**
 * connected method comment.
 */
public void connected() {
	if (this.getHeldLeg() != null)
		this.bridged();
	else {
		this.setState(ACTIVE);
		this.getListener().connected();
	}
}
/**
 * consult method comment.
 */
public boolean consult() {
	this.setState(PhoneModel.CONSULT);
	this.getListener().consult();
	return true;
}
/**
 * createCall method comment.
 */
public boolean createCall() {
	PhoneManager pm = this.getManager();
	boolean res = this.add(new Leg(new RawCall(pm), this, pm.getListener(), Leg.IDLE));
	if (res) {
		int state = this.getState();
		if (state == PhoneModel.IDLE)
			this.setState(PhoneModel.DIALTONE);
		if (state == PhoneModel.HOLD)
			this.setState(PhoneModel.CONSULT);
	}
	return res;
}
/**
 * Cause the phone to dial a set of digits
 */
public void dial(String digits) throws javax.telephony.InvalidPartyException, RawStateException {
	int state = this.getState();
	Leg leg = this.getActiveLeg();
		// test if we are on hold with no idle leg
	if (state == PhoneModel.HOLD && leg == null) {
		this.getManager().createCall(this, digits);
		this.consult();
		return;
	}
		// test if we are dialing on a idle leg
	if (state == PhoneModel.DIALTONE && leg.getState() == Leg.IDLE) {
		leg.inProgress();
		leg.getCall().dial(this, digits);
		this.getListener().dialing();
		return;
	}
		// test if we are sending on a connected leg
	if ((state == PhoneModel.ACTIVE || state == PhoneModel.CONSULT || state == PhoneModel.BRIDGED) && leg.getState() == Leg.CONNECTED) {
		this.sendDTMF(digits);
		return;
	}

	System.err.println("Error in state machine: ");
	new Exception().printStackTrace(System.err);
	
}
/**
 * dialing method comment.
 */
public void dialing() {
	this.getListener().dialing();
}
/**
 * drop the active leg
 */
public boolean drop() {
	Leg leg = this.getActiveLeg();
	if (leg != null)
		return leg.drop();
		
	return false;
}
/**
 * See if I'm logically equal to a TestPhone.  This calls the generic equals method.
 * Creation date: (2000-03-01 9:43:21)
 * @author: Richard Deadman
 * @return true if the TestPhone has me as its model
 * @param tp The test phone.
 */
public boolean equals(TestPhone tp) {
	return tp.equals(this);
}
/**
 * getCall method comment.
 */
private Leg getActiveLeg() {
	return this.activeCall;
}
/**
 * Insert the method's description here.
 * Creation date: (2000-02-28 12:45:43)
 * @author: 
 * @return java.lang.String
 */
public java.lang.String getAddress() {
	return address;
}
/**
 * Return an array of Calls associated with me.
 */
public RawCall[] getCalls() {
	java.util.Vector calls = new java.util.Vector();
	int size = 0;
	Leg l = null;
	if ((l = this.getActiveLeg()) != null) {
		calls.add(l.getCall());
		size++;
	}
	if ((l = this.getHeldLeg()) != null) {
		calls.add(l.getCall());
		size++;
	}
	return (RawCall[])calls.toArray(new RawCall[size]);
}
/**
 * Insert the method's description here.
 * Creation date: (2000-03-31 14:50:56)
 * @author: 
 * @return java.lang.StringBuffer
 */
private java.lang.StringBuffer getDigitBucket() {
	return digitBucket;
}
/**
 * Held leg accessor
 * Creation date: (2000-02-28 12:43:28)
 * @author: Richard Deadman
 * @return The leg on hold
 */
private Leg getHeldLeg() {
	return heldCall;
}
/**
 * Insert the method's description here.
 * Creation date: (2000-03-01 9:46:36)
 * @author: 
 * @return net.sourceforge.gjtapi.raw.emulator.PhoneListener
 */
private PhoneListener getListener() {
	return listener;
}
/**
 * Get the Phone Manager
 * Creation date: (2000-02-29 13:50:27)
 * @author: Richard Deadman
 * @return The manager of all phones
 */
private PhoneManager getManager() {
	return manager;
}
/**
 * Get any Play Thread associated with this terminal.
 * Creation date: (2000-05-10 15:27:55)
 * @author: Richard Deadman
 * @return A Thread currently playing on the phone, or null if none.
 */
Thread getPlayThread() {
	return playThread;
}
/**
 * Get any Record Thread associated with this terminal.
 * Creation date: (2000-05-10 15:27:55)
 * @author: Richard Deadman
 * @return A Thread currently recording on the phone, or null if none.
 */
Thread getRecordThread() {
	return recordThread;
}
/**
 * Get the DTMF event sending flag
 * Creation date: (2000-02-29 13:50:27)
 * @author: Richard Deadman
 * @return true if I am to send on DTMF incoming signals as events.

⌨️ 快捷键说明

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