001: package test.tck.msgflow.callflows.prack;
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.Logger;
009:
010: import test.tck.TestHarness;
011: import test.tck.msgflow.callflows.ProtocolObjects;
012:
013: import java.util.*;
014:
015: /**
016: * This class is a UAC template. Shootist is the guy that shoots and shootme is
017: * the guy that gets shot.
018: *
019: * @author M. Ranganathan
020: */
021:
022: public class Shootme implements SipListener {
023:
024: protected static final int PRACK_CODE = 183;
025:
026: private static SipProvider sipProvider;
027:
028: private static AddressFactory addressFactory;
029:
030: private static MessageFactory messageFactory;
031:
032: private static HeaderFactory headerFactory;
033:
034: private static SipStack sipStack;
035:
036: private static final String myAddress = "127.0.0.1";
037:
038: protected ServerTransaction inviteTid;
039:
040: private Request inviteRequest;
041:
042: private Dialog dialog;
043:
044: private String toTag;
045:
046: private String transport;
047:
048: private boolean prackRequestReceived;
049:
050: private boolean inviteReceived;
051:
052: public static final int myPort = 5080;
053:
054: private static Logger logger = Logger.getLogger("test.tck");
055:
056: public Shootme(ProtocolObjects protObjects) {
057: addressFactory = protObjects.addressFactory;
058: messageFactory = protObjects.messageFactory;
059: headerFactory = protObjects.headerFactory;
060: sipStack = protObjects.sipStack;
061: transport = protObjects.transport;
062: }
063:
064: public void processRequest(RequestEvent requestEvent) {
065: Request request = requestEvent.getRequest();
066: ServerTransaction serverTransactionId = requestEvent
067: .getServerTransaction();
068:
069: logger.info("\n\nRequest " + request.getMethod()
070: + " received at " + sipStack.getStackName()
071: + " with server transaction id " + serverTransactionId);
072:
073: if (request.getMethod().equals(Request.INVITE)) {
074: processInvite(requestEvent, serverTransactionId);
075: } else if (request.getMethod().equals(Request.ACK)) {
076: processAck(requestEvent, serverTransactionId);
077: } else if (request.getMethod().equals(Request.BYE)) {
078: processBye(requestEvent, serverTransactionId);
079: } else if (request.getMethod().equals(Request.PRACK)) {
080: processPrack(requestEvent, serverTransactionId);
081: }
082:
083: }
084:
085: private void processPrack(RequestEvent requestEvent,
086: ServerTransaction serverTransactionId) {
087: prackRequestReceived = true;
088: try {
089: logger.info("shootme: got an PRACK! ");
090: logger.info("Dialog State = " + dialog.getState());
091:
092: /**
093: * JvB: First, send 200 OK for PRACK
094: */
095: Request prack = requestEvent.getRequest();
096: Response prackOk = messageFactory
097: .createResponse(200, prack);
098: serverTransactionId.sendResponse(prackOk);
099:
100: /**
101: * Send a 200 OK response to complete the 3 way handshake for the
102: * INIVTE.
103: */
104: Response response = messageFactory.createResponse(200,
105: inviteRequest);
106: ToHeader to = (ToHeader) response.getHeader(ToHeader.NAME);
107: to.setTag(this .toTag);
108: Address address = addressFactory
109: .createAddress("Shootme <sip:" + myAddress + ":"
110: + myPort + ">");
111: ContactHeader contactHeader = headerFactory
112: .createContactHeader(address);
113: response.addHeader(contactHeader);
114: inviteTid.sendResponse(response);
115: } catch (Exception ex) {
116: TestHarness.fail(ex.getMessage());
117: }
118: }
119:
120: public void processResponse(ResponseEvent responseEvent) {
121: }
122:
123: /**
124: * Process the ACK request. Send the bye and complete the call flow.
125: */
126: public void processAck(RequestEvent requestEvent,
127: ServerTransaction serverTransaction) {
128:
129: try {
130: logger.info("shootme: got an ACK! Sending a BYE");
131: logger.info("Dialog State = " + dialog.getState());
132:
133: // JvB: there should not be a transaction for ACKs; requestEvent
134: // can be used to get it instead
135: // Dialog dialog = serverTransaction.getDialog();
136: Dialog dialog = requestEvent.getDialog();
137:
138: SipProvider provider = (SipProvider) requestEvent
139: .getSource();
140: Request byeRequest = dialog.createRequest(Request.BYE);
141: ClientTransaction ct = provider
142: .getNewClientTransaction(byeRequest);
143: dialog.sendRequest(ct);
144: } catch (Exception ex) {
145: ex.printStackTrace();
146: TestHarness.fail(ex.getMessage());
147: }
148:
149: }
150:
151: /**
152: * Process the invite request.
153: */
154: public void processInvite(RequestEvent requestEvent,
155: ServerTransaction serverTransaction) {
156: inviteReceived = true;
157: SipProvider sipProvider = (SipProvider) requestEvent
158: .getSource();
159: Request request = requestEvent.getRequest();
160: try {
161: logger.info("shootme: got an Invite sending Trying");
162: // logger.info("shootme: " + request);
163: Response response = messageFactory.createResponse(
164: Response.TRYING, request);
165: ServerTransaction st = requestEvent.getServerTransaction();
166:
167: if (st == null) {
168: st = sipProvider.getNewServerTransaction(request);
169: }
170: dialog = st.getDialog();
171:
172: st.sendResponse(response);
173:
174: // reliable provisional response.
175:
176: Response okResponse = messageFactory.createResponse(
177: PRACK_CODE, request);
178: ToHeader toHeader = (ToHeader) okResponse
179: .getHeader(ToHeader.NAME);
180: this .toTag = "4321";
181: toHeader.setTag(toTag); // Application is supposed to set.
182: this .inviteTid = st;
183: this .inviteRequest = request;
184:
185: logger.info("sending reliable provisional response.");
186:
187: RequireHeader requireHeader = headerFactory
188: .createRequireHeader("100rel");
189: okResponse.addHeader(requireHeader);
190: dialog.sendReliableProvisionalResponse(okResponse);
191: } catch (Exception ex) {
192: ex.printStackTrace();
193: TestHarness.fail(ex.getMessage());
194:
195: }
196: }
197:
198: /**
199: * Process the bye request.
200: */
201: public void processBye(RequestEvent requestEvent,
202: ServerTransaction serverTransactionId) {
203: Request request = requestEvent.getRequest();
204: try {
205: logger.info("shootme: got a bye sending OK.");
206: Response response = messageFactory.createResponse(200,
207: request);
208: serverTransactionId.sendResponse(response);
209: logger.info("Dialog State is "
210: + serverTransactionId.getDialog().getState());
211:
212: } catch (Exception ex) {
213: TestHarness.fail(ex.getMessage());
214: System.exit(0);
215:
216: }
217: }
218:
219: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
220: Transaction transaction;
221: if (timeoutEvent.isServerTransaction()) {
222: transaction = timeoutEvent.getServerTransaction();
223: } else {
224: transaction = timeoutEvent.getClientTransaction();
225: }
226: logger.info("state = " + transaction.getState());
227: logger.info("dialog = " + transaction.getDialog());
228: logger.info("dialogState = "
229: + transaction.getDialog().getState());
230: logger.info("Transaction Time out");
231: }
232:
233: public SipProvider createProvider() throws Exception {
234: ListeningPoint lp = sipStack.createListeningPoint("127.0.0.1",
235: myPort, transport);
236:
237: sipProvider = sipStack.createSipProvider(lp);
238: logger.info(transport + " SIP provider " + sipProvider);
239:
240: return sipProvider;
241: }
242:
243: public void processIOException(IOExceptionEvent exceptionEvent) {
244: logger.info("IOException");
245:
246: }
247:
248: public void processTransactionTerminated(
249: TransactionTerminatedEvent transactionTerminatedEvent) {
250: logger.info("Transaction terminated event recieved");
251:
252: }
253:
254: public void processDialogTerminated(
255: DialogTerminatedEvent dialogTerminatedEvent) {
256: logger.info("Dialog terminated event recieved");
257:
258: }
259:
260: public void checkState() {
261: TestHarness.assertTrue(prackRequestReceived && inviteReceived);
262: }
263:
264: }
|