001: package examples.simplecallsetup;
002:
003: import javax.sip.*;
004: import javax.sip.address.*;
005: import javax.sip.header.*;
006: import javax.sip.message.*;
007:
008: import java.text.ParseException;
009: import java.util.*;
010:
011: /**
012: * This class is a UAS template.
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: private static final String myAddress = "127.0.0.1";
028:
029: private static final int myPort = 5070;
030:
031: protected ServerTransaction inviteTid;
032:
033: private Response okResponse;
034:
035: private Request inviteRequest;
036:
037: private Dialog dialog;
038:
039: public static final boolean callerSendsBye = true;
040:
041: class MyTimerTask extends TimerTask {
042: Shootme shootme;
043:
044: public MyTimerTask(Shootme shootme) {
045: this .shootme = shootme;
046:
047: }
048:
049: public void run() {
050: shootme.sendInviteOK();
051: }
052:
053: }
054:
055: protected static final String usageString = "java "
056: + "examples.shootist.Shootist \n"
057: + ">>>> is your class path set to the root?";
058:
059: private static void usage() {
060: System.out.println(usageString);
061: System.exit(0);
062:
063: }
064:
065: public void processRequest(RequestEvent requestEvent) {
066: Request request = requestEvent.getRequest();
067: ServerTransaction serverTransactionId = requestEvent
068: .getServerTransaction();
069:
070: System.out.println("\n\nRequest " + request.getMethod()
071: + " received at " + sipStack.getStackName()
072: + " with server transaction id " + serverTransactionId);
073:
074: if (request.getMethod().equals(Request.INVITE)) {
075: processInvite(requestEvent, serverTransactionId);
076: } else if (request.getMethod().equals(Request.ACK)) {
077: processAck(requestEvent, serverTransactionId);
078: } else if (request.getMethod().equals(Request.BYE)) {
079: processBye(requestEvent, serverTransactionId);
080: } else if (request.getMethod().equals(Request.CANCEL)) {
081: processCancel(requestEvent, serverTransactionId);
082: } else {
083: try {
084: serverTransactionId.sendResponse(messageFactory
085: .createResponse(202, request));
086:
087: // send one back
088: SipProvider prov = (SipProvider) requestEvent
089: .getSource();
090: Request refer = requestEvent.getDialog().createRequest(
091: "REFER");
092: requestEvent.getDialog().sendRequest(
093: prov.getNewClientTransaction(refer));
094:
095: } catch (SipException e) {
096: // TODO Auto-generated catch block
097: e.printStackTrace();
098: } catch (InvalidArgumentException e) {
099: // TODO Auto-generated catch block
100: e.printStackTrace();
101: } catch (ParseException e) {
102: // TODO Auto-generated catch block
103: e.printStackTrace();
104: }
105: }
106:
107: }
108:
109: public void processResponse(ResponseEvent responseEvent) {
110: }
111:
112: /**
113: * Process the ACK request. Send the bye and complete the call flow.
114: */
115: public void processAck(RequestEvent requestEvent,
116: ServerTransaction serverTransaction) {
117: try {
118: System.out.println("shootme: got an ACK! ");
119: System.out.println("Dialog State = " + dialog.getState());
120: SipProvider provider = (SipProvider) requestEvent
121: .getSource();
122: if (!callerSendsBye) {
123: Request byeRequest = dialog.createRequest(Request.BYE);
124: ClientTransaction ct = provider
125: .getNewClientTransaction(byeRequest);
126: dialog.sendRequest(ct);
127: }
128: } catch (Exception ex) {
129: ex.printStackTrace();
130: }
131:
132: }
133:
134: /**
135: * Process the invite request.
136: */
137: public void processInvite(RequestEvent requestEvent,
138: ServerTransaction serverTransaction) {
139: SipProvider sipProvider = (SipProvider) requestEvent
140: .getSource();
141: Request request = requestEvent.getRequest();
142: try {
143: System.out.println("shootme: got an Invite sending Trying");
144: // System.out.println("shootme: " + request);
145: Response response = messageFactory.createResponse(
146: Response.RINGING, request);
147: ServerTransaction st = requestEvent.getServerTransaction();
148:
149: if (st == null) {
150: st = sipProvider.getNewServerTransaction(request);
151: }
152: dialog = st.getDialog();
153:
154: st.sendResponse(response);
155:
156: this .okResponse = messageFactory.createResponse(
157: Response.OK, request);
158: Address address = addressFactory
159: .createAddress("Shootme <sip:" + myAddress + ":"
160: + myPort + ">");
161: ContactHeader contactHeader = headerFactory
162: .createContactHeader(address);
163: response.addHeader(contactHeader);
164: ToHeader toHeader = (ToHeader) okResponse
165: .getHeader(ToHeader.NAME);
166: toHeader.setTag("4321"); // Application is supposed to set.
167: okResponse.addHeader(contactHeader);
168: this .inviteTid = st;
169: // Defer sending the OK to simulate the phone ringing.
170: // Answered in 1 second ( this guy is fast at taking calls)
171: this .inviteRequest = request;
172:
173: new Timer().schedule(new MyTimerTask(this ), 1000);
174: } catch (Exception ex) {
175: ex.printStackTrace();
176: System.exit(0);
177: }
178: }
179:
180: private void sendInviteOK() {
181: try {
182: if (inviteTid.getState() != TransactionState.COMPLETED) {
183: System.out.println("shootme: Dialog state before 200: "
184: + inviteTid.getDialog().getState());
185: inviteTid.sendResponse(okResponse);
186: System.out.println("shootme: Dialog state after 200: "
187: + inviteTid.getDialog().getState());
188: }
189: } catch (SipException ex) {
190: ex.printStackTrace();
191: } catch (InvalidArgumentException ex) {
192: ex.printStackTrace();
193: }
194: }
195:
196: /**
197: * Process the bye request.
198: */
199: public void processBye(RequestEvent requestEvent,
200: ServerTransaction serverTransactionId) {
201: SipProvider sipProvider = (SipProvider) requestEvent
202: .getSource();
203: Request request = requestEvent.getRequest();
204: Dialog dialog = requestEvent.getDialog();
205: System.out.println("local party = " + dialog.getLocalParty());
206: try {
207: System.out.println("shootme: got a bye sending OK.");
208: Response response = messageFactory.createResponse(200,
209: request);
210: serverTransactionId.sendResponse(response);
211: System.out.println("Dialog State is "
212: + serverTransactionId.getDialog().getState());
213:
214: } catch (Exception ex) {
215: ex.printStackTrace();
216: System.exit(0);
217:
218: }
219: }
220:
221: public void processCancel(RequestEvent requestEvent,
222: ServerTransaction serverTransactionId) {
223: SipProvider sipProvider = (SipProvider) requestEvent
224: .getSource();
225: Request request = requestEvent.getRequest();
226: try {
227: System.out.println("shootme: got a cancel.");
228: if (serverTransactionId == null) {
229: System.out.println("shootme: null tid.");
230: return;
231: }
232: Response response = messageFactory.createResponse(200,
233: request);
234: serverTransactionId.sendResponse(response);
235: if (dialog.getState() != DialogState.CONFIRMED) {
236: response = messageFactory.createResponse(
237: Response.REQUEST_TERMINATED, inviteRequest);
238: inviteTid.sendResponse(response);
239: }
240:
241: } catch (Exception ex) {
242: ex.printStackTrace();
243: System.exit(0);
244:
245: }
246: }
247:
248: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
249: Transaction transaction;
250: if (timeoutEvent.isServerTransaction()) {
251: transaction = timeoutEvent.getServerTransaction();
252: } else {
253: transaction = timeoutEvent.getClientTransaction();
254: }
255: System.out.println("state = " + transaction.getState());
256: System.out.println("dialog = " + transaction.getDialog());
257: System.out.println("dialogState = "
258: + transaction.getDialog().getState());
259: System.out.println("Transaction Time out");
260: }
261:
262: public void init() {
263: SipFactory sipFactory = null;
264: sipStack = null;
265: sipFactory = SipFactory.getInstance();
266: sipFactory.setPathName("gov.nist");
267: Properties properties = new Properties();
268: properties.setProperty("javax.sip.STACK_NAME", "shootme");
269: // You need 16 for logging traces. 32 for debug + traces.
270: // Your code will limp at 32 but it is best for debugging.
271: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
272: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
273: "shootmedebug.txt");
274: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
275: "shootmelog.txt");
276:
277: try {
278: // Create SipStack object
279: sipStack = sipFactory.createSipStack(properties);
280: System.out.println("sipStack = " + sipStack);
281: } catch (PeerUnavailableException e) {
282: // could not find
283: // gov.nist.jain.protocol.ip.sip.SipStackImpl
284: // in the classpath
285: e.printStackTrace();
286: System.err.println(e.getMessage());
287: if (e.getCause() != null)
288: e.getCause().printStackTrace();
289: System.exit(0);
290: }
291:
292: try {
293: headerFactory = sipFactory.createHeaderFactory();
294: addressFactory = sipFactory.createAddressFactory();
295: messageFactory = sipFactory.createMessageFactory();
296: ListeningPoint lp = sipStack.createListeningPoint(
297: "127.0.0.1", myPort, "udp");
298:
299: Shootme listener = this ;
300:
301: SipProvider sipProvider = sipStack.createSipProvider(lp);
302: System.out.println("udp provider " + sipProvider);
303: sipProvider.addSipListener(listener);
304:
305: } catch (Exception ex) {
306: System.out.println(ex.getMessage());
307: ex.printStackTrace();
308: usage();
309: }
310:
311: }
312:
313: public static void main(String args[]) {
314: new Shootme().init();
315: }
316:
317: public void processIOException(IOExceptionEvent exceptionEvent) {
318: System.out.println("IOException");
319:
320: }
321:
322: public void processTransactionTerminated(
323: TransactionTerminatedEvent transactionTerminatedEvent) {
324: if (transactionTerminatedEvent.isServerTransaction())
325: System.out
326: .println("Transaction terminated event recieved"
327: + transactionTerminatedEvent
328: .getServerTransaction());
329: else
330: System.out
331: .println("Transaction terminated "
332: + transactionTerminatedEvent
333: .getClientTransaction());
334:
335: }
336:
337: public void processDialogTerminated(
338: DialogTerminatedEvent dialogTerminatedEvent) {
339: System.out.println("Dialog terminated event recieved");
340: Dialog d = dialogTerminatedEvent.getDialog();
341: System.out.println("Local Party = " + d.getLocalParty());
342:
343: }
344:
345: }
|