001: package test.tck.msgflow.callflows.refer;
002:
003: import java.util.ArrayList;
004:
005: import javax.sip.ClientTransaction;
006: import javax.sip.Dialog;
007: import javax.sip.DialogTerminatedEvent;
008: import javax.sip.IOExceptionEvent;
009: import javax.sip.ListeningPoint;
010: import javax.sip.RequestEvent;
011: import javax.sip.ResponseEvent;
012: import javax.sip.ServerTransaction;
013: import javax.sip.SipListener;
014: import javax.sip.SipProvider;
015: import javax.sip.SipStack;
016: import javax.sip.Transaction;
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.FromHeader;
025: import javax.sip.header.HeaderFactory;
026: import javax.sip.header.MaxForwardsHeader;
027: import javax.sip.header.ReferToHeader;
028: import javax.sip.header.SubscriptionStateHeader;
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.ConsoleAppender;
036: import org.apache.log4j.FileAppender;
037: import org.apache.log4j.Level;
038: import org.apache.log4j.Logger;
039: import org.apache.log4j.SimpleLayout;
040:
041: import test.tck.TestHarness;
042: import test.tck.msgflow.callflows.ProtocolObjects;
043:
044: /**
045: * This example shows an out-of-dialog REFER scenario:
046: *
047: * referer sends REFER to referee, with Refer-To set to Shootme
048: * referee sends INVITE to Shootme, and NOTIFYs to referer about call progress
049: *
050: * @author Jeroen van Bemmel
051: * @author Ivelin Ivanov
052: */
053:
054: public class Referrer implements SipListener {
055:
056: private SipProvider sipProvider;
057:
058: private AddressFactory addressFactory;
059:
060: private MessageFactory messageFactory;
061:
062: private HeaderFactory headerFactory;
063:
064: private SipStack sipStack;
065:
066: private ContactHeader contactHeader;
067:
068: private String transport;
069:
070: public static final int myPort = 5080;
071:
072: private int count;
073:
074: private static Logger logger = Logger.getLogger(Referrer.class);
075:
076: static {
077: try {
078: logger.setLevel(Level.INFO);
079: logger.addAppender(new FileAppender(new SimpleLayout(),
080: "logs/refereroutputlog.txt"));
081: } catch (Exception ex) {
082: throw new RuntimeException(ex);
083: }
084: }
085:
086: private ClientTransaction subscribeTid;
087:
088: private ListeningPoint listeningPoint;
089:
090: public Referrer(ProtocolObjects protObjects) {
091: addressFactory = protObjects.addressFactory;
092: messageFactory = protObjects.messageFactory;
093: headerFactory = protObjects.headerFactory;
094: sipStack = protObjects.sipStack;
095: transport = protObjects.transport;
096: }
097:
098: public void processRequest(RequestEvent requestReceivedEvent) {
099: Request request = requestReceivedEvent.getRequest();
100: ServerTransaction serverTransactionId = requestReceivedEvent
101: .getServerTransaction();
102: String viaBranch = ((ViaHeader) (request
103: .getHeaders(ViaHeader.NAME).next()))
104: .getParameter("branch");
105:
106: logger.info("\n\nRequest " + request.getMethod()
107: + " received at " + sipStack.getStackName()
108: + " with server transaction id " + serverTransactionId
109: + " branch ID = " + viaBranch);
110: //logger.info( request );
111: if (request.getMethod().equals(Request.NOTIFY))
112: processNotify(requestReceivedEvent, serverTransactionId);
113:
114: }
115:
116: public void processNotify(RequestEvent requestEvent,
117: ServerTransaction serverTransactionId) {
118: SipProvider provider = (SipProvider) requestEvent.getSource();
119: Request notify = requestEvent.getRequest();
120: try {
121: logger
122: .info("referer: got a notify count "
123: + this .count++);
124: if (serverTransactionId == null) {
125: logger.info("referer: null TID.");
126: serverTransactionId = provider
127: .getNewServerTransaction(notify);
128: }
129: Dialog dialog = serverTransactionId.getDialog();
130: logger.info("Dialog = " + dialog);
131:
132: TestHarness.assertTrue("Dialog should not be null",
133: dialog != null);
134: logger.info("Dialog State = " + dialog.getState());
135:
136: Response response = messageFactory.createResponse(200,
137: notify);
138: // SHOULD add a Contact
139: ContactHeader contact = (ContactHeader) contactHeader
140: .clone();
141: ((SipURI) contact.getAddress().getURI()).setParameter("id",
142: "sub");
143: response.addHeader(contact);
144: logger.info("Transaction State = "
145: + serverTransactionId.getState());
146: serverTransactionId.sendResponse(response);
147: logger.info("Dialog State = " + dialog.getState());
148: SubscriptionStateHeader subscriptionState = (SubscriptionStateHeader) notify
149: .getHeader(SubscriptionStateHeader.NAME);
150:
151: // Subscription is terminated?
152: String state = subscriptionState.getState();
153: if (state
154: .equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
155: dialog.delete();
156: } else {
157: logger.info("Referer: state now " + state);
158: }
159:
160: } catch (Exception ex) {
161: TestHarness.fail("Failed processing notify, because of "
162: + ex);
163:
164: }
165: }
166:
167: public void processResponse(ResponseEvent responseReceivedEvent) {
168: logger.info("Got a response");
169: Response response = (Response) responseReceivedEvent
170: .getResponse();
171: Transaction tid = responseReceivedEvent.getClientTransaction();
172:
173: logger.info("Response received with client transaction id "
174: + tid + ":\n" + response.getStatusCode());
175: if (tid == null) {
176: logger.info("Stray response -- dropping ");
177: return;
178: }
179: logger.info("transaction state is " + tid.getState());
180: logger.info("Dialog = " + tid.getDialog());
181: if (tid.getDialog() != null)
182: logger
183: .info("Dialog State is "
184: + tid.getDialog().getState());
185:
186: }
187:
188: public SipProvider createProvider() throws Exception {
189:
190: this .listeningPoint = sipStack.createListeningPoint(
191: "127.0.0.1", myPort, transport);
192: sipProvider = sipStack.createSipProvider(listeningPoint);
193: return sipProvider;
194:
195: }
196:
197: public void sendRefer() {
198:
199: try {
200:
201: String fromName = "BigGuy";
202: String fromSipAddress = "here.com";
203: String fromDisplayName = "The Master Blaster";
204:
205: String toSipAddress = "127.0.0.1";
206: String toUser = "referee";
207: String toDisplayName = "Referee";
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: toSipAddress);
231: requestURI.setPort(Referee.myPort); // referee
232: requestURI.setTransportParam(transport);
233:
234: // Create ViaHeaders
235:
236: ArrayList viaHeaders = new ArrayList();
237: int port = sipProvider.getListeningPoint(transport)
238: .getPort();
239: ViaHeader viaHeader = headerFactory.createViaHeader(
240: "127.0.0.1", port, transport, null);
241:
242: // add via headers
243: viaHeaders.add(viaHeader);
244:
245: // Create a new CallId header
246: CallIdHeader callIdHeader = sipProvider.getNewCallId();
247: // JvB: Make sure that the implementation matches the messagefactory
248: callIdHeader = headerFactory
249: .createCallIdHeader(callIdHeader.getCallId());
250:
251: // Create a new Cseq header
252: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
253: Request.REFER);
254:
255: // Create a new MaxForwardsHeader
256: MaxForwardsHeader maxForwards = headerFactory
257: .createMaxForwardsHeader(70);
258:
259: // Create the request.
260: Request request = messageFactory.createRequest(requestURI,
261: Request.REFER, callIdHeader, cSeqHeader,
262: fromHeader, toHeader, viaHeaders, maxForwards);
263: // Create contact headers
264: String host = listeningPoint.getIPAddress();
265:
266: SipURI contactUrl = addressFactory.createSipURI(fromName,
267: host);
268: contactUrl.setPort(listeningPoint.getPort());
269:
270: // Create the contact name address.
271: SipURI contactURI = addressFactory.createSipURI(fromName,
272: host);
273: contactURI.setTransportParam(transport);
274: contactURI.setPort(sipProvider.getListeningPoint(transport)
275: .getPort());
276:
277: Address contactAddress = addressFactory
278: .createAddress(contactURI);
279:
280: // Add the contact address.
281: contactAddress.setDisplayName(fromName);
282:
283: contactHeader = headerFactory
284: .createContactHeader(contactAddress);
285: request.addHeader(contactHeader);
286:
287: // Create the client transaction.
288: subscribeTid = sipProvider.getNewClientTransaction(request);
289:
290: // REFER has an implicit "refer" event
291: // Create an event header for the subscription.
292: // EventHeader eventHeader = headerFactory.createEventHeader("foo");
293: // eventHeader.setEventId("foo");
294: // request.addHeader(eventHeader);
295:
296: ReferToHeader referTo = headerFactory
297: .createReferToHeader(addressFactory
298: .createAddress("<sip:127.0.0.1:" + 5070
299: + ">"));
300: request.addHeader(referTo);
301:
302: logger.info("Refer Dialog = " + subscribeTid.getDialog());
303:
304: // send the request out.
305: subscribeTid.sendRequest();
306: } catch (Throwable ex) {
307: TestHarness
308: .fail("Referrer failed sending Subscribe request, because of "
309: + ex);
310: }
311: }
312:
313: public void processIOException(IOExceptionEvent exceptionEvent) {
314: logger.info("io exception event recieved");
315: TestHarness.fail("IOException unexpected");
316: }
317:
318: public void processTransactionTerminated(
319: TransactionTerminatedEvent tte) {
320: logger.info("transaction terminated:" + tte);
321:
322: }
323:
324: public void processDialogTerminated(
325: DialogTerminatedEvent dialogTerminatedEvent) {
326: logger.info("dialog terminated event recieved");
327: }
328:
329: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
330: logger.info("Transaction Time out");
331: }
332: }
|