📄 messengermanager.java
字号:
messageRequest);
if (dialog != null )
dialog.sendRequest(messageTransaction);
else {
messageTransaction.sendRequest();
IMCall imcall = new IMCall(contact);
imcall.setDialog(messageTransaction.getDialog());
callManager.addIMCall(imcall);
}
} catch (TransactionUnavailableException ex) {
System.out.println(
"Failed to create message Transaction.\n"
+ "This is most probably a network connection error. ");
ex.printStackTrace();
} catch (SipException se) {
se.printStackTrace();
}
}
/**
* Send a request for subscription
* @param subscriberAddress - the address of the contact we want to
* subscribe to
*/
public void sendSubscribe(String subscriberAddress) {
//Store the callee in the call status
String calleeURI = subscriberAddress.trim();
//Request URI
SipURI contactURI = null;
try {
//Create the SIP URI for the user URI
String user = calleeURI.substring(0, calleeURI.indexOf("@"));
String host =
calleeURI.substring(
calleeURI.indexOf("@") + 1,
calleeURI.length());
contactURI =
MessageListener.addressFactory.createSipURI(user, host);
} catch (ParseException pe) {
pe.printStackTrace();
}
Request subscribe =
createRequest(Request.SUBSCRIBE, contactURI, userSipURI);
//Content
ContentTypeHeader contentTypeHeader = null;
try {
contentTypeHeader =
MessageListener.headerFactory.createContentTypeHeader(
"application",
"sdp");
} catch (ParseException ex) {
//Shouldn't happen
System.out.println(
"Failed to create a content type header for the SUBSCRIBE request "
+ ex);
}
//Transaction
ClientTransaction subscribeTransaction = null;
try {
subscribeTransaction =
messageListener.sipProvider.getNewClientTransaction(subscribe);
System.out.println("send request:\n" + subscribe);
subscribeTransaction.sendRequest();
//call.setDialog(subscribeTransaction.getDialog());
} catch (TransactionUnavailableException ex) {
System.out.println(
"Failed to create subscribeTransaction.\n"
+ "This is most probably a network connection error. ");
ex.printStackTrace();
} catch (SipException ex) {
System.out.println(
"An error occurred while sending subscribe request " + ex);
}
}
/**
* Send an OK in repsonse to an incoming invite
* @param sdpBody - the sdpBody to include in the response
*/
private void sendOK(Object sdpBody, String caller) {
//Find the Audio call
AudioCall call = callManager.findAudioCall(caller);
//Listening Point
ListeningPoint listeningPoint =
messageListener.sipProvider.getListeningPoint();
//Get the request
Request request = call.getDialog().getFirstTransaction().getRequest();
if (!call.getDialog().isServer())
System.out.println("Problem, this is a client transaction");
//Get the server Transaction
ServerTransaction serverTransaction =
(ServerTransaction) call.getDialog().getFirstTransaction();
try {
Response ok =
(Response) MessageListener.messageFactory.createResponse(
Response.OK,
request);
//Put a tag on the To Header
((ToHeader) ok.getHeader(ToHeader.NAME)).setTag(generateTag());
//Specify the contact Header
SipURI contactURI =
MessageListener.addressFactory.createSipURI(
userSipURI.getUser(),
messageListener.getConfiguration().contactIPAddress);
contactURI.setTransportParam(
messageListener.getConfiguration().signalingTransport);
ContactHeader contactHeader =
MessageListener.headerFactory.createContactHeader(
MessageListener.addressFactory.createAddress(contactURI));
contactURI.setPort(listeningPoint.getPort());
ok.addHeader(contactHeader);
//IF the call is voice messaging we add the Accept Headers
if (call.getVoiceMessaging()) {
AcceptHeader acceptHeader =
MessageListener.headerFactory.createAcceptHeader(
"audio",
"gsm");
ok.addHeader(acceptHeader);
acceptHeader =
MessageListener.headerFactory.createAcceptHeader(
"audio",
"x-gsm");
ok.addHeader(acceptHeader);
acceptHeader =
MessageListener.headerFactory.createAcceptHeader(
"application",
"text");
ok.addHeader(acceptHeader);
//initialize the voiceRecorder
VoiceRecorder.getInstance();
} else {
//Adding the sdp Body describing the media session
ContentTypeHeader contentTypeHeader =
(ContentTypeHeader) request.getHeader(
ContentTypeHeader.NAME);
if (contentTypeHeader != null && sdpBody != null)
ok.setContent(sdpBody, contentTypeHeader);
else
ok.setHeader(contentTypeHeader);
}
//Send the ok
serverTransaction.sendResponse(ok);
} catch (SipException ex) {
System.out.println("Failed to send the OK response " + ex);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
/**
* Send a BUSY in response to an incoming invite
*/
public void sendBusy(String caller) {
//Find the Audio call
AudioCall call = callManager.findAudioCall(caller);
//Get the request
Request request = call.getDialog().getFirstTransaction().getRequest();
if (!call.getDialog().isServer())
System.out.println("Problem, this is a client transaction");
//Get the server Transaction
ServerTransaction serverTransaction =
(ServerTransaction) call.getDialog().getFirstTransaction();
try {
Response busy =
(Response) MessageListener.messageFactory.createResponse(
Response.BUSY_HERE,
request);
//If the user has put an URL for the BUSY we add i in the CALL-Info
if(messageListener.getConfiguration().httpBusy!=null){
CallInfoHeader callInfoHeader=
MessageListener.headerFactory.createCallInfoHeader(
MessageListener.addressFactory.createURI(
messageListener.getConfiguration().httpBusy));
busy.addHeader(callInfoHeader);
}
serverTransaction.sendResponse(busy);
System.out.println(
"Audio Call removed : " + call.getDialog().getDialogId());
callManager.removeAudioCall(call);
} catch (SipException ex) {
System.out.println("Failed to send the BUSY response " + ex);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
public void sendUnavailable(String caller) {
//Find the Audio call
AudioCall call = callManager.findAudioCall(caller);
//Get the request
Request request = call.getDialog().getFirstTransaction().getRequest();
if (!call.getDialog().isServer())
System.out.println("Problem, this is a client transaction");
//Get the server Transaction
ServerTransaction serverTransaction =
(ServerTransaction) call.getDialog().getFirstTransaction();
try {
Response unavailable =
(Response) MessageListener.messageFactory.createResponse(
Response.TEMPORARILY_UNAVAILABLE,
request);
serverTransaction.sendResponse(unavailable);
System.out.println(
"Audio Call removed : " + call.getDialog().getDialogId());
callManager.removeAudioCall(call);
} catch (SipException ex) {
System.out.println("Failed to send the Unavailable response " + ex);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
/**
* The subscriber is added now and the user is notified that a new person
* wants to be added to the contact list, if he doesn't want this person
* to be added, the subscriber is gonna be removed from the GUI
* @param subscriber - the subscirber to add
*/
public void notifySubscribe(Subscriber subscriber) {
//the subscriber is added now and the user is notified that a new person
//wants to be added to the contact list, if he doesn't want this person
//to be added, the subscriber is gonna be removed from the GUI
presentityManager.addSubscriber(subscriber);
//Notify the GUI and the controller that the status has changed
setChanged();
notifyObservers(subscriber);
}
/**
* Notify the GUI and the controller that the status has changed
* @param presenceTag - presence tag containing all the presence information
*/
public void notifyPresence(PresenceTag presenceTag) {
Vector atomTagList = presenceTag.getAtomTagList();
AtomTag atomTag = (AtomTag) atomTagList.firstElement();
AddressTag addressTag = atomTag.getAddressTag();
MSNSubStatusTag msnSubStatusTag = addressTag.getMSNSubStatusTag();
Subscriber subscriber =
presentityManager.getSubscriber(presenceTag.getAddress());
if (subscriber != null) {
subscriber.setStatus(msnSubStatusTag.getMSNSubStatus());
}
//Notify the GUI and the controller that the status has changed
setChanged();
notifyObservers(presenceTag);
}
/**
* Retrieve the messageListener
* @return the messageListener
*/
public MessageListener getMessageListener() {
return messageListener;
}
/**
* Set the current status of the call
* @param callStatus - the current status of the call
*/
public void notifyObserversNewCallStatus(Call call) {
//Notify the GUI and the controller that the status has changed
setChanged();
notifyObservers(call);
}
/**
* Retrieve the contact List
* @return the contact List
*/
public Vector getContactList() {
return contactList;
}
/**
* Retrieve the call Manager
* @return the call Manager
*/
public CallManager getCallManager() {
return callManager;
}
/**
* Retrieve the presentity manager
* @return the presentity manager
*/
public PresentityManager getPresentityManager() {
return presentityManager;
}
/**
* Retrieve the current status of the registration
* @return the current status of the registration
*/
public String getRegisterStatus() {
return this.registerStatus.getStatus();
}
/**
* Set the current status of the registration
* @param registerStatus - the current status of the registration
*/
public void setRegisterStatus(String registerStatus) {
this.registerStatus.setStatus(registerStatus);
//Notify the GUI and the controller that the status has changed
setChanged();
notifyObservers(this.registerStatus);
}
/**
* Notify Observers an Instant Message has been received
* @param message - the message received
*/
public void notifyObserversIMReceived(String message, String sender) {
InstantMessage im = new InstantMessage(message, sender);
setChanged();
notifyObservers(im);
}
/**
* Generate a Tag
* @return the tag generated
*/
public static String generateTag() {
return new Integer((int) (Math.random() * 10000)).toString();
}
/**
* Get the current sip user URI
* @return the current sip user URI
*/
public SipURI getUserURI() {
return userSipURI;
}
/**
* Set the current sip user URI
* @param userURI - the current sip user URI
*/
public void setUserURI(String userURI) {
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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -