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