001: package test.load.leakcheck.busy;
002:
003: import javax.sip.*;
004: import javax.sip.address.*;
005: import javax.sip.header.*;
006: import javax.sip.message.*;
007:
008: import java.util.*;
009:
010: /**
011: * This class is a UAC template.
012: * @author M. Ranganathan
013: */
014:
015: public class Shootist implements SipListener {
016:
017: private static SipProvider tcpProvider;
018:
019: private static SipProvider udpProvider;
020:
021: private static AddressFactory addressFactory;
022:
023: private static MessageFactory messageFactory;
024:
025: private static HeaderFactory headerFactory;
026:
027: private static SipStack sipStack;
028:
029: private int reInviteCount;
030:
031: private ContactHeader contactHeader;
032:
033: private ListeningPoint tcpListeningPoint;
034:
035: private ListeningPoint udpListeningPoint;
036:
037: private int counter;
038:
039: protected static final String usageString = "java "
040: + "examples.shootist.Shootist \n"
041: + ">>>> is your class path set to the root?";
042:
043: private static void usage() {
044: System.out.println(usageString);
045: System.exit(0);
046:
047: }
048:
049: private void shutDown() {
050: try {
051: try {
052: Thread.sleep(2000);
053: } catch (InterruptedException e) {
054: }
055: System.out.println("nulling reference");
056: sipStack.deleteListeningPoint(tcpListeningPoint);
057: sipStack.deleteListeningPoint(udpListeningPoint);
058: // This will close down the stack and exit all threads
059: tcpProvider.removeSipListener(this );
060: udpProvider.removeSipListener(this );
061: while (true) {
062: try {
063: sipStack.deleteSipProvider(udpProvider);
064: sipStack.deleteSipProvider(tcpProvider);
065: break;
066: } catch (ObjectInUseException ex) {
067: try {
068: Thread.sleep(2000);
069: } catch (InterruptedException e) {
070: continue;
071: }
072: }
073: }
074: sipStack = null;
075: tcpProvider = null;
076: udpProvider = null;
077: this .contactHeader = null;
078: addressFactory = null;
079: headerFactory = null;
080: messageFactory = null;
081: this .udpListeningPoint = null;
082: this .tcpListeningPoint = null;
083: this .reInviteCount = 0;
084: System.gc();
085: //Redo this from the start.
086: if (counter < 10)
087: this .init();
088: else
089: counter++;
090: } catch (Exception ex) {
091: ex.printStackTrace();
092: }
093: }
094:
095: public void processRequest(RequestEvent requestReceivedEvent) {
096: Request request = requestReceivedEvent.getRequest();
097: ServerTransaction serverTransactionId = requestReceivedEvent
098: .getServerTransaction();
099:
100: System.out.println("\n\nRequest " + request.getMethod()
101: + " received at " + sipStack.getStackName()
102: + " with server transaction id " + serverTransactionId);
103:
104: // We are the UAC so the only request we get is the BYE.
105: if (request.getMethod().equals(Request.BYE))
106: processBye(request, serverTransactionId);
107:
108: }
109:
110: public void processBye(Request request,
111: ServerTransaction serverTransactionId) {
112: try {
113: System.out.println("shootist: got a bye .");
114: if (serverTransactionId == null) {
115: System.out.println("shootist: null TID.");
116: return;
117: }
118: Dialog dialog = serverTransactionId.getDialog();
119: System.out.println("Dialog State = " + dialog.getState());
120: Response response = messageFactory.createResponse(200,
121: request);
122: serverTransactionId.sendResponse(response);
123: System.out.println("shootist: Sending OK.");
124: System.out.println("Dialog State = " + dialog.getState());
125:
126: this .shutDown();
127:
128: } catch (Exception ex) {
129: ex.printStackTrace();
130: System.exit(0);
131:
132: }
133: }
134:
135: public void processResponse(ResponseEvent responseReceivedEvent) {
136: System.out.println("Got a response");
137: Response response = (Response) responseReceivedEvent
138: .getResponse();
139: Transaction tid = responseReceivedEvent.getClientTransaction();
140:
141: System.out
142: .println("Response received with client transaction id "
143: + tid + ":\n" + response.getStatusCode());
144: if (tid == null) {
145: System.out.println("Stray response -- dropping ");
146: return;
147: }
148: System.out.println("transaction state is " + tid.getState());
149: System.out.println("Dialog = " + tid.getDialog());
150: System.out.println("Dialog State is "
151: + tid.getDialog().getState());
152:
153: try {
154: if (response.getStatusCode() == Response.OK
155: && ((CSeqHeader) response
156: .getHeader(CSeqHeader.NAME)).getMethod()
157: .equals(Request.INVITE)) {
158: // Request cancel = inviteTid.createCancel();
159: // ClientTransaction ct =
160: // sipProvider.getNewClientTransaction(cancel);
161: // ct.sendRequest();
162: Dialog dialog = tid.getDialog();
163: CSeqHeader cseq = (CSeqHeader) response
164: .getHeader(CSeqHeader.NAME);
165: Request ackRequest = dialog.createAck(cseq
166: .getSeqNumber());
167: System.out.println("Sending ACK");
168: dialog.sendAck(ackRequest);
169:
170: // Send a Re INVITE but this time force it
171: // to use UDP as the transport. Else, it will
172: // Use whatever transport was used to create
173: // the dialog.
174: if (reInviteCount == 0) {
175: Request inviteRequest = dialog
176: .createRequest(Request.INVITE);
177: ((SipURI) inviteRequest.getRequestURI())
178: .removeParameter("transport");
179: ((ViaHeader) inviteRequest
180: .getHeader(ViaHeader.NAME))
181: .setTransport("udp");
182: inviteRequest.addHeader(contactHeader);
183: try {
184: Thread.sleep(100);
185: } catch (Exception ex) {
186: }
187: ClientTransaction ct = udpProvider
188: .getNewClientTransaction(inviteRequest);
189: dialog.sendRequest(ct);
190: reInviteCount++;
191: }
192:
193: }
194: } catch (Exception ex) {
195: ex.printStackTrace();
196: System.exit(0);
197: }
198:
199: }
200:
201: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
202:
203: System.out.println("Transaction Time out");
204: }
205:
206: public void init() {
207: SipFactory sipFactory = null;
208: sipStack = null;
209: sipFactory = SipFactory.getInstance();
210: sipFactory.setPathName("gov.nist");
211: Properties properties = new Properties();
212: // If you want to try TCP transport change the following to
213: String transport = "udp";
214: String peerHostPort = "127.0.0.1:5070";
215: properties.setProperty("javax.sip.IP_ADDRESS", "127.0.0.1");
216: properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort
217: + "/" + transport);
218: properties.setProperty("javax.sip.STACK_NAME", "shootist");
219: properties.setProperty("javax.sip.RETRANSMISSION_FILTER", "on");
220:
221: // The following properties are specific to nist-sip
222: // and are not necessarily part of any other jain-sip
223: // implementation.
224: // You can set a max message size for tcp transport to
225: // guard against denial of service attack.
226: properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
227: "1048576");
228: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
229: "shootistdebug.txt");
230: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
231: "shootistlog.txt");
232:
233: // Drop the client connection after we are done with the transaction.
234: properties.setProperty(
235: "gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS", "false");
236: // Set to 0 in your production code for max speed.
237: // You need 16 for logging traces. 32 for debug + traces.
238: // Your code will limp at 32 but it is best for debugging.
239: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "0");
240:
241: try {
242: // Create SipStack object
243: sipStack = sipFactory.createSipStack(properties);
244: System.out.println("createSipStack " + sipStack);
245: } catch (PeerUnavailableException e) {
246: // could not find
247: // gov.nist.jain.protocol.ip.sip.SipStackImpl
248: // in the classpath
249: e.printStackTrace();
250: System.err.println(e.getMessage());
251: System.exit(0);
252: }
253:
254: try {
255: headerFactory = sipFactory.createHeaderFactory();
256: addressFactory = sipFactory.createAddressFactory();
257: messageFactory = sipFactory.createMessageFactory();
258: udpListeningPoint = sipStack.createListeningPoint(sipStack
259: .getIPAddress(), 5060, "udp");
260: udpProvider = sipStack.createSipProvider(udpListeningPoint);
261: Shootist listener = this ;
262: udpProvider.addSipListener(listener);
263:
264: tcpListeningPoint = sipStack.createListeningPoint(sipStack
265: .getIPAddress(), 5060, "tcp");
266: tcpProvider = sipStack.createSipProvider(tcpListeningPoint);
267: tcpProvider.addSipListener(listener);
268:
269: SipProvider sipProvider = transport.equalsIgnoreCase("udp") ? udpProvider
270: : tcpProvider;
271:
272: String fromName = "BigGuy";
273: String fromSipAddress = "here.com";
274: String fromDisplayName = "The Master Blaster";
275:
276: String toSipAddress = "there.com";
277: String toUser = "LittleGuy";
278: String toDisplayName = "The Little Blister";
279:
280: // send the request out.
281: for (int i = 0; i < 100; i++) {
282: // create >From Header
283: SipURI fromAddress = addressFactory.createSipURI(
284: fromName, fromSipAddress);
285:
286: Address fromNameAddress = addressFactory
287: .createAddress(fromAddress);
288: fromNameAddress.setDisplayName(fromDisplayName);
289: FromHeader fromHeader = headerFactory.createFromHeader(
290: fromNameAddress, "12345" + i);
291:
292: // create To Header
293: SipURI toAddress = addressFactory.createSipURI(toUser,
294: toSipAddress);
295: Address toNameAddress = addressFactory
296: .createAddress(toAddress);
297: toNameAddress.setDisplayName(toDisplayName);
298: ToHeader toHeader = headerFactory.createToHeader(
299: toNameAddress, null);
300:
301: // create Request URI
302: SipURI requestURI = addressFactory.createSipURI(toUser,
303: peerHostPort);
304:
305: // Create ViaHeaders
306:
307: ArrayList viaHeaders = new ArrayList();
308: int port = sipProvider.getListeningPoint(transport)
309: .getPort();
310: ViaHeader viaHeader = headerFactory.createViaHeader(
311: sipStack.getIPAddress(), port, transport, null);
312:
313: // add via headers
314: viaHeaders.add(viaHeader);
315:
316: // Create ContentTypeHeader
317: ContentTypeHeader contentTypeHeader = headerFactory
318: .createContentTypeHeader("application", "sdp");
319:
320: // Create a new CallId header
321: CallIdHeader callIdHeader = sipProvider.getNewCallId();
322:
323: // Create a new Cseq header
324: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(
325: 1L, Request.INVITE);
326:
327: // Create a new MaxForwardsHeader
328: MaxForwardsHeader maxForwards = headerFactory
329: .createMaxForwardsHeader(70);
330:
331: // Create the request.
332: Request request = messageFactory.createRequest(
333: requestURI, Request.INVITE, callIdHeader,
334: cSeqHeader, fromHeader, toHeader, viaHeaders,
335: maxForwards);
336: // Create contact headers
337: String host = sipStack.getIPAddress();
338:
339: SipURI contactUrl = addressFactory.createSipURI(
340: fromName, host);
341: contactUrl.setPort(tcpListeningPoint.getPort());
342:
343: // Create the contact name address.
344: SipURI contactURI = addressFactory.createSipURI(
345: fromName, host);
346: contactURI.setPort(sipProvider.getListeningPoint(
347: transport).getPort());
348:
349: Address contactAddress = addressFactory
350: .createAddress(contactURI);
351:
352: // Add the contact address.
353: contactAddress.setDisplayName(fromName);
354:
355: contactHeader = headerFactory
356: .createContactHeader(contactAddress);
357: request.addHeader(contactHeader);
358:
359: // Add the extension header.
360: Header extensionHeader = headerFactory.createHeader(
361: "My-Header", "my header value");
362: request.addHeader(extensionHeader);
363:
364: String sdpData = "v=0\r\n"
365: + "o=4855 13760799956958020 13760799956958020"
366: + " IN IP4 129.6.55.78\r\n"
367: + "s=mysession session\r\n"
368: + "p=+46 8 52018010\r\n"
369: + "c=IN IP4 129.6.55.78\r\n" + "t=0 0\r\n"
370: + "m=audio 6022 RTP/AVP 0 4 18\r\n"
371: + "a=rtpmap:0 PCMU/8000\r\n"
372: + "a=rtpmap:4 G723/8000\r\n"
373: + "a=rtpmap:18 G729A/8000\r\n"
374: + "a=ptime:20\r\n";
375: byte[] contents = sdpData.getBytes();
376:
377: request.setContent(contents, contentTypeHeader);
378:
379: extensionHeader = headerFactory.createHeader(
380: "My-Other-Header", "my new header value ");
381: request.addHeader(extensionHeader);
382:
383: Header callInfoHeader = headerFactory.createHeader(
384: "Call-Info", "<http://www.antd.nist.gov>");
385: request.addHeader(callInfoHeader);
386:
387: // Create the client transaction.
388: ClientTransaction inviteTid = sipProvider
389: .getNewClientTransaction(request);
390: inviteTid.sendRequest();
391: }
392:
393: } catch (Exception ex) {
394: System.out.println(ex.getMessage());
395: ex.printStackTrace();
396: usage();
397: }
398: }
399:
400: public void processIOException(IOExceptionEvent exceptionEvent) {
401: System.out
402: .println("IOException occured while retransmitting requests:"
403: + exceptionEvent);
404: }
405:
406: public void processTransactionTerminated(
407: TransactionTerminatedEvent transactionTerminatedEvent) {
408: System.out.println("Transaction Terminated event: "
409: + transactionTerminatedEvent);
410: }
411:
412: public void processDialogTerminated(
413: DialogTerminatedEvent dialogTerminatedEvent) {
414: System.out.println("Dialog Terminated event: "
415: + dialogTerminatedEvent);
416: }
417:
418: public static void main(String args[]) {
419: new Shootist().init();
420:
421: }
422: }
|