001: package test.tck.msgflow.callflows.prack;
002:
003: import java.util.ArrayList;
004:
005: import javax.sip.ClientTransaction;
006: import javax.sip.Dialog;
007: import javax.sip.DialogState;
008: import javax.sip.DialogTerminatedEvent;
009: import javax.sip.IOExceptionEvent;
010: import javax.sip.ListeningPoint;
011: import javax.sip.RequestEvent;
012: import javax.sip.ResponseEvent;
013: import javax.sip.ServerTransaction;
014: import javax.sip.SipListener;
015: import javax.sip.SipProvider;
016: import javax.sip.SipStack;
017: import javax.sip.TransactionTerminatedEvent;
018: import javax.sip.address.Address;
019: import javax.sip.address.AddressFactory;
020: import javax.sip.address.SipURI;
021: import javax.sip.header.CSeqHeader;
022: import javax.sip.header.CallIdHeader;
023: import javax.sip.header.ContactHeader;
024: import javax.sip.header.ContentTypeHeader;
025: import javax.sip.header.FromHeader;
026: import javax.sip.header.HeaderFactory;
027: import javax.sip.header.MaxForwardsHeader;
028: import javax.sip.header.RequireHeader;
029: import javax.sip.header.ToHeader;
030: import javax.sip.header.ViaHeader;
031: import javax.sip.message.MessageFactory;
032: import javax.sip.message.Request;
033: import javax.sip.message.Response;
034:
035: import org.apache.log4j.Logger;
036:
037: import test.tck.TestHarness;
038: import test.tck.msgflow.callflows.ProtocolObjects;
039:
040: /**
041: * This class is a UAC template. Shootist is the guy that shoots and shootme is
042: * the guy that gets shot.
043: *
044: * @author M. Ranganathan
045: */
046:
047: public class Shootist implements SipListener {
048:
049: private static SipProvider sipProvider;
050:
051: private static AddressFactory addressFactory;
052:
053: private static MessageFactory messageFactory;
054:
055: private static HeaderFactory headerFactory;
056:
057: private static SipStack sipStack;
058:
059: private ContactHeader contactHeader;
060:
061: private ClientTransaction inviteTid;
062:
063: private Dialog dialog;
064:
065: private String transport;
066:
067: private boolean prackTriggerReceived;
068: private boolean prackConfirmed;
069:
070: public static final int myPort = 5070;
071:
072: private static Logger logger = Logger.getLogger("test.tck");
073:
074: private String toUser = "LittleGuy";
075:
076: public Shootist(ProtocolObjects protObjects) {
077: addressFactory = protObjects.addressFactory;
078: messageFactory = protObjects.messageFactory;
079: headerFactory = protObjects.headerFactory;
080: sipStack = protObjects.sipStack;
081: transport = protObjects.transport;
082: }
083:
084: public SipProvider createProvider() throws Exception {
085: ListeningPoint lp = sipStack.createListeningPoint("127.0.0.1",
086: myPort, transport);
087:
088: sipProvider = sipStack.createSipProvider(lp);
089: logger.info(transport + " SIP provider " + sipProvider);
090:
091: return sipProvider;
092: }
093:
094: public void processRequest(RequestEvent requestReceivedEvent) {
095: Request request = requestReceivedEvent.getRequest();
096: ServerTransaction serverTransactionId = requestReceivedEvent
097: .getServerTransaction();
098:
099: logger.info("\n\nRequest " + request.getMethod()
100: + " received at " + sipStack.getStackName()
101: + " with server transaction id " + serverTransactionId);
102:
103: // We are the UAC so the only request we get is the BYE.
104: if (request.getMethod().equals(Request.BYE))
105: processBye(request, serverTransactionId);
106:
107: }
108:
109: public void processBye(Request request,
110: ServerTransaction serverTransactionId) {
111: try {
112: logger.info("shootist: got a bye .");
113: if (serverTransactionId == null) {
114: logger.info("shootist: null TID.");
115: return;
116: }
117: Dialog dialog = serverTransactionId.getDialog();
118: logger.info("Dialog State = " + dialog.getState());
119: Response response = messageFactory.createResponse(200,
120: request);
121: serverTransactionId.sendResponse(response);
122: logger.info("shootist: Sending OK.");
123: logger.info("Dialog State = " + dialog.getState());
124:
125: } catch (Exception ex) {
126: TestHarness.fail(ex.getMessage());
127: System.exit(0);
128:
129: }
130: }
131:
132: public void processResponse(ResponseEvent responseReceivedEvent) {
133: logger.info("Got a response");
134: Response response = (Response) responseReceivedEvent
135: .getResponse();
136: ClientTransaction tid = responseReceivedEvent
137: .getClientTransaction();
138: CSeqHeader cseq = (CSeqHeader) response
139: .getHeader(CSeqHeader.NAME);
140:
141: logger.info("Response received : Status Code = "
142: + response.getStatusCode() + " " + cseq);
143:
144: if (cseq.getMethod() == Request.PRACK) {
145: prackConfirmed = true;
146: }
147:
148: if (tid == null) {
149: logger.info("Stray response -- dropping ");
150: return;
151: }
152: logger.info("transaction state is " + tid.getState());
153: logger.info("Dialog = " + tid.getDialog());
154: logger.info("Dialog State is " + tid.getDialog().getState());
155: SipProvider provider = (SipProvider) responseReceivedEvent
156: .getSource();
157: dialog = tid.getDialog();
158:
159: try {
160: if (response.getStatusCode() == Response.OK) {
161: if (cseq.getMethod().equals(Request.INVITE)) {
162: Request ackRequest = dialog
163: .createAck(((CSeqHeader) response
164: .getHeader(CSeqHeader.NAME))
165: .getSeqNumber());
166: logger.info("Sending ACK");
167: dialog.sendAck(ackRequest);
168: }
169:
170: } else if (response.getStatusCode() == Shootme.PRACK_CODE) {
171: prackTriggerReceived = true;
172: RequireHeader requireHeader = (RequireHeader) response
173: .getHeader(RequireHeader.NAME);
174: if (requireHeader.getOptionTag().equalsIgnoreCase(
175: "100rel")) {
176: Dialog dialog = tid.getDialog();
177: Request prackRequest = dialog.createPrack(response);
178: // create Request URI
179: SipURI requestURI = addressFactory.createSipURI(
180: toUser, "127.0.0.1:" + Shootme.myPort);
181: prackRequest.setRequestURI(requestURI);
182: ClientTransaction ct = provider
183: .getNewClientTransaction(prackRequest);
184: dialog.sendRequest(ct);
185: }
186: }
187: } catch (Exception ex) {
188: ex.printStackTrace();
189: TestHarness.fail("Unexpected exception " + ex.getMessage());
190:
191: }
192:
193: }
194:
195: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
196:
197: logger.info("Transaction Time out");
198: }
199:
200: public void sendInvite() {
201: try {
202: String fromName = "BigGuy";
203: String fromSipAddress = "here.com";
204: String fromDisplayName = "The Master Blaster";
205:
206: String toSipAddress = "there.com";
207: String toDisplayName = "The Little Blister";
208:
209: // create >From Header
210: SipURI fromAddress = addressFactory.createSipURI(fromName,
211: fromSipAddress);
212:
213: Address fromNameAddress = addressFactory
214: .createAddress(fromAddress);
215: fromNameAddress.setDisplayName(fromDisplayName);
216: FromHeader fromHeader = headerFactory.createFromHeader(
217: fromNameAddress, "12345");
218:
219: // create To Header
220: SipURI toAddress = addressFactory.createSipURI(toUser,
221: toSipAddress);
222: Address toNameAddress = addressFactory
223: .createAddress(toAddress);
224: toNameAddress.setDisplayName(toDisplayName);
225: ToHeader toHeader = headerFactory.createToHeader(
226: toNameAddress, null);
227:
228: // create Request URI
229: SipURI requestURI = addressFactory.createSipURI(toUser,
230: "127.0.0.1:" + Shootme.myPort);
231:
232: // Create ViaHeaders
233:
234: ArrayList viaHeaders = new ArrayList();
235: ViaHeader viaHeader = headerFactory.createViaHeader(
236: "127.0.0.1", sipProvider.getListeningPoint(
237: transport).getPort(), transport, null);
238:
239: // add via headers
240: viaHeaders.add(viaHeader);
241:
242: // Create ContentTypeHeader
243: ContentTypeHeader contentTypeHeader = headerFactory
244: .createContentTypeHeader("application", "sdp");
245:
246: // Create a new CallId header
247: CallIdHeader callIdHeader = sipProvider.getNewCallId();
248: // JvB: Make sure that the implementation matches the messagefactory
249: callIdHeader = headerFactory
250: .createCallIdHeader(callIdHeader.getCallId());
251:
252: // Create a new Cseq header
253: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
254: Request.INVITE);
255:
256: // Create a new MaxForwardsHeader
257: MaxForwardsHeader maxForwards = headerFactory
258: .createMaxForwardsHeader(70);
259:
260: // Create the request.
261: Request request = messageFactory.createRequest(requestURI,
262: Request.INVITE, callIdHeader, cSeqHeader,
263: fromHeader, toHeader, viaHeaders, maxForwards);
264: // Create contact headers
265: String host = "127.0.0.1";
266:
267: SipURI contactUrl = addressFactory.createSipURI(fromName,
268: host);
269: ListeningPoint lp = sipProvider
270: .getListeningPoint(transport);
271: contactUrl.setPort(lp.getPort());
272:
273: // Create the contact name address.
274: SipURI contactURI = addressFactory.createSipURI(fromName,
275: host);
276: contactURI.setPort(sipProvider.getListeningPoint(transport)
277: .getPort());
278:
279: Address contactAddress = addressFactory
280: .createAddress(contactURI);
281:
282: // Add the contact address.
283: contactAddress.setDisplayName(fromName);
284:
285: contactHeader = headerFactory
286: .createContactHeader(contactAddress);
287: request.addHeader(contactHeader);
288:
289: /*
290: * When the UAC creates a new request, it can insist on reliable
291: * delivery of provisional responses for that request. To do that,
292: * it inserts a Require header field with the option tag 100rel into
293: * the request.
294: */
295:
296: RequireHeader requireHeader = headerFactory
297: .createRequireHeader("100rel");
298: request.addHeader(requireHeader);
299: // Create the client transaction.
300: inviteTid = sipProvider.getNewClientTransaction(request);
301:
302: this .dialog = inviteTid.getDialog();
303:
304: // send the request out.
305: inviteTid.sendRequest();
306:
307: } catch (Exception ex) {
308: TestHarness.fail("sendInvite failed because of "
309: + ex.getMessage());
310: }
311: }
312:
313: public void processIOException(IOExceptionEvent exceptionEvent) {
314: logger.info("IOException happened for "
315: + exceptionEvent.getHost() + " port = "
316: + exceptionEvent.getPort());
317:
318: }
319:
320: public void processTransactionTerminated(
321: TransactionTerminatedEvent transactionTerminatedEvent) {
322: logger.info("Transaction terminated event recieved");
323: }
324:
325: public void processDialogTerminated(
326: DialogTerminatedEvent dialogTerminatedEvent) {
327: logger.info("dialogTerminatedEvent");
328: }
329:
330: public void checkState() {
331: TestHarness.assertTrue(this.prackTriggerReceived);
332: TestHarness.assertTrue(prackConfirmed);
333: }
334:
335: }
|