MessengerManager.java
Upload User: liulanlin
Upload Date: 2017-12-08
Package Size: 1274k
Code Size: 52k
Category:

VOIP program

Development Platform:

Java

  1. /*
  2.  * MessengerManager.java
  3.  *
  4.  * Created on November 25, 2003, 9:14 AM
  5.  */
  6. package gov.nist.applet.phone.ua;
  7. import javax.sip.TransactionUnavailableException;
  8. import javax.sip.InvalidArgumentException;
  9. import javax.sip.ClientTransaction;
  10. import javax.sip.ServerTransaction;
  11. import javax.sip.SipException;
  12. import javax.sip.ListeningPoint;
  13. import javax.sip.header.CallInfoHeader;
  14. import javax.sip.header.Header;
  15. import javax.sip.header.FromHeader;
  16. import javax.sip.header.ViaHeader;
  17. import javax.sip.header.MaxForwardsHeader;
  18. import javax.sip.header.ExpiresHeader;
  19. import javax.sip.header.ToHeader;
  20. import javax.sip.header.CSeqHeader;
  21. import javax.sip.header.ContactHeader;
  22. import javax.sip.header.CallIdHeader;
  23. import javax.sip.header.ContentTypeHeader;
  24. import javax.sip.address.Address;
  25. import javax.sip.address.SipURI;
  26. import javax.sip.message.Request;
  27. import javax.sip.message.Response;
  28. import javax.sip.header.AcceptHeader;
  29. import javax.sdp.SdpFactory;
  30. import javax.sdp.SessionDescription;
  31. import javax.sdp.Version;
  32. import javax.sdp.Origin;
  33. import javax.sdp.Time;
  34. import javax.sdp.Connection;
  35. import javax.sdp.SessionName;
  36. import javax.sdp.MediaDescription;
  37. import javax.sdp.SdpException;
  38. import java.text.ParseException;
  39. import java.util.ArrayList;
  40. import java.util.Enumeration;
  41. import java.util.Vector;
  42. import gov.nist.applet.phone.media.MediaManager;
  43. import gov.nist.applet.phone.media.messaging.VoiceRecorder;
  44. import gov.nist.applet.phone.ua.call.AudioCall;
  45. import gov.nist.applet.phone.ua.call.Call;
  46. import gov.nist.applet.phone.ua.call.CallManager;
  47. import gov.nist.applet.phone.ua.call.IMCall;
  48. import gov.nist.applet.phone.ua.pidf.parser.AddressTag;
  49. import gov.nist.applet.phone.ua.pidf.parser.AtomTag;
  50. import gov.nist.applet.phone.ua.pidf.parser.MSNSubStatusTag;
  51. import gov.nist.applet.phone.ua.pidf.parser.PresenceTag;
  52. import gov.nist.applet.phone.ua.presence.PresentityManager;
  53. import gov.nist.applet.phone.ua.presence.Subscriber;
  54. import gov.nist.applet.phone.ua.gui.NISTMessengerGUI;
  55. /**
  56.  * This class will manage all the calls and their status, and the subscriptions
  57.  * to the presence.
  58.  * This application has been designed in following the MVC design pattern.
  59.  * Thus this class is part of the Model.
  60.  *
  61.  * @author  DERUELLE Jean
  62.  */
  63. public class MessengerManager extends java.util.Observable {
  64.     private MessageListener messageListener = null;
  65.     protected CallManager callManager = null;
  66.     private PresentityManager presentityManager = null;
  67.     protected RegisterStatus registerStatus = null;
  68.     private SipURI userSipURI = null;
  69.     private Vector contactList = null;
  70.     protected boolean presenceAllowed = false;
  71.     private NISTMessengerGUI appletHandle;
  72.     protected boolean reRegisterFlag = false;
  73.     
  74.     /** Creates a new instance of CallManager
  75.      * @param sipURI - the sipURI of the user
  76.      */
  77.     public MessengerManager(
  78.     Configuration configuration,
  79.     NISTMessengerGUI appletHandle) {
  80.         MediaManager.detectSupportedCodecs();
  81.         contactList = new Vector();
  82.         callManager = new CallManager();
  83.         //Create a new instance of the sip Listener
  84.         messageListener =
  85.         new MessageListener(this, configuration, appletHandle);
  86.         messageListener.start();
  87.         
  88.         presentityManager = new PresentityManager(this);
  89.         //Set the registration status to Not registered
  90.         registerStatus = new RegisterStatus();
  91.         String userURI = messageListener.getConfiguration().userURI;
  92.         try {
  93.             //Create the SIP URI for the user URI
  94.             String user = userURI.substring(0, userURI.indexOf("@"));
  95.             String host =
  96.             userURI.substring(userURI.indexOf("@") + 1, userURI.length());
  97.             userSipURI =
  98.             MessageListener.addressFactory.createSipURI(user, host);
  99.             //userSipURI .setTransportParam(messageListener.getConfiguration().signalingTransport);
  100.         } catch (ParseException ex) {
  101.             System.out.println(userURI + " is not a legal SIP uri! " + ex);
  102.         }
  103.     }
  104.     
  105.     /**
  106.      * Call the user specified in parameter
  107.      * @param contactURI - the user to call
  108.      */
  109.     public void call(String contactURI) {
  110.         if(!callManager.isAlreadyInAudioCall()){
  111.             //Create a new Call
  112.             AudioCall call = new AudioCall(messageListener);
  113.             //Store the callee in the call
  114.             contactURI = contactURI.trim();
  115.             call.setCallee(contactURI);
  116.             if (messageListener
  117.             .getConfiguration()
  118.             .mediaTransport
  119.             .equalsIgnoreCase("tcp"))
  120.                 call.setVoiceMesaging(true);
  121.             callManager.addAudioCall(call);
  122.             String sdpBody = null;
  123.             if (!call.getVoiceMessaging())
  124.                 sdpBody = createSDPBody(call.getMediaManager().getAudioPort());
  125.             sendInvite(contactURI, sdpBody);
  126.         }
  127.     }
  128.     
  129.     /**
  130.      * Answer the call, i.e. answer ok to an incoming invite
  131.      * after have found the good media session
  132.      */
  133.     public void answerCall(String caller) {
  134.         //Find the Audio call
  135.         AudioCall call = callManager.findAudioCall(caller);
  136.         //Get the request
  137.         Request request = call.getDialog().getFirstTransaction().getRequest();
  138.         //Getting the media Manager for this call
  139.         MediaManager mediaManager = call.getMediaManager();
  140.         //Getting the sdp body for creating the response
  141.         //This sdp body will present what codec has been chosen
  142.         //and on which port every media will be received
  143.         Object responseSdpBody = null;
  144.         if (!call.getVoiceMessaging()) {
  145.             ContentTypeHeader contentTypeHeader =
  146.             (ContentTypeHeader) request.getHeader(ContentTypeHeader.NAME);
  147.             String contentType = contentTypeHeader.getContentType();
  148.             String subType = contentTypeHeader.getContentSubType();
  149.             System.out.println("the other end invite us to " + subType);
  150.             
  151.             if (contentType.equals("application") && subType.equals("sdp")) {
  152.                 //Get the Sdp Body
  153.                 String content = new String(request.getRawContent());
  154.                 responseSdpBody = mediaManager.getResponseSdpBody(content);
  155.             }
  156.         }
  157.         sendOK(responseSdpBody, caller);
  158.     }
  159.     
  160.     /**
  161.      * Cancel the current call
  162.      */
  163.     public void cancelCall(String calleeURI) {
  164.         //Find the Audio call
  165.         AudioCall call = callManager.findAudioCall(calleeURI);
  166.         Request request = call.getDialog().getFirstTransaction().getRequest();
  167.         if (call.getDialog().isServer()) {
  168.             System.out.println("Cannot cancel a server transaction");
  169.             
  170.         }
  171.         ClientTransaction clientTransaction =
  172.         (ClientTransaction) call.getDialog().getFirstTransaction();
  173.         try {
  174.             if (call.getDialog().getState() == javax.sip.DialogState.EARLY ||
  175.                 call.getDialog().getState() == null) {
  176.                 Request cancel = clientTransaction.createCancel();
  177.                 ClientTransaction cancelTransaction =
  178.                 messageListener.sipProvider.getNewClientTransaction(cancel);
  179.                 cancelTransaction.sendRequest();
  180.             } else System.out.println("Too late to cancel -- send BYE instead!");
  181.         } catch (SipException ex) {
  182.             System.out.println("Failed to send the CANCEL request " + ex);
  183.         }
  184.     }
  185.     
  186.     /**
  187.      * End the current call
  188.      */
  189.     public void endCall(String calleeURI) {
  190.         //Find the Audio call
  191.         AudioCall call = callManager.findAudioCall(calleeURI);
  192. if (call == null) {
  193. System.out.println("Call not found " +  calleeURI );
  194. return;
  195. }
  196.         //Request
  197.         Request request = null;
  198.         try {
  199.             request = call.getDialog().createRequest("BYE");
  200.         } catch (SipException ex) {
  201.             System.out.println("Could not create the bye request! " + ex);
  202.         }
  203.         //Transaction
  204.         ClientTransaction clientTransaction = null;
  205.         try {
  206.             clientTransaction =
  207.             messageListener.sipProvider.getNewClientTransaction(request);
  208.             System.out.println(request.toString());
  209.             call.getDialog().sendRequest(clientTransaction);
  210.             
  211.         } catch (TransactionUnavailableException ex) {
  212.             System.out.println(
  213.             "Could not create a register transaction!n"
  214.             + "Check that the Registrar address is correct!"
  215.             + " "
  216.             + ex);
  217.         } catch (SipException ex) {
  218.             System.out.println(
  219.             "Could not send out the register request! " + ex);
  220.         }
  221.         if (call.getVoiceMessaging()) {
  222.             //Stop the voice messages schedule and the voiceRecorder
  223.             messageListener.messageProcessor.stopVoiceMessagingSchedule();
  224.         } else {
  225.             call.getMediaManager().stopMediaSession();
  226.         }
  227.     }
  228.     
  229.     /**
  230.      * Add a contact to our contact list
  231.      * @param contact - contact to add
  232.      */
  233.     public void addContact(String contact) {
  234.         if (isInContactList(contact))
  235.             System.out.println(
  236.             "The contact is already in our contact list,"
  237.             + "he's not going to be added");
  238.         else
  239.             contactList.addElement(contact);
  240.     }
  241.     
  242.     /**
  243.      * Remove a contact to our contact list
  244.      * @param contact - contact to remove
  245.      */
  246.     public void removeContact(String contact) {
  247.         if (isInContactList(contact))
  248.             contactList.remove(contact);
  249.         else
  250.             System.out.println(
  251.             "The contact is not in our contact list,"
  252.             + "he can not going to be removed");
  253.     }
  254.     
  255.     /**
  256.      * Check if the contact is in the contact List
  257.      * @param contactAddress - the contact
  258.      * @return true if the contact is in the contact List
  259.      */
  260.     protected boolean isInContactList(String contactAddress) {
  261.         Enumeration e = contactList.elements();
  262.         while (e.hasMoreElements()) {
  263.             if (((String) e.nextElement()).equals(contactAddress))
  264.                 return true;
  265.         }
  266.         return false;
  267.     }
  268.     
  269.     /**
  270.      * Stop an opened instantMessaging Session
  271.      * @param calleeURI - sip address of the person the application is chatting with
  272.      */
  273.     public void stopInstantMessagingSession(String calleeURI) {
  274.         calleeURI = calleeURI.trim();
  275.         IMCall call = callManager.findIMCall(calleeURI);
  276.         //If no instant messaging session exists
  277.         if (call == null)
  278.             return;
  279.         Request bye = null;
  280.         try {
  281.             bye = call.getDialog().createRequest("BYE");
  282.         } catch (SipException se) {
  283.             se.printStackTrace();
  284.         }
  285.         //Transaction
  286.         ClientTransaction clientTransaction = null;
  287.         try {
  288.             clientTransaction =
  289.             messageListener.sipProvider.getNewClientTransaction(bye);
  290.         } catch (TransactionUnavailableException ex) {
  291.             System.out.println("Could not create a bye transaction!n" + ex);
  292.         }
  293.         System.out.println(bye.toString());
  294.         try {
  295.             call.getDialog().sendRequest(clientTransaction);
  296.         } catch (SipException ex) {
  297.             System.out.println("Could not send out the bye request! " + ex);
  298.         }
  299.     }
  300.     
  301.     /**
  302.      *
  303.      * @param requestName
  304.      * @param callee
  305.      * @return
  306.      */
  307.     protected Request createRequest(
  308.     String requestName,
  309.     SipURI callee,
  310.     SipURI caller) {
  311.         //Listening Point
  312.         ListeningPoint listeningPoint =
  313.         messageListener.sipProvider.getListeningPoint();
  314.         //Request URI
  315.         SipURI requestURI = null;
  316.         try {
  317.             requestURI = callee;
  318.             requestURI.setTransportParam(
  319.             messageListener.getConfiguration().signalingTransport);
  320.             //Call ID
  321.             CallIdHeader callIdHeader =
  322.             messageListener.sipProvider.getNewCallId();
  323.             //CSeq
  324.             CSeqHeader cSeqHeader =
  325.             MessageListener.headerFactory.createCSeqHeader(1, requestName);
  326.             //From Header
  327.             Address fromAddress =
  328.             MessageListener.addressFactory.createAddress(
  329.             MessageListener.addressFactory.createSipURI(
  330.             caller.getUser(),
  331.             caller.getHost()));
  332.             FromHeader fromHeader =
  333.             MessageListener.headerFactory.createFromHeader(
  334.             fromAddress,
  335.             generateTag());
  336.             //ToHeader
  337.             Address toAddress =
  338.             MessageListener.addressFactory.createAddress(
  339.             MessageListener.addressFactory.createSipURI(
  340.             callee.getUser(),
  341.             callee.getHost()));
  342.             // From and To are logical identifiers (should have no parameters)
  343.             ToHeader toHeader =
  344.             MessageListener.headerFactory.createToHeader(toAddress, null);
  345.             //ViaHeader
  346.             ArrayList viaHeaders = new ArrayList();
  347.             ViaHeader viaHeader =
  348.             MessageListener.headerFactory.createViaHeader(
  349.             messageListener.getConfiguration().contactIPAddress,
  350.             listeningPoint.getPort(),
  351.             listeningPoint.getTransport(),
  352.             null);
  353.             viaHeaders.add(viaHeader);
  354.             //Max Forward Header - just forward by one hop.
  355.             MaxForwardsHeader maxForwardsHeader =
  356.             MessageListener.headerFactory.createMaxForwardsHeader(2);
  357.             Request request =
  358.             MessageListener.messageFactory.createRequest(
  359.             requestURI,
  360.             requestName,
  361.             callIdHeader,
  362.             cSeqHeader,
  363.             fromHeader,
  364.             toHeader,
  365.             viaHeaders,
  366.             maxForwardsHeader);
  367.             //Contact Header
  368.             SipURI contactURI =
  369.             MessageListener.addressFactory.createSipURI(
  370.             caller.getUser(),
  371.             messageListener.getConfiguration().contactIPAddress);
  372.             contactURI.setTransportParam(
  373.             messageListener.getConfiguration().signalingTransport);
  374.             ContactHeader contactHeader =
  375.             MessageListener.headerFactory.createContactHeader(
  376.             MessageListener.addressFactory.createAddress(contactURI));
  377.             contactURI.setPort(listeningPoint.getPort());
  378.             request.addHeader(contactHeader);
  379.             //Route Header
  380.             
  381.             /**
  382.              * SipURI routeURI= MessageListener.addressFactory.createSipURI(
  383.              * null,
  384.              * messageListener.getConfiguration().outboundProxy);
  385.              * routeURI.setPort(messageListener.getConfiguration().proxyPort);
  386.              * routeURI.setTransportParam(messageListener.getConfiguration().signalingTransport);
  387.              * RouteHeader routeHeader = MessageListener.headerFactory.createRouteHeader(
  388.              * MessageListener.addressFactory.createAddress(routeURI));
  389.              * request.addHeader(routeHeader);
  390.              **/
  391.             
  392.             return request;
  393.         } catch (Exception ex) {
  394.             ex.printStackTrace();
  395.             System.out.println(
  396.             "Something bad happened when creating request!!");
  397.             System.out.println("method = " + requestName);
  398.             System.out.println("caller = " + caller);
  399.             System.out.println("caller = " + callee);
  400.             return null;
  401.         }
  402.     }
  403.     
  404.     /**
  405.      * Send A Register to the proxy with the authentication acquired from the user.
  406.      * @param userName - the user name
  407.      * @param password - the password's user
  408.      * @param realm - the realm's user
  409.      * TODO : Keep a trace of the expires and schedule a new registration
  410.      */
  411.     public void registerWithAuthentication(
  412.     String userName,
  413.     String password,
  414.     String realm) {
  415.         //WE start the authentication process!!!
  416.         // Let's get the Request related to this response:
  417.         Request request = registerStatus.getRegisterTransaction().getRequest();
  418.         if (request == null) {
  419.             System.out.println(
  420.             "IMUserAgent, processResponse(), the request "
  421.             + " that caused the 407 has not been retrieved!!! Return cancelled!");
  422.         } else {
  423.             Request clonedRequest = (Request) request.clone();
  424.             // Let's increase the Cseq:
  425.             CSeqHeader cseqHeader =
  426.             (CSeqHeader) clonedRequest.getHeader(CSeqHeader.NAME);
  427.             try {
  428.                 cseqHeader.setSequenceNumber(
  429.                 cseqHeader.getSequenceNumber() + 1);
  430.             } catch (InvalidArgumentException iae) {
  431.                 iae.printStackTrace();
  432.             }
  433.             
  434.             // Let's add a Proxy-Authorization header:
  435.             // We send the informations stored:
  436.             Header header =
  437.             registerStatus.getHeader(
  438.             registerStatus.getRegisterResponse(),
  439.             userName,
  440.             password,
  441.             messageListener.getConfiguration().outboundProxy,
  442.             messageListener.getConfiguration().proxyPort);
  443.             
  444.             if (header == null) {
  445.                 System.out.println(
  446.                 "IMUserAgent, processResponse(), Proxy-Authorization "
  447.                 + " header is null, the request is not resent");
  448.             } else {
  449.                 clonedRequest.setHeader(header);
  450.                 ClientTransaction newClientTransaction = null;
  451.                 try {
  452.                     newClientTransaction =
  453.                     messageListener.sipProvider.getNewClientTransaction(
  454.                     clonedRequest);
  455.                 } catch (TransactionUnavailableException tue) {
  456.                     tue.printStackTrace();
  457.                 }
  458.                 
  459.                 try {
  460.                     newClientTransaction.sendRequest();
  461.                 } catch (SipException se) {
  462.                     se.printStackTrace();
  463.                 }
  464.                 System.out.println(
  465.                 "IMUserAgent, processResponse(), REGISTER "
  466.                 + "with credentials sent:n"
  467.                 + clonedRequest);
  468.                 System.out.println();
  469.             }
  470.         }
  471.     }
  472.     
  473.     /**
  474.      * Send A Register to the proxy.
  475.      * TODO : Keep a trace of the expires and schedule a new registration
  476.      */
  477.     public void register() {
  478.         SipURI proxyURI = null;
  479.         try {
  480.             proxyURI =
  481.             MessageListener.addressFactory.createSipURI(
  482.             null,
  483.             messageListener.getConfiguration().outboundProxy);
  484.             proxyURI.setPort(messageListener.getConfiguration().proxyPort);
  485.         } catch (ParseException pe) {
  486.             pe.printStackTrace();
  487.         }
  488.         Request request =
  489.         createRequest(Request.REGISTER, userSipURI, userSipURI);
  490.         // Resource to which this is directed is the proxy.
  491.         request.setRequestURI(proxyURI);
  492.         //Transaction
  493.         ClientTransaction regTrans = null;
  494.         try {
  495.             regTrans =
  496.             messageListener.sipProvider.getNewClientTransaction(request);
  497.             // System.out.println(request.toString());
  498.             regTrans.sendRequest();
  499.             this.setRegisterStatus(RegisterStatus.REGISTRATION_IN_PROGRESS);
  500.         } catch (TransactionUnavailableException ex) {
  501.             System.out.println(
  502.             "Could not create a register transaction!n"
  503.             + "Check that the Registrar address is correct!"
  504.             + " "
  505.             + ex);
  506.         } catch (SipException ex) {
  507.             System.out.println(
  508.             "Could not send out the register request! " + ex);
  509.             ex.printStackTrace();
  510.         }
  511.         
  512.     }
  513.     
  514.     public void unRegisterAndReRegister() {
  515.         this.reRegisterFlag = true;
  516.         this.deRegister();
  517.     }
  518.     
  519.     public void unRegister() {
  520.         this.reRegisterFlag = false;
  521.         this.deRegister();
  522.     }
  523.     /**
  524.      * Stop the registration
  525.      */
  526.     private void deRegister() {
  527.         SipURI proxyURI = null;
  528.         try {
  529.             proxyURI =
  530.             MessageListener.addressFactory.createSipURI(
  531.             null,
  532.             messageListener.getConfiguration().outboundProxy);
  533.             proxyURI.setPort(messageListener.getConfiguration().proxyPort);
  534.         } catch (ParseException pe) {
  535.             pe.printStackTrace();
  536.         }
  537.         Request request =
  538.         createRequest(Request.REGISTER, userSipURI, userSipURI);
  539.         // Direct the request towards the proxy.
  540.         request.setRequestURI(proxyURI);
  541.         //ExpiresHeader
  542.         try {
  543.             ExpiresHeader expires =
  544.             MessageListener.headerFactory.createExpiresHeader(0);
  545.             request.setHeader(expires);
  546.         } catch (InvalidArgumentException iae) {
  547.             iae.printStackTrace();
  548.         }
  549.         ContactHeader contactHeader =
  550.         MessageListener.headerFactory.createContactHeader();
  551.         request.setHeader(contactHeader);
  552.         //Transaction
  553.         ClientTransaction regTrans = null;
  554.         try {
  555.             regTrans =
  556.             messageListener.sipProvider.getNewClientTransaction(request);
  557.             System.out.println(request.toString());
  558.             regTrans.sendRequest();
  559.         } catch (TransactionUnavailableException ex) {
  560.             System.out.println(
  561.             "Could not create a un-register transaction!n"
  562.             + "Check that the Registrar address is correct!"
  563.             + " "
  564.             + ex);
  565.         } catch (SipException ex) {
  566.             System.out.println(
  567.             "Could not send out the un-register request! " + ex);
  568.             ex.printStackTrace();
  569.         }
  570.     }
  571.     
  572.     /**
  573.      * Create the SDP body of the INVITE message
  574.      * for the initiation of the media session
  575.      */
  576.     private String createSDPBody(int audioPort) {
  577.         try {
  578.             SdpFactory sdpFactory = SdpFactory.getInstance();
  579.             SessionDescription sessionDescription =
  580.             sdpFactory.createSessionDescription();
  581.             //Protocol version
  582.             Version version = sdpFactory.createVersion(0);
  583.             sessionDescription.setVersion(version);
  584.             //Owner
  585.             long sdpSessionId=(long)(Math.random() * 1000000);
  586.             Origin origin =
  587.             sdpFactory.createOrigin(
  588.             userSipURI.getUser(),
  589.             sdpSessionId,
  590.             sdpSessionId+1369,
  591.             "IN",
  592.             "IP4",
  593.             messageListener.getConfiguration().contactIPAddress);
  594.             sessionDescription.setOrigin(origin);
  595.             //Session Name
  596.             SessionName sessionName = sdpFactory.createSessionName("-");
  597.             sessionDescription.setSessionName(sessionName);
  598.             //Connection
  599.             Connection connection =
  600.             sdpFactory.createConnection(
  601.             messageListener.getConfiguration().contactIPAddress);
  602.             sessionDescription.setConnection(connection);
  603.             //Time
  604.             Time time = sdpFactory.createTime();
  605.             Vector timeDescriptions = new Vector();
  606.             timeDescriptions.add(time);
  607.             sessionDescription.setTimeDescriptions(timeDescriptions);
  608.             //Media Description
  609.             MediaDescription mediaDescription =
  610.             sdpFactory.createMediaDescription(
  611.             "audio",
  612.             audioPort,
  613.             1,
  614.             "RTP/AVP",
  615.             MediaManager.getSdpAudioSupportedCodecs());
  616.             Vector mediaDescriptions = new Vector();
  617.             mediaDescriptions.add(mediaDescription);
  618.             sessionDescription.setMediaDescriptions(mediaDescriptions);
  619.             return sessionDescription.toString();
  620.         } catch (SdpException se) {
  621.             se.printStackTrace();
  622.         }
  623.         return null;
  624.     }
  625.     
  626.     /**
  627.      * Send an Invite to the sip URI in parameter
  628.      * @param calleeURI - the URI of the user to call
  629.      * @param sdpBody - the sdp content describing the media session
  630.      * to include to the message
  631.      */
  632.     private void sendInvite(String calleeURI, String sdpBody) {
  633.         if (calleeURI.indexOf("sip:") != -1)
  634.             calleeURI = calleeURI.substring("sip:".length());
  635.         //Request URI
  636.         SipURI contactURI = null;
  637.         try {
  638.             //Create the SIP URI for the user URI
  639.             String user = calleeURI.substring(0, calleeURI.indexOf("@"));
  640.             String host =
  641.             calleeURI.substring(
  642.             calleeURI.indexOf("@") + 1,
  643.             calleeURI.length());
  644.             contactURI =
  645.             MessageListener.addressFactory.createSipURI(user, host);
  646.         } catch (ParseException pe) {
  647.             pe.printStackTrace();
  648.         }
  649.         Request invite = createRequest(Request.INVITE, contactURI, userSipURI);
  650.         // Indicate to the other end the type of media I am willing to accept.
  651.         try {
  652.             if (messageListener
  653.             .getConfiguration()
  654.             .mediaTransport
  655.             .equalsIgnoreCase("tcp")){
  656.                 AcceptHeader acceptHeader =
  657.                 MessageListener.headerFactory.createAcceptHeader(
  658.                 "audio",
  659.                 "gsm");
  660.                 invite.addHeader(acceptHeader);
  661.                 acceptHeader =
  662.                 MessageListener.headerFactory.createAcceptHeader(
  663.                 "audio",
  664.                 "x-gsm");
  665.                 invite.addHeader(acceptHeader);
  666.                 acceptHeader =
  667.                 MessageListener.headerFactory.createAcceptHeader(
  668.                 "text",
  669.                 "plain");
  670.                 invite.addHeader(acceptHeader);
  671.             }
  672.             //Content
  673.             ContentTypeHeader contentTypeHeader = null;
  674.             if (sdpBody != null) {
  675.                 contentTypeHeader =
  676.                 MessageListener.headerFactory.createContentTypeHeader(
  677.                 "application",
  678.                 "sdp");
  679.                 invite.setContent(sdpBody, contentTypeHeader);
  680.             }
  681.         } catch (ParseException ex) {
  682.             //Shouldn't happen
  683.             System.out.println(
  684.             "Failed to create a content type header for the INVITE request "
  685.             + ex);
  686.         }
  687.         //Transaction
  688.         ClientTransaction inviteTransaction = null;
  689.         try {
  690.             inviteTransaction =
  691.             messageListener.sipProvider.getNewClientTransaction(invite);
  692.         } catch (TransactionUnavailableException ex) {
  693.             System.out.println(
  694.             "Failed to create inviteTransaction.n"
  695.             + "This is most probably a network connection error. ");
  696.             ex.printStackTrace();
  697.         }
  698.         System.out.println("send request:n" + invite);
  699.         try {
  700.             inviteTransaction.sendRequest();
  701.             //Find the Audio call
  702.             AudioCall call = callManager.findAudioCall("sip:" + calleeURI);
  703.             call.setDialog(inviteTransaction.getDialog());
  704.         } catch (SipException ex) {
  705.             System.out.println(
  706.             "An error occurred while sending invite request " + ex);
  707.         }
  708.     }
  709.     
  710.     /**
  711.      * Send a MESSAGE to notify the proxy that a mail must be send to the callee
  712.      */
  713.     public void sendMessage(String emailBody, String calleeURI) {
  714.         //Store the callee in the call status
  715.         calleeURI = calleeURI.trim();
  716.         //call.setCallee(calleeURI);
  717.         //Listening Point
  718.         ListeningPoint listeningPoint =
  719.         messageListener.sipProvider.getListeningPoint();
  720.         SipURI requestURI = null;
  721.         try {
  722.             requestURI =
  723.             MessageListener.addressFactory.createSipURI(
  724.             null,
  725.             messageListener.getConfiguration().outboundProxy);
  726.             requestURI.setPort(messageListener.getConfiguration().proxyPort);
  727.             requestURI.setTransportParam(
  728.             messageListener.getConfiguration().signalingTransport);
  729.         } catch (ParseException ex) {
  730.             ex.printStackTrace();
  731.         } catch (NullPointerException ex) {
  732.             ex.printStackTrace();
  733.             return;
  734.         }
  735.         //Call ID
  736.         CallIdHeader callIdHeader = messageListener.sipProvider.getNewCallId();
  737.         //CSeq
  738.         CSeqHeader cSeqHeader = null;
  739.         try {
  740.             cSeqHeader =
  741.             MessageListener.headerFactory.createCSeqHeader(
  742.             1,
  743.             Request.MESSAGE);
  744.         } catch (ParseException ex) {
  745.             //Shouldn't happen
  746.             ex.printStackTrace();
  747.         } catch (InvalidArgumentException ex) {
  748.             //Shouldn't happen
  749.             System.out.println(
  750.             "An unexpected error occurred while"
  751.             + "constructing the CSeqHeader "
  752.             + ex);
  753.         }
  754.         FromHeader fromHeader = null;
  755.         //From
  756.         try {
  757.             fromHeader =
  758.             MessageListener.headerFactory.createFromHeader(
  759.             MessageListener.addressFactory.createAddress(userSipURI),
  760.             generateTag());
  761.         } catch (ParseException ex) {
  762.             ex.printStackTrace();
  763.         }
  764.         //To Header
  765.         SipURI ToURI = null;
  766.         try {
  767.             //Create the SIP URI for the user URI
  768.             String user = calleeURI.substring(0, calleeURI.indexOf("@"));
  769.             String host =
  770.             calleeURI.substring(
  771.             calleeURI.indexOf("@") + 1,
  772.             calleeURI.length());
  773.             ToURI = MessageListener.addressFactory.createSipURI(user, host);
  774.             ToURI.setTransportParam(
  775.             messageListener.getConfiguration().signalingTransport);
  776.         } catch (ParseException ex) {
  777.             System.out.println(calleeURI + " is not a legal SIP uri! " + ex);
  778.         }
  779.         Address toAddress = MessageListener.addressFactory.createAddress(ToURI);
  780.         ToHeader toHeader = null;
  781.         try {
  782.             toHeader =
  783.             MessageListener.headerFactory.createToHeader(toAddress, null);
  784.         } catch (ParseException ex) {
  785.             //Shouldn't happen
  786.             System.out.println(
  787.             "Null is not an allowed tag for the to header! " + ex);
  788.         }
  789.         //ViaHeader
  790.         ArrayList viaHeaders = null;
  791.         viaHeaders = new ArrayList();
  792.         try {
  793.             ViaHeader viaHeader =
  794.             MessageListener.headerFactory.createViaHeader(
  795.             messageListener.getConfiguration().contactIPAddress,
  796.             listeningPoint.getPort(),
  797.             listeningPoint.getTransport(),
  798.             null);
  799.             viaHeaders.add(viaHeader);
  800.         } catch (ParseException ex) {
  801.             //Shouldn't happen
  802.             ex.printStackTrace();
  803.         } catch (InvalidArgumentException ex) {
  804.             //Should never happen
  805.             System.out.println("The application is corrupt");
  806.         }
  807.         //Max Forward Header
  808.         MaxForwardsHeader maxForwardsHeader = null;
  809.         try {
  810.             maxForwardsHeader =
  811.             MessageListener.headerFactory.createMaxForwardsHeader(70);
  812.         } catch (InvalidArgumentException ex) {
  813.             //Should never happen
  814.             System.out.println("The application is corrupt");
  815.         }
  816.         Request message = null;
  817.         try {
  818.             message =
  819.             MessageListener.messageFactory.createRequest(
  820.             requestURI,
  821.             Request.MESSAGE,
  822.             callIdHeader,
  823.             cSeqHeader,
  824.             fromHeader,
  825.             toHeader,
  826.             viaHeaders,
  827.             maxForwardsHeader);
  828.         } catch (ParseException ex) {
  829.             System.out.println("Failed to create message Request! ");
  830.             ex.printStackTrace();
  831.         }
  832.         //Contact Header
  833.         try {
  834.             SipURI contactURI =
  835.             MessageListener.addressFactory.createSipURI(
  836.             userSipURI.getUser(),
  837.             messageListener.getConfiguration().contactIPAddress);
  838.             contactURI.setTransportParam(
  839.             messageListener.getConfiguration().signalingTransport);
  840.             ContactHeader contactHeader =
  841.             MessageListener.headerFactory.createContactHeader(
  842.             MessageListener.addressFactory.createAddress(contactURI));
  843.             contactURI.setPort(listeningPoint.getPort());
  844.             message.addHeader(contactHeader);
  845.         } catch (ParseException ex) {
  846.             System.out.println("Could not create the message request! " + ex);
  847.         }
  848.         //Content
  849.         ContentTypeHeader contentTypeHeader = null;
  850.         try {
  851.             contentTypeHeader =
  852.             MessageListener.headerFactory.createContentTypeHeader(
  853.             "text",
  854.             "plain;charset=UTF-8");
  855.         } catch (ParseException ex) {
  856.             //Shouldn't happen
  857.             System.out.println(
  858.             "Failed to create a content type header for the MESSAGE request "
  859.             + ex);
  860.         }
  861.         try {
  862.             message.setContent(emailBody, contentTypeHeader);
  863.         } catch (ParseException ex) {
  864.             System.out.println(
  865.             "Failed to parse sdp data while creating message request! "
  866.             + ex);
  867.         }
  868.         //Transaction
  869.         //Send the message with the sip Provider
  870.         //So that he doesn't retransmit the message even if he doesn't receive
  871.         //an OK (the drawback is that we don't know if the other end ever
  872.         //received the message)
  873.         //TODO : send the message in an invite transaction
  874.         //to know more about delivery
  875.         //ClientTransaction inviteTransaction=null;
  876.         try {
  877.             //inviteTransaction =
  878.             messageListener.sipProvider.sendRequest(message);
  879.         } catch (SipException ex) {
  880.             System.out.println(
  881.             "An error occurred while sending message request " + ex);
  882.             ex.printStackTrace();
  883.         }
  884.         System.out.println("request sent :n" + message);
  885.                 /*try {
  886.                         inviteTransaction.sendRequest();
  887.                         call.setDialog(inviteTransaction.getDialog());
  888.                 }
  889.                 catch (SipException ex) {
  890.                         System.out.println(
  891.                                 "An error occurred while sending invite request "+ ex);
  892.                 } */
  893.     }
  894.     
  895.     /**
  896.      *
  897.      * @param contactAddress
  898.      * @param voiceMessage
  899.      */
  900.     public void sendVoiceMessage(String contactAddress, byte[] voiceMessage) {
  901.         Request messageRequest = null;
  902.         String contact = contactAddress.trim();
  903.         //Try to find if we have an existing transaction for this one
  904.         
  905.         AudioCall call = callManager.findAudioCall(contact);
  906.         javax.sip.Dialog dialog = null;
  907.         
  908.         if (call != null)  {
  909.             dialog = call.getDialog();
  910.     if (dialog == null     || 
  911. dialog.getState() == null     || 
  912. dialog.getState() == javax.sip.DialogState.COMPLETED ||
  913. dialog.getState() == javax.sip.DialogState.TERMINATED)  {
  914. System.out.println("Cannot send message on terminated or un-established dialog!");
  915. // stop the audio stuff 
  916.                 messageListener.messageProcessor.stopVoiceMessagingSchedule();
  917. callManager.removeAudioCall(call);
  918. return;
  919.     }
  920.             
  921.             try {
  922.                 messageRequest = dialog.createRequest(Request.MESSAGE);
  923.                 System.out.println(
  924.                 "SEND VOICE MESSAGE: " + messageRequest.toString());
  925.             } catch (SipException se) {
  926.                 se.printStackTrace();
  927.                 return;
  928.             }
  929.             
  930.         } else {
  931.             return;
  932.         }
  933.         
  934.         
  935.         System.out.println("voice messaging session found!!");
  936.         //Content
  937.         ContentTypeHeader contentTypeHeader = null;
  938.         try {
  939.             contentTypeHeader =
  940.             MessageListener.headerFactory.createContentTypeHeader(
  941.             "audio",
  942.             "x-gsm");
  943.         } catch (ParseException ex) {
  944.             //Shouldn't happen
  945.             System.out.println(
  946.             "Failed to create a content type header for the MESSAGE request "
  947.             + ex);
  948.         }
  949.         try {
  950.             messageRequest.setContent(voiceMessage, contentTypeHeader);
  951.         } catch (ParseException ex) {
  952.             System.out.println(
  953.             "Failed to parse sdp data while creating message request! "
  954.             + ex);
  955.         }
  956.         try {
  957.             ListeningPoint listeningPoint =
  958.             messageListener.sipProvider.getListeningPoint();
  959.             SipURI contactURI =
  960.             MessageListener.addressFactory.createSipURI(
  961.             userSipURI.getUser(),
  962.             messageListener.getConfiguration().contactIPAddress);
  963.             contactURI.setTransportParam(
  964.             messageListener.getConfiguration().signalingTransport);
  965.             ContactHeader contactHeader =
  966.             MessageListener.headerFactory.createContactHeader(
  967.             MessageListener.addressFactory.createAddress(
  968.             contactURI));
  969.             contactURI.setPort(listeningPoint.getPort());
  970.             messageRequest.addHeader(contactHeader);
  971.             SipURI routeURI =
  972.             MessageListener.addressFactory.createSipURI(
  973.             null,
  974.             messageListener.getConfiguration().outboundProxy);
  975.             
  976.         } catch (ParseException ex) {
  977.             System.out.println(
  978.             "Could not create the message request! " + ex);
  979.         }
  980.         //Transaction
  981.         ClientTransaction messageTransaction = null;
  982.         
  983.         try {
  984.             messageTransaction =
  985.             messageListener.sipProvider.getNewClientTransaction(
  986.             messageRequest);
  987.             dialog.sendRequest(messageTransaction);
  988.         } catch (Exception ex) {
  989.             System.out.println(
  990.             "Failed to create Message Transaction.n"
  991.             + "This is most probably a network connection error. ");
  992.             ex.printStackTrace();
  993.             return;
  994.         }
  995.         
  996.         
  997.     }
  998.     
  999.     /**
  1000.      * Send a MESSAGE to the contact we want to chat with
  1001.      * @param contactAddress - the contact to send the message to
  1002.      * @parma message - message to be sent
  1003.      */
  1004.     public void sendInstantMessage(String contactAddress, String message) {
  1005.         Request messageRequest = null;
  1006.        
  1007.         //Try to find if we have an existing transaction for this one
  1008.         SipURI callee = null;
  1009.         try {
  1010.               callee = (SipURI) MessageListener.addressFactory.createURI(contactAddress);
  1011.         } catch (ParseException ex) {
  1012.                 ex.printStackTrace();
  1013.                 return;
  1014.         }
  1015.         String contact = callee.getUser()+"@"+callee.getHost();
  1016.         IMCall call = callManager.findIMCall(contact);
  1017.         javax.sip.Dialog dialog = null;
  1018.         
  1019.         
  1020.         // If we have a call record for this IM session then use the dialog.
  1021.         if (call != null)  {
  1022.             dialog = call.getDialog();
  1023.             System.out.println("im session found!!");
  1024.             
  1025.             try {
  1026.                 messageRequest = dialog.createRequest(Request.MESSAGE);
  1027.                 System.out.println(
  1028.                 "SEND VOICE MESSAGE: " + messageRequest.toString());
  1029.             } catch (SipException se) {
  1030. // The dialog must be dead so get rid of it.
  1031. // proceed on bravely and re-establish the dialog.
  1032. callManager.removeIMCall(contact);
  1033. System.out.println("dialog is dead!");
  1034.                 // Signal that the dialog is dead.
  1035.                 dialog = null;
  1036.             }
  1037.             
  1038.         } 
  1039. if (messageRequest == null)  {
  1040.     System.out.println("could not find IM session for " + contact);
  1041.             try{
  1042.                 messageRequest = createRequest(Request.MESSAGE, callee, userSipURI);
  1043.                 ((FromHeader)messageRequest.getHeader(FromHeader.NAME)).setTag(generateTag());
  1044.             } catch (ParseException pe) {
  1045. // should not happen.
  1046.                 pe.printStackTrace();
  1047.             }
  1048.         }
  1049.         //Content
  1050.         ContentTypeHeader contentTypeHeader = null;
  1051.         try {
  1052.             contentTypeHeader =
  1053.             MessageListener.headerFactory.createContentTypeHeader(
  1054.             "text",
  1055.             "plain;charset=UTF-8");
  1056.         } catch (ParseException ex) {
  1057.             //Shouldn't happen
  1058.             System.out.println(
  1059.             "Failed to create a content type header for the MESSAGE request "
  1060.             + ex);
  1061.         }
  1062.         try {
  1063.             messageRequest.setContent(message, contentTypeHeader);
  1064.         } catch (ParseException ex) {
  1065.             System.out.println(
  1066.             "Failed to parse sdp data while creating message request! "
  1067.             + ex);
  1068.         }
  1069.         //Transaction
  1070.         ClientTransaction messageTransaction = null;
  1071.         try {
  1072.             messageTransaction =
  1073.             messageListener.sipProvider.getNewClientTransaction(
  1074.             messageRequest);
  1075.             if (dialog != null )
  1076.                 dialog.sendRequest(messageTransaction);
  1077.             else  {
  1078.                 messageTransaction.sendRequest();
  1079.                 
  1080.                 IMCall imcall = new IMCall(contact);
  1081.                 imcall.setDialog(messageTransaction.getDialog());
  1082.                 callManager.addIMCall(imcall);
  1083.             }
  1084.             
  1085.         } catch (TransactionUnavailableException ex) {
  1086.             System.out.println(
  1087.             "Failed to create message Transaction.n"
  1088.             + "This is most probably a network connection error. ");
  1089.             ex.printStackTrace();
  1090.         } catch (SipException se) {
  1091.             se.printStackTrace();
  1092.         }
  1093.         
  1094.     }
  1095.     
  1096.     /**
  1097.      * Send a request for subscription
  1098.      * @param subscriberAddress - the address of the contact we want to
  1099.      * subscribe to
  1100.      */
  1101.     public void sendSubscribe(String subscriberAddress) {
  1102.         //Store the callee in the call status
  1103.         String calleeURI = subscriberAddress.trim();
  1104.         //Request URI
  1105.         SipURI contactURI = null;
  1106.         try {
  1107.             //Create the SIP URI for the user URI
  1108.             String user = calleeURI.substring(0, calleeURI.indexOf("@"));
  1109.             String host =
  1110.             calleeURI.substring(
  1111.             calleeURI.indexOf("@") + 1,
  1112.             calleeURI.length());
  1113.             contactURI =
  1114.             MessageListener.addressFactory.createSipURI(user, host);
  1115.         } catch (ParseException pe) {
  1116.             pe.printStackTrace();
  1117.         }
  1118.         Request subscribe =
  1119.         createRequest(Request.SUBSCRIBE, contactURI, userSipURI);
  1120.         //Content
  1121.         ContentTypeHeader contentTypeHeader = null;
  1122.         try {
  1123.             contentTypeHeader =
  1124.             MessageListener.headerFactory.createContentTypeHeader(
  1125.             "application",
  1126.             "sdp");
  1127.         } catch (ParseException ex) {
  1128.             //Shouldn't happen
  1129.             System.out.println(
  1130.             "Failed to create a content type header for the SUBSCRIBE request "
  1131.             + ex);
  1132.         }
  1133.         //Transaction
  1134.         ClientTransaction subscribeTransaction = null;
  1135.         try {
  1136.             subscribeTransaction =
  1137.             messageListener.sipProvider.getNewClientTransaction(subscribe);
  1138.             System.out.println("send request:n" + subscribe);
  1139.             subscribeTransaction.sendRequest();
  1140.             //call.setDialog(subscribeTransaction.getDialog());
  1141.         } catch (TransactionUnavailableException ex) {
  1142.             System.out.println(
  1143.             "Failed to create subscribeTransaction.n"
  1144.             + "This is most probably a network connection error. ");
  1145.             ex.printStackTrace();
  1146.         } catch (SipException ex) {
  1147.             System.out.println(
  1148.             "An error occurred while sending subscribe request " + ex);
  1149.         }
  1150.     }
  1151.     
  1152.     /**
  1153.      * Send an OK in repsonse to an incoming invite
  1154.      * @param sdpBody - the sdpBody to include in the response
  1155.      */
  1156.     private void sendOK(Object sdpBody, String caller) {
  1157.         //Find the Audio call
  1158.         AudioCall call = callManager.findAudioCall(caller);
  1159.         //Listening Point
  1160.         ListeningPoint listeningPoint =
  1161.         messageListener.sipProvider.getListeningPoint();
  1162.         //Get the request
  1163.         Request request = call.getDialog().getFirstTransaction().getRequest();
  1164.         if (!call.getDialog().isServer())
  1165.             System.out.println("Problem, this is a client transaction");
  1166.         //Get the server Transaction
  1167.         ServerTransaction serverTransaction =
  1168.         (ServerTransaction) call.getDialog().getFirstTransaction();
  1169.         try {
  1170.             Response ok =
  1171.             (Response) MessageListener.messageFactory.createResponse(
  1172.             Response.OK,
  1173.             request);
  1174.             //Put a tag on the To Header
  1175.             ((ToHeader) ok.getHeader(ToHeader.NAME)).setTag(generateTag());
  1176.             //Specify the contact Header
  1177.             SipURI contactURI =
  1178.             MessageListener.addressFactory.createSipURI(
  1179.             userSipURI.getUser(),
  1180.             messageListener.getConfiguration().contactIPAddress);
  1181.             contactURI.setTransportParam(
  1182.             messageListener.getConfiguration().signalingTransport);
  1183.             ContactHeader contactHeader =
  1184.             MessageListener.headerFactory.createContactHeader(
  1185.             MessageListener.addressFactory.createAddress(contactURI));
  1186.             contactURI.setPort(listeningPoint.getPort());
  1187.             ok.addHeader(contactHeader);
  1188.             //IF the call is voice messaging we add the Accept Headers
  1189.             if (call.getVoiceMessaging()) {
  1190.                 AcceptHeader acceptHeader =
  1191.                 MessageListener.headerFactory.createAcceptHeader(
  1192.                 "audio",
  1193.                 "gsm");
  1194.                 ok.addHeader(acceptHeader);
  1195.                 acceptHeader =
  1196.                 MessageListener.headerFactory.createAcceptHeader(
  1197.                 "audio",
  1198.                 "x-gsm");
  1199.                 ok.addHeader(acceptHeader);
  1200.                 acceptHeader =
  1201.                 MessageListener.headerFactory.createAcceptHeader(
  1202.                 "application",
  1203.                 "text");
  1204.                 ok.addHeader(acceptHeader);
  1205.                 //initialize the voiceRecorder
  1206.                 VoiceRecorder.getInstance();
  1207.             } else {
  1208.                 //Adding the sdp Body describing the media session
  1209.                 ContentTypeHeader contentTypeHeader =
  1210.                 (ContentTypeHeader) request.getHeader(
  1211.                 ContentTypeHeader.NAME);
  1212.                 if (contentTypeHeader != null && sdpBody != null)
  1213.                     ok.setContent(sdpBody, contentTypeHeader);
  1214.                 else
  1215.                     ok.setHeader(contentTypeHeader);
  1216.             }
  1217.             //Send the ok
  1218.             serverTransaction.sendResponse(ok);
  1219.         } catch (SipException ex) {
  1220.             System.out.println("Failed to send the OK response " + ex);
  1221.         } catch (ParseException ex) {
  1222.             ex.printStackTrace();
  1223.         }catch (InvalidArgumentException iae) {
  1224.                         iae.printStackTrace();
  1225.         } 
  1226.     }
  1227.     
  1228.     /**
  1229.      * Send a BUSY in response to an incoming invite
  1230.      */
  1231.     public void sendBusy(String caller) {
  1232.         //Find the Audio call
  1233.         AudioCall call = callManager.findAudioCall(caller);
  1234.         //Get the request
  1235.         Request request = call.getDialog().getFirstTransaction().getRequest();
  1236.         if (!call.getDialog().isServer())
  1237.             System.out.println("Problem, this is a client transaction");
  1238.         //Get the server Transaction
  1239.         ServerTransaction serverTransaction =
  1240.         (ServerTransaction) call.getDialog().getFirstTransaction();
  1241.         try {
  1242.             Response busy =
  1243.             (Response) MessageListener.messageFactory.createResponse(
  1244.             Response.BUSY_HERE,
  1245.             request);
  1246.             //If the user has put an URL for the BUSY we add i in the CALL-Info
  1247.             if(messageListener.getConfiguration().httpBusy!=null){
  1248.                 CallInfoHeader callInfoHeader=
  1249.                 MessageListener.headerFactory.createCallInfoHeader(
  1250.                 MessageListener.addressFactory.createURI(
  1251.                 messageListener.getConfiguration().httpBusy));
  1252.                 busy.addHeader(callInfoHeader);
  1253.             }
  1254.             serverTransaction.sendResponse(busy);
  1255.             System.out.println(
  1256.             "Audio Call removed : " + call.getDialog().getDialogId());
  1257.             callManager.removeAudioCall(call);
  1258.         } catch (SipException ex) {
  1259.             System.out.println("Failed to send the BUSY response " + ex);
  1260.         } catch (ParseException ex) {
  1261.             ex.printStackTrace();
  1262.         }catch (InvalidArgumentException iae) {
  1263.                         iae.printStackTrace();
  1264.         } 
  1265.     }
  1266.     
  1267.     /**
  1268.      * The subscriber is added now and the user is notified that a new person
  1269.      * wants to be added to the contact list, if he doesn't want this person
  1270.      * to be added, the subscriber is gonna be removed from the GUI
  1271.      * @param subscriber - the subscirber to add
  1272.      */
  1273.     public void notifySubscribe(Subscriber subscriber) {
  1274.         //the subscriber is added now and the user is notified that a new person
  1275.         //wants to be added to the contact list, if he doesn't want this person
  1276.         //to be added, the subscriber is gonna be removed from the GUI
  1277.         presentityManager.addSubscriber(subscriber);
  1278.         //Notify the GUI and the controller that the status has changed
  1279.         setChanged();
  1280.         notifyObservers(subscriber);
  1281.     }
  1282.     
  1283.     /**
  1284.      * Notify the GUI and the controller that the status has changed
  1285.      * @param presenceTag - presence tag containing all the presence information
  1286.      */
  1287.     public void notifyPresence(PresenceTag presenceTag) {
  1288.         Vector atomTagList = presenceTag.getAtomTagList();
  1289.         AtomTag atomTag = (AtomTag) atomTagList.firstElement();
  1290.         AddressTag addressTag = atomTag.getAddressTag();
  1291.         MSNSubStatusTag msnSubStatusTag = addressTag.getMSNSubStatusTag();
  1292.         Subscriber subscriber =
  1293.         presentityManager.getSubscriber(presenceTag.getAddress());
  1294.         if (subscriber != null) {
  1295.             subscriber.setStatus(msnSubStatusTag.getMSNSubStatus());
  1296.         }
  1297.         //Notify the GUI and the controller that the status has changed
  1298.         setChanged();
  1299.         notifyObservers(presenceTag);
  1300.     }
  1301.     
  1302.     /**
  1303.      * Retrieve the messageListener
  1304.      * @return the messageListener
  1305.      */
  1306.     public MessageListener getMessageListener() {
  1307.         return messageListener;
  1308.     }
  1309.     
  1310.     /**
  1311.      * Set the current status of the call
  1312.      * @param callStatus - the current status of the call
  1313.      */
  1314.     public void notifyObserversNewCallStatus(Call call) {
  1315.         //Notify the GUI and the controller that the status has changed
  1316.         setChanged();
  1317.         notifyObservers(call);
  1318.     }
  1319.     
  1320.     /**
  1321.      * Retrieve the contact List
  1322.      * @return the contact List
  1323.      */
  1324.     public Vector getContactList() {
  1325.         return contactList;
  1326.     }
  1327.     
  1328.     /**
  1329.      * Retrieve the call Manager
  1330.      * @return the call Manager
  1331.      */
  1332.     public CallManager getCallManager() {
  1333.         return callManager;
  1334.     }
  1335.     
  1336.     /**
  1337.      * Retrieve the presentity manager
  1338.      * @return the presentity manager
  1339.      */
  1340.     public PresentityManager getPresentityManager() {
  1341.         return presentityManager;
  1342.     }
  1343.     
  1344.     /**
  1345.      * Retrieve the current status of the registration
  1346.      * @return the current status of the registration
  1347.      */
  1348.     public String getRegisterStatus() {
  1349.         return this.registerStatus.getStatus();
  1350.     }
  1351.     
  1352.     /**
  1353.      * Set the current status of the registration
  1354.      * @param registerStatus - the current status of the registration
  1355.      */
  1356.     public void setRegisterStatus(String registerStatus) {
  1357.         this.registerStatus.setStatus(registerStatus);
  1358.         //Notify the GUI and the controller that the status has changed
  1359.         setChanged();
  1360.         notifyObservers(this.registerStatus);
  1361.     }
  1362.     
  1363.     /**
  1364.      * Notify Observers an Instant Message has been received
  1365.      * @param message - the message received
  1366.      */
  1367.     public void notifyObserversIMReceived(String message, String sender) {
  1368.         InstantMessage im = new InstantMessage(message, sender);
  1369.         setChanged();
  1370.         notifyObservers(im);
  1371.     }
  1372.     
  1373.     /**
  1374.      * Generate a Tag
  1375.      * @return the tag generated
  1376.      */
  1377.     public static String generateTag() {
  1378.         return new Integer((int) (Math.random() * 10000)).toString();
  1379.     }
  1380.     
  1381.     /**
  1382.      * Get the current sip user URI
  1383.      * @return the current sip user URI
  1384.      */
  1385.     public SipURI getUserURI() {
  1386.         return userSipURI;
  1387.     }
  1388.     
  1389.     /**
  1390.      * Set the current sip user URI
  1391.      * @param userURI - the current sip user URI
  1392.      */
  1393.     public void setUserURI(String userURI) {
  1394.         try {
  1395.             //Create the SIP URI for the user URI
  1396.             String user = userURI.substring(0, userURI.indexOf("@"));
  1397.             String host =
  1398.             userURI.substring(userURI.indexOf("@") + 1, userURI.length());
  1399.             userSipURI =
  1400.             MessageListener.addressFactory.createSipURI(user, host);
  1401.             userSipURI.setTransportParam(
  1402.             messageListener.getConfiguration().signalingTransport);
  1403.         } catch (ParseException ex) {
  1404.             System.out.println(userURI + " is not a legal SIP uri! " + ex);
  1405.         }
  1406.     }
  1407. }