001: package test.load.subsnotify;
002:
003: import java.util.ArrayList;
004: import java.util.Properties;
005: import java.util.Random;
006:
007: import javax.sip.ClientTransaction;
008: import javax.sip.Dialog;
009: import javax.sip.DialogTerminatedEvent;
010: import javax.sip.IOExceptionEvent;
011: import javax.sip.ListeningPoint;
012: import javax.sip.PeerUnavailableException;
013: import javax.sip.RequestEvent;
014: import javax.sip.ResponseEvent;
015: import javax.sip.ServerTransaction;
016: import javax.sip.SipFactory;
017: import javax.sip.SipListener;
018: import javax.sip.SipProvider;
019: import javax.sip.SipStack;
020: import javax.sip.TransactionTerminatedEvent;
021: import javax.sip.address.Address;
022: import javax.sip.address.AddressFactory;
023: import javax.sip.address.SipURI;
024: import javax.sip.header.CSeqHeader;
025: import javax.sip.header.CallIdHeader;
026: import javax.sip.header.ContactHeader;
027: import javax.sip.header.ContentTypeHeader;
028: import javax.sip.header.EventHeader;
029: import javax.sip.header.ExpiresHeader;
030: import javax.sip.header.FromHeader;
031: import javax.sip.header.HeaderFactory;
032: import javax.sip.header.MaxForwardsHeader;
033: import javax.sip.header.ToHeader;
034: import javax.sip.header.ViaHeader;
035: import javax.sip.message.MessageFactory;
036: import javax.sip.message.Request;
037: import javax.sip.message.Response;
038:
039: /**
040: * This class is a UAC template.
041: *
042: * @author M. Ranganathan
043: */
044:
045: public class Subscriber implements SipListener {
046:
047: private static SipProvider sipProvider;
048:
049: private static AddressFactory addressFactory;
050:
051: private static MessageFactory messageFactory;
052:
053: private static HeaderFactory headerFactory;
054:
055: private static SipStack sipStack;
056:
057: private ContactHeader contactHeader;
058:
059: private ListeningPoint udpListeningPoint;
060:
061: private SipFactory sipFactory = null;
062:
063: private int seq_num = 1;
064:
065: protected static final String usageString = "java "
066: + "examples.subscriber.Shootist \n"
067: + ">>>> is your class path set to the root?";
068:
069: private static void usage() {
070: System.out.println(usageString);
071: System.exit(0);
072:
073: }
074:
075: public void processRequest(RequestEvent requestReceivedEvent) {
076: Request request = requestReceivedEvent.getRequest();
077: ServerTransaction serverTransactionId = requestReceivedEvent
078: .getServerTransaction();
079:
080: System.out.println("\n\nRequest " + request.getMethod()
081: + " received at " + sipStack.getStackName()
082: + " with server transaction id " + serverTransactionId);
083:
084: // We are the UAC so the only request we get is the BYE.
085: if (request.getMethod().equals(Request.NOTIFY))
086: processNotify(request, serverTransactionId);
087:
088: }
089:
090: public void processNotify(Request request,
091: ServerTransaction serverTransactionId) {
092: try {
093: System.out.println("subsciber: got a notify .");
094: if (serverTransactionId == null) {
095: System.out.println("subsciber: null TID.");
096: return;
097: }
098: Dialog dialog = serverTransactionId.getDialog();
099: System.out.println("Dialog State = " + dialog.getState());
100: Response response = messageFactory.createResponse(200,
101: request);
102: serverTransactionId.sendResponse(response);
103: System.out.println("subsciber: Sending OK.");
104: System.out.println("Dialog State = " + dialog.getState());
105:
106: // Unsubscribe to the Subscription. Note that if you do not unsubscribe the dialog will
107: // live on and eventually you will run out of memory.
108:
109: Request unsub = dialog.createRequest(Request.SUBSCRIBE);
110: ExpiresHeader expiresHeader = headerFactory
111: .createExpiresHeader(0);
112: unsub.addHeader(expiresHeader);
113:
114: EventHeader eventHeader = null;
115: eventHeader = headerFactory.createEventHeader("reg");
116: unsub.addHeader(eventHeader);
117:
118: ClientTransaction ct = sipProvider
119: .getNewClientTransaction(unsub);
120: ct.sendRequest();
121:
122: dialog.delete();
123:
124: } catch (Exception ex) {
125: ex.printStackTrace();
126: System.exit(0);
127:
128: }
129: }
130:
131: public void processResponse(ResponseEvent responseReceivedEvent) {
132: System.out.println("Got a response");
133: Response response = (Response) responseReceivedEvent
134: .getResponse();
135: ClientTransaction tid = responseReceivedEvent
136: .getClientTransaction();
137: CSeqHeader cseq = (CSeqHeader) response
138: .getHeader(CSeqHeader.NAME);
139:
140: System.out.println("Response received : Status Code = "
141: + response.getStatusCode() + " " + cseq);
142: if (tid == null) {
143: System.out.println("Stray response -- dropping ");
144: return;
145: }
146: System.out.println("transaction state is " + tid.getState());
147: System.out.println("Dialog = " + tid.getDialog());
148: System.out.println("Dialog State is "
149: + tid.getDialog().getState());
150:
151: // responses are OKs for SUBSCRIBE. Do nothing
152:
153: }
154:
155: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
156:
157: System.out.println("Transaction Time out");
158: }
159:
160: public void init() {
161: sipStack = null;
162: sipFactory = SipFactory.getInstance();
163: sipFactory.setPathName("gov.nist");
164: Properties properties = new Properties();
165: // If you want to try TCP transport change the following to
166: String transport = "udp";
167: String peerHostPort = "127.0.0.1:5070";
168: properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort
169: + "/" + transport);
170: // If you want to use UDP then uncomment this.
171: properties.setProperty("javax.sip.STACK_NAME", "subscriber");
172:
173: // The following properties are specific to nist-sip
174: // and are not necessarily part of any other jain-sip
175: // implementation.
176: // You can set a max message size for tcp transport to
177: // guard against denial of service attack.
178: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
179: "shootistdebug.txt");
180: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
181: "shootistlog.txt");
182:
183: // Drop the client connection after we are done with the transaction.
184: properties.setProperty(
185: "gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS", "false");
186: // Set to 0 (or NONE) in your production code for max speed.
187: // You need 16 (or TRACE) for logging traces. 32 (or DEBUG) for debug +
188: // traces.
189: // Your code will limp at 32 but it is best for debugging.
190: properties
191: .setProperty("gov.nist.javax.sip.TRACE_LEVEL", "NONE");
192: properties.setProperty(
193: "gov.nist.javax.sip.MAX_LISTENER_RESPONSE_TIME", "20");
194:
195: try {
196: // Create SipStack object
197: sipStack = sipFactory.createSipStack(properties);
198: System.out.println("createSipStack " + sipStack);
199: } catch (PeerUnavailableException e) {
200: // could not find
201: // gov.nist.jain.protocol.ip.sip.SipStackImpl
202: // in the classpath
203: e.printStackTrace();
204: System.err.println(e.getMessage());
205: System.exit(0);
206: }
207:
208: try {
209: headerFactory = sipFactory.createHeaderFactory();
210: addressFactory = sipFactory.createAddressFactory();
211: messageFactory = sipFactory.createMessageFactory();
212: udpListeningPoint = sipStack.createListeningPoint(
213: "127.0.0.1", 5060, "udp");
214: sipProvider = sipStack.createSipProvider(udpListeningPoint);
215: Subscriber listener = this ;
216: sipProvider.addSipListener(listener);
217:
218: // Now we want to start sending out subscribes on this thread.
219: while (true) {
220: CreateSubscribeRequest();
221: java.lang.Thread.sleep(200);
222: }
223:
224: } catch (Exception ex) {
225: System.out.println(ex.getMessage());
226: ex.printStackTrace();
227: usage();
228: }
229:
230: }
231:
232: public void CreateSubscribeRequest() {
233:
234: try {
235: String transport = "udp";
236: String peerHostPort = "127.0.0.1:5070";
237:
238: String fromName = "subscriber";
239: String fromSipAddress = "10.10.10.10";
240: String fromDisplayName = "Mr. subscriber";
241:
242: String toSipAddress = "notifier";
243: String toUser = "8000@10.10.10.10";
244: String toDisplayName = "Mr. notifier";
245:
246: // create >From Header
247: SipURI fromAddress = addressFactory.createSipURI(fromName,
248: fromSipAddress);
249:
250: // Create a new CallId header
251: CallIdHeader callIdHeader = sipProvider.getNewCallId();// headerFactory.createCallIdHeader("8000dlg");
252:
253: Address fromNameAddress = addressFactory
254: .createAddress(fromAddress);
255: fromNameAddress.setDisplayName(fromDisplayName);
256:
257: FromHeader fromHeader = headerFactory.createFromHeader(
258: fromNameAddress, new Long(new Random().nextLong())
259: .toString());
260:
261: // create To Header
262: SipURI toAddress = addressFactory.createSipURI(toUser,
263: toSipAddress);
264: Address toNameAddress = addressFactory
265: .createAddress(toAddress);
266: toNameAddress.setDisplayName(toDisplayName);
267: ToHeader toHeader = headerFactory.createToHeader(
268: toNameAddress, null);
269:
270: // create Request URI
271: SipURI requestURI = addressFactory.createSipURI(toUser,
272: peerHostPort);
273:
274: // Create ViaHeaders
275:
276: ArrayList viaHeaders = new ArrayList();
277: ViaHeader viaHeader = headerFactory.createViaHeader(
278: "127.0.0.1", sipProvider.getListeningPoint(
279: transport).getPort(), transport, null);
280:
281: // add via headers
282: viaHeaders.add(viaHeader);
283:
284: // Create a new Cseq header
285: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(
286: (long) seq_num, Request.SUBSCRIBE);
287: seq_num++;
288:
289: // Create a new MaxForwardsHeader
290: MaxForwardsHeader maxForwards = headerFactory
291: .createMaxForwardsHeader(70);
292:
293: // Create the request.
294: Request request = messageFactory.createRequest(requestURI,
295: Request.SUBSCRIBE, callIdHeader, cSeqHeader,
296: fromHeader, toHeader, viaHeaders, maxForwards);
297: // Create contact headers
298: String host = "127.0.0.1";
299:
300: SipURI contactUrl = addressFactory.createSipURI(fromName,
301: host);
302: contactUrl.setPort(udpListeningPoint.getPort());
303: contactUrl.setLrParam();
304:
305: // Create the contact name address.
306: SipURI contactURI = addressFactory.createSipURI(fromName,
307: host);
308: contactURI.setPort(sipProvider.getListeningPoint(transport)
309: .getPort());
310:
311: Address contactAddress = addressFactory
312: .createAddress(contactURI);
313:
314: // Add the contact address.
315: contactAddress.setDisplayName(fromName);
316:
317: contactHeader = headerFactory
318: .createContactHeader(contactAddress);
319: request.addHeader(contactHeader);
320:
321: ExpiresHeader expires_header = headerFactory
322: .createExpiresHeader(200);
323: request.addHeader(expires_header);
324:
325: EventHeader event_header = null;
326: event_header = headerFactory.createEventHeader("reg");
327: request.addHeader(event_header);
328:
329: ClientTransaction ct = sipProvider
330: .getNewClientTransaction(request);
331: ct.sendRequest();
332:
333: // send the request out.
334:
335: } catch (Exception ex) {
336: System.out.println(ex.getMessage());
337: ex.printStackTrace();
338: usage();
339: }
340:
341: }
342:
343: public static void main(String args[]) {
344: new Subscriber().init();
345:
346: }
347:
348: public void processIOException(IOExceptionEvent exceptionEvent) {
349: System.out.println("IOException happened for "
350: + exceptionEvent.getHost() + " port = "
351: + exceptionEvent.getPort());
352:
353: }
354:
355: public void processTransactionTerminated(
356: TransactionTerminatedEvent transactionTerminatedEvent) {
357: System.out.println("Transaction terminated event recieved");
358: }
359:
360: public void processDialogTerminated(
361: DialogTerminatedEvent dialogTerminatedEvent) {
362: System.out.println("dialogTerminatedEvent");
363:
364: }
365: }
|