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