001: package examples.prack;
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 Shootme implements SipListener {
017:
018: private static AddressFactory addressFactory;
019:
020: private static MessageFactory messageFactory;
021:
022: private static HeaderFactory headerFactory;
023:
024: private static SipStack sipStack;
025:
026: private static final String myAddress = "127.0.0.1";
027:
028: private static final int myPort = 5070;
029:
030: protected ServerTransaction inviteTid;
031:
032: private Request inviteRequest;
033:
034: private Dialog dialog;
035:
036: private String toTag;
037:
038: protected static final String usageString = "java "
039: + "examples.shootist.Shootist \n"
040: + ">>>> is your class path set to the root?";
041:
042: private static void usage() {
043: System.out.println(usageString);
044: System.exit(0);
045:
046: }
047:
048: public void processRequest(RequestEvent requestEvent) {
049: Request request = requestEvent.getRequest();
050: ServerTransaction serverTransactionId = requestEvent
051: .getServerTransaction();
052:
053: System.out.println("\n\nRequest " + request.getMethod()
054: + " received at " + sipStack.getStackName()
055: + " with server transaction id " + serverTransactionId);
056:
057: if (request.getMethod().equals(Request.INVITE)) {
058: processInvite(requestEvent, serverTransactionId);
059: } else if (request.getMethod().equals(Request.ACK)) {
060: processAck(requestEvent, serverTransactionId);
061: } else if (request.getMethod().equals(Request.BYE)) {
062: processBye(requestEvent, serverTransactionId);
063: } else if (request.getMethod().equals(Request.CANCEL)) {
064: processCancel(requestEvent, serverTransactionId);
065: } else if (request.getMethod().equals(Request.PRACK)) {
066: processPrack(requestEvent, serverTransactionId);
067: }
068:
069: }
070:
071: private void processPrack(RequestEvent requestEvent,
072: ServerTransaction serverTransactionId) {
073: try {
074: System.out.println("shootme: got an PRACK! ");
075: System.out.println("Dialog State = " + dialog.getState());
076:
077: /**
078: * JvB: First, send 200 OK for PRACK
079: */
080: Request prack = requestEvent.getRequest();
081: Response prackOk = messageFactory
082: .createResponse(200, prack);
083: serverTransactionId.sendResponse(prackOk);
084:
085: /**
086: * Send a 200 OK response to complete the 3 way handshake for the
087: * INIVTE.
088: */
089: Response response = messageFactory.createResponse(200,
090: inviteRequest);
091: ToHeader to = (ToHeader) response.getHeader(ToHeader.NAME);
092: to.setTag(this .toTag);
093: Address address = addressFactory
094: .createAddress("Shootme <sip:" + myAddress + ":"
095: + myPort + ">");
096: ContactHeader contactHeader = headerFactory
097: .createContactHeader(address);
098: response.addHeader(contactHeader);
099: inviteTid.sendResponse(response);
100: } catch (Exception ex) {
101: ex.printStackTrace();
102:
103: }
104:
105: }
106:
107: public void processResponse(ResponseEvent responseEvent) {
108: }
109:
110: /**
111: * Process the ACK request. Send the bye and complete the call flow.
112: */
113: public void processAck(RequestEvent requestEvent,
114: ServerTransaction serverTransaction) {
115:
116: try {
117: System.out.println("shootme: got an ACK! Sending a BYE");
118: System.out.println("Dialog State = " + dialog.getState());
119: Dialog dialog = serverTransaction.getDialog();
120:
121: SipProvider provider = (SipProvider) requestEvent
122: .getSource();
123: Request byeRequest = dialog.createRequest(Request.BYE);
124: ClientTransaction ct = provider
125: .getNewClientTransaction(byeRequest);
126: dialog.sendRequest(ct);
127: } catch (Exception ex) {
128: ex.printStackTrace();
129: }
130:
131: }
132:
133: /**
134: * Process the invite request.
135: */
136: public void processInvite(RequestEvent requestEvent,
137: ServerTransaction serverTransaction) {
138: SipProvider sipProvider = (SipProvider) requestEvent
139: .getSource();
140: Request request = requestEvent.getRequest();
141: try {
142: System.out.println("shootme: got an Invite sending Trying");
143: // System.out.println("shootme: " + request);
144: Response response = messageFactory.createResponse(
145: Response.TRYING, request);
146: ServerTransaction st = requestEvent.getServerTransaction();
147:
148: if (st == null) {
149: st = sipProvider.getNewServerTransaction(request);
150: }
151: dialog = st.getDialog();
152:
153: st.sendResponse(response);
154:
155: // reliable provisional response. Use the API here!
156: // Response okResponse = messageFactory.createResponse(183, request);
157: Response sessionProgress = dialog
158: .createReliableProvisionalResponse(Response.SESSION_PROGRESS);
159:
160: ToHeader toHeader = (ToHeader) sessionProgress
161: .getHeader(ToHeader.NAME);
162: this .toTag = "4321";
163: toHeader.setTag(toTag); // Application is supposed to set.
164: this .inviteTid = st;
165: this .inviteRequest = request;
166:
167: System.out
168: .println("sending reliable provisional response.");
169:
170: // It is essential to use this API here!
171: dialog.sendReliableProvisionalResponse(sessionProgress);
172:
173: // new Timer().schedule(new MyTimerTask(this), 100);
174: } catch (Exception ex) {
175: ex.printStackTrace();
176: System.exit(0);
177: }
178: }
179:
180: /**
181: * Process the bye request.
182: */
183: public void processBye(RequestEvent requestEvent,
184: ServerTransaction serverTransactionId) {
185: SipProvider sipProvider = (SipProvider) requestEvent
186: .getSource();
187: Request request = requestEvent.getRequest();
188: try {
189: System.out.println("shootme: got a bye sending OK.");
190: Response response = messageFactory.createResponse(200,
191: request);
192: serverTransactionId.sendResponse(response);
193: System.out.println("Dialog State is "
194: + serverTransactionId.getDialog().getState());
195:
196: } catch (Exception ex) {
197: ex.printStackTrace();
198: System.exit(0);
199:
200: }
201: }
202:
203: public void processCancel(RequestEvent requestEvent,
204: ServerTransaction serverTransactionId) {
205: SipProvider sipProvider = (SipProvider) requestEvent
206: .getSource();
207: Request request = requestEvent.getRequest();
208: try {
209: System.out.println("shootme: got a cancel.");
210: if (serverTransactionId == null) {
211: System.out.println("shootme: null tid.");
212: return;
213: }
214: Response response = messageFactory.createResponse(200,
215: request);
216: serverTransactionId.sendResponse(response);
217: if (dialog.getState() != DialogState.CONFIRMED) {
218: response = messageFactory.createResponse(
219: Response.REQUEST_TERMINATED, inviteRequest);
220: inviteTid.sendResponse(response);
221: }
222:
223: } catch (Exception ex) {
224: ex.printStackTrace();
225: System.exit(0);
226:
227: }
228: }
229:
230: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
231: Transaction transaction;
232: if (timeoutEvent.isServerTransaction()) {
233: transaction = timeoutEvent.getServerTransaction();
234: } else {
235: transaction = timeoutEvent.getClientTransaction();
236: }
237: System.out.println("state = " + transaction.getState());
238: System.out.println("dialog = " + transaction.getDialog());
239: System.out.println("dialogState = "
240: + transaction.getDialog().getState());
241: System.out.println("Transaction Time out");
242: }
243:
244: public void init() {
245: SipFactory sipFactory = null;
246: sipStack = null;
247: sipFactory = SipFactory.getInstance();
248: sipFactory.setPathName("gov.nist");
249: Properties properties = new Properties();
250: properties.setProperty("javax.sip.STACK_NAME", "shootme");
251: // You need 16 for logging traces. 32 for debug + traces.
252: // Your code will limp at 32 but it is best for debugging.
253: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
254: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
255: "shootmedebug.txt");
256: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
257: "shootmelog.txt");
258:
259: try {
260: // Create SipStack object
261: sipStack = sipFactory.createSipStack(properties);
262: System.out.println("sipStack = " + sipStack);
263: } catch (PeerUnavailableException e) {
264: // could not find
265: // gov.nist.jain.protocol.ip.sip.SipStackImpl
266: // in the classpath
267: e.printStackTrace();
268: System.err.println(e.getMessage());
269: if (e.getCause() != null)
270: e.getCause().printStackTrace();
271: System.exit(0);
272: }
273:
274: try {
275: headerFactory = sipFactory.createHeaderFactory();
276: addressFactory = sipFactory.createAddressFactory();
277: messageFactory = sipFactory.createMessageFactory();
278: ListeningPoint lp = sipStack.createListeningPoint(
279: "127.0.0.1", 5070, "udp");
280:
281: Shootme listener = this ;
282:
283: SipProvider sipProvider = sipStack.createSipProvider(lp);
284: System.out.println("udp 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: public void processIOException(IOExceptionEvent exceptionEvent) {
300: System.out.println("IOException");
301:
302: }
303:
304: public void processTransactionTerminated(
305: TransactionTerminatedEvent transactionTerminatedEvent) {
306: System.out.println("Transaction terminated event recieved");
307:
308: }
309:
310: public void processDialogTerminated(
311: DialogTerminatedEvent dialogTerminatedEvent) {
312: System.out.println("Dialog terminated event recieved");
313:
314: }
315:
316: }
|