PageRenderTime 23ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/protocols/jain-mgcp/stack/src/test/java/org/mobicents/protocols/mgcp/stack/test/concurrency/MGW.java

http://mobicents.googlecode.com/
Java | 177 lines | 98 code | 47 blank | 32 comment | 3 complexity | 8fb756232881df1a805c1ae9cc49c08d MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.mgcp.stack.test.concurrency;
  23. import jain.protocol.ip.mgcp.CreateProviderException;
  24. import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
  25. import jain.protocol.ip.mgcp.JainMgcpEvent;
  26. import jain.protocol.ip.mgcp.JainMgcpListener;
  27. import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
  28. import jain.protocol.ip.mgcp.message.Constants;
  29. import jain.protocol.ip.mgcp.message.CreateConnectionResponse;
  30. import jain.protocol.ip.mgcp.message.DeleteConnectionResponse;
  31. import jain.protocol.ip.mgcp.message.NotificationRequestResponse;
  32. import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
  33. import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
  34. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  35. import java.net.InetAddress;
  36. import java.net.UnknownHostException;
  37. import java.util.TooManyListenersException;
  38. import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
  39. import org.mobicents.protocols.mgcp.stack.JainMgcpStackImpl;
  40. import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
  41. import org.mobicents.protocols.mgcp.stack.test.TestHarness;
  42. public class MGW extends TestHarness implements JainMgcpExtendedListener {
  43. protected static final String CLIENT_ADDRESS = "127.0.0.1";
  44. protected static final String SERVER_ADDRESS = "127.0.0.1";
  45. protected static final int CA_PORT = 2724;
  46. protected static final int MGW_PORT = 2727;
  47. static int NDIALOGS = 1000;
  48. private int terminationCount = 0;
  49. private int timedOut = 0;
  50. protected InetAddress mgwIPAddress = null;
  51. protected JainMgcpStackImpl mgwStack = null;
  52. private JainMgcpStackProviderImpl mgwProvider = null;
  53. long start;
  54. // //////////////////////////////////
  55. // //// Listeners Method start /////
  56. // /////////////////////////////////
  57. public void transactionEnded(int handle) {
  58. // TODO Auto-generated method stub
  59. }
  60. public void transactionRxTimedOut(JainMgcpCommandEvent command) {
  61. this.timedOut ++;
  62. System.out.println("Timed Out Transaction "+timedOut);
  63. }
  64. public void transactionTxTimedOut(JainMgcpCommandEvent command) {
  65. this.timedOut ++;
  66. System.out.println("Timed Out Transaction "+timedOut);
  67. }
  68. public void processMgcpCommandEvent(JainMgcpCommandEvent jainmgcpcommandevent) {
  69. switch (jainmgcpcommandevent.getObjectIdentifier()) {
  70. case Constants.CMD_CREATE_CONNECTION:
  71. String identifier = ((CallIdentifier) mgwProvider.getUniqueCallIdentifier()).toString();
  72. ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(identifier);
  73. CreateConnectionResponse response = new CreateConnectionResponse(jainmgcpcommandevent.getSource(),
  74. ReturnCode.Transaction_Executed_Normally, connectionIdentifier);
  75. response.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle());
  76. response.setSpecificEndpointIdentifier(jainmgcpcommandevent.getEndpointIdentifier());
  77. mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { response });
  78. break;
  79. case Constants.CMD_NOTIFICATION_REQUEST:
  80. NotificationRequestResponse ntrqResponse = new NotificationRequestResponse(
  81. jainmgcpcommandevent.getSource(), ReturnCode.Transaction_Executed_Normally);
  82. ntrqResponse.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle());
  83. mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { ntrqResponse });
  84. break;
  85. case Constants.CMD_DELETE_CONNECTION:
  86. DeleteConnectionResponse dlcxResponse = new DeleteConnectionResponse(jainmgcpcommandevent.getSource(),
  87. ReturnCode.Transaction_Executed_Normally);
  88. dlcxResponse.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle());
  89. mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { dlcxResponse });
  90. terminationCount++;
  91. if(terminationCount % 100 == 0) System.out.println("MGCP cycle termination count = " + terminationCount);
  92. // if(terminationCount == 10100){
  93. // stop();
  94. // }
  95. break;
  96. default:
  97. System.out.println("This COMMAND is unexpected " + jainmgcpcommandevent);
  98. break;
  99. }
  100. }
  101. public void processMgcpResponseEvent(JainMgcpResponseEvent jainmgcpresponseevent) {
  102. // TODO Auto-generated method stub
  103. }
  104. private void stop(){
  105. this.mgwStack.close();
  106. }
  107. // //////////////////////////////////
  108. // //// Listeners Method over //////
  109. // /////////////////////////////////
  110. public void createMgcpStack(JainMgcpListener listener) throws UnknownHostException, CreateProviderException,
  111. TooManyListenersException {
  112. mgwIPAddress = InetAddress.getByName(SERVER_ADDRESS);
  113. mgwStack = new JainMgcpStackImpl(mgwIPAddress, MGW_PORT);
  114. mgwProvider = (JainMgcpStackProviderImpl) mgwStack.createProvider();
  115. mgwProvider.addJainMgcpListener(listener);
  116. }
  117. public static void main(String args[]) {
  118. final MGW mgw = new MGW();
  119. try {
  120. mgw.createMgcpStack(mgw);
  121. mgw.start = System.currentTimeMillis();
  122. } catch (UnknownHostException e) {
  123. e.printStackTrace();
  124. } catch (CreateProviderException e) {
  125. e.printStackTrace();
  126. } catch (TooManyListenersException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. }