001: /*
002: * Conditions Of Use
003: *
004: * This software was developed by employees of the National Institute of
005: * Standards and Technology (NIST), and others.
006: * This software is has been contributed to the public domain.
007: * As a result, a formal license is not needed to use the software.
008: *
009: * This software is provided "AS IS."
010: * NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
011: * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
012: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
013: * AND DATA ACCURACY. NIST does not warrant or make any representations
014: * regarding the use of the software or the results thereof, including but
015: * not limited to the correctness, accuracy, reliability or usefulness of
016: * the software.
017: *
018: *
019: */
020: package test.tck.msgflow.callflows.router;
021:
022: import javax.sip.*;
023: import javax.sip.address.*;
024: import javax.sip.header.*;
025: import javax.sip.message.*;
026:
027: import org.apache.log4j.Logger;
028:
029: import test.tck.TestHarness;
030: import test.tck.msgflow.callflows.ProtocolObjects;
031:
032: import java.util.*;
033:
034: /**
035: * This class is a UAC template.
036: *
037: * @author M. Ranganathan
038: */
039:
040: public class Shootme implements SipListener {
041:
042: private static SipProvider sipProvider;
043:
044: private static AddressFactory addressFactory;
045:
046: private static MessageFactory messageFactory;
047:
048: private static HeaderFactory headerFactory;
049:
050: private static SipStack sipStack;
051:
052: private static final String myAddress = "127.0.0.1";
053:
054: protected ServerTransaction inviteTid;
055:
056: private Dialog dialog;
057:
058: private String toTag;
059:
060: private String transport;
061:
062: private boolean inviteReceived;
063:
064: public static final int myPort = 5080;
065:
066: private static Logger logger = Logger.getLogger("test.tck");
067:
068: public Shootme(ProtocolObjects protObjects) {
069: addressFactory = protObjects.addressFactory;
070: messageFactory = protObjects.messageFactory;
071: headerFactory = protObjects.headerFactory;
072: sipStack = protObjects.sipStack;
073: transport = protObjects.transport;
074: }
075:
076: public void processRequest(RequestEvent requestEvent) {
077: Request request = requestEvent.getRequest();
078: ServerTransaction serverTransactionId = requestEvent
079: .getServerTransaction();
080:
081: logger.info("\n\nRequest " + request.getMethod()
082: + " received at " + sipStack.getStackName()
083: + " with server transaction id " + serverTransactionId);
084:
085: if (request.getMethod().equals(Request.INVITE)) {
086: processInvite(requestEvent, serverTransactionId);
087: } else if (request.getMethod().equals(Request.ACK)) {
088: processAck(requestEvent, serverTransactionId);
089: } else if (request.getMethod().equals(Request.BYE)) {
090: processBye(requestEvent, serverTransactionId);
091: }
092:
093: }
094:
095: public void processResponse(ResponseEvent responseEvent) {
096: }
097:
098: /**
099: * Process the ACK request. Send the bye and complete the call flow.
100: */
101: public void processAck(RequestEvent requestEvent,
102: ServerTransaction serverTransaction) {
103:
104: try {
105: logger.info("shootme: got an ACK! Sending a BYE");
106: logger.info("Dialog State = " + dialog.getState());
107: Dialog dialog = serverTransaction.getDialog();
108: AbstractRouterTestCase.assertTrue("Dialog mismatch",
109: dialog == this .dialog);
110: SipProvider provider = (SipProvider) requestEvent
111: .getSource();
112: AbstractRouterTestCase.assertTrue("Provider mismatch",
113: sipProvider == provider);
114: Request byeRequest = dialog.createRequest(Request.BYE);
115: ClientTransaction ct = provider
116: .getNewClientTransaction(byeRequest);
117: dialog.sendRequest(ct);
118: } catch (Exception ex) {
119: TestHarness.fail(ex.getMessage());
120: }
121:
122: }
123:
124: /**
125: * Process the invite request.
126: */
127: public void processInvite(RequestEvent requestEvent,
128: ServerTransaction serverTransaction) {
129: inviteReceived = true;
130: SipProvider sipProvider = (SipProvider) requestEvent
131: .getSource();
132: Request request = requestEvent.getRequest();
133: try {
134: logger.info("shootme: got an Invite sending Trying");
135: // logger.info("shootme: " + request);
136: Response response = messageFactory.createResponse(
137: Response.TRYING, request);
138: ServerTransaction st = requestEvent.getServerTransaction();
139:
140: if (st == null) {
141: st = sipProvider.getNewServerTransaction(request);
142: }
143: dialog = st.getDialog();
144:
145: st.sendResponse(response);
146:
147: // reliable provisional response.
148:
149: Response okResponse = messageFactory.createResponse(
150: Response.OK, request);
151: ToHeader toHeader = (ToHeader) okResponse
152: .getHeader(ToHeader.NAME);
153: this .toTag = "4321";
154: toHeader.setTag(toTag); // Application is supposed to set.
155: this .inviteTid = st;
156: Address address = addressFactory
157: .createAddress("Shootme <sip:" + myAddress + ":"
158: + myPort + ">");
159: ContactHeader contactHeader = headerFactory
160: .createContactHeader(address);
161: okResponse.addHeader(contactHeader);
162:
163: logger.info("sending response.");
164:
165: st.sendResponse(okResponse);
166:
167: // new Timer().schedule(new MyTimerTask(this), 100);
168: } catch (Exception ex) {
169: TestHarness.fail(ex.getMessage());
170:
171: }
172: }
173:
174: /**
175: * Process the bye request.
176: */
177: public void processBye(RequestEvent requestEvent,
178: ServerTransaction serverTransactionId) {
179: Request request = requestEvent.getRequest();
180: try {
181: logger.info("shootme: got a bye sending OK.");
182: Response response = messageFactory.createResponse(200,
183: request);
184: serverTransactionId.sendResponse(response);
185: logger.info("Dialog State is "
186: + serverTransactionId.getDialog().getState());
187:
188: } catch (Exception ex) {
189: TestHarness.fail(ex.getMessage());
190: System.exit(0);
191:
192: }
193: }
194:
195: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
196: Transaction transaction;
197: if (timeoutEvent.isServerTransaction()) {
198: transaction = timeoutEvent.getServerTransaction();
199: } else {
200: transaction = timeoutEvent.getClientTransaction();
201: }
202: logger.info("state = " + transaction.getState());
203: logger.info("dialog = " + transaction.getDialog());
204: logger.info("dialogState = "
205: + transaction.getDialog().getState());
206: logger.info("Transaction Time out");
207: }
208:
209: public SipProvider createProvider() throws Exception {
210: ListeningPoint lp = sipStack.createListeningPoint("127.0.0.1",
211: myPort, transport);
212:
213: sipProvider = sipStack.createSipProvider(lp);
214: logger.info(transport + " SIP provider " + sipProvider);
215:
216: return sipProvider;
217: }
218:
219: public void processIOException(IOExceptionEvent exceptionEvent) {
220: logger.info("IOException");
221:
222: }
223:
224: public void processTransactionTerminated(
225: TransactionTerminatedEvent transactionTerminatedEvent) {
226: logger.info("Transaction terminated event recieved");
227:
228: }
229:
230: public void processDialogTerminated(
231: DialogTerminatedEvent dialogTerminatedEvent) {
232: logger.info("Dialog terminated event recieved");
233:
234: }
235:
236: public void checkState() {
237: TestHarness.assertTrue(inviteReceived);
238: }
239:
240: }
|