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