001: package examples.publish;
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: * The notifier
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: protected SipProvider tcpProvider;
026:
027: protected SipProvider udpProvider;
028:
029: protected Dialog dialog;
030:
031: protected static final String usageString = "java "
032: + "examples.shootist.Shootist \n"
033: + ">>>> is your class path set to the root?";
034:
035: private static void usage() {
036: System.out.println(usageString);
037: System.exit(0);
038:
039: }
040:
041: public void processRequest(RequestEvent requestEvent) {
042: Request request = requestEvent.getRequest();
043: ServerTransaction serverTransactionId = requestEvent
044: .getServerTransaction();
045:
046: System.out.println("\n\nRequest " + request.getMethod()
047: + " received at " + sipStack.getStackName()
048: + " with server transaction id " + serverTransactionId);
049:
050: if (request.getMethod().equals(Request.SUBSCRIBE)) {
051: processSubscribe(requestEvent, serverTransactionId);
052: } else if (request.getMethod().equals(Request.PUBLISH)) {
053: processPublish(requestEvent, serverTransactionId);
054: }
055:
056: }
057:
058: private void processPublish(RequestEvent requestEvent,
059: ServerTransaction serverTransactionId) {
060: try {
061: EventHeader eventHeader = (EventHeader) requestEvent
062: .getRequest().getHeader(EventHeader.NAME);
063: Request request = this .dialog.createRequest(Request.NOTIFY);
064: SubscriptionStateHeader subscriptionState = headerFactory
065: .createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE);
066: request.addHeader(subscriptionState);
067: request.addHeader(eventHeader);
068: ClientTransaction ct = udpProvider
069: .getNewClientTransaction(request);
070: this .dialog.sendRequest(ct);
071: } catch (Exception ex) {
072: ex.printStackTrace();
073: System.exit(0);
074: }
075: }
076:
077: /**
078: * Process the invite request.
079: */
080: public void processSubscribe(RequestEvent requestEvent,
081: ServerTransaction serverTransaction) {
082: SipProvider sipProvider = (SipProvider) requestEvent
083: .getSource();
084: Request request = requestEvent.getRequest();
085: try {
086: System.out.println("notifier: got an Subscribe sending OK");
087: System.out.println("notifier: " + request);
088: EventHeader eventHeader = (EventHeader) request
089: .getHeader(EventHeader.NAME);
090: if (eventHeader == null) {
091: System.out
092: .println("Cannot find event header.... dropping request.");
093: return;
094: }
095: Response response = messageFactory.createResponse(202,
096: request);
097: ToHeader toHeader = (ToHeader) response
098: .getHeader(ToHeader.NAME);
099: toHeader.setTag("4321"); // Application is supposed to set.
100: Address address = addressFactory
101: .createAddress("Notifier <sip:127.0.0.1:5070>");
102: ContactHeader contactHeader = headerFactory
103: .createContactHeader(address);
104: response.addHeader(contactHeader);
105: ServerTransaction st = requestEvent.getServerTransaction();
106:
107: if (st == null) {
108: st = sipProvider.getNewServerTransaction(request);
109: }
110: System.out.println("got a server transaction " + st);
111:
112: this .dialog = st.getDialog();
113: // subscribe dialogs do not terminate on bye.
114: this .dialog.terminateOnBye(false);
115: if (dialog != null) {
116: System.out.println("Dialog " + dialog);
117: System.out.println("Dialog state " + dialog.getState());
118: }
119: st.sendResponse(response);
120: if (dialog != null) {
121: System.out.println("Dialog " + dialog);
122: System.out.println("Dialog state " + dialog.getState());
123: }
124: /*
125: * NOTIFY requests MUST contain a "Subscription-State" header with a
126: * value of "active", "pending", or "terminated". The "active" value
127: * indicates that the subscription has been accepted and has been
128: * authorized (in most cases; see section 5.2.). The "pending" value
129: * indicates that the subscription has been received, but that
130: * policy information is insufficient to accept or deny the
131: * subscription at this time. The "terminated" value indicates that
132: * the subscription is not active.
133: */
134: Request notifyRequest = dialog
135: .createRequest(Request.NOTIFY);
136: SubscriptionStateHeader sstate = headerFactory
137: .createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE);
138: notifyRequest.addHeader(sstate);
139: notifyRequest.addHeader(eventHeader);
140: ClientTransaction ct = udpProvider
141: .getNewClientTransaction(notifyRequest);
142: dialog.sendRequest(ct);
143:
144: } catch (Exception ex) {
145: ex.printStackTrace();
146: System.exit(0);
147: }
148: }
149:
150: public void processResponse(ResponseEvent responseReceivedEvent) {
151: System.out.println("Got a response");
152: Response response = (Response) responseReceivedEvent
153: .getResponse();
154: Transaction tid = responseReceivedEvent.getClientTransaction();
155:
156: System.out
157: .println("Response received with client transaction id "
158: + tid + ":\n" + response);
159:
160: }
161:
162: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
163: Transaction transaction;
164: if (timeoutEvent.isServerTransaction()) {
165: transaction = timeoutEvent.getServerTransaction();
166: } else {
167: transaction = timeoutEvent.getClientTransaction();
168: }
169: System.out.println("state = " + transaction.getState());
170: System.out.println("dialog = " + transaction.getDialog());
171: System.out.println("dialogState = "
172: + transaction.getDialog().getState());
173: System.out.println("Transaction Time out");
174: }
175:
176: public void init() {
177: SipFactory sipFactory = null;
178: sipStack = null;
179: sipFactory = SipFactory.getInstance();
180: sipFactory.setPathName("gov.nist");
181: Properties properties = new Properties();
182:
183: properties.setProperty("javax.sip.STACK_NAME", "notifier");
184: // You need 16 for logging traces. 32 for debug + traces.
185: // Your code will limp at 32 but it is best for debugging.
186: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
187: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
188: "notifierdebug.txt");
189: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
190: "notifierlog.txt");
191: properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
192: "4096");
193: properties.setProperty(
194: "gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS", "false");
195:
196: try {
197: // Create SipStack object
198: sipStack = sipFactory.createSipStack(properties);
199: System.out.println("sipStack = " + sipStack);
200: } catch (PeerUnavailableException e) {
201: // could not find
202: // gov.nist.jain.protocol.ip.sip.SipStackImpl
203: // in the classpath
204: e.printStackTrace();
205: System.err.println(e.getMessage());
206: if (e.getCause() != null)
207: e.getCause().printStackTrace();
208: System.exit(0);
209: }
210:
211: try {
212: headerFactory = sipFactory.createHeaderFactory();
213: addressFactory = sipFactory.createAddressFactory();
214: messageFactory = sipFactory.createMessageFactory();
215: ListeningPoint lp = sipStack.createListeningPoint(
216: "127.0.0.1", 5070, "udp");
217: ListeningPoint lp1 = sipStack.createListeningPoint(
218: "127.0.0.1", 5070, "tcp");
219:
220: Notifier listener = this ;
221:
222: this .udpProvider = sipStack.createSipProvider(lp);
223: System.out.println("udp provider " + udpProvider);
224: udpProvider.addSipListener(listener);
225: this .tcpProvider = sipStack.createSipProvider(lp1);
226: System.out.println("tcp provider " + tcpProvider);
227: tcpProvider.addSipListener(listener);
228:
229: } catch (Exception ex) {
230: System.out.println(ex.getMessage());
231: ex.printStackTrace();
232: usage();
233: }
234:
235: }
236:
237: public static void main(String args[]) {
238: new Notifier().init();
239: }
240:
241: public void processIOException(IOExceptionEvent exceptionEvent) {
242: // TODO Auto-generated method stub
243:
244: }
245:
246: public void processTransactionTerminated(
247: TransactionTerminatedEvent transactionTerminatedEvent) {
248: // TODO Auto-generated method stub
249:
250: }
251:
252: public void processDialogTerminated(
253: DialogTerminatedEvent dialogTerminatedEvent) {
254: // TODO Auto-generated method stub
255:
256: }
257:
258: }
|