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