Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack.subsnotify;
  
  
  import javax.sip.*;
  import javax.sip.header.*;
  
 
 
 import java.util.*;
 
 import  junit.framework.TestCase;

This class is a Subscriber template. Shootist is the guy that shoots and shootme is the guy that gets shot.

Author(s):
M. Ranganathan
 
 
 public class Subscriber implements SipListener {
 
     private SipProvider sipProvider;
 
     private static AddressFactory addressFactory;
 
     private static MessageFactory messageFactory;
 
     private static HeaderFactory headerFactory;
 
     private static SipStack sipStack;
 
     private ContactHeader contactHeader;
 
     private static String notifierPort;
 
     private static String transport;
 
     private int count;
 
     private Dialog subscriberDialog;
 
 
     private static Logger logger = Logger.getLogger(Subscriber.class);
 
     static {
         try {
             .setLevel(.);
             .addAppender(new ConsoleAppender(new SimpleLayout()));
             .addAppender(new FileAppender(new SimpleLayout(),
                     "subscriberoutputlog.txt"));
         } catch (Exception ex) {
             throw new RuntimeException(ex);
         }
     }
 
     private ClientTransaction subscribeTid;
 
     private ListeningPoint listeningPoint;
 
 	private Dialog notifyDialog;
 
     protected static final String usageString = "java "
             + "examples.subsnotify.Subscriber \n"
             + ">>>> is your class path set to the root?";
 
     private static void usage() {
         .info();
         System.exit(0);
 
     }
 
     public void processRequest(RequestEvent requestReceivedEvent) {
         Request request = requestReceivedEvent.getRequest();
         ServerTransaction serverTransactionId = requestReceivedEvent
                 .getServerTransaction();
         String viaBranch = ((ViaHeader)(request.getHeaders(.).next())).getParameter("branch");
 
         .info("\n\nRequest " + request.getMethod() + " received at "
                 + .getStackName() + " with server transaction id "
                 + serverTransactionId +
                 " branch ID = " + viaBranch);
 
         if (request.getMethod().equals(.))
             processNotify(requestReceivedEventserverTransactionId);
 
     }
 
     public synchronized void processNotify(RequestEvent requestEvent,
             ServerTransaction serverTransactionId) {
         SipProvider provider = (SipProviderrequestEvent.getSource();
         Request notify = requestEvent.getRequest();
        try {
            .info("subscriber:  got a notify count  " + this.++ );
            if (serverTransactionId == null) {
                .info("subscriber:  null TID.");
                serverTransactionId = provider.getNewServerTransaction(notify);
            }
            Dialog dialog = serverTransactionId.getDialog();
            .info("Dialog = " + dialog);
            if (dialog != null) {
                .info("Dialog State = " + dialog.getState());
            }
            
            this. = dialog;
            
            NotifyBefore202Test.assertNotNull"NOTIFY must set remote tag".getRemoteTag());
            
            NotifyBefore202Test.assertEquals("DialogState should be CONFIRMED".dialog.getState());
            
           
            Response response = .createResponse(200, notify);
            // SHOULD add a Contact
            ContactHeader contact = (ContactHeader.clone();
            ((SipURI)contact.getAddress().getURI()).setParameter"id""sub" );
            response.addHeadercontact );
            .info("Transaction State = " + serverTransactionId.getState());
            serverTransactionId.sendResponse(response);
            if (dialog != null ) {
                .info("Dialog State = " + dialog.getState());
            }
            SubscriptionStateHeader subscriptionState = (SubscriptionStateHeadernotify
                    .getHeader(.);
            // Subscription is terminated?
            String state = subscriptionState.getState();
            if (state.equalsIgnoreCase(.)) {
                dialog.delete();
            } else {
                .info("Subscriber: state now " + state);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            .error("Unexpected exception",ex);
            System.exit(0);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        .info("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        .info("Response received with client transaction id " + tid
                + ":\n" + response.getStatusCode()  );
        if (tid == null) {
            .info("Stray response -- dropping ");
            return;
        }
        .info("transaction state is " + tid.getState());
        .info("Dialog = " + tid.getDialog());
        if ( tid.getDialog () != null )
        .info("Dialog State is " + tid.getDialog().getState());
        
        NotifyBefore202Test.assertEquals("Dialog should be same as NOTIFY dialog"this.,tid.getDialog());
    }
    public void createProvider() throws Exception {
        this. = .createListeningPoint("127.0.0.1", 5060,
                );
    }
    public void sendSubscribe() {
        try {
            String fromName = "BigGuy";
            String fromSipAddress = "here.com";
            String fromDisplayName = "The Master Blaster";
            String toSipAddress = "there.com";
            String toUser = "LittleGuy";
            String toDisplayName = "The Little Blister";
            // create >From Header
            SipURI fromAddress = .createSipURI(fromName,
                    fromSipAddress);
            Address fromNameAddress = .createAddress(fromAddress);
            fromNameAddress.setDisplayName(fromDisplayName);
            FromHeader fromHeader = .createFromHeader(
                    fromNameAddress"12345");
            // 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,
                    toSipAddress);
            // Create ViaHeaders
            ArrayList viaHeaders = new ArrayList();
            int port = .getListeningPoint().getPort();
            ViaHeader viaHeader = .createViaHeader("127.0.0.1",
                    portnull);
            // add via headers
            viaHeaders.add(viaHeader);
            // Create a new CallId header
            CallIdHeader callIdHeader = .getNewCallId();
            // Create a new Cseq header
            CSeqHeader cSeqHeader = .createCSeqHeader(1L,
                    .);
            // Create a new MaxForwardsHeader
            MaxForwardsHeader maxForwards = 
                    .createMaxForwardsHeader(70);
            // Create the request.
            Request request = .createRequest(requestURI,
                    .callIdHeadercSeqHeaderfromHeader,
                    toHeaderviaHeadersmaxForwards);
            // Create contact headers
            String host = .getIPAddress();
            SipURI contactUrl = .createSipURI(fromNamehost);
            contactUrl.setPort(.getPort());
            // Create the contact name address.
            SipURI contactURI = .createSipURI(fromNamehost);
            contactURI.setTransportParam();
            contactURI.setPort(.getListeningPoint().getPort());
            Address contactAddress = .createAddress(contactURI);
            // Add the contact address.
            contactAddress.setDisplayName(fromName);
             = .createContactHeader(contactAddress);
            request.addHeader();
            // JvB: To test forked SUBSCRIBEs, send it via the Forker
            // Note: BIG Gotcha: Need to do this before creating the
            // ClientTransaction!
                    .createAddress("<sip:127.0.0.1:" + 
                            + ";transport=" +  + ";lr>"));
            request.addHeader(route);
            // JvB end added
            // Create the client transaction.
             = .getNewClientTransaction(request);
            // Create an event header for the subscription.
            EventHeader eventHeader = .createEventHeader("foo");
            eventHeader.setEventId("foo");
            request.addHeader(eventHeader);
            .info("Subscribe Dialog = " + .getDialog());
            this. = .getDialog();
            // send the request out.
            .sendRequest();
        } catch (Throwable ex) {
            .info(ex.getMessage());
            ex.printStackTrace();
            usage();
        }
    }
    public static  Subscriber createSubcriber() throws Exception  {
        // 5065 sends to the forker.
        // 5070 sends to the subscriber1
         = "5070";
         = "udp";
        SipFactory sipFactory = SipFactory.getInstance();
        sipFactory.setPathName("gov.nist");
        Properties properties = new Properties();
        properties.setProperty("javax.sip.USE_ROUTER_FOR_ALL_URIS""false");
        properties.setProperty("javax.sip.STACK_NAME""subscriber");
        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
                "subscriberdebug.txt");
        properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
                "subscriberlog.txt");
        properties.setProperty("javax.sip.FORKABLE_EVENTS""foo");
        // Set to 0 in your production code for max speed.
        // 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");
         = sipFactory.createSipStack(properties);
        .info("createSipStack " + );
         = sipFactory.createHeaderFactory();
         = sipFactory.createAddressFactory();
         = sipFactory.createMessageFactory();
        Subscriber subscriber = new Subscriber();
        subscriber.createProvider();
        subscriber.sipProvider.addSipListener(subscriber);
        return subscriber;
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("io exception event recieved");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("dialog terminated event recieved");
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("Transaction Time out");
    }
	public void tearDown() {
		this..stop();
	}
New to GrepCode? Check out our FAQ X