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