PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/unit/gov/nist/javax/sip/stack/timeoutontermineted/Shootist.java

https://github.com/mranga/jain-sip
Java | 301 lines | 185 code | 82 blank | 34 comment | 7 complexity | 06f00f9725098318ec49dcb1ab5556fc MD5 | raw file
  1. package test.unit.gov.nist.javax.sip.stack.timeoutontermineted;
  2. import java.util.ArrayList;
  3. import javax.sip.ClientTransaction;
  4. import javax.sip.Dialog;
  5. import javax.sip.DialogState;
  6. import javax.sip.DialogTerminatedEvent;
  7. import javax.sip.IOExceptionEvent;
  8. import javax.sip.ListeningPoint;
  9. import javax.sip.RequestEvent;
  10. import javax.sip.ResponseEvent;
  11. import javax.sip.SipListener;
  12. import javax.sip.SipProvider;
  13. import javax.sip.SipStack;
  14. import javax.sip.TransactionTerminatedEvent;
  15. import javax.sip.address.Address;
  16. import javax.sip.address.AddressFactory;
  17. import javax.sip.address.SipURI;
  18. import javax.sip.header.CSeqHeader;
  19. import javax.sip.header.CallIdHeader;
  20. import javax.sip.header.ContactHeader;
  21. import javax.sip.header.ContentTypeHeader;
  22. import javax.sip.header.FromHeader;
  23. import javax.sip.header.Header;
  24. import javax.sip.header.HeaderFactory;
  25. import javax.sip.header.MaxForwardsHeader;
  26. import javax.sip.header.ToHeader;
  27. import javax.sip.header.ViaHeader;
  28. import javax.sip.message.MessageFactory;
  29. import javax.sip.message.Request;
  30. import javax.sip.message.Response;
  31. import junit.framework.TestCase;
  32. import org.apache.log4j.Logger;
  33. /**
  34. * This class is a UAC template. Shootist is the guy that shoots and shootme is
  35. * the guy that gets shot.
  36. *
  37. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  38. */
  39. public class Shootist implements SipListener {
  40. private ContactHeader contactHeader;
  41. private ClientTransaction inviteTid;
  42. private Dialog inviteDialog;
  43. private SipProvider sipProvider;
  44. private final String host = "127.0.0.1";
  45. private final int port;
  46. private final String peerHost = "127.0.0.1";
  47. private final int peerPort;
  48. private ListeningPoint listeningPoint;
  49. private static String unexpectedException = "Unexpected exception ";
  50. private static Logger logger = Logger.getLogger(Shootist.class);
  51. private final SipStack sipStack;
  52. private static HeaderFactory headerFactory;
  53. private static MessageFactory messageFactory;
  54. private static AddressFactory addressFactory;
  55. private static final String transport = "udp";
  56. private boolean seen_txTerm, seen_txTimeout, seen_dte;
  57. public Shootist(int myPort, int proxyPort) {
  58. this.port = myPort;
  59. SipObjects sipObjects = new SipObjects(myPort, "shootist", "on");
  60. addressFactory = sipObjects.addressFactory;
  61. messageFactory = sipObjects.messageFactory;
  62. headerFactory = sipObjects.headerFactory;
  63. this.sipStack = sipObjects.sipStack;
  64. this.peerPort = proxyPort;
  65. }
  66. public void processRequest(RequestEvent requestReceivedEvent) {
  67. }
  68. public synchronized void processResponse(ResponseEvent responseReceivedEvent) {
  69. logger.info("Got a response");
  70. Response response = responseReceivedEvent.getResponse();
  71. ClientTransaction tid = responseReceivedEvent.getClientTransaction();
  72. CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
  73. logger.info("Response received : Status Code = " + response.getStatusCode() + " " + cseq);
  74. logger.info("Response = " + response + " class=" + response.getClass());
  75. Dialog dialog = responseReceivedEvent.getDialog();
  76. TestCase.assertNotNull(dialog);
  77. if (tid != null)
  78. logger.info("transaction state is " + tid.getState());
  79. else
  80. logger.info("transaction = " + tid);
  81. logger.info("Dialog = " + dialog);
  82. logger.info("Dialog state is " + dialog.getState());
  83. try {
  84. if (response.getStatusCode() == Response.OK) {
  85. if (cseq.getMethod().equals(Request.INVITE)) {
  86. TestCase.assertEquals(DialogState.CONFIRMED, dialog.getState());
  87. } else {
  88. logger.info("Response method = " + cseq.getMethod());
  89. }
  90. }
  91. } catch (Throwable ex) {
  92. ex.printStackTrace();
  93. // System.exit(0);
  94. }
  95. }
  96. public SipProvider createSipProvider() {
  97. try {
  98. listeningPoint = sipStack.createListeningPoint(host, port, "udp");
  99. logger.info("listening point = " + host + " port = " + port);
  100. logger.info("listening point = " + listeningPoint);
  101. sipProvider = sipStack.createSipProvider(listeningPoint);
  102. return sipProvider;
  103. } catch (Exception ex) {
  104. logger.error(unexpectedException, ex);
  105. ex.printStackTrace();
  106. TestCase.fail(unexpectedException);
  107. return null;
  108. }
  109. }
  110. public void checkState() {
  111. TestCase.assertTrue("INVITE transaction should temrinate.", seen_txTerm);
  112. TestCase.assertFalse("INVITE transaction should not timeout.", seen_txTimeout);
  113. TestCase.assertTrue("INVITE Dialog should die.", seen_dte);
  114. }
  115. public void sendInvite() {
  116. try {
  117. String fromName = "BigGuy";
  118. String fromSipAddress = "here.com";
  119. String fromDisplayName = "The Master Blaster";
  120. String toSipAddress = "there.com";
  121. String toUser = "LittleGuy";
  122. String toDisplayName = "The Little Blister";
  123. // create >From Header
  124. SipURI fromAddress = addressFactory.createSipURI(fromName, fromSipAddress);
  125. Address fromNameAddress = addressFactory.createAddress(fromAddress);
  126. fromNameAddress.setDisplayName(fromDisplayName);
  127. FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, "12345");
  128. // create To Header
  129. SipURI toAddress = addressFactory.createSipURI(toUser, toSipAddress);
  130. Address toNameAddress = addressFactory.createAddress(toAddress);
  131. toNameAddress.setDisplayName(toDisplayName);
  132. ToHeader toHeader = headerFactory.createToHeader(toNameAddress, null);
  133. // create Request URI
  134. String peerHostPort = peerHost + ":" + peerPort;
  135. SipURI requestURI = addressFactory.createSipURI(toUser, peerHostPort);
  136. // Create ViaHeaders
  137. ArrayList viaHeaders = new ArrayList();
  138. ViaHeader viaHeader = headerFactory.createViaHeader(host, sipProvider.getListeningPoint(transport).getPort(), transport, null);
  139. // add via headers
  140. viaHeaders.add(viaHeader);
  141. //SipURI sipuri = addressFactory.createSipURI(null, host);
  142. //sipuri.setPort(peerPort);
  143. //sipuri.setLrParam();
  144. //
  145. //RouteHeader routeHeader = headerFactory.createRouteHeader(addressFactory.createAddress(sipuri));
  146. // Create ContentTypeHeader
  147. ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("application", "sdp");
  148. // Create a new CallId header
  149. CallIdHeader callIdHeader = sipProvider.getNewCallId();
  150. // JvB: Make sure that the implementation matches the messagefactory
  151. callIdHeader = headerFactory.createCallIdHeader(callIdHeader.getCallId());
  152. // Create a new Cseq header
  153. CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L, Request.INVITE);
  154. // Create a new MaxForwardsHeader
  155. MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
  156. // Create the request.
  157. Request request = messageFactory.createRequest(requestURI, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
  158. // Create contact headers
  159. SipURI contactUrl = addressFactory.createSipURI(fromName, host);
  160. contactUrl.setPort(listeningPoint.getPort());
  161. // Create the contact name address.
  162. SipURI contactURI = addressFactory.createSipURI(fromName, host);
  163. contactURI.setPort(sipProvider.getListeningPoint(transport).getPort());
  164. contactURI.setTransportParam(transport);
  165. Address contactAddress = addressFactory.createAddress(contactURI);
  166. // Add the contact address.
  167. contactAddress.setDisplayName(fromName);
  168. contactHeader = headerFactory.createContactHeader(contactAddress);
  169. request.addHeader(contactHeader);
  170. // Dont use the Outbound Proxy. Use Lr instead.
  171. //request.setHeader(routeHeader);
  172. // Add the extension header.
  173. Header extensionHeader = headerFactory.createHeader("My-Header", "my header value");
  174. request.addHeader(extensionHeader);
  175. String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4 129.6.55.78\r\n" + "s=mysession session\r\n" + "p=+46 8 52018010\r\n" + "c=IN IP4 129.6.55.78\r\n"
  176. + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n" + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
  177. byte[] contents = sdpData.getBytes();
  178. request.setContent(contents, contentTypeHeader);
  179. extensionHeader = headerFactory.createHeader("My-Other-Header", "my new header value ");
  180. request.addHeader(extensionHeader);
  181. Header callInfoHeader = headerFactory.createHeader("Call-Info", "<http://www.antd.nist.gov>");
  182. request.addHeader(callInfoHeader);
  183. // Create the client transaction.
  184. inviteTid = sipProvider.getNewClientTransaction(request);
  185. inviteDialog = inviteTid.getDialog();
  186. TestCase.assertTrue("Initial dialog state should be null", inviteDialog.getState() == null);
  187. // send the request out.
  188. inviteTid.sendRequest();
  189. } catch (Exception ex) {
  190. logger.error(unexpectedException, ex);
  191. TestCase.fail(unexpectedException);
  192. }
  193. }
  194. public void processIOException(IOExceptionEvent exceptionEvent) {
  195. logger.error("IOException happened for " + exceptionEvent.getHost() + " port = " + exceptionEvent.getPort());
  196. TestCase.fail("Unexpected exception");
  197. }
  198. public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
  199. logger.info("[c]Transaction terminated event recieved: "+transactionTerminatedEvent.getClientTransaction());
  200. System.err.println("[c]Transaction terminated event recieved: "+transactionTerminatedEvent.getClientTransaction());
  201. //if (transactionTerminatedEvent.getClientTransaction() == inviteTid)
  202. seen_txTerm = true;
  203. }
  204. public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
  205. logger.info("[c]Transaction timedout event recieved: "+timeoutEvent.getClientTransaction());
  206. System.err.println("[c]Transaction timedout event recieved: "+timeoutEvent.getClientTransaction());
  207. //if (timeoutEvent.getClientTransaction() == inviteTid)
  208. seen_txTimeout = true;
  209. }
  210. public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
  211. logger.info("[c]Dialog terminated event recieved: "+dialogTerminatedEvent.getDialog());
  212. System.err.println("[c]Dialog terminated event recieved: "+dialogTerminatedEvent.getDialog());
  213. //if (dialogTerminatedEvent.getDialog() == inviteDialog)
  214. seen_dte = true;
  215. }
  216. public void stop() {
  217. this.sipStack.stop();
  218. }
  219. }