📄 messengermanager.java
字号:
/*
* MessengerManager.java
*
* Created on November 25, 2003, 9:14 AM
*/
package gov.nist.applet.phone.ua;
import javax.sip.TransactionUnavailableException;
import javax.sip.InvalidArgumentException;
import javax.sip.ClientTransaction;
import javax.sip.ServerTransaction;
import javax.sip.SipException;
import javax.sip.ListeningPoint;
import javax.sip.header.CallInfoHeader;
import javax.sip.header.Header;
import javax.sip.header.FromHeader;
import javax.sip.header.ViaHeader;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.ExpiresHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.CSeqHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ContentTypeHeader;
import javax.sip.header.RouteHeader;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.message.Request;
import javax.sip.message.Response;
import javax.sip.header.AcceptHeader;
import javax.sdp.SdpFactory;
import javax.sdp.SessionDescription;
import javax.sdp.Version;
import javax.sdp.Origin;
import javax.sdp.Time;
import javax.sdp.Connection;
import javax.sdp.SessionName;
import javax.sdp.MediaDescription;
import javax.sdp.SdpException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
import gov.nist.applet.phone.media.MediaManager;
import gov.nist.applet.phone.media.messaging.VoiceRecorder;
import gov.nist.applet.phone.ua.call.AudioCall;
import gov.nist.applet.phone.ua.call.Call;
import gov.nist.applet.phone.ua.call.CallManager;
import gov.nist.applet.phone.ua.call.IMCall;
import gov.nist.applet.phone.ua.pidf.parser.AddressTag;
import gov.nist.applet.phone.ua.pidf.parser.AtomTag;
import gov.nist.applet.phone.ua.pidf.parser.MSNSubStatusTag;
import gov.nist.applet.phone.ua.pidf.parser.PresenceTag;
import gov.nist.applet.phone.ua.presence.PresentityManager;
import gov.nist.applet.phone.ua.presence.Subscriber;
import gov.nist.applet.phone.ua.gui.NISTMessengerGUI;
/**
* This class will manage all the calls and their status, and the subscriptions
* to the presence.
* This application has been designed in following the MVC design pattern.
* Thus this class is part of the Model.
*
* @author DERUELLE Jean
*/
public class MessengerManager extends java.util.Observable {
private MessageListener messageListener = null;
protected CallManager callManager = null;
private PresentityManager presentityManager = null;
protected RegisterStatus registerStatus = null;
private SipURI userSipURI = null;
private Vector contactList = null;
protected boolean presenceAllowed = false;
private NISTMessengerGUI appletHandle;
protected boolean reRegisterFlag = false;
/** Creates a new instance of CallManager
* @param sipURI - the sipURI of the user
*/
public MessengerManager(
Configuration configuration,
NISTMessengerGUI appletHandle) {
MediaManager.detectSupportedCodecs();
contactList = new Vector();
callManager = new CallManager();
//Create a new instance of the sip Listener
messageListener =
new MessageListener(this, configuration, appletHandle);
messageListener.start();
presentityManager = new PresentityManager(this);
//Set the registration status to Not registered
registerStatus = new RegisterStatus();
String userURI = messageListener.getConfiguration().userURI;//userURI="dog@nist.gov"
try {
//Create the SIP URI for the user URI
String user = userURI.substring(0, userURI.indexOf("@"));
String host =
userURI.substring(userURI.indexOf("@") + 1, userURI.length());
userSipURI =
MessageListener.addressFactory.createSipURI(user, host);
//userSipURI .setTransportParam(messageListener.getConfiguration().signalingTransport);
} catch (ParseException ex) {
System.out.println(userURI + " is not a legal SIP uri! " + ex);
}
}
/**
* Call the user specified in parameter
* @param contactURI - the user to call
*/
public void call(String contactURI) {
if(!callManager.isAlreadyInAudioCall()){
//Create a new Call
AudioCall call = new AudioCall(messageListener);
//Store the callee in the call
contactURI = contactURI.trim();
call.setCallee(contactURI);
if (messageListener
.getConfiguration()
.mediaTransport
.equalsIgnoreCase("tcp"))
call.setVoiceMesaging(true);
callManager.addAudioCall(call);
String sdpBody = null;
if (!call.getVoiceMessaging())
sdpBody = createSDPBody(call.getMediaManager().getAudioPort());
sendInvite(contactURI, sdpBody);
}
}
/**
* Answer the call, i.e. answer ok to an incoming invite
* after have found the good media session
*/
public void answerCall(String caller) {
//Find the Audio call
AudioCall call = callManager.findAudioCall(caller);
//Get the request
Request request = call.getDialog().getFirstTransaction().getRequest();
//Getting the media Manager for this call
MediaManager mediaManager = call.getMediaManager();
//Getting the sdp body for creating the response
//This sdp body will present what codec has been chosen
//and on which port every media will be received
Object responseSdpBody = null;
if (!call.getVoiceMessaging()) {
ContentTypeHeader contentTypeHeader =
(ContentTypeHeader) request.getHeader(ContentTypeHeader.NAME);
String contentType = contentTypeHeader.getContentType();
String subType = contentTypeHeader.getContentSubType();
System.out.println("the other end invite us to " + subType);
if (contentType.equals("application") && subType.equals("sdp")) {
//Get the Sdp Body
String content = new String(request.getRawContent());
responseSdpBody = mediaManager.getResponseSdpBody(content);
}
}
sendOK(responseSdpBody, caller);
}
/**
* Cancel the current call
*/
public void cancelCall(String calleeURI) {
//Find the Audio call
AudioCall call = callManager.findAudioCall(calleeURI);
Request request = call.getDialog().getFirstTransaction().getRequest();
if (call.getDialog().isServer()) {
System.out.println("Cannot cancel a server transaction");
}
ClientTransaction clientTransaction =
(ClientTransaction) call.getDialog().getFirstTransaction();
try {
if (call.getDialog().getState() == javax.sip.DialogState.EARLY ||
call.getDialog().getState() == null) {
Request cancel = clientTransaction.createCancel();
ClientTransaction cancelTransaction =
messageListener.sipProvider.getNewClientTransaction(cancel);
cancelTransaction.sendRequest();
} else System.out.println("Too late to cancel -- send BYE instead!");
} catch (SipException ex) {
System.out.println("Failed to send the CANCEL request " + ex);
}
}
/**
* End the current call
*/
public void endCall(String calleeURI) {
//Find the Audio call
AudioCall call = callManager.findAudioCall(calleeURI);
if (call == null) {
System.out.println("Call not found " + calleeURI );
return;
}
//Request
Request request = null;
try {
request = call.getDialog().createRequest("BYE");
} catch (SipException ex) {
System.out.println("Could not create the bye request! " + ex);
}
//Transaction
ClientTransaction clientTransaction = null;
try {
clientTransaction =
messageListener.sipProvider.getNewClientTransaction(request);
System.out.println(request.toString());
call.getDialog().sendRequest(clientTransaction);
} catch (TransactionUnavailableException ex) {
System.out.println(
"Could not create a register transaction!\n"
+ "Check that the Registrar address is correct!"
+ " "
+ ex);
} catch (SipException ex) {
System.out.println(
"Could not send out the register request! " + ex);
}
if (call.getVoiceMessaging()) {
//Stop the voice messages schedule and the voiceRecorder
messageListener.messageProcessor.stopVoiceMessagingSchedule();
} else {
call.getMediaManager().stopMediaSession();
}
}
/**
* Add a contact to our contact list
* @param contact - contact to add
*/
public void addContact(String contact) {
if (isInContactList(contact))
System.out.println(
"The contact is already in our contact list,"
+ "he's not going to be added");
else
contactList.addElement(contact);
}
/**
* Remove a contact to our contact list
* @param contact - contact to remove
*/
public void removeContact(String contact) {
if (isInContactList(contact))
contactList.remove(contact);
else
System.out.println(
"The contact is not in our contact list,"
+ "he can not going to be removed");
}
/**
* Check if the contact is in the contact List
* @param contactAddress - the contact
* @return true if the contact is in the contact List
*/
protected boolean isInContactList(String contactAddress) {
Enumeration e = contactList.elements();
while (e.hasMoreElements()) {
if (((String) e.nextElement()).equals(contactAddress))
return true;
}
return false;
}
/**
* Stop an opened instantMessaging Session
* @param calleeURI - sip address of the person the application is chatting with
*/
public void stopInstantMessagingSession(String calleeURI) {
calleeURI = calleeURI.trim();
IMCall call = callManager.findIMCall(calleeURI);
//If no instant messaging session exists
if (call == null)
return;
Request bye = null;
try {
bye = call.getDialog().createRequest("BYE");
} catch (SipException se) {
se.printStackTrace();
}
//Transaction
ClientTransaction clientTransaction = null;
try {
clientTransaction =
messageListener.sipProvider.getNewClientTransaction(bye);
} catch (TransactionUnavailableException ex) {
System.out.println("Could not create a bye transaction!\n" + ex);
}
System.out.println(bye.toString());
try {
call.getDialog().sendRequest(clientTransaction);
} catch (SipException ex) {
System.out.println("Could not send out the bye request! " + ex);
}
}
/**
*
* @param requestName
* @param callee
* @return
*/
//创建请求
protected Request createRequest(
String requestName,
SipURI callee,
SipURI caller) {
//Listening Point
ListeningPoint listeningPoint =
messageListener.sipProvider.getListeningPoint();
//Request URI
SipURI requestURI = null;
try {
requestURI = callee;
requestURI.setTransportParam(
messageListener.getConfiguration().signalingTransport);
//Call ID
CallIdHeader callIdHeader =
messageListener.sipProvider.getNewCallId();
//CSeq
CSeqHeader cSeqHeader =
MessageListener.headerFactory.createCSeqHeader(1, requestName);
//From Header
Address fromAddress =
MessageListener.addressFactory.createAddress(
MessageListener.addressFactory.createSipURI(
caller.getUser(),
caller.getHost()));
FromHeader fromHeader =
MessageListener.headerFactory.createFromHeader(
fromAddress,
generateTag());
//ToHeader
Address toAddress =
MessageListener.addressFactory.createAddress(
MessageListener.addressFactory.createSipURI(
callee.getUser(),
callee.getHost()));
// From and To are logical identifiers (should have no parameters)
ToHeader toHeader =
MessageListener.headerFactory.createToHeader(toAddress, null);
//ViaHeader
ArrayList viaHeaders = new ArrayList();
ViaHeader viaHeader =
MessageListener.headerFactory.createViaHeader(
messageListener.getConfiguration().contactIPAddress,
listeningPoint.getPort(),
listeningPoint.getTransport(),
null);
viaHeaders.add(viaHeader);
//Max Forward Header - just forward by one hop.
MaxForwardsHeader maxForwardsHeader =
MessageListener.headerFactory.createMaxForwardsHeader(70);
Request request =
MessageListener.messageFactory.createRequest(
requestURI,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -