001: package test.load.concurrency;
002:
003: import javax.sip.ClientTransaction;
004: import javax.sip.Dialog;
005: import javax.sip.DialogTerminatedEvent;
006: import javax.sip.IOExceptionEvent;
007: import javax.sip.ListeningPoint;
008: import javax.sip.RequestEvent;
009: import javax.sip.ResponseEvent;
010: import javax.sip.ServerTransaction;
011: import javax.sip.SipException;
012: import javax.sip.SipListener;
013: import javax.sip.SipProvider;
014: import javax.sip.SipStack;
015: import javax.sip.Transaction;
016: import javax.sip.TransactionAlreadyExistsException;
017: import javax.sip.TransactionTerminatedEvent;
018: import javax.sip.address.Address;
019: import javax.sip.address.AddressFactory;
020: import javax.sip.header.CSeqHeader;
021: import javax.sip.header.ContactHeader;
022: import javax.sip.header.ContentTypeHeader;
023: import javax.sip.header.HeaderFactory;
024: import javax.sip.header.ToHeader;
025: import javax.sip.message.MessageFactory;
026: import javax.sip.message.Request;
027: import javax.sip.message.Response;
028: import junit.framework.TestCase;
029:
030: /**
031: * This class is a UAC template. Shootist is the guy that shoots and shootme
032: * is the guy that gets shot.
033: *
034: *@author M. Ranganathan
035: */
036:
037: public class Shootme extends TestCase implements SipListener {
038:
039: static AddressFactory addressFactory;
040: static MessageFactory messageFactory;
041: static HeaderFactory headerFactory;
042: static SipStack sipStack;
043:
044: static String transport;
045:
046: protected static final String usageString = "java "
047: + "examples.shootist.Shootist \n"
048: + ">>>> is your class path set to the root?";
049:
050: private int terminationCount;
051: private int droppedCall;
052:
053: public void processRequest(RequestEvent requestEvent) {
054: Request request = requestEvent.getRequest();
055: ServerTransaction serverTransactionId = requestEvent
056: .getServerTransaction();
057:
058: if (request.getMethod().equals(Request.INVITE)) {
059: processInvite(requestEvent, serverTransactionId);
060: } else if (request.getMethod().equals(Request.ACK)) {
061: processAck(requestEvent, serverTransactionId);
062: } else if (request.getMethod().equals(Request.BYE)) {
063: processBye(requestEvent, serverTransactionId);
064: }
065:
066: }
067:
068: /** Process the ACK request. Send the bye and complete the call flow.
069: */
070: public void processAck(RequestEvent requestEvent,
071: ServerTransaction serverTransaction) {
072: SipProvider sipProvider = (SipProvider) requestEvent
073: .getSource();
074: try {
075: // System.out.println("shootme: got an ACK " );
076: // maybe a late arriving ack.
077: if (serverTransaction == null)
078: return;
079: Dialog dialog = serverTransaction.getDialog();
080: dialog = serverTransaction.getDialog();
081: Request byeRequest = dialog.createRequest(Request.BYE);
082: ClientTransaction tr = sipProvider
083: .getNewClientTransaction(byeRequest);
084: dialog.sendRequest(tr);
085: } catch (Exception ex) {
086: ex.printStackTrace();
087: fail("unexpected exception");
088: }
089: }
090:
091: /** Process the invite request.
092: */
093: public void processInvite(RequestEvent requestEvent,
094: ServerTransaction serverTransaction) {
095: SipProvider sipProvider = (SipProvider) requestEvent
096: .getSource();
097: Request request = requestEvent.getRequest();
098: try {
099: Response response = messageFactory.createResponse(180,
100: request);
101: ToHeader toHeader = (ToHeader) response
102: .getHeader(ToHeader.NAME);
103: String toTag = new Integer((int) (Math.random() * 1000))
104: .toString();
105: toHeader.setTag(toTag); // Application is supposed to set.
106: //System.out.println("toTag = " + toTag);
107:
108: Address address = addressFactory
109: .createAddress("Shootme <sip:127.0.0.1:5070;transport="
110: + transport + ">");
111:
112: ContactHeader contactHeader = headerFactory
113: .createContactHeader(address);
114: response.addHeader(contactHeader);
115: ServerTransaction st = requestEvent.getServerTransaction();
116:
117: if (st == null) {
118: try {
119: st = sipProvider.getNewServerTransaction(request);
120: } catch (TransactionAlreadyExistsException ex) {
121: System.out.println("Tx aready exists -- ignoring ");
122: return;
123: }
124:
125: }
126: byte[] content = request.getRawContent();
127: if (content != null) {
128: ContentTypeHeader contentTypeHeader = headerFactory
129: .createContentTypeHeader("application", "sdp");
130: // System.out.println("response = " + response);
131: response.setContent(content, contentTypeHeader);
132: }
133: Dialog dialog = st.getDialog();
134: //System.out.println("dialog = " + dialog);
135: st.sendResponse(response);
136: response = messageFactory.createResponse(200, request);
137: toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
138: toHeader.setTag(toTag); // Application is supposed to set.
139: response.addHeader(contactHeader);
140: st.sendResponse(response);
141: } catch (Exception ex) {
142: ex.printStackTrace();
143: fail("unexpected exception");
144: }
145: }
146:
147: /** Process the bye request.
148: */
149: public void processBye(RequestEvent requestEvent,
150: ServerTransaction serverTransactionId) {
151: SipProvider sipProvider = (SipProvider) requestEvent
152: .getSource();
153: System.out.println("sip provider = " + sipProvider);
154:
155: Request request = requestEvent.getRequest();
156: try {
157: Response response = messageFactory.createResponse(200,
158: request, null, null);
159: serverTransactionId.sendResponse(response);
160:
161: } catch (Exception ex) {
162: ex.printStackTrace();
163: fail("unexpected exception");
164:
165: }
166: }
167:
168: public void processResponse(ResponseEvent responseReceivedEvent) {
169: Response response = (Response) responseReceivedEvent
170: .getResponse();
171: Transaction tid = responseReceivedEvent.getClientTransaction();
172: if (tid == null) {
173: System.out.println("Stray response -- dropping!");
174: return;
175: }
176:
177: try {
178: if (response.getStatusCode() == Response.OK
179: && ((CSeqHeader) response
180: .getHeader(CSeqHeader.NAME)).getMethod()
181: .equals(Request.INVITE)) {
182: Dialog dialog = tid.getDialog();
183: // Save the tags for the dialog here.
184: Request request = tid.getRequest();
185: dialog.sendAck(request);
186: }
187: Dialog dialog = tid.getDialog();
188: assertTrue("Dialog should not be null", dialog != null);
189: //System.out.println("dialog = " + dialog);
190: } catch (SipException ex) {
191: ex.printStackTrace();
192: System.exit(0);
193: }
194:
195: }
196:
197: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
198: Transaction transaction;
199: Request request = null;
200: if (timeoutEvent.isServerTransaction()) {
201: transaction = timeoutEvent.getServerTransaction();
202: } else {
203: transaction = timeoutEvent.getClientTransaction();
204: request = ((ClientTransaction) transaction).getRequest();
205: }
206: /*
207: * System.out.println("request = " + request); System.out.println("state = " +
208: * transaction.getState()); System.out.println("dialog = " +
209: * transaction.getDialog()); System.out.println( "dialogState = " +
210: * transaction.getDialog().getState()); System.out.println("Transaction
211: * Time out"); System.out.println("Transaction " + transaction);
212: * System.out.println("request " + transaction.getRequest());
213: */
214: this .droppedCall++;
215: System.out.println("droped call " + this .droppedCall);
216: }
217:
218: public SipProvider createSipProvider() throws Exception {
219: ListeningPoint listeningPoint = sipStack.createListeningPoint(
220: "127.0.0.1", 5070, transport);
221: SipProvider sipProvider = sipStack
222: .createSipProvider(listeningPoint);
223: return sipProvider;
224: }
225:
226: public void processIOException(IOExceptionEvent exceptionEvent) {
227: System.out
228: .println("IOException occured while retransmitting requests:"
229: + exceptionEvent);
230: }
231:
232: public void processTransactionTerminated(
233: TransactionTerminatedEvent transactionTerminatedEvent) {
234: //System.out.println("Transaction Terminated event: " + transactionTerminatedEvent );
235: }
236:
237: public void processDialogTerminated(
238: DialogTerminatedEvent dialogTerminatedEvent) {
239: //System.out.println("Dialog Terminated event: " + dialogTerminatedEvent);
240: this .terminationCount++;
241:
242: if (terminationCount % 100 == 0)
243: System.out.println("DialogTermination count = "
244: + this .terminationCount);
245: }
246:
247: public static void main(String args[]) throws Exception {
248: ProtocolObjects.init("shootme", true);
249: Shootme.addressFactory = ProtocolObjects.addressFactory;
250: Shootme.messageFactory = ProtocolObjects.messageFactory;
251: Shootme.headerFactory = ProtocolObjects.headerFactory;
252: Shootme.sipStack = ProtocolObjects.sipStack;
253: Shootme.transport = ProtocolObjects.transport;
254: Shootme shootme = new Shootme();
255: SipProvider sipProvider = shootme.createSipProvider();
256: sipProvider.addSipListener(shootme);
257: }
258:
259: }
|