PageRenderTime 46ms CodeModel.GetById 20ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 457 lines | 309 code | 110 blank | 38 comment | 30 complexity | 51c74fe4a93bc0d4b14a48656e69b6a3 MD5 | raw file
  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
 23package org.mobicents.protocols.mgcp.stack.test.concurrency;
 24
 25import jain.protocol.ip.mgcp.CreateProviderException;
 26import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
 27import jain.protocol.ip.mgcp.JainMgcpEvent;
 28import jain.protocol.ip.mgcp.JainMgcpListener;
 29import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
 30import jain.protocol.ip.mgcp.message.Constants;
 31import jain.protocol.ip.mgcp.message.CreateConnection;
 32import jain.protocol.ip.mgcp.message.CreateConnectionResponse;
 33import jain.protocol.ip.mgcp.message.DeleteConnection;
 34import jain.protocol.ip.mgcp.message.NotificationRequest;
 35import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
 36import jain.protocol.ip.mgcp.message.parms.ConnectionDescriptor;
 37import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
 38import jain.protocol.ip.mgcp.message.parms.ConnectionMode;
 39import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
 40import jain.protocol.ip.mgcp.message.parms.EventName;
 41import jain.protocol.ip.mgcp.message.parms.RequestedAction;
 42import jain.protocol.ip.mgcp.message.parms.RequestedEvent;
 43import jain.protocol.ip.mgcp.pkg.MgcpEvent;
 44import jain.protocol.ip.mgcp.pkg.PackageName;
 45
 46import java.net.InetAddress;
 47import java.net.UnknownHostException;
 48import java.util.Timer;
 49import java.util.TimerTask;
 50import java.util.TooManyListenersException;
 51import java.util.concurrent.atomic.AtomicInteger;
 52
 53import org.apache.log4j.Logger;
 54import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
 55import org.mobicents.protocols.mgcp.stack.JainMgcpStackImpl;
 56import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
 57import org.mobicents.protocols.mgcp.stack.test.TestHarness;
 58
 59public class CA extends TestHarness implements JainMgcpExtendedListener {
 60
 61	private static Logger logger = Logger.getLogger(CA.class);
 62
 63	protected static final String CLIENT_ADDRESS = "127.0.0.1";
 64
 65	protected static final String SERVER_ADDRESS = "127.0.0.1";
 66
 67	protected static final int CA_PORT = 2427;
 68
 69	protected static final int MGW_PORT = 2727;
 70	static int NDIALOGS = 50000;
 71
 72	static int MAXCONCURRENTCRCX = 15;
 73
 74	// a ramp-up period is required for performance testing.
 75	int deleteCount = -100;
 76
 77	protected InetAddress caIPAddress = null;
 78	protected InetAddress mgIPAddress = null;
 79
 80	protected JainMgcpStackImpl caStack = null;
 81
 82	private int ENDPOINT_ID = 1;
 83
 84	AtomicInteger nbConcurrentInvite = new AtomicInteger(0);
 85
 86	private JainMgcpStackProviderImpl caProvider;
 87
 88	long start = 0l;
 89
 90	private static Timer timer;
 91
 92	static {
 93		timer = new Timer();
 94	}
 95
 96	// //////////////////////////////////
 97	// //// Listeners Method start /////
 98	// /////////////////////////////////
 99
100	public void transactionEnded(int handle) {
101		// TODO Auto-generated method stub
102
103	}
104
105	public void transactionRxTimedOut(JainMgcpCommandEvent command) {
106		System.out.println("Transaction Request Time out");
107		fail("Unexpected event: Rx TimeoutEvent ");
108	}
109
110	public void transactionTxTimedOut(JainMgcpCommandEvent command) {
111		System.out.println("Transaction Time out \n"+ command);
112		fail("Unexpected event: TimeoutEvent ");
113
114	}
115
116	public void processMgcpCommandEvent(JainMgcpCommandEvent jainmgcpcommandevent) {
117		// TODO Auto-generated method stub
118
119	}
120
121	public void processMgcpResponseEvent(JainMgcpResponseEvent jainmgcpresponseevent) {
122		Appdata appdatad = (Appdata) jainmgcpresponseevent.getSource();
123
124		EndpointIdentifier endpointID = new EndpointIdentifier(appdatad.getEndpointId(), SERVER_ADDRESS + ":"
125				+ MGW_PORT);
126
127		switch (jainmgcpresponseevent.getObjectIdentifier()) {
128		case Constants.RESP_CREATE_CONNECTION:
129			CreateConnectionResponse crcxResp = (CreateConnectionResponse) jainmgcpresponseevent;
130
131			if (logger.isDebugEnabled()) {
132				logger.debug("Received CRCX Response Tx Id = " + crcxResp.getTransactionHandle() + " Connection ID = "
133						+ crcxResp.getConnectionIdentifier());
134			}
135
136			appdatad.setReceivedCrcxResponse(true);
137
138			ConnectionIdentifier connectionIdentifier = crcxResp.getConnectionIdentifier();
139			appdatad.setConnectionIdentifier(connectionIdentifier);
140			// send RQNT
141
142			NotificationRequest notificationRequest = new NotificationRequest(appdatad, endpointID, caProvider
143					.getUniqueRequestIdentifier());
144			notificationRequest.setTransactionHandle(caProvider.getUniqueTransactionHandler());
145
146			RequestedAction[] actions = new RequestedAction[] { RequestedAction.NotifyImmediately };
147
148			RequestedEvent[] requestedEvents = {
149					new RequestedEvent(new EventName(PackageName.Announcement, MgcpEvent.oc, connectionIdentifier),
150							actions),
151					new RequestedEvent(new EventName(PackageName.Announcement, MgcpEvent.of, connectionIdentifier),
152							actions) };
153
154			notificationRequest.setRequestedEvents(requestedEvents);
155
156			if (logger.isDebugEnabled()) {
157				logger.debug("Sending RQNT Tx Id = " + notificationRequest.getTransactionHandle() + " Connection ID = "
158						+ connectionIdentifier);
159			}
160
161			caProvider.sendMgcpEvents(new JainMgcpEvent[] { notificationRequest });
162			break;
163		case Constants.RESP_NOTIFICATION_REQUEST:
164
165			if (logger.isDebugEnabled()) {
166				logger.debug("Received RQNT Response Tx Id = " + jainmgcpresponseevent.getTransactionHandle()
167						+ " Connection ID = " + appdatad.getConnectionIdentifier());
168			}
169
170			appdatad.setReceivedRqntResponse(true);
171
172			// Send DLCX
173
174			DeleteConnection deleteConnection = new DeleteConnection(appdatad, endpointID);
175			deleteConnection.setConnectionIdentifier(appdatad.getConnectionIdentifier());
176			deleteConnection.setTransactionHandle(caProvider.getUniqueTransactionHandler());
177
178			if (logger.isDebugEnabled()) {
179				logger.debug("Sending DLCX Tx Id = " + deleteConnection.getTransactionHandle() + " Connection ID = "
180						+ appdatad.getConnectionIdentifier());
181			}
182
183			caProvider.sendMgcpEvents(new JainMgcpEvent[] { deleteConnection });
184
185			break;
186		case Constants.RESP_DELETE_CONNECTION:
187
188			if (logger.isDebugEnabled()) {
189				logger.debug("Received DLCX Response Tx Id = " + jainmgcpresponseevent.getTransactionHandle()
190						+ " Connection ID = " + appdatad.getConnectionIdentifier());
191			}
192
193			appdatad.setReceivedDlcxResponse(true);
194
195			int ndialogs = nbConcurrentInvite.decrementAndGet();
196			// System.out.println(nbConcurrentInvite);
197			if (ndialogs > MAXCONCURRENTCRCX) {
198				System.out.println("Concurrent invites = " + ndialogs);
199			}
200			synchronized (this) {
201				if (ndialogs < MAXCONCURRENTCRCX / 2)
202					this.notify();
203			}
204
205			this.deleteCount++;
206
207			if (this.deleteCount == NDIALOGS) {
208				long current = System.currentTimeMillis();
209				float sec = (float) (current - start) / 1000f;
210
211				logger.info("Total time in sec = " + sec);
212				logger.info("Thrupt = " + (float) (NDIALOGS / sec));
213				System.out.println("Total time in sec = " + sec);
214				System.out.println("Thrupt = " + (float) (NDIALOGS / sec));
215			}
216			break;
217		default:
218			System.out.println("This RESPONSE is unexpected " + jainmgcpresponseevent);
219			logger.error("This RESPONSE is unexpected " + jainmgcpresponseevent);
220			break;
221
222		}
223
224	}
225
226	// //////////////////////////////////
227	// //// Listeners Method over //////
228	// /////////////////////////////////
229
230	public void createMgcpStack(JainMgcpListener listener) throws UnknownHostException, CreateProviderException,
231			TooManyListenersException {
232		caIPAddress = InetAddress.getByName(CLIENT_ADDRESS);
233		caStack = new JainMgcpStackImpl(caIPAddress, CA_PORT);
234		caProvider = (JainMgcpStackProviderImpl) caStack.createProvider();
235		caProvider.addJainMgcpListener(listener);
236	}
237
238	public void sendCreateConnection() {
239
240		try {
241
242			CallIdentifier callID = caProvider.getUniqueCallIdentifier();
243
244			if (ENDPOINT_ID == 11) {
245				ENDPOINT_ID = 1;
246			}
247			EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/enp-" + ENDPOINT_ID++,
248					SERVER_ADDRESS + ":" + MGW_PORT);
249
250			String endpointName = endpointID.getLocalEndpointName();
251
252			Appdata appdata = new Appdata(endpointName);
253
254			CreateConnection createConnection = new CreateConnection(appdata, callID, endpointID,
255					ConnectionMode.SendRecv);
256
257			String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4  127.0.0.1\r\n"
258					+ "s=mysession session\r\n" + "p=+46 8 52018010\r\n" + "c=IN IP4  127.0.0.1\r\n" + "t=0 0\r\n"
259					+ "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
260					+ "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
261
262			createConnection.setRemoteConnectionDescriptor(new ConnectionDescriptor(sdpData));
263
264			createConnection.setTransactionHandle(caProvider.getUniqueTransactionHandler());
265			nbConcurrentInvite.incrementAndGet();
266			if (logger.isDebugEnabled()) {
267				logger.debug("Sending CRCX Tx Id = " + createConnection.getTransactionHandle());
268			}
269			caProvider.sendMgcpEvents(new JainMgcpEvent[] { createConnection });
270
271		} catch (Exception e) {
272			// TODO Auto-generated catch block
273			e.printStackTrace();
274			fail("Unexpected Exception " + e.getMessage());
275		}
276	}
277
278	class Appdata {
279
280		protected TTask ttask;
281
282		protected long startTime;
283
284		protected long endTime;
285
286		private ConnectionIdentifier connectionIdentifier = null;
287		private boolean receivedCrcxResponse = false;
288		private boolean receivedRqntResponse = false;
289		private boolean receivedDlcxResponse = false;
290
291		String endpointId = null;
292
293		Appdata(String endpointId) {
294			this.endpointId = endpointId;
295			ttask = new TTask(this);
296			timer.schedule(ttask, 20 * 1000 * NDIALOGS / 100);
297			startTime = System.currentTimeMillis();
298		}
299
300		public void setReceivedCrcxResponse(boolean response) {
301			if (!receivedRqntResponse && !receivedDlcxResponse) {
302				this.receivedCrcxResponse = response;
303			} else {
304
305				System.out.println("receivedCrcxResponse after receivedRqntResponse or receivedDlcxResponse");
306				System.out.println("System falsed " + this.toString());
307				System.exit(0);
308			}
309		}
310
311		public void setReceivedRqntResponse(boolean response) {
312			if (receivedCrcxResponse && !receivedDlcxResponse) {
313				this.receivedRqntResponse = response;
314			} else {
315
316				System.out.println("receivedRqntResponse after receivedDlcxResponse or before receivedCrcxResponse");
317				System.out.println("System falsed " + this.toString());
318				System.exit(0);
319			}
320		}
321
322		public void setReceivedDlcxResponse(boolean response) {
323			if (receivedCrcxResponse && receivedRqntResponse) {
324				this.receivedDlcxResponse = response;
325			} else {
326
327				System.out.println("receivedDlcxResponse before receivedCrcxResponse or receivedDlcxResponse");
328				System.out.println("System falsed " + this.toString());
329				System.exit(0);
330			}
331		}
332
333		public TTask getTtask() {
334			return ttask;
335		}
336
337		public void setTtask(TTask ttask) {
338			this.ttask = ttask;
339		}
340
341		public String getEndpointId() {
342			return endpointId;
343		}
344
345		public void setEndpointId(String endpointId) {
346			this.endpointId = endpointId;
347		}
348
349		public boolean isReceivedCrcxResponse() {
350			return receivedCrcxResponse;
351		}
352
353		public boolean isReceivedRqntResponse() {
354			return receivedRqntResponse;
355		}
356
357		public boolean isReceivedDlcxResponse() {
358			return receivedDlcxResponse;
359		}
360
361		public void cancelTimer() {
362			this.ttask.cancel();
363			endTime = System.currentTimeMillis();
364		}
365
366		public String toString() {
367			return new StringBuffer("EndpointId = ").append(this.endpointId).append(" receivedCrcxResponse = ").append(
368					receivedCrcxResponse).append(" receivedRqntResponse= ").append(receivedRqntResponse).append(
369					" receivedDlcxResponse= ").append(receivedDlcxResponse).toString();
370
371		}
372
373		public ConnectionIdentifier getConnectionIdentifier() {
374			return connectionIdentifier;
375		}
376
377		public void setConnectionIdentifier(ConnectionIdentifier connectionIdentifier) {
378			this.connectionIdentifier = connectionIdentifier;
379		}
380	}
381
382	class TTask extends TimerTask {
383		Appdata appdata;
384
385		public TTask(Appdata appdata) {
386			this.appdata = appdata;
387		}
388
389		public void run() {
390			if (!this.appdata.isReceivedCrcxResponse() || !this.appdata.isReceivedDlcxResponse()
391					|| !this.appdata.isReceivedRqntResponse()) {
392				System.out.println("Appdata " + appdata.toString());
393				logger.info("Appdata " + appdata.toString());
394				System.exit(0);
395			} else {
396				this.appdata = null;
397			}
398		}
399	}
400
401	public static void main(String args[]) {
402
403		int noOfCalls = Integer.parseInt(args[0]);
404		int noOfConcurrentCalls = Integer.parseInt(args[1]);
405
406		System.out.println("Number calls to be completed = " + noOfCalls
407				+ " Number of concurrent calls to be maintained = " + noOfConcurrentCalls);
408
409		logger.info("Number calls to be completed = " + noOfCalls + " Number of concurrent calls to be maintained = "
410				+ noOfConcurrentCalls);
411
412		NDIALOGS = noOfCalls;
413		MAXCONCURRENTCRCX = noOfConcurrentCalls;
414
415		final CA ca = new CA();
416		try {
417			ca.createMgcpStack(ca);
418			ca.start = System.currentTimeMillis();
419
420			while (ca.deleteCount < NDIALOGS) {
421
422				while (ca.nbConcurrentInvite.intValue() >= MAXCONCURRENTCRCX) {
423					System.out.println("nbConcurrentInvite = " + ca.nbConcurrentInvite.intValue()
424							+ " Waiting for max CRCX count to go down!");
425
426					logger.info("nbConcurrentInvite = " + ca.nbConcurrentInvite.intValue()
427							+ " Waiting for max CRCX count to go down!");
428					synchronized (ca) {
429						try {
430							ca.wait();
431						} catch (Exception ex) {
432						}
433					}
434				}
435
436				if (ca.deleteCount == 0) {
437					ca.start = System.currentTimeMillis();
438				}
439
440//				try {
441//					Thread.sleep(2);
442//				} catch (InterruptedException e) {
443//				}
444
445				ca.sendCreateConnection();
446			}
447		} catch (UnknownHostException e) {
448			e.printStackTrace();
449		} catch (CreateProviderException e) {
450			e.printStackTrace();
451		} catch (TooManyListenersException e) {
452			e.printStackTrace();
453		}
454
455	}
456
457}