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. This illustrates how to go about when you want
012: * explicit control over creation of dialogs.
013: *
014: * @author M. Ranganathan
015: */
016:
017: public class Shootme implements SipListener {
018:
019: private static AddressFactory addressFactory;
020:
021: private static MessageFactory messageFactory;
022:
023: private static HeaderFactory headerFactory;
024:
025: private static SipStack sipStack;
026:
027: // To run on two machines change these to suit.
028: public static final String myAddress = "127.0.0.1";
029:
030: private static final int myPort = 5070;
031:
032: protected ServerTransaction inviteTid;
033:
034: protected ClientTransaction clientTid;
035:
036: protected static final String usageString = "java "
037: + "examples.shootist.Shootist \n"
038: + ">>>> is your class path set to the root?";
039:
040: private static void usage() {
041: System.out.println(usageString);
042: System.exit(0);
043:
044: }
045:
046: public void processRequest(RequestEvent requestEvent) {
047: Request request = requestEvent.getRequest();
048: ServerTransaction serverTransactionId = requestEvent
049: .getServerTransaction();
050:
051: System.out.println("\n\nRequest " + request.getMethod()
052: + " received at " + sipStack.getStackName()
053: + " with server transaction id " + serverTransactionId);
054:
055: if (request.getMethod().equals(Request.INVITE)) {
056: processInvite(requestEvent, serverTransactionId);
057: } else if (request.getMethod().equals(Request.ACK)) {
058: processAck(requestEvent, serverTransactionId);
059: } else if (request.getMethod().equals(Request.BYE)) {
060: processBye(requestEvent, serverTransactionId);
061: }
062:
063: }
064:
065: /**
066: * Process the ACK request.
067: */
068: public void processAck(RequestEvent requestEvent,
069: ServerTransaction serverTransaction) {
070: SipProvider sipProvider = (SipProvider) requestEvent
071: .getSource();
072: try {
073: System.out.println("shootme: got an ACK "
074: + requestEvent.getRequest());
075:
076: } catch (Exception ex) {
077: ex.printStackTrace();
078: System.exit(0);
079: }
080: }
081:
082: /**
083: * Process the invite request.
084: */
085: public void processInvite(RequestEvent requestEvent,
086: ServerTransaction serverTransaction) {
087: SipProvider sipProvider = (SipProvider) requestEvent
088: .getSource();
089: Request request = requestEvent.getRequest();
090: System.out.println("Got an INVITE " + request);
091: try {
092: System.out
093: .println("shootme: got an Invite sending 180 and 200");
094: //System.out.println("shootme: " + request);
095: Response response = messageFactory.createResponse(180,
096: request);
097: ToHeader toHeader = (ToHeader) response
098: .getHeader(ToHeader.NAME);
099: toHeader.setTag("4321"); // Application is supposed to set.
100: Address address = addressFactory
101: .createAddress("Shootme <sip:" + myAddress + ":"
102: + myPort + ">");
103: ContactHeader contactHeader = headerFactory
104: .createContactHeader(address);
105: response.addHeader(contactHeader);
106: ServerTransaction st = requestEvent.getServerTransaction();
107:
108: if (st == null) {
109: st = sipProvider.getNewServerTransaction(request);
110: System.out.println("Server transaction created!"
111: + request);
112:
113: System.out.println("Dialog = " + st.getDialog());
114:
115: }
116: // Thread.sleep(5000);
117: System.out.println("got a server tranasaction " + st);
118: byte[] content = request.getRawContent();
119: if (content != null) {
120: System.out.println(" content = " + new String(content));
121: ContentTypeHeader contentTypeHeader = headerFactory
122: .createContentTypeHeader("application", "sdp");
123: System.out.println("response = " + response);
124: response.setContent(content, contentTypeHeader);
125: }
126: Dialog dialog = st.getDialog();
127:
128: System.out.println("Dialog " + dialog);
129:
130: dialog = sipProvider.getNewDialog(st);
131:
132: System.out.println("Dialog " + st.getDialog());
133:
134: if (dialog != st.getDialog()) {
135: throw new RuntimeException("Dialog mismatch ");
136:
137: }
138:
139: response = messageFactory.createResponse(200, request);
140: toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
141: toHeader.setTag("4321");
142: // Application is supposed to set.
143: response.addHeader(contactHeader);
144: st.sendResponse(response);
145: this .inviteTid = st;
146: } catch (Exception ex) {
147: ex.printStackTrace();
148: System.exit(0);
149: }
150: }
151:
152: /**
153: * Process the bye request.
154: */
155: public void processBye(RequestEvent requestEvent,
156: ServerTransaction serverTransactionId) {
157: SipProvider sipProvider = (SipProvider) requestEvent
158: .getSource();
159: Request request = requestEvent.getRequest();
160: try {
161: System.out.println("shootme: got a bye sending OK.");
162: Response response = messageFactory.createResponse(200,
163: request);
164: if (serverTransactionId == null) {
165: System.out
166: .println("Note that we can create a new tx here!");
167: serverTransactionId = sipProvider
168: .getNewServerTransaction(request);
169: System.out
170: .println("Creating new server transaction for bye "
171: + serverTransactionId);
172: }
173: // Fire and forget.
174: serverTransactionId.sendResponse(response);
175: System.out
176: .println("We are not creating a dialog for this.");
177: System.out.println("Dialog is "
178: + serverTransactionId.getDialog());
179:
180: } catch (Exception ex) {
181: ex.printStackTrace();
182: System.exit(0);
183:
184: }
185: }
186:
187: public void processResponse(ResponseEvent responseReceivedEvent) {
188: System.out.println("Got a response");
189: Response response = (Response) responseReceivedEvent
190: .getResponse();
191: Transaction tid = responseReceivedEvent.getClientTransaction();
192:
193: System.out
194: .println("Response received with client transaction id "
195: + tid + ":\n" + response);
196: try {
197: if (response.getStatusCode() == Response.OK
198: && ((CSeqHeader) response
199: .getHeader(CSeqHeader.NAME)).getMethod()
200: .equals(Request.INVITE)) {
201: Dialog dialog = tid.getDialog();
202: Request request = dialog.createRequest(Request.ACK);
203: dialog.sendAck(request);
204: }
205: Dialog dialog = tid.getDialog();
206: System.out.println("Dalog State = " + dialog.getState());
207: } catch (Exception ex) {
208: ex.printStackTrace();
209: System.exit(0);
210: }
211:
212: }
213:
214: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
215: Transaction transaction;
216: if (timeoutEvent.isServerTransaction()) {
217: transaction = timeoutEvent.getServerTransaction();
218: } else {
219: transaction = timeoutEvent.getClientTransaction();
220: }
221: System.out.println("state = " + transaction.getState());
222: System.out.println("dialog = " + transaction.getDialog());
223: System.out.println("dialogState = "
224: + transaction.getDialog().getState());
225: System.out.println("Transaction Time out");
226: }
227:
228: public void init() {
229: SipFactory sipFactory = null;
230: sipStack = null;
231: sipFactory = SipFactory.getInstance();
232: sipFactory.setPathName("gov.nist");
233: Properties properties = new Properties();
234: properties.setProperty("javax.sip.STACK_NAME", "shootme");
235: // Note that this turns off automatic dialog creation. This
236: // mode of operaton is good for proxy servers.
237: properties.setProperty("javax.sip.AUTOMATIC_DIALOG_SUPPORT",
238: "off");
239: // You need 16 for logging traces. 32 for debug + traces.
240: // Your code will limp at 32 but it is best for debugging.
241: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
242: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
243: "shootmedebug.txt");
244: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
245: "shootmelog.txt");
246: // Guard against starvation.
247: properties.setProperty("gov.nist.javax.sip.READ_TIMEOUT",
248: "1000");
249: // properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
250: // "4096");
251: properties.setProperty(
252: "gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS", "false");
253:
254: try {
255: // Create SipStack object
256: sipStack = sipFactory.createSipStack(properties);
257: System.out.println("sipStack = " + sipStack);
258: } catch (PeerUnavailableException e) {
259: // could not find
260: // gov.nist.jain.protocol.ip.sip.SipStackImpl
261: // in the classpath
262: e.printStackTrace();
263: System.err.println(e.getMessage());
264: if (e.getCause() != null)
265: e.getCause().printStackTrace();
266: System.exit(0);
267: }
268:
269: try {
270: headerFactory = sipFactory.createHeaderFactory();
271: addressFactory = sipFactory.createAddressFactory();
272: messageFactory = sipFactory.createMessageFactory();
273: ListeningPoint lp = sipStack.createListeningPoint(
274: myAddress, 5070, "udp");
275: ListeningPoint lp1 = sipStack.createListeningPoint(
276: myAddress, 5070, "tcp");
277:
278: Shootme listener = this ;
279:
280: SipProvider sipProvider = sipStack.createSipProvider(lp);
281: System.out.println("udp provider " + sipProvider);
282: sipProvider.addSipListener(listener);
283: sipProvider = sipStack.createSipProvider(lp1);
284: System.out.println("tcp provider " + sipProvider);
285: sipProvider.addSipListener(listener);
286:
287: } catch (Exception ex) {
288: System.out.println(ex.getMessage());
289: ex.printStackTrace();
290: usage();
291: }
292:
293: }
294:
295: public static void main(String args[]) {
296: new Shootme().init();
297: }
298:
299: /*
300: * (non-Javadoc)
301: *
302: * @see javax.sip.SipListener#processIOException(javax.sip.IOExceptionEvent)
303: */
304: public void processIOException(IOExceptionEvent exceptionEvent) {
305: System.out.println("An IO Exception was detected : "
306: + exceptionEvent.getHost());
307:
308: }
309:
310: /*
311: * (non-Javadoc)
312: *
313: * @see javax.sip.SipListener#processTransactionTerminated(javax.sip.TransactionTerminatedEvent)
314: */
315: public void processTransactionTerminated(
316: TransactionTerminatedEvent transactionTerminatedEvent) {
317: System.out.println("Tx terminated event ");
318:
319: }
320:
321: /*
322: * (non-Javadoc)
323: *
324: * @see javax.sip.SipListener#processDialogTerminated(javax.sip.DialogTerminatedEvent)
325: */
326: public void processDialogTerminated(
327: DialogTerminatedEvent dialogTerminatedEvent) {
328: System.out.println("Dialog terminated event detected ");
329:
330: }
331:
332: }
|