Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples.publish;
  
  import javax.sip.*;
  import javax.sip.header.*;
  import java.util.*;

The notifier

Author(s):
M. Ranganathan
 
 
 public class Notifier implements SipListener {
 
     private static AddressFactory addressFactory;
 
     private static MessageFactory messageFactory;
 
     private static HeaderFactory headerFactory;
 
     private static SipStack sipStack;
 
     protected SipProvider tcpProvider;
 
     protected SipProvider udpProvider;
 
     protected Dialog dialog;
 
 
     protected static final String usageString = "java "
             + "examples.shootist.Shootist \n"
             + ">>>> is your class path set to the root?";
 
     private static void usage() {
         ..println();
         System.exit(0);
 
     }
 
     public void processRequest(RequestEvent requestEvent) {
         Request request = requestEvent.getRequest();
         ServerTransaction serverTransactionId = requestEvent
                 .getServerTransaction();
 
         ..println("\n\nRequest " + request.getMethod()
                 + " received at " + .getStackName()
                 + " with server transaction id " + serverTransactionId);
 
         if (request.getMethod().equals(.)) {
             processSubscribe(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processPublish(requestEventserverTransactionId);
         }
 
     }
 
     private void processPublish(RequestEvent requestEvent,
             ServerTransaction serverTransactionId) {
         try {
             EventHeader eventHeader = (EventHeaderrequestEvent.getRequest()
                     .getHeader(.);
             Request request = this..createRequest(.);
             SubscriptionStateHeader subscriptionState = 
                     .createSubscriptionStateHeader(.);
             request.addHeader(subscriptionState);
             request.addHeader(eventHeader);
             ClientTransaction ct = .getNewClientTransaction(request);
             this..sendRequest(ct);
         } catch (Exception ex) {
             ex.printStackTrace();
             System.exit(0);
         }
     }

    
Process the invite request.
 
     public void processSubscribe(RequestEvent requestEvent,
             ServerTransaction serverTransaction) {
         SipProvider sipProvider = (SipProviderrequestEvent.getSource();
         Request request = requestEvent.getRequest();
         try {
             ..println("notifier: got an Subscribe sending OK");
             ..println("notifier:  " + request);
             EventHeader eventHeader = (EventHeaderrequest
                     .getHeader(.);
             if (eventHeader == null) {
                 .
                         .println("Cannot find event header.... dropping request.");
                 return;
             }
             Response response = .createResponse(202, request);
             ToHeader toHeader = (ToHeaderresponse.getHeader(.);
             toHeader.setTag("4321"); // Application is supposed to set.
             Address address = 
                     .createAddress("Notifier <sip:127.0.0.1:5070>");
             ContactHeader contactHeader = 
                    .createContactHeader(address);
            response.addHeader(contactHeader);
            ServerTransaction st = requestEvent.getServerTransaction();
            if (st == null) {
                st = sipProvider.getNewServerTransaction(request);
            }
            ..println("got a server transaction " + st);
            this. = st.getDialog();
            // subscribe dialogs do not terminate on bye.
            this..terminateOnBye(false);
            if ( != null) {
                ..println("Dialog " + );
                ..println("Dialog state " + .getState());
            }
            st.sendResponse(response);
            if ( != null) {
                ..println("Dialog " + );
                ..println("Dialog state " + .getState());
            }
            /*
             * NOTIFY requests MUST contain a "Subscription-State" header with a
             * value of "active", "pending", or "terminated". The "active" value
             * indicates that the subscription has been accepted and has been
             * authorized (in most cases; see section 5.2.). The "pending" value
             * indicates that the subscription has been received, but that
             * policy information is insufficient to accept or deny the
             * subscription at this time. The "terminated" value indicates that
             * the subscription is not active.
             */
            Request notifyRequest = .createRequest(.);
            SubscriptionStateHeader sstate = 
                    .createSubscriptionStateHeader(.);
            notifyRequest.addHeader(sstate);
            notifyRequest.addHeader(eventHeader);
            ClientTransaction ct = 
                    .getNewClientTransaction(notifyRequest);
            .sendRequest(ct);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        ..println("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        ..println("Response received with client transaction id "
                + tid + ":\n" + response);
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        Transaction transaction;
        if (timeoutEvent.isServerTransaction()) {
            transaction = timeoutEvent.getServerTransaction();
        } else {
            transaction = timeoutEvent.getClientTransaction();
        }
        ..println("state = " + transaction.getState());
        ..println("dialog = " + transaction.getDialog());
        ..println("dialogState = "
                + transaction.getDialog().getState());
        ..println("Transaction Time out");
    }
    public void init() {
        SipFactory sipFactory = null;
         = null;
        sipFactory = SipFactory.getInstance();
        sipFactory.setPathName("gov.nist");
        Properties properties = new Properties();
        properties.setProperty("javax.sip.STACK_NAME""notifier");
        // You need 16 for logging traces. 32 for debug + traces.
        // Your code will limp at 32 but it is best for debugging.
        properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL""32");
        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
                "notifierdebug.txt");
        properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
                "notifierlog.txt");
        properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE""4096");
        properties.setProperty("gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS",
                "false");
        try {
            // Create SipStack object
             = sipFactory.createSipStack(properties);
            ..println("sipStack = " + );
        } catch (PeerUnavailableException e) {
            // could not find
            // gov.nist.jain.protocol.ip.sip.SipStackImpl
            // in the classpath
            e.printStackTrace();
            ..println(e.getMessage());
            if (e.getCause() != null)
                e.getCause().printStackTrace();
            System.exit(0);
        }
        try {
             = sipFactory.createHeaderFactory();
             = sipFactory.createAddressFactory();
             = sipFactory.createMessageFactory();
            ListeningPoint lp = .createListeningPoint("127.0.0.1",
                    5070, "udp");
            ListeningPoint lp1 = .createListeningPoint("127.0.0.1",
                    5070, "tcp");
            Notifier listener = this;
            this. = .createSipProvider(lp);
            ..println("udp provider " + );
            .addSipListener(listener);
            this. = .createSipProvider(lp1);
            ..println("tcp provider " + );
            .addSipListener(listener);
        } catch (Exception ex) {
            ..println(ex.getMessage());
            ex.printStackTrace();
            usage();
        }
    }
    public static void main(String args[]) {
        new Notifier().init();
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        // TODO Auto-generated method stub
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        // TODO Auto-generated method stub
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        // TODO Auto-generated method stub
    }
New to GrepCode? Check out our FAQ X