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

VOIP program

Development Platform:

Java

  1. /*
  2.  * MessageProcessor.java
  3.  *
  4.  * Created on November 17, 2003, 5:52 PM
  5.  */
  6. package gov.nist.applet.phone.ua;
  7. import java.text.ParseException;
  8. import javax.sip.InvalidArgumentException;
  9. import javax.sip.SipException;
  10. import javax.sip.Transaction;
  11. import javax.sip.SipFactory;
  12. import javax.sip.ServerTransaction;
  13. import javax.sip.ClientTransaction;
  14. import javax.sip.message.Request;
  15. import javax.sip.message.Response;
  16. import javax.sip.message.MessageFactory;
  17. import javax.sip.address.AddressFactory;
  18. import javax.sip.address.Address;
  19. import javax.sip.address.SipURI;
  20. import javax.sip.address.URI;
  21. import javax.sip.header.AcceptHeader;
  22. import javax.sip.header.CallInfoHeader;
  23. import javax.sip.header.ContentTypeHeader;
  24. import javax.sip.header.HeaderFactory;
  25. import javax.sip.header.FromHeader;
  26. import javax.sip.header.ToHeader;
  27. import javax.sip.header.ExpiresHeader;
  28. import javax.sip.header.RouteHeader;
  29. import javax.sip.header.RecordRouteHeader;
  30. import javax.sip.header.ContactHeader;
  31. import java.util.ListIterator;
  32. import java.util.LinkedList;
  33. import java.util.Iterator;
  34. import gov.nist.applet.phone.media.MediaManager;
  35. import gov.nist.applet.phone.media.messaging.VoicePlayer;
  36. import gov.nist.applet.phone.media.messaging.VoiceRecorder;
  37. import gov.nist.applet.phone.ua.call.*;
  38. import gov.nist.applet.phone.ua.pidf.parser.PresenceTag;
  39. import gov.nist.applet.phone.ua.pidf.parser.XMLpidfParser;
  40. import gov.nist.applet.phone.ua.presence.*;
  41. /**
  42.  * Handler of a major part of sip messages
  43.  * 
  44.  * @author  DERUELLE Jean
  45.  */
  46. public class MessageProcessor {
  47. /**
  48.  * The SipFactory instance used to create the SipStack and the Address
  49.  * Message and Header Factories.
  50.  */
  51. SipFactory sipFactory;
  52. /**
  53.  * The AddressFactory used to create URLs ans Address objects.
  54.  */
  55. AddressFactory addressFactory;
  56. /**
  57.  * The HeaderFactory used to create SIP message headers.
  58.  */
  59. HeaderFactory headerFactory;
  60. /**
  61.  * The Message Factory used to create SIP messages.
  62.  */
  63. MessageFactory messageFactory;
  64. /**
  65.  * The MessageListener used to handle incoming messages
  66.  */
  67. MessageListener messageListener;
  68. /**
  69.  * the configuration
  70.  */
  71. private Configuration configuration;
  72. VoicePlayer voicePlayer = null;
  73. /**
  74.  * 
  75.  */
  76. MessengerManager messengerManager;
  77. VoiceMessagingTask voiceMessagingTask;
  78.         
  79.         CallManager callManager;
  80. class VoiceMessagingTask implements Runnable {
  81. Thread voiceMessagingThread = null;
  82. String contactAddress = null;
  83. protected static final String STARTED = "Started";
  84. protected static final String STOPPED = "Stopped";
  85. String state = STOPPED;
  86. public VoiceMessagingTask(String contactAddress) {
  87. this.contactAddress = contactAddress;
  88. }
  89. public synchronized void start() {
  90. if (voiceMessagingThread == null) {
  91. voiceMessagingThread = new Thread(this);
  92. }
  93. state = STARTED;
  94. voiceMessagingThread.start();
  95. }
  96. public synchronized void stop() {
  97. state = STOPPED;
  98. }
  99. public void run() {
  100. while (state.equals(STARTED)) {
  101. try {
  102. byte[] buffer = VoiceRecorder.getInstance().getRecord();
  103. if (buffer != null)
  104. messengerManager.sendVoiceMessage(
  105. contactAddress,
  106. buffer);
  107. voiceMessagingThread.sleep(
  108. Configuration.latency4VoiceMessaging);
  109. } catch (Exception ex) {
  110. ex.printStackTrace();
  111. }
  112. }
  113. }
  114. }
  115. /**
  116.  * 
  117.  */
  118. protected void startVoiceMessagingSchedule(String callee) {
  119. //schedule to send the voice messages every 2 sec.
  120. VoiceRecorder.getInstance().initialize();
  121. VoiceRecorder.getInstance().start();
  122. voiceMessagingTask = new VoiceMessagingTask(callee);
  123. voiceMessagingTask.start();
  124. }
  125. /**
  126.  * 
  127.  */
  128. protected void stopVoiceMessagingSchedule() {
  129. //Stop the voice messages schedule and the voiceRecorder
  130. voiceMessagingTask.stop();
  131. VoiceRecorder.getInstance().stop();
  132. if (!VoiceRecorder.isClosed())
  133. VoiceRecorder.getInstance().close();
  134. }
  135. /** Creates a new instance of MessageProcessor 
  136.  * @param callListener - the sip Listener used to handle incoming messages
  137.  */
  138. public MessageProcessor(MessageListener messageListener) {
  139. voicePlayer = new VoicePlayer();
  140. this.messageListener = messageListener;
  141. this.addressFactory = MessageListener.addressFactory;
  142. this.sipFactory = MessageListener.sipFactory;
  143. this.headerFactory = MessageListener.headerFactory;
  144. this.messageFactory = MessageListener.messageFactory;
  145. this.configuration = messageListener.getConfiguration();
  146. this.messengerManager = messageListener.sipMeetingManager;
  147. }
  148. /**********************************************************************/
  149. /*                                                                    */
  150. /*                    Handling request messages                       */
  151. /*                                                                    */
  152. /**********************************************************************/
  153. /**
  154.  * Process the INVITE received request
  155.  * @param serverTransaction - the server transaction associated with the request
  156.  * @param invite - the request 
  157.  */
  158. public void processInvite(
  159. ServerTransaction serverTransaction,
  160. Request invite) {
  161. //Strip out the callee
  162. SipURI calleeURI =
  163. (SipURI) ((FromHeader) invite.getHeader(FromHeader.NAME))
  164. .getAddress()
  165. .getURI();
  166. String callee =
  167. "sip:" + calleeURI.getUser() + "@" + calleeURI.getHost();
  168. // Note that IM Calls are always accepted but VOICE calls may be rejected as there is
  169. // only one Voice line.
  170. try {
  171. CallManager callManager =
  172. messageListener.sipMeetingManager.getCallManager();
  173. //The audio device cannot be shared so if the user is already
  174. //in a call, a busy here is sent back
  175. if (callManager.isAlreadyInAudioCall()) {
  176. Response busyHere =
  177. (Response) MessageListener.messageFactory.createResponse(
  178. Response.BUSY_HERE,
  179. invite);
  180. //If the user has put an URL for the BUSY we add i in the CALL-Info
  181.    if(messageListener.getConfiguration().httpBusy!=null){
  182. CallInfoHeader callInfoHeader=
  183. MessageListener.headerFactory.createCallInfoHeader(
  184.    MessageListener.addressFactory.createURI(
  185.    messageListener.getConfiguration().httpBusy));
  186.    busyHere.addHeader(callInfoHeader);
  187.   }
  188.   System.out.println("send response : " + busyHere.toString());
  189.   serverTransaction.sendResponse(busyHere);
  190. } else {
  191.                                 
  192. Response ringing =
  193. (Response) MessageListener.messageFactory.createResponse(
  194. Response.RINGING,
  195. invite);
  196. // System.out.println("send response : "+ringing.toString());
  197. serverTransaction.sendResponse(ringing);
  198. AudioCall audioCall = new AudioCall(messageListener);
  199. audioCall.setCallee(callee);
  200. audioCall.setDialog(serverTransaction.getDialog());
  201. audioCall.setStatus(AudioCall.INCOMING_CALL);
  202. System.out.println(
  203. "Audio Call created : "
  204. + audioCall.getDialog().getDialogId());
  205. callManager.addAudioCall(audioCall);
  206. //Check if someone is inviting us to voice messaging
  207. //or regular RTP media session
  208. boolean voiceMessaging = false;
  209. ContentTypeHeader contentTypeHeader =
  210. (ContentTypeHeader) invite.getHeader(
  211. ContentTypeHeader.NAME);
  212. if (contentTypeHeader == null) {
  213. ListIterator acceptHeaderList =
  214. invite.getHeaders(AcceptHeader.NAME);
  215. while (acceptHeaderList.hasNext() && !voiceMessaging) {
  216. AcceptHeader acceptHeader =
  217. (AcceptHeader) acceptHeaderList.next();
  218. if (acceptHeader
  219. .getContentSubType()
  220. .equalsIgnoreCase("gsm")
  221. || acceptHeader.getContentSubType().equalsIgnoreCase(
  222. "x-gsm"))
  223. voiceMessaging = true;
  224. }
  225. }
  226. audioCall.setVoiceMesaging(voiceMessaging);
  227. // The SDP tool will be created when you send OK.
  228. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  229. audioCall);
  230. }
  231. } catch (SipException se) {
  232. se.printStackTrace();
  233. } catch (ParseException pe) {
  234. pe.printStackTrace();
  235. }catch (InvalidArgumentException iae) {
  236.                         iae.printStackTrace();
  237.                 } 
  238. }
  239. /**
  240.  * Process the BYE received request
  241.  * @param serverTransaction - the server transaction associated with the request
  242.  * @param bye - the bye request 
  243.  */
  244. public void processBye(ServerTransaction serverTransaction, Request bye) {
  245. CallManager callManager =
  246. messageListener.sipMeetingManager.getCallManager();
  247. //Find the matching call
  248. Call call =
  249. callManager.findCall(serverTransaction.getDialog().getDialogId());
  250. if (call == null) return;
  251. //Send OK
  252. try {
  253. Response ok =
  254. (Response) MessageListener.messageFactory.createResponse(
  255. Response.OK,
  256. bye);
  257. serverTransaction.sendResponse(ok);
  258. if (call instanceof AudioCall) {
  259. AudioCall audioCall = (AudioCall) call;
  260. audioCall.setStatus(AudioCall.NOT_IN_A_CALL);
  261. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  262. audioCall);
  263. if (audioCall.getVoiceMessaging()) {
  264. stopVoiceMessagingSchedule();
  265. } else {
  266. audioCall.getMediaManager().stopMediaSession();
  267. }
  268. //Remove the call
  269. System.out.println(
  270. "Audio Call removed : " + call.getDialog().getDialogId());
  271. callManager.removeAudioCall(audioCall);
  272. } else {
  273. IMCall imCall = (IMCall) call;
  274. //Remove the call
  275. System.out.println(
  276. "IM Call removed : " + call.getDialog().getDialogId());
  277. callManager.removeIMCall(imCall);
  278. }
  279. } catch (SipException se) {
  280. se.printStackTrace();
  281. } catch (ParseException pe) {
  282. pe.printStackTrace();
  283. }catch (InvalidArgumentException iae) {
  284.                         iae.printStackTrace();
  285.                 } 
  286. }
  287. /**
  288.  * Process the ACK received request
  289.  * @param serverTransaction - the server transaction associated with the request
  290.  * @param ack - the ack request 
  291.  */
  292. public void processAck(ServerTransaction serverTransaction, Request ack) {
  293. CallManager callManager =
  294. messageListener.sipMeetingManager.getCallManager();
  295. //Strip out the callee
  296. SipURI calleeURI =
  297. (SipURI) ((FromHeader) ack.getHeader(FromHeader.NAME))
  298. .getAddress()
  299. .getURI();
  300. String callee =
  301. "sip:" + calleeURI.getUser() + "@" + calleeURI.getHost();
  302. //Find the Audio call
  303. AudioCall call = callManager.findAudioCall(callee);
  304. if (call != null) {
  305. if (!call.getVoiceMessaging())
  306. call.getMediaManager().startMediaSession(false);
  307. else
  308. startVoiceMessagingSchedule(callee);
  309. call.setStatus(AudioCall.IN_A_CALL);
  310. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  311. call);
  312. }
  313. }
  314. /**
  315.  * Process the CANCEL received request
  316.  * @param serverTransaction - the server transaction associated with the request
  317.  * @param cancel - the cancel request 
  318.  */
  319. public void processCancel(
  320. ServerTransaction serverTransaction,
  321. Request cancel) {
  322. CallManager callManager =
  323. messageListener.sipMeetingManager.getCallManager();
  324. //Strip out the callee
  325. SipURI calleeURI =
  326. (SipURI) ((FromHeader) cancel.getHeader(FromHeader.NAME))
  327. .getAddress()
  328. .getURI();
  329. String callee =
  330. "sip:" + calleeURI.getUser()
  331. + "@"
  332. + calleeURI.getHost().trim().toLowerCase();
  333. //Find the Audio call
  334. AudioCall call = callManager.findAudioCall(callee);
  335. //Send OK
  336. try {
  337. Response ok =
  338. (Response) MessageListener.messageFactory.createResponse(
  339. Response.OK,
  340. cancel);
  341. serverTransaction.sendResponse(ok);
  342. call.setStatus(AudioCall.CANCEL);
  343. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  344. call);
  345. System.out.println(
  346. "Audio Call removed : " + call.getDialog().getDialogId());
  347. callManager.removeAudioCall(call);
  348. } catch (SipException se) {
  349. se.printStackTrace();
  350. } catch (ParseException pe) {
  351. pe.printStackTrace();
  352. }catch (InvalidArgumentException iae) {
  353.                         iae.printStackTrace();
  354.                 } 
  355. }
  356. /**
  357.  * Process the MESSAGE received request. MESSAGE may be handled statefully or
  358.  * statelessly.
  359.  * @param serverTransaction - the server transaction associated with the request
  360.  * @param message - the request 
  361.  */
  362. public void processMessage(
  363. ServerTransaction serverTrans,
  364. Request message) {
  365.                     
  366.                ServerTransaction serverTransaction = null;
  367.                 try {
  368.                     serverTransaction = 
  369. (serverTrans == null? 
  370. messageListener.sipProvider.getNewServerTransaction(message): 
  371. serverTrans);
  372.                 } catch (javax.sip.TransactionAlreadyExistsException ex) {
  373.                     ex.printStackTrace();
  374.                     return;
  375.                 } catch (javax.sip.TransactionUnavailableException ex1) {
  376.                     ex1.printStackTrace();
  377.                     return;
  378.                 }
  379.                            
  380.                        
  381. //Send OK
  382. try {
  383. String sender = null;
  384. Address address =
  385. ((FromHeader) message.getHeader(FromHeader.NAME)).getAddress();
  386. if (address.getURI().isSipURI()) {
  387. SipURI sipURI = ((SipURI) address.getURI());
  388. String host = sipURI.getHost();
  389. String user = sipURI.getUser();
  390. sender = user + "@" + host;
  391. }
  392.                         
  393. //Getting the sdp body for creating the response
  394. //This sdp body will present what codec has been chosen
  395. //and on which port every media will be received
  396. ContentTypeHeader contentTypeHeader =
  397. (ContentTypeHeader) message.getHeader(ContentTypeHeader.NAME);
  398. if (contentTypeHeader == null
  399. || contentTypeHeader.getContentType() == null
  400. || contentTypeHeader.getContentSubType() == null)
  401. return;
  402. String subType = contentTypeHeader.getContentSubType();
  403. //If we have a voice messaging plays it
  404. if (contentTypeHeader.getContentType().equals("audio")) {
  405. if (contentTypeHeader.getContentSubType().equals("x-gsm")
  406. || contentTypeHeader.getContentSubType().equals("gsm")) {
  407. Response ok =
  408. (
  409. Response) MessageListener
  410. .messageFactory
  411. .createResponse(
  412. Response.OK,
  413. message);
  414. ToHeader toHeader =
  415. (ToHeader) message.getHeader(ToHeader.NAME);
  416. if (toHeader.getTag() == null)
  417. toHeader.setTag(
  418. new Integer((int) (Math.random() * 10000))
  419. .toString());
  420. serverTransaction.sendResponse(ok);
  421. byte[] voiceMessage = message.getRawContent();
  422. //if(!voicePlayer.isInitialized())
  423. voicePlayer.initialize(voiceMessage);
  424. /*else
  425. voicePlayer.setData(voiceMessage);*/
  426. voicePlayer.play();
  427. message.removeContent();
  428. } else {
  429. System.out.println(
  430. "Cannot handle this codec "
  431. + contentTypeHeader.getContentSubType());
  432. }
  433. } else if (
  434. contentTypeHeader.getContentType().equals("text")
  435. && contentTypeHeader.getContentSubType().equals("plain")) {
  436. Response ok =
  437. (Response) MessageListener.messageFactory.createResponse(
  438. Response.OK,
  439. message);
  440. ToHeader toHeader = (ToHeader) message.getHeader(ToHeader.NAME);
  441. if (toHeader.getTag() == null) {
  442. toHeader.setTag(
  443. new Integer((int) (Math.random() * 10000)).toString());
  444.                                         IMCall imcall = this.messengerManager.callManager.findIMCall(sender);
  445.                                         if (imcall == null) {
  446.                                                 imcall = new IMCall(sender);
  447.                                                 imcall.setDialog(serverTransaction.getDialog());
  448.                                                 this.messengerManager.callManager.addIMCall(imcall);
  449.                                         } else imcall.setDialog(serverTransaction.getDialog());
  450.                                 }
  451. serverTransaction.sendResponse(ok);
  452. String content = new String(message.getRawContent());
  453. FromHeader fromHeader =
  454. (FromHeader) message.getHeader(FromHeader.NAME);
  455. messageListener.sipMeetingManager.notifyObserversIMReceived(
  456. content,
  457. sender);
  458. } else {
  459. // send a Not Acceptable here message.
  460. Response notok =
  461. (Response) MessageListener.messageFactory.createResponse(
  462. Response.NOT_ACCEPTABLE,
  463. message);
  464. ToHeader toHeader = (ToHeader) message.getHeader(ToHeader.NAME);
  465. serverTransaction.sendResponse(notok);
  466. System.out.println(
  467. "Cannot handle this content Type " + contentTypeHeader);
  468. }
  469.                         
  470. message.removeContent();
  471. } catch (SipException se) {
  472. se.printStackTrace();
  473. } catch (ParseException pe) {
  474. pe.printStackTrace();
  475. }catch (InvalidArgumentException iae) {
  476.                         iae.printStackTrace();
  477.                 } 
  478. }
  479. /**
  480.  * Process the SUBSCRIBE received request
  481.  * @param serverTransaction - the server transaction associated with the request
  482.  * @param subscribe - the request 
  483.  */
  484. public void processSubscribe(
  485. ServerTransaction serverTransaction,
  486. Request subscribe) {
  487. Address address =
  488. ((FromHeader) subscribe.getHeader(FromHeader.NAME)).getAddress();
  489. String sender = null;
  490. if (address.getURI().isSipURI()) {
  491. SipURI sipURI = ((SipURI) address.getURI());
  492. String host = sipURI.getHost();
  493. String user = sipURI.getUser();
  494. sender = user + "@" + host;
  495. }
  496. Subscriber subscriber = new Subscriber(sender);
  497. subscriber.setDialog(serverTransaction.getDialog());
  498. messageListener.sipMeetingManager.notifySubscribe(subscriber);
  499. }
  500. /**
  501.  * Process the NOTIFY received request
  502.  * @param serverTransaction - the server transaction associated with the request
  503.  * @param notify - the request 
  504.  */
  505. public void processNotify(
  506. ServerTransaction serverTransaction,
  507. Request notify) {
  508. //Send OK
  509. try {
  510. Response ok =
  511. (Response) MessageListener.messageFactory.createResponse(
  512. Response.OK,
  513. notify);
  514. ToHeader toHeader = (ToHeader) notify.getHeader(ToHeader.NAME);
  515. if (toHeader.getTag() == null)
  516. toHeader.setTag(
  517. new Integer((int) (Math.random() * 10000)).toString());
  518. serverTransaction.sendResponse(ok);
  519. Address address =
  520. ((FromHeader) notify.getHeader(FromHeader.NAME)).getAddress();
  521. String sender = null;
  522. if (address.getURI().isSipURI()) {
  523. SipURI sipURI = ((SipURI) address.getURI());
  524. String host = sipURI.getHost();
  525. String user = sipURI.getUser();
  526. sender = user + "@" + host;
  527. }
  528. ContentTypeHeader contentTypeHeader =
  529. (ContentTypeHeader) notify.getHeader(ContentTypeHeader.NAME);
  530. if (contentTypeHeader != null) {
  531. String xmlType = contentTypeHeader.getContentSubType();
  532. if (xmlType.equals("xpidf+xml")) {
  533. String pidfContent = new String(notify.getRawContent());
  534. int endDocIndex = pidfContent.indexOf("<presence>");
  535. pidfContent = pidfContent.substring(endDocIndex);
  536. XMLpidfParser pidfParser = new XMLpidfParser();
  537. pidfParser.parsePidfString(pidfContent);
  538. PresenceTag presenceTag = pidfParser.getPresenceTag();
  539. presenceTag.setAddress(sender);
  540. messageListener.sipMeetingManager.notifyPresence(
  541. presenceTag);
  542. }
  543. }
  544. } catch (SipException se) {
  545. se.printStackTrace();
  546. } catch (ParseException pe) {
  547. pe.printStackTrace();
  548. }catch (InvalidArgumentException iae) {
  549.                         iae.printStackTrace();
  550.                 } 
  551. }
  552. /**
  553.  * After a
  554.  * @param clientTransaction
  555.  * @param response
  556.  */
  557. public void processRequestTerminated(
  558. ClientTransaction clientTransaction,
  559. Response response) {
  560. }
  561. /**********************************************************************/
  562. /*                                                                    */
  563. /*                    Handling response messages                      */
  564. /*                                                                    */
  565. /**********************************************************************/
  566. /**
  567.  * Process the Not found received response
  568.  * @param clientTransaction - the client transaction associated with the response
  569.  * @param notFound - the not found response
  570.  */
  571. public void processNotFound(
  572. ClientTransaction clientTransaction,
  573. Response notFound) {
  574. }
  575. /**
  576.  * Process the Not Implemented received response
  577.  * @param clientTransaction - the client transaction associated with the response
  578.  * @param notImplemented - the not found response
  579.  */
  580. public void processNotImplemented(
  581. ClientTransaction clientTransaction,
  582. Response notImplemented) {
  583. }
  584. /**
  585.  * Process the Trying received response
  586.  * @param clientTransaction - the client transaction associated with the response
  587.  * @param trying - the trying response
  588.  */
  589. public void processTrying(
  590. ClientTransaction clientTransaction,
  591. Response trying) {
  592. CallManager callManager =
  593. messageListener.sipMeetingManager.getCallManager();
  594. //Strip out the callee
  595. SipURI calleeURI =
  596. (SipURI) ((ToHeader) trying.getHeader(ToHeader.NAME))
  597. .getAddress()
  598. .getURI();
  599. String callee =
  600. "sip:" + calleeURI.getUser() + "@" + calleeURI.getHost();
  601. //Find the Audio call
  602. AudioCall call = callManager.findAudioCall(callee);
  603. if (call != null) {
  604. call.setStatus(AudioCall.TRYING);
  605. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  606. call);
  607. }
  608. }
  609. /**
  610.  * Process the Ringing received response
  611.  * @param clientTransaction - the client transaction associated with the response
  612.  * @param ringing - the ringing response
  613.  */
  614. public void processRinging(
  615. ClientTransaction clientTransaction,
  616. Response ringing) {
  617. CallManager callManager =
  618. messageListener.sipMeetingManager.getCallManager();
  619. //Strip out the callee
  620. SipURI calleeURI =
  621. (SipURI) ((ToHeader) ringing.getHeader(ToHeader.NAME))
  622. .getAddress()
  623. .getURI();
  624. String callee =
  625. "sip:" + calleeURI.getUser() + "@" + calleeURI.getHost();
  626. //Find the Audio call
  627. AudioCall call = callManager.findAudioCall(callee);
  628. call.setStatus(AudioCall.RINGING);
  629. messageListener.sipMeetingManager.notifyObserversNewCallStatus(call);
  630. }
  631. /**
  632.  * Process the OK received response for a REGISTER 
  633.  * @param clientTransaction - the client transaction associated with the response
  634.  * @param registerOK - the OK received response for a REGISTER 
  635.  */
  636. public void processRegisterOK(
  637. ClientTransaction clientTransaction,
  638. Response registerOK) {
  639. FromHeader fromHeader =
  640. ((FromHeader) registerOK.getHeader(FromHeader.NAME));
  641. Address address = fromHeader.getAddress();
  642. ExpiresHeader expires = registerOK.getExpires();
  643. if (expires != null && expires.getExpires() == 0) {
  644. if (messageListener.sipMeetingManager.reRegisterFlag)
  645. // This is an OK for a cleared registration.
  646. messageListener.sipMeetingManager.register();
  647. else
  648. messageListener.sipMeetingManager.setRegisterStatus(
  649. RegisterStatus.NOT_REGISTERED);
  650. } else {
  651. messageListener.sipMeetingManager.setRegisterStatus(
  652. RegisterStatus.REGISTERED);
  653. }
  654. }
  655. /**
  656.  * Process the OK received response for a BYE
  657.  * @param clientTransaction - the client transaction associated with the response
  658.  * @param byeOK - the OK received response for a BYE
  659.  */
  660. public void processByeOK(
  661. ClientTransaction clientTransaction,
  662. Response byeOK) {
  663. CallManager callManager =
  664. messageListener.sipMeetingManager.getCallManager();
  665. //Find the Audio call
  666. Call call =
  667. callManager.findCall(clientTransaction.getDialog().getDialogId());
  668. if (call instanceof AudioCall) {
  669. AudioCall audioCall = (AudioCall) call;
  670. audioCall.setStatus(AudioCall.NOT_IN_A_CALL);
  671. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  672. audioCall);
  673. /*if(audioCall.getVoiceMessaging()){
  674. stopVoiceMessagingSchedule();
  675. }
  676. else{
  677. audioCall.getMediaManager().stopMediaSession();
  678. }*/
  679. System.out.println(
  680. "Audio Call removed : " + call.getDialog().getDialogId());
  681. //Remove the call
  682. callManager.removeAudioCall(audioCall);
  683. } else {
  684. IMCall imCall = (IMCall) call;
  685. //Remove the call
  686. System.out.println(
  687. "IM Call removed : " + call.getDialog().getDialogId());
  688. callManager.removeIMCall(imCall);
  689. }
  690. }
  691. /**
  692.  * Process the OK received response for a CANCEL
  693.  * @param clientTransaction - the client transaction associated with the response
  694.  * @param cancelOK - the OK received response for a CANCEL
  695.  */
  696. public void processCancelOK(
  697. ClientTransaction clientTransaction,
  698. Response cancelOK) {
  699. CallManager callManager =
  700. messageListener.sipMeetingManager.getCallManager();
  701. //Strip out the callee
  702. SipURI calleeURI =
  703. (SipURI) ((ToHeader) cancelOK.getHeader(ToHeader.NAME))
  704. .getAddress()
  705. .getURI();
  706. String callee =
  707. "sip:"
  708. + calleeURI.getUser()
  709. + "@"
  710. + calleeURI.getHost().trim().toLowerCase();
  711. //Find the Audio call
  712. AudioCall call = callManager.findAudioCall(callee);
  713. call.setStatus(AudioCall.NOT_IN_A_CALL);
  714. messageListener.sipMeetingManager.notifyObserversNewCallStatus(call);
  715. callManager.removeAudioCall(call);
  716. }
  717. /**
  718.  * Process the OK received response for a MESSAGE
  719.  * @param clientTransaction - the client transaction associated with the response
  720.  * @param cancelOK - the OK received response for a MESSAGE
  721.  */
  722. public void processMessageOK(
  723. ClientTransaction clientTransaction,
  724. Response messageOK) {
  725. }
  726. /**
  727.  * Process the OK received response for a SUBSCRIBE
  728.  * @param clientTransaction - the client transaction associated with the response
  729.  * @param cancelOK - the OK received response for a MESSAGE
  730.  */
  731. public void processSubscribeOK(
  732. ClientTransaction clientTransaction,
  733. Response subscribeOK) {
  734. messageListener.sipMeetingManager.presenceAllowed = true;
  735. Address address =
  736. ((FromHeader) subscribeOK.getHeader(FromHeader.NAME)).getAddress();
  737. String sender = null;
  738. if (address.getURI().isSipURI()) {
  739. SipURI sipURI = ((SipURI) address.getURI());
  740. String host = sipURI.getHost();
  741. String user = sipURI.getUser();
  742. sender = user + "@" + host;
  743. }
  744. /*Subscriber subscriber = new Subscriber(sender);
  745. subscriber.setDialog(clientTransaction.getDialog());
  746. messageListener.sipMeetingManager.getPresentityManager().
  747. sendNotifyToSubscriber(
  748. subscriber,
  749. "open",
  750. "online");*/
  751. }
  752. /**
  753.  * Process the OK received response for a SUBSCRIBE
  754.  * @param clientTransaction - the client transaction associated with the response
  755.  * @param cancelOK - the OK received response for a MESSAGE
  756.  */
  757. public void processSubscribeAccepted(
  758. ClientTransaction clientTransaction,
  759. Response subscribeAccepted) {
  760. messageListener.sipMeetingManager.presenceAllowed = true;
  761. /*Address address=((FromHeader)subscribeOK.getHeader(FromHeader.NAME)).getAddress();
  762. String sender=null;
  763. if(address.getURI().isSipURI()){
  764. SipURI sipURI=((SipURI)address.getURI());
  765. String host=sipURI.getHost();
  766. String user=sipURI.getUser();
  767. sender=user+"@"+host;
  768. }*/
  769. /*Subscriber subscriber = new Subscriber(sender);
  770. subscriber.setDialog(clientTransaction.getDialog());
  771. messageListener.sipMeetingManager.getPresentityManager().
  772. sendNotifyToSubscriber(
  773. subscriber,
  774. "open",
  775. "online");*/
  776. }
  777. /**
  778.  * Process the OK received response for a INVITE
  779.  * @param clientTransaction - the client transaction associated with the response
  780.  * @param inviteOK - the OK received response for a INVITE
  781.  */
  782. public void processInviteOK(
  783. ClientTransaction clientTransaction,
  784. Response inviteOK) {
  785. CallManager callManager =
  786. messageListener.sipMeetingManager.getCallManager();
  787. //Strip out the callee
  788. SipURI calleeURI =
  789. (SipURI) ((ToHeader) inviteOK.getHeader(ToHeader.NAME))
  790. .getAddress()
  791. .getURI();
  792. String callee =
  793. "sip:" + calleeURI.getUser() + "@" + calleeURI.getHost();
  794. //Find the Audio call
  795. AudioCall call = callManager.findAudioCall(callee);
  796. //Send ACK
  797. try {
  798. Request ack =
  799. (Request) clientTransaction.getDialog().createRequest(
  800. Request.ACK);
  801. //ack.setRequestURI(calleeURI);
  802. System.out.println("Sending ACK : n" + ack.toString());
  803. try {
  804. clientTransaction.getDialog().sendAck(ack);
  805. } catch (SipException ex) {
  806. System.out.println("Could not send out the ACK request! ");
  807. ex.printStackTrace();
  808. }
  809. //messageListener.sipProvider.sendRequest(ack);                        
  810. } catch (SipException ex) {
  811. ex.printStackTrace();
  812. }
  813. ContentTypeHeader contentTypeHeader =
  814. (ContentTypeHeader) inviteOK.getHeader(ContentTypeHeader.NAME);
  815. // no content type header means other end does not support sdp.
  816. if (contentTypeHeader != null) {
  817. String type = contentTypeHeader.getContentType();
  818. String subType = contentTypeHeader.getContentSubType();
  819. //System.out.println("the other end answer us with "+subType);         
  820. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  821. call);
  822. if (type.equals("application") && subType.equals("sdp")) {
  823. //Start the media session
  824. MediaManager mediaManager = call.getMediaManager();
  825. call.setVoiceMesaging(false);
  826. mediaManager.prepareMediaSession(
  827. new String(inviteOK.getRawContent()));
  828. mediaManager.startMediaSession(true);
  829. }
  830. } else {
  831. // else start the media messaging session.
  832. ListIterator it = inviteOK.getHeaders(AcceptHeader.NAME);
  833. while (it.hasNext()) {
  834. AcceptHeader next = (AcceptHeader) it.next();
  835. if (next.getContentType().equals("audio")
  836. && (next.getContentSubType().equals("gsm")
  837. || next.getContentSubType().equals("x-gsm"))) {
  838. call.setVoiceMesaging(true);
  839. //schedule to send the voice messages every 2 sec.
  840. startVoiceMessagingSchedule(callee);
  841. } else if (
  842. next.getContentType().equals("text")
  843. && next.getContentSubType().equals("plain")) {
  844. // TODO -- textMessaging.setTextMessaging(true);
  845. }
  846. }
  847. }
  848. call.setStatus(AudioCall.IN_A_CALL);
  849. messageListener.sipMeetingManager.notifyObserversNewCallStatus(call);
  850. }
  851. /**
  852.  * Process the Busy here response
  853.  * @param clientTransaction - the client transaction associated with the response
  854.  * @param busyhere - the Busy here response
  855.  */
  856. public void processBusyHere(
  857. ClientTransaction clientTransaction,
  858. Response busyHere) {
  859. CallManager callManager =
  860. messageListener.sipMeetingManager.getCallManager();
  861. //Strip out the callee
  862. SipURI calleeURI =
  863. (SipURI) ((ToHeader) busyHere.getHeader(ToHeader.NAME))
  864. .getAddress()
  865. .getURI();
  866. String callee =
  867. "sip:" + calleeURI.getUser() + "@" + calleeURI.getHost();
  868. Call call =
  869. callManager.findCall(clientTransaction.getDialog().getDialogId());
  870. if (call instanceof AudioCall) {
  871. //Strip out the Call info Header
  872. CallInfoHeader callInfoHeader=(CallInfoHeader)busyHere.getHeader(CallInfoHeader.NAME);
  873. URI uri=callInfoHeader.getInfo();
  874. ((AudioCall)call).setURL(uri);
  875. call.setStatus(AudioCall.BUSY);
  876. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  877. call);
  878. System.out.println(
  879. "Audio Call removed : " + call.getDialog().getDialogId());
  880. callManager.removeAudioCall((AudioCall) call);
  881. }
  882. }
  883. /**
  884.  * Process the temporary Unavailable response
  885.  * @param clientTransaction - the client transaction associated with the response
  886.  * @param temporaryUnavailable - the temporary Unavailable response
  887.  */
  888. public void processUnavailable(
  889. ClientTransaction clientTransaction,
  890. Response temporaryUnavailable) {
  891. CallManager callManager =
  892. messageListener.sipMeetingManager.getCallManager();
  893. //Find the call
  894. Call call =
  895. callManager.findCall(clientTransaction.getDialog().getDialogId());
  896. if (call instanceof AudioCall) {
  897. AudioCall audioCall = (AudioCall) call;
  898. audioCall.setStatus(Call.TEMPORARY_UNAVAILABLE);
  899. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  900. audioCall);
  901. System.out.println(
  902. "Audio Call removed : " + call.getDialog().getDialogId());
  903. callManager.removeAudioCall(audioCall);
  904. } else if (call instanceof IMCall) {
  905. IMCall imCall = (IMCall) call;
  906. imCall.setStatus(Call.TEMPORARY_UNAVAILABLE);
  907. messageListener.sipMeetingManager.notifyObserversNewCallStatus(
  908. imCall);
  909. System.out.println(
  910. "IM Call removed : " + call.getDialog().getDialogId());
  911. callManager.removeIMCall(imCall);
  912. }
  913. }
  914. /**
  915.  * Process the 407 - Proxy Authentication Required
  916.  * @param clientTransaction - the client transaction associated with the response
  917.  * @param proxyAuthenticationRequired - the temporary Unavailable response
  918.  */
  919. public void processProxyAuthenticationRequired(
  920. ClientTransaction clientTransaction,
  921. Response proxyAuthenticationRequired) {
  922. messageListener.sipMeetingManager.setRegisterStatus(
  923. RegisterStatus.PROXY_AUTHENTICATION_REQUIRED);
  924. }
  925. /**
  926.  * Process the 405 - Method Not Allowed
  927.  * @param clientTransaction - the client transaction associated with the response
  928.  * @param proxyAuthenticationRequired - the temporary Unavailable response
  929.  */
  930. public void processMethodNotAllowed(
  931. ClientTransaction clientTransaction,
  932. Response methodNotAllowed) {
  933. messageListener.sipMeetingManager.presenceAllowed = false;
  934. }
  935. /**********************************************************************/
  936. /*                                                                    */
  937. /*                    Handling timeout messages                       */
  938. /*                                                                    */
  939. /**********************************************************************/
  940. /**
  941.  * Process the timed out MESSAGE
  942.  * @param message - the timedout request 
  943.  */
  944. public void processTimedOutMessage(Request message) {
  945. ToHeader toHeader = (ToHeader) (message.getHeader(ToHeader.NAME));
  946. Address address = toHeader.getAddress();
  947. if (address.getURI().isSipURI()) {
  948. SipURI toURI = (SipURI) address.getURI();
  949. messageListener.sipMeetingManager.notifyObserversIMReceived(
  950. new String(message.getRawContent())
  951. + " has not been delivered successfully",
  952. toURI.getUser() + "@" + toURI.getHost());
  953. }
  954. SipURI toUri = ((SipURI)((ToHeader) message.getHeader(ToHeader.NAME)).getAddress().getURI());
  955.                 String id = toUri.getUser() + "@" + toUri.getHost();
  956.                 this.callManager.removeIMCall(id);
  957.                 
  958. }
  959. /**
  960.  * Process the timed out REGISTER
  961.  * @param message - the timedout request 
  962.  */
  963. public void processTimedOutRegister(Request register) {
  964. messageListener.sipMeetingManager.setRegisterStatus(
  965. RegisterStatus.NOT_REGISTERED);
  966. }
  967. /**
  968.  * Process the timed out REGISTER
  969.  * @param message - the timedout request 
  970.  */
  971. public void processTimedOutInvite(Request invite) {
  972. messageListener.sipMeetingManager.setRegisterStatus(
  973. AudioCall.NOT_IN_A_CALL);
  974. }
  975. /**
  976.  * Process the Timeout received request
  977.  * @param transaction - the transaction associated with the request
  978.  * @param timeout - the timeout request
  979.  */
  980. public void processTimeout(Transaction transaction, Request timeout) {
  981. }
  982. }