Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.load.subsnotify;
  
  import javax.sip.*;
  import javax.sip.header.*;
  import java.util.*;

This class is a UAS template.

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;
 
     private static final int myPort = 5070;
 
     protected ServerTransaction inviteTid;
 
     private SipProvider sipProvider;
 
     class MyTimerTask extends TimerTask {
         Notifier notify;
 
         public MyTimerTask(Notifier notify1) {
              = notify1;
 
         }
 
         public void run() {
 
         }
 
     }
 
     protected static final String usageString = "java " + "notifier \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(.)) {
             ServerTransaction st = processSubscribe(requestEvent,
                     serverTransactionId);
             // This could be a retransmission for a Server Transaction
             // associated with Dialog that we have already deleted.
             if (st.getDialog().getState() != .) {
                 sendNotify(st.getDialog());
             }
         }
 
     }
 
     public void sendNotify(Dialog dialog) {
 
         try {
 
             Request request = dialog.createRequest(.);
 
             // Create a new MaxForwardsHeader
             MaxForwardsHeader maxForwards = 
                     .createMaxForwardsHeader(70);
             request.setHeader(maxForwards);
 
             // Create contact headers
             String host = "127.0.0.1";
 
             SipURI contactUrl = .createSipURI(nullhost);
             contactUrl.setLrParam();
             contactUrl.setPort(.getListeningPoint("udp").getPort());
 
             Address contactAddress = .createAddress(contactUrl);
 
             request
                     .setHeader(
                             .createContactHeader(contactAddress));
 
             EventHeader eventHeader = null;
            eventHeader = .createEventHeader("reg");
            request.addHeader(eventHeader);
            SubscriptionStateHeader subscriptionStateHeader = 
                    .createSubscriptionStateHeader("active");
            subscriptionStateHeader.setExpires(200);
            request.addHeader(subscriptionStateHeader);
            ClientTransaction ct = .getNewClientTransaction(request);
            dialog.sendRequest(ct);
            // send the request out.
        } catch (Exception ex) {
            ..println(ex.getMessage());
            ex.printStackTrace();
            usage();
        }
    }
    public void processResponse(ResponseEvent responseEvent) {
    }
    public ServerTransaction processSubscribe(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        Request request = requestEvent.getRequest();
        try {
            ..println("notifier:  got a subscribe. sending OK.");
            Response response = .createResponse(200, request);
            if (serverTransactionId == null) {
                serverTransactionId = sipProvider
                        .getNewServerTransaction(request);
            }
            ExpiresHeader expires = (ExpiresHeaderrequest
                    .getHeader(.);
            response.addHeader(request.getHeader(.));
            response.addHeader(request.getHeader(.));
            ToHeader toHeader = (ToHeaderresponse.getHeader(.);
            if (toHeader.getTag() == null) {
                String tag = new Long(new java.util.Random().nextLong())
                        .toString();
                toHeader.setTag(tag);
            }
            serverTransactionId.sendResponse(response);
            ..println("Dialog State is "
                    + serverTransactionId.getDialog().getState());
            if (expires.getExpires() == 0) {
                ..println("Deleting Dialog ");
                serverTransactionId.getDialog().delete();
            }
            return serverTransactionId;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
        return null;
    }
    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();
        String transport = "udp";
        String peerHostPort = "127.0.0.1:5060";
        properties.setProperty("javax.sip.OUTBOUND_PROXY"peerHostPort + "/"
                + transport);
        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""NONE");
        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
                "shootmedebug.txt");
        properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
                "shootmelog.txt");
        properties.setProperty("gov.nist.javax.sip.MAX_LISTENER_RESPONSE_TIME",
                "20");
        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",
                    "udp");
            Notifier listener = this;
             = .createSipProvider(lp);
            ..println("udp 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) {
        ..println("IOException");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        ..println("Transaction terminated event recieved");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        ..println("Dialog terminated event recieved");
    }
New to GrepCode? Check out our FAQ X