001: package test.tck.msgflow.callflows.forkedinvite;
002:
003: import javax.sip.*;
004: import javax.sip.address.*;
005: import javax.sip.header.*;
006: import javax.sip.message.*;
007:
008: import org.apache.log4j.ConsoleAppender;
009: import org.apache.log4j.Logger;
010: import org.apache.log4j.SimpleLayout;
011:
012: import test.tck.TestHarness;
013: import test.tck.msgflow.callflows.ProtocolObjects;
014:
015: import java.util.*;
016:
017: import junit.framework.TestCase;
018:
019: /**
020: * This class is a UAC template. Shootist is the guy that shoots and shootme is
021: * the guy that gets shot.
022: *
023: * @author M. Ranganathan
024: */
025:
026: public class Shootme implements SipListener {
027:
028: private static final String myAddress = "127.0.0.1";
029:
030: private Hashtable serverTxTable = new Hashtable();
031:
032: private SipProvider sipProvider;
033:
034: private int myPort;
035:
036: private static String unexpectedException = "Unexpected exception ";
037:
038: private static Logger logger = Logger.getLogger(Shootme.class);
039:
040: private ProtocolObjects protocolObjects;
041:
042: private boolean inviteSeen;
043:
044: private boolean byeSeen;
045:
046: private boolean ackSeen;
047:
048: private boolean actAsNonRFC3261UAS;
049:
050: /**
051: * Causes this UAS to act as a non-RFC3261 UAS, i.e. does not set a to-tag
052: */
053: public void setNonRFC3261(boolean b) {
054: this .actAsNonRFC3261UAS = b;
055: }
056:
057: class MyTimerTask extends TimerTask {
058: RequestEvent requestEvent;
059: // String toTag;
060: ServerTransaction serverTx;
061:
062: public MyTimerTask(RequestEvent requestEvent,
063: ServerTransaction tx) {
064: logger.info("MyTimerTask ");
065: this .requestEvent = requestEvent;
066: // this.toTag = toTag;
067: this .serverTx = tx;
068:
069: }
070:
071: public void run() {
072: sendInviteOK(requestEvent, serverTx);
073: }
074:
075: }
076:
077: public void processRequest(RequestEvent requestEvent) {
078: Request request = requestEvent.getRequest();
079: ServerTransaction serverTransactionId = requestEvent
080: .getServerTransaction();
081:
082: logger.info("\n\nRequest " + request.getMethod()
083: + " received at "
084: + protocolObjects.sipStack.getStackName()
085: + " with server transaction id " + serverTransactionId);
086:
087: if (request.getMethod().equals(Request.INVITE)) {
088: processInvite(requestEvent, serverTransactionId);
089: } else if (request.getMethod().equals(Request.ACK)) {
090: processAck(requestEvent, serverTransactionId);
091: } else if (request.getMethod().equals(Request.BYE)) {
092: processBye(requestEvent, serverTransactionId);
093: } else if (request.getMethod().equals(Request.CANCEL)) {
094: processCancel(requestEvent, serverTransactionId);
095: }
096:
097: }
098:
099: public void processResponse(ResponseEvent responseEvent) {
100: }
101:
102: /**
103: * Process the ACK request. Send the bye and complete the call flow.
104: */
105: public void processAck(RequestEvent requestEvent,
106: ServerTransaction serverTransaction) {
107: logger.info("shootme: got an ACK! ");
108: logger.info("Dialog = " + requestEvent.getDialog());
109: logger.info("Dialog State = "
110: + requestEvent.getDialog().getState());
111:
112: this .ackSeen = true;
113: }
114:
115: /**
116: * Process the invite request.
117: */
118: public void processInvite(RequestEvent requestEvent,
119: ServerTransaction serverTransaction) {
120: SipProvider sipProvider = (SipProvider) requestEvent
121: .getSource();
122: Request request = requestEvent.getRequest();
123: try {
124: logger.info("shootme: got an Invite sending Trying");
125: // logger.info("shootme: " + request);
126:
127: ServerTransaction st = requestEvent.getServerTransaction();
128:
129: if (st == null) {
130: logger.info("null server tx -- getting a new one");
131: st = sipProvider.getNewServerTransaction(request);
132: }
133:
134: logger.info("getNewServerTransaction : " + st);
135:
136: String txId = ((ViaHeader) request
137: .getHeader(ViaHeader.NAME)).getBranch();
138: this .serverTxTable.put(txId, st);
139:
140: // Create the 100 Trying response.
141: Response response = protocolObjects.messageFactory
142: .createResponse(Response.TRYING, request);
143: ListeningPoint lp = sipProvider
144: .getListeningPoint(protocolObjects.transport);
145: int myPort = lp.getPort();
146:
147: Address address = protocolObjects.addressFactory
148: .createAddress("Shootme <sip:" + myAddress + ":"
149: + myPort + ">");
150:
151: // Add a random sleep to stagger the two OK's for the benifit of implementations
152: // that may not be too good about handling re-entrancy.
153: int timeToSleep = (int) (Math.random() * 1000);
154:
155: Thread.sleep(timeToSleep);
156:
157: st.sendResponse(response);
158:
159: Response ringingResponse = protocolObjects.messageFactory
160: .createResponse(Response.RINGING, request);
161: ContactHeader contactHeader = protocolObjects.headerFactory
162: .createContactHeader(address);
163: response.addHeader(contactHeader);
164: ToHeader toHeader = (ToHeader) ringingResponse
165: .getHeader(ToHeader.NAME);
166: String toTag = actAsNonRFC3261UAS ? null : new Integer(
167: (int) (Math.random() * 10000)).toString();
168: if (!actAsNonRFC3261UAS)
169: toHeader.setTag(toTag); // Application is supposed to set.
170: ringingResponse.addHeader(contactHeader);
171: st.sendResponse(ringingResponse);
172: Dialog dialog = st.getDialog();
173: dialog.setApplicationData(st);
174:
175: this .inviteSeen = true;
176:
177: new Timer().schedule(
178: new MyTimerTask(requestEvent, st/*,toTag*/), 1000);
179: } catch (Exception ex) {
180: ex.printStackTrace();
181: System.exit(0);
182: }
183: }
184:
185: private void sendInviteOK(RequestEvent requestEvent,
186: ServerTransaction inviteTid) {
187: try {
188: logger.info("sendInviteOK: " + inviteTid);
189: if (inviteTid.getState() != TransactionState.COMPLETED) {
190: logger.info("shootme: Dialog state before OK: "
191: + inviteTid.getDialog().getState());
192:
193: SipProvider sipProvider = (SipProvider) requestEvent
194: .getSource();
195: Request request = requestEvent.getRequest();
196: Response okResponse = protocolObjects.messageFactory
197: .createResponse(Response.OK, request);
198: ListeningPoint lp = sipProvider
199: .getListeningPoint(protocolObjects.transport);
200: int myPort = lp.getPort();
201:
202: Address address = protocolObjects.addressFactory
203: .createAddress("Shootme <sip:" + myAddress
204: + ":" + myPort + ">");
205: ContactHeader contactHeader = protocolObjects.headerFactory
206: .createContactHeader(address);
207: okResponse.addHeader(contactHeader);
208: inviteTid.sendResponse(okResponse);
209: logger.info("shootme: Dialog state after OK: "
210: + inviteTid.getDialog().getState());
211: TestHarness.assertEquals(DialogState.CONFIRMED,
212: inviteTid.getDialog().getState());
213: } else {
214: logger.info("semdInviteOK: inviteTid = " + inviteTid
215: + " state = " + inviteTid.getState());
216: }
217: } catch (Exception ex) {
218: ex.printStackTrace();
219: }
220: }
221:
222: /**
223: * Process the bye request.
224: */
225: public void processBye(RequestEvent requestEvent,
226: ServerTransaction serverTransactionId) {
227: Request request = requestEvent.getRequest();
228: try {
229: logger.info("shootme: got a bye sending OK.");
230: logger.info("shootme: dialog = "
231: + requestEvent.getDialog());
232: logger.info("shootme: dialogState = "
233: + requestEvent.getDialog().getState());
234: Response response = protocolObjects.messageFactory
235: .createResponse(200, request);
236: if (serverTransactionId != null) {
237: serverTransactionId.sendResponse(response);
238: }
239: logger.info("shootme: dialogState = "
240: + requestEvent.getDialog().getState());
241:
242: this .byeSeen = true;
243:
244: } catch (Exception ex) {
245: ex.printStackTrace();
246: System.exit(0);
247:
248: }
249: }
250:
251: public void processCancel(RequestEvent requestEvent,
252: ServerTransaction serverTransactionId) {
253: Request request = requestEvent.getRequest();
254: SipProvider sipProvider = (SipProvider) requestEvent
255: .getSource();
256: try {
257: logger.info("shootme: got a cancel. ");
258: // Because this is not an In-dialog request, you will get a null server Tx id here.
259: if (serverTransactionId == null) {
260: serverTransactionId = sipProvider
261: .getNewServerTransaction(request);
262: }
263: Response response = protocolObjects.messageFactory
264: .createResponse(200, request);
265: serverTransactionId.sendResponse(response);
266:
267: String serverTxId = ((ViaHeader) response
268: .getHeader(ViaHeader.NAME)).getBranch();
269: ServerTransaction serverTx = (ServerTransaction) this .serverTxTable
270: .get(serverTxId);
271: if (serverTx != null
272: && (serverTx.getState().equals(
273: TransactionState.TRYING) || serverTx
274: .getState().equals(
275: TransactionState.PROCEEDING))) {
276: Request originalRequest = serverTx.getRequest();
277: Response resp = protocolObjects.messageFactory
278: .createResponse(Response.REQUEST_TERMINATED,
279: originalRequest);
280: serverTx.sendResponse(resp);
281: }
282:
283: } catch (Exception ex) {
284: ex.printStackTrace();
285: System.exit(0);
286:
287: }
288: }
289:
290: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
291: Transaction transaction;
292: if (timeoutEvent.isServerTransaction()) {
293: transaction = timeoutEvent.getServerTransaction();
294: } else {
295: transaction = timeoutEvent.getClientTransaction();
296: }
297: logger.info("state = " + transaction.getState());
298: logger.info("dialog = " + transaction.getDialog());
299: logger.info("dialogState = "
300: + transaction.getDialog().getState());
301: logger.info("Transaction Time out");
302: }
303:
304: public SipProvider createProvider() {
305: try {
306:
307: ListeningPoint lp = protocolObjects.sipStack
308: .createListeningPoint(myAddress, myPort,
309: protocolObjects.transport);
310:
311: sipProvider = protocolObjects.sipStack
312: .createSipProvider(lp);
313: logger.info("provider " + sipProvider);
314: logger.info("sipStack = " + protocolObjects.sipStack);
315: return sipProvider;
316: } catch (Exception ex) {
317: logger.error(ex);
318: TestHarness.fail(unexpectedException);
319: return null;
320:
321: }
322:
323: }
324:
325: public Shootme(int myPort, ProtocolObjects protocolObjects) {
326: this .myPort = myPort;
327: this .protocolObjects = protocolObjects;
328: }
329:
330: public void processIOException(IOExceptionEvent exceptionEvent) {
331: logger.info("IOException");
332:
333: }
334:
335: public void processTransactionTerminated(
336: TransactionTerminatedEvent transactionTerminatedEvent) {
337: logger.info("Transaction terminated event recieved");
338:
339: }
340:
341: public void processDialogTerminated(
342: DialogTerminatedEvent dialogTerminatedEvent) {
343: logger.info("Dialog terminated event recieved");
344:
345: }
346:
347: public void checkState() {
348: TestHarness.assertTrue("Should see invite", inviteSeen);
349:
350: TestHarness.assertTrue(
351: "Should see either an ACK or a BYE, or both", byeSeen
352: || ackSeen);
353:
354: }
355:
356: }
|