Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.load.subsnotify;
  
  import java.util.Random;
  
  import javax.sip.Dialog;
This class is a UAC template.

Author(s):
M. Ranganathan
 
 
 public class Subscriber implements SipListener {
 
     private static SipProvider sipProvider;
 
     private static AddressFactory addressFactory;
 
     private static MessageFactory messageFactory;
 
     private static HeaderFactory headerFactory;
 
     private static SipStack sipStack;
 
     private ContactHeader contactHeader;
 
     private ListeningPoint udpListeningPoint;
 
     private SipFactory sipFactory = null;
 
     private int seq_num = 1;
 
     protected static final String usageString = "java "
             + "examples.subscriber.Shootist \n"
             + ">>>> is your class path set to the root?";
 
     private static void usage() {
         ..println();
         System.exit(0);
 
     }
 
     public void processRequest(RequestEvent requestReceivedEvent) {
         Request request = requestReceivedEvent.getRequest();
         ServerTransaction serverTransactionId = requestReceivedEvent
                 .getServerTransaction();
 
         ..println("\n\nRequest " + request.getMethod()
                 + " received at " + .getStackName()
                 + " with server transaction id " + serverTransactionId);
 
         // We are the UAC so the only request we get is the BYE.
         if (request.getMethod().equals(.))
             processNotify(requestserverTransactionId);
 
     }
 
     public void processNotify(Request request,
             ServerTransaction serverTransactionId) {
         try {
             ..println("subsciber:  got a notify .");
             if (serverTransactionId == null) {
                 ..println("subsciber:  null TID.");
                 return;
             }
             Dialog dialog = serverTransactionId.getDialog();
             ..println("Dialog State = " + dialog.getState());
            Response response = .createResponse(200, request);
            serverTransactionId.sendResponse(response);
            ..println("subsciber:  Sending OK.");
            ..println("Dialog State = " + dialog.getState());
            // Unsubscribe to the Subscription. Note that if you do not unsubscribe the dialog will
            // live on and eventually you will run out of memory.
            Request unsub = dialog.createRequest(.);
            ExpiresHeader expiresHeader = 
                    .createExpiresHeader(0);
            unsub.addHeader(expiresHeader);
            EventHeader eventHeader = null;
            eventHeader = .createEventHeader("reg");
            unsub.addHeader(eventHeader);
            ClientTransaction ct = .getNewClientTransaction(unsub);
            ct.sendRequest();
            dialog.delete();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        ..println("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        ClientTransaction tid = responseReceivedEvent.getClientTransaction();
        CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
        ..println("Response received : Status Code = "
                + response.getStatusCode() + " " + cseq);
        if (tid == null) {
            ..println("Stray response -- dropping ");
            return;
        }
        ..println("transaction state is " + tid.getState());
        ..println("Dialog = " + tid.getDialog());
        ..println("Dialog State is " + tid.getDialog().getState());
        // responses are OKs for SUBSCRIBE. Do nothing
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        ..println("Transaction Time out");
    }
    public void init() {
         = null;
         = SipFactory.getInstance();
        .setPathName("gov.nist");
        Properties properties = new Properties();
        // If you want to try TCP transport change the following to
        String transport = "udp";
        String peerHostPort = "127.0.0.1:5070";
        properties.setProperty("javax.sip.OUTBOUND_PROXY"peerHostPort + "/"
                + transport);
        // If you want to use UDP then uncomment this.
        properties.setProperty("javax.sip.STACK_NAME""subscriber");
        // The following properties are specific to nist-sip
        // and are not necessarily part of any other jain-sip
        // implementation.
        // You can set a max message size for tcp transport to
        // guard against denial of service attack.
        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
                "shootistdebug.txt");
        properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
                "shootistlog.txt");
        // Drop the client connection after we are done with the transaction.
        properties.setProperty("gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS",
                "false");
        // Set to 0 (or NONE) in your production code for max speed.
        // You need 16 (or TRACE) for logging traces. 32 (or DEBUG) 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.MAX_LISTENER_RESPONSE_TIME",
                "20");
        try {
            // Create SipStack object
             = .createSipStack(properties);
            ..println("createSipStack " + );
        } catch (PeerUnavailableException e) {
            // could not find
            // gov.nist.jain.protocol.ip.sip.SipStackImpl
            // in the classpath
            e.printStackTrace();
            ..println(e.getMessage());
            System.exit(0);
        }
        try {
             = .createHeaderFactory();
             = .createAddressFactory();
             = .createMessageFactory();
             = .createListeningPoint("127.0.0.1",
                    5060, "udp");
            Subscriber listener = this;
            .addSipListener(listener);
            // Now we want to start sending out subscribes on this thread.
            while (true) {
                CreateSubscribeRequest();
                java.lang.Thread.sleep(200);
            }
        } catch (Exception ex) {
            ..println(ex.getMessage());
            ex.printStackTrace();
            usage();
        }
    }
    public void CreateSubscribeRequest() {
        try {
            String transport = "udp";
            String peerHostPort = "127.0.0.1:5070";
            String fromName = "subscriber";
            String fromSipAddress = "10.10.10.10";
            String fromDisplayName = "Mr. subscriber";
            String toSipAddress = "notifier";
            String toUser = "8000@10.10.10.10";
            String toDisplayName = "Mr. notifier";
            // create >From Header
            SipURI fromAddress = .createSipURI(fromName,
                    fromSipAddress);
            // Create a new CallId header
            CallIdHeader callIdHeader = .getNewCallId();// headerFactory.createCallIdHeader("8000dlg");
            Address fromNameAddress = .createAddress(fromAddress);
            fromNameAddress.setDisplayName(fromDisplayName);
            FromHeader fromHeader = .createFromHeader(
                    fromNameAddressnew Long(new Random().nextLong())
                            .toString());
            // create To Header
            SipURI toAddress = 
                    .createSipURI(toUsertoSipAddress);
            Address toNameAddress = .createAddress(toAddress);
            toNameAddress.setDisplayName(toDisplayName);
            ToHeader toHeader = .createToHeader(toNameAddress,
                    null);
            // create Request URI
            SipURI requestURI = .createSipURI(toUser,
                    peerHostPort);
            // Create ViaHeaders
            ArrayList viaHeaders = new ArrayList();
            ViaHeader viaHeader = .createViaHeader("127.0.0.1",
                    .getListeningPoint(transport).getPort(),
                    transportnull);
            // add via headers
            viaHeaders.add(viaHeader);
            // Create a new Cseq header
            CSeqHeader cSeqHeader = .createCSeqHeader(
                    (long.);
            ++;
            // Create a new MaxForwardsHeader
            MaxForwardsHeader maxForwards = 
                    .createMaxForwardsHeader(70);
            // Create the request.
            Request request = .createRequest(requestURI,
                    .callIdHeadercSeqHeaderfromHeader,
                    toHeaderviaHeadersmaxForwards);
            // Create contact headers
            String host = "127.0.0.1";
            SipURI contactUrl = .createSipURI(fromNamehost);
            contactUrl.setPort(.getPort());
            contactUrl.setLrParam();
            // Create the contact name address.
            SipURI contactURI = .createSipURI(fromNamehost);
            contactURI.setPort(.getListeningPoint(transport)
                    .getPort());
            Address contactAddress = .createAddress(contactURI);
            // Add the contact address.
            contactAddress.setDisplayName(fromName);
             = .createContactHeader(contactAddress);
            request.addHeader();
            ExpiresHeader expires_header = 
                    .createExpiresHeader(200);
            request.addHeader(expires_header);
            EventHeader event_header = null;
            event_header = .createEventHeader("reg");
            request.addHeader(event_header);
            ClientTransaction ct = .getNewClientTransaction(request);
            ct.sendRequest();
            // send the request out.
        } catch (Exception ex) {
            ..println(ex.getMessage());
            ex.printStackTrace();
            usage();
        }
    }
    public static void main(String args[]) {
        new Subscriber().init();
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        ..println("IOException happened for "
                + exceptionEvent.getHost() + " port = "
                + exceptionEvent.getPort());
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        ..println("Transaction terminated event recieved");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        ..println("dialogTerminatedEvent");
    }
New to GrepCode? Check out our FAQ X