001: package test.load.subsnotify;
002:
003: import javax.sip.*;
004: import javax.sip.address.*;
005: import javax.sip.header.*;
006: import javax.sip.message.*;
007: import java.util.*;
008:
009: /**
010: * This class is a UAS template.
011: *
012: * @author M. Ranganathan
013: */
014:
015: public class Notifier implements SipListener {
016:
017: private static AddressFactory addressFactory;
018:
019: private static MessageFactory messageFactory;
020:
021: private static HeaderFactory headerFactory;
022:
023: private static SipStack sipStack;
024:
025: private static final int myPort = 5070;
026:
027: protected ServerTransaction inviteTid;
028:
029: private SipProvider sipProvider;
030:
031: class MyTimerTask extends TimerTask {
032: Notifier notify;
033:
034: public MyTimerTask(Notifier notify1) {
035: notify = notify1;
036:
037: }
038:
039: public void run() {
040:
041: }
042:
043: }
044:
045: protected static final String usageString = "java " + "notifier \n"
046: + ">>>> is your class path set to the root?";
047:
048: private static void usage() {
049: System.out.println(usageString);
050: System.exit(0);
051:
052: }
053:
054: public void processRequest(RequestEvent requestEvent) {
055: Request request = requestEvent.getRequest();
056: ServerTransaction serverTransactionId = requestEvent
057: .getServerTransaction();
058:
059: System.out.println("\n\nRequest " + request.getMethod()
060: + " received at " + sipStack.getStackName()
061: + " with server transaction id " + serverTransactionId);
062:
063: if (request.getMethod().equals(Request.SUBSCRIBE)) {
064: ServerTransaction st = processSubscribe(requestEvent,
065: serverTransactionId);
066: // This could be a retransmission for a Server Transaction
067: // associated with Dialog that we have already deleted.
068: if (st.getDialog().getState() != DialogState.TERMINATED) {
069: sendNotify(st.getDialog());
070: }
071: }
072:
073: }
074:
075: public void sendNotify(Dialog dialog) {
076:
077: try {
078:
079: Request request = dialog.createRequest(Request.NOTIFY);
080:
081: // Create a new MaxForwardsHeader
082: MaxForwardsHeader maxForwards = headerFactory
083: .createMaxForwardsHeader(70);
084: request.setHeader(maxForwards);
085:
086: // Create contact headers
087: String host = "127.0.0.1";
088:
089: SipURI contactUrl = addressFactory.createSipURI(null, host);
090: contactUrl.setLrParam();
091: contactUrl.setPort(sipProvider.getListeningPoint("udp")
092: .getPort());
093:
094: Address contactAddress = addressFactory
095: .createAddress(contactUrl);
096:
097: request.setHeader(headerFactory
098: .createContactHeader(contactAddress));
099:
100: EventHeader eventHeader = null;
101: eventHeader = headerFactory.createEventHeader("reg");
102: request.addHeader(eventHeader);
103:
104: SubscriptionStateHeader subscriptionStateHeader = headerFactory
105: .createSubscriptionStateHeader("active");
106: subscriptionStateHeader.setExpires(200);
107: request.addHeader(subscriptionStateHeader);
108:
109: ClientTransaction ct = sipProvider
110: .getNewClientTransaction(request);
111: dialog.sendRequest(ct);
112:
113: // send the request out.
114:
115: } catch (Exception ex) {
116: System.out.println(ex.getMessage());
117: ex.printStackTrace();
118: usage();
119: }
120:
121: }
122:
123: public void processResponse(ResponseEvent responseEvent) {
124: }
125:
126: public ServerTransaction processSubscribe(
127: RequestEvent requestEvent,
128: ServerTransaction serverTransactionId) {
129: SipProvider sipProvider = (SipProvider) requestEvent
130: .getSource();
131: Request request = requestEvent.getRequest();
132: try {
133: System.out
134: .println("notifier: got a subscribe. sending OK.");
135: Response response = messageFactory.createResponse(200,
136: request);
137: if (serverTransactionId == null) {
138: serverTransactionId = sipProvider
139: .getNewServerTransaction(request);
140: }
141:
142: ExpiresHeader expires = (ExpiresHeader) request
143: .getHeader(ExpiresHeader.NAME);
144: response.addHeader(request.getHeader(ExpiresHeader.NAME));
145: response.addHeader(request.getHeader(EventHeader.NAME));
146: ToHeader toHeader = (ToHeader) response
147: .getHeader(ToHeader.NAME);
148: if (toHeader.getTag() == null) {
149: String tag = new Long(new java.util.Random().nextLong())
150: .toString();
151: toHeader.setTag(tag);
152: }
153: serverTransactionId.sendResponse(response);
154: System.out.println("Dialog State is "
155: + serverTransactionId.getDialog().getState());
156: if (expires.getExpires() == 0) {
157: System.out.println("Deleting Dialog ");
158: serverTransactionId.getDialog().delete();
159: }
160: return serverTransactionId;
161: } catch (Exception ex) {
162: ex.printStackTrace();
163: System.exit(0);
164:
165: }
166: return null;
167: }
168:
169: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
170: Transaction transaction;
171: if (timeoutEvent.isServerTransaction()) {
172: transaction = timeoutEvent.getServerTransaction();
173: } else {
174: transaction = timeoutEvent.getClientTransaction();
175: }
176: System.out.println("state = " + transaction.getState());
177: System.out.println("dialog = " + transaction.getDialog());
178: System.out.println("dialogState = "
179: + transaction.getDialog().getState());
180: System.out.println("Transaction Time out");
181: }
182:
183: public void init() {
184: SipFactory sipFactory = null;
185: sipStack = null;
186: sipFactory = SipFactory.getInstance();
187: sipFactory.setPathName("gov.nist");
188: Properties properties = new Properties();
189: String transport = "udp";
190: String peerHostPort = "127.0.0.1:5060";
191: properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort
192: + "/" + transport);
193: properties.setProperty("javax.sip.STACK_NAME", "notifier");
194: // You need 16 for logging traces. 32 for debug + traces.
195: // Your code will limp at 32 but it is best for debugging.
196: properties
197: .setProperty("gov.nist.javax.sip.TRACE_LEVEL", "NONE");
198: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
199: "shootmedebug.txt");
200: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
201: "shootmelog.txt");
202: properties.setProperty(
203: "gov.nist.javax.sip.MAX_LISTENER_RESPONSE_TIME", "20");
204:
205: try {
206: // Create SipStack object
207: sipStack = sipFactory.createSipStack(properties);
208: System.out.println("sipStack = " + sipStack);
209: } catch (PeerUnavailableException e) {
210: // could not find
211: // gov.nist.jain.protocol.ip.sip.SipStackImpl
212: // in the classpath
213: e.printStackTrace();
214: System.err.println(e.getMessage());
215: if (e.getCause() != null)
216: e.getCause().printStackTrace();
217: System.exit(0);
218: }
219:
220: try {
221: headerFactory = sipFactory.createHeaderFactory();
222: addressFactory = sipFactory.createAddressFactory();
223: messageFactory = sipFactory.createMessageFactory();
224: ListeningPoint lp = sipStack.createListeningPoint(
225: "127.0.0.1", myPort, "udp");
226:
227: Notifier listener = this ;
228:
229: sipProvider = sipStack.createSipProvider(lp);
230: System.out.println("udp provider " + sipProvider);
231: sipProvider.addSipListener(listener);
232:
233: } catch (Exception ex) {
234: System.out.println(ex.getMessage());
235: ex.printStackTrace();
236: usage();
237: }
238:
239: }
240:
241: public static void main(String args[]) {
242: new Notifier().init();
243: }
244:
245: public void processIOException(IOExceptionEvent exceptionEvent) {
246: System.out.println("IOException");
247:
248: }
249:
250: public void processTransactionTerminated(
251: TransactionTerminatedEvent transactionTerminatedEvent) {
252: System.out.println("Transaction terminated event recieved");
253:
254: }
255:
256: public void processDialogTerminated(
257: DialogTerminatedEvent dialogTerminatedEvent) {
258: System.out.println("Dialog terminated event recieved");
259:
260: }
261:
262: }
|