001: package examples.refer;
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.ConsoleAppender;
009: import org.apache.log4j.FileAppender;
010: import org.apache.log4j.Level;
011: import org.apache.log4j.Logger;
012: import org.apache.log4j.SimpleLayout;
013:
014: import java.util.*;
015:
016: /**
017: * This example shows an out-of-dialog REFER scenario:
018: *
019: * referer sends REFER to referee, with Refer-To set to Shootme
020: * referee sends INVITE to Shootme, and NOTIFYs to referer about call progress
021: *
022: * @author Jeroen van Bemmel
023: */
024:
025: public class Referer implements SipListener {
026:
027: private SipProvider sipProvider;
028:
029: private static AddressFactory addressFactory;
030:
031: private static MessageFactory messageFactory;
032:
033: private static HeaderFactory headerFactory;
034:
035: private static SipStack sipStack;
036:
037: private ContactHeader contactHeader;
038:
039: private static String transport;
040:
041: private int count;
042:
043: private static Logger logger = Logger.getLogger(Referer.class);
044:
045: static {
046: try {
047: logger.setLevel(Level.INFO);
048: logger.addAppender(new ConsoleAppender(new SimpleLayout()));
049: logger.addAppender(new FileAppender(new SimpleLayout(),
050: "refereroutputlog.txt"));
051: } catch (Exception ex) {
052: throw new RuntimeException(ex);
053: }
054: }
055:
056: private ClientTransaction subscribeTid;
057:
058: private ListeningPoint listeningPoint;
059:
060: protected static final String usageString = "java "
061: + "examples.subsnotify.Subscriber \n"
062: + ">>>> is your class path set to the root?";
063:
064: private static void usage() {
065: logger.info(usageString);
066: System.exit(0);
067:
068: }
069:
070: public void processRequest(RequestEvent requestReceivedEvent) {
071: Request request = requestReceivedEvent.getRequest();
072: ServerTransaction serverTransactionId = requestReceivedEvent
073: .getServerTransaction();
074: String viaBranch = ((ViaHeader) (request
075: .getHeaders(ViaHeader.NAME).next()))
076: .getParameter("branch");
077:
078: logger.info("\n\nRequest " + request.getMethod()
079: + " received at " + sipStack.getStackName()
080: + " with server transaction id " + serverTransactionId
081: + " branch ID = " + viaBranch);
082: logger.info(request);
083: if (request.getMethod().equals(Request.NOTIFY))
084: processNotify(requestReceivedEvent, serverTransactionId);
085:
086: }
087:
088: public void processNotify(RequestEvent requestEvent,
089: ServerTransaction serverTransactionId) {
090: SipProvider provider = (SipProvider) requestEvent.getSource();
091: Request notify = requestEvent.getRequest();
092: try {
093: logger
094: .info("referer: got a notify count "
095: + this .count++);
096: if (serverTransactionId == null) {
097: logger.info("referer: null TID.");
098: serverTransactionId = provider
099: .getNewServerTransaction(notify);
100: }
101: Dialog dialog = serverTransactionId.getDialog();
102: logger.info("Dialog = " + dialog);
103:
104: if (dialog != null) {
105: logger.info("Dialog State = " + dialog.getState());
106: }
107: Response response = messageFactory.createResponse(200,
108: notify);
109: // SHOULD add a Contact
110: ContactHeader contact = (ContactHeader) contactHeader
111: .clone();
112: ((SipURI) contact.getAddress().getURI()).setParameter("id",
113: "sub");
114: response.addHeader(contact);
115: logger.info("Transaction State = "
116: + serverTransactionId.getState());
117: serverTransactionId.sendResponse(response);
118: if (dialog != null) {
119: logger.info("Dialog State = " + dialog.getState());
120: }
121: SubscriptionStateHeader subscriptionState = (SubscriptionStateHeader) notify
122: .getHeader(SubscriptionStateHeader.NAME);
123:
124: // Subscription is terminated?
125: String state = subscriptionState.getState();
126: if (state
127: .equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
128: dialog.delete();
129: } else {
130: logger.info("Referer: state now " + state);
131: }
132:
133: } catch (Exception ex) {
134: ex.printStackTrace();
135: logger.error("Unexpected exception", ex);
136: System.exit(0);
137:
138: }
139: }
140:
141: public void processResponse(ResponseEvent responseReceivedEvent) {
142: logger.info("Got a response");
143: Response response = (Response) responseReceivedEvent
144: .getResponse();
145: Transaction tid = responseReceivedEvent.getClientTransaction();
146:
147: logger.info("Response received with client transaction id "
148: + tid + ":\n" + response.getStatusCode());
149: if (tid == null) {
150: logger.info("Stray response -- dropping ");
151: return;
152: }
153: logger.info("transaction state is " + tid.getState());
154: logger.info("Dialog = " + tid.getDialog());
155: if (tid.getDialog() != null)
156: logger
157: .info("Dialog State is "
158: + tid.getDialog().getState());
159:
160: }
161:
162: public void createProvider() throws Exception {
163:
164: this .listeningPoint = sipStack.createListeningPoint(
165: "127.0.0.1", 5060, transport);
166: sipProvider = sipStack.createSipProvider(listeningPoint);
167:
168: }
169:
170: public void sendSubscribe() {
171:
172: try {
173:
174: String fromName = "BigGuy";
175: String fromSipAddress = "here.com";
176: String fromDisplayName = "The Master Blaster";
177:
178: String toSipAddress = "127.0.0.1";
179: String toUser = "referee";
180: String toDisplayName = "Referee";
181:
182: // create >From Header
183: SipURI fromAddress = addressFactory.createSipURI(fromName,
184: fromSipAddress);
185:
186: Address fromNameAddress = addressFactory
187: .createAddress(fromAddress);
188: fromNameAddress.setDisplayName(fromDisplayName);
189: FromHeader fromHeader = headerFactory.createFromHeader(
190: fromNameAddress, "12345");
191:
192: // create To Header
193: SipURI toAddress = addressFactory.createSipURI(toUser,
194: toSipAddress);
195: Address toNameAddress = addressFactory
196: .createAddress(toAddress);
197: toNameAddress.setDisplayName(toDisplayName);
198: ToHeader toHeader = headerFactory.createToHeader(
199: toNameAddress, null);
200:
201: // create Request URI
202: SipURI requestURI = addressFactory.createSipURI(toUser,
203: toSipAddress);
204: requestURI.setPort(5065); // referee
205:
206: // Create ViaHeaders
207:
208: ArrayList viaHeaders = new ArrayList();
209: int port = sipProvider.getListeningPoint(transport)
210: .getPort();
211: ViaHeader viaHeader = headerFactory.createViaHeader(
212: "127.0.0.1", port, transport, null);
213:
214: // add via headers
215: viaHeaders.add(viaHeader);
216:
217: // Create a new CallId header
218: CallIdHeader callIdHeader = sipProvider.getNewCallId();
219:
220: // Create a new Cseq header
221: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
222: Request.REFER);
223:
224: // Create a new MaxForwardsHeader
225: MaxForwardsHeader maxForwards = headerFactory
226: .createMaxForwardsHeader(70);
227:
228: // Create the request.
229: Request request = messageFactory.createRequest(requestURI,
230: Request.REFER, callIdHeader, cSeqHeader,
231: fromHeader, toHeader, viaHeaders, maxForwards);
232: // Create contact headers
233: String host = listeningPoint.getIPAddress();
234:
235: SipURI contactUrl = addressFactory.createSipURI(fromName,
236: host);
237: contactUrl.setPort(listeningPoint.getPort());
238:
239: // Create the contact name address.
240: SipURI contactURI = addressFactory.createSipURI(fromName,
241: host);
242: contactURI.setTransportParam(transport);
243: contactURI.setPort(sipProvider.getListeningPoint(transport)
244: .getPort());
245:
246: Address contactAddress = addressFactory
247: .createAddress(contactURI);
248:
249: // Add the contact address.
250: contactAddress.setDisplayName(fromName);
251:
252: contactHeader = headerFactory
253: .createContactHeader(contactAddress);
254: request.addHeader(contactHeader);
255:
256: // Create the client transaction.
257: subscribeTid = sipProvider.getNewClientTransaction(request);
258:
259: // REFER has an implicit "refer" event
260: // Create an event header for the subscription.
261: // EventHeader eventHeader = headerFactory.createEventHeader("foo");
262: // eventHeader.setEventId("foo");
263: // request.addHeader(eventHeader);
264:
265: ReferToHeader referTo = headerFactory
266: .createReferToHeader(addressFactory
267: .createAddress("<sip:127.0.0.1:" + 5070
268: + ">"));
269: request.addHeader(referTo);
270:
271: logger.info("Refer Dialog = " + subscribeTid.getDialog());
272:
273: // send the request out.
274: subscribeTid.sendRequest();
275:
276: } catch (Throwable ex) {
277: logger.info(ex.getMessage());
278: ex.printStackTrace();
279: usage();
280: }
281: }
282:
283: public static void main(String args[]) throws Exception {
284:
285: transport = "udp";
286:
287: SipFactory sipFactory = SipFactory.getInstance();
288: sipFactory.setPathName("gov.nist");
289: Properties properties = new Properties();
290:
291: properties.setProperty("javax.sip.USE_ROUTER_FOR_ALL_URIS",
292: "false");
293:
294: properties.setProperty("javax.sip.STACK_NAME", "referer");
295: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
296: "refererdebug.txt");
297: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
298: "refererlog.txt");
299:
300: properties.setProperty("javax.sip.FORKABLE_EVENTS", "foo");
301:
302: // Set to 0 in your production code for max speed.
303: // You need 16 for logging traces. 32 for debug + traces.
304: // Your code will limp at 32 but it is best for debugging.
305: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
306:
307: sipStack = sipFactory.createSipStack(properties);
308: logger.info("createSipStack " + sipStack);
309: headerFactory = sipFactory.createHeaderFactory();
310: addressFactory = sipFactory.createAddressFactory();
311: messageFactory = sipFactory.createMessageFactory();
312:
313: Referer subscriber = new Referer();
314: subscriber.createProvider();
315: subscriber.sipProvider.addSipListener(subscriber);
316: subscriber.sendSubscribe();
317:
318: }
319:
320: public void processIOException(IOExceptionEvent exceptionEvent) {
321: logger.info("io exception event recieved");
322: }
323:
324: public void processTransactionTerminated(
325: TransactionTerminatedEvent tte) {
326: logger.info("transaction terminated:" + tte);
327:
328: }
329:
330: public void processDialogTerminated(
331: DialogTerminatedEvent dialogTerminatedEvent) {
332: logger.info("dialog terminated event recieved");
333: }
334:
335: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
336:
337: logger.info("Transaction Time out");
338: }
339: }
|