Search in sources :

Example 6 with SubscriptionStateHeader

use of javax.sip.header.SubscriptionStateHeader in project load-balancer by RestComm.

the class TestSipListener method processSubscribe.

/**
 * Process the invite request.
 */
public void processSubscribe(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    try {
        logger.info("notifier: got an Subscribe sending OK");
        logger.info("notifier:  " + request);
        logger.info("notifier : dialog = " + requestEvent.getDialog());
        EventHeader eventHeader = (EventHeader) request.getHeader(EventHeader.NAME);
        // this.gotSubscribeRequest = true;
        // Always create a ServerTransaction, best as early as possible in the code
        Response response = null;
        ServerTransaction st = requestEvent.getServerTransaction();
        if (st == null) {
            st = sipProvider.getNewServerTransaction(request);
        }
        // Check if it is an initial SUBSCRIBE or a refresh / unsubscribe
        boolean isInitial = requestEvent.getDialog() == null;
        if (isInitial) {
            // JvB: need random tags to test forking
            String toTag = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
            response = protocolObjects.messageFactory.createResponse(202, request);
            ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
            // Sanity check: to header should not ahve a tag. Else the dialog
            // should have matched
            // Application is supposed to set.
            toHeader.setTag(toTag);
            this.dialog = st.getDialog();
            // subscribe dialogs do not terminate on bye.
            this.dialog.terminateOnBye(false);
        } else {
            response = protocolObjects.messageFactory.createResponse(202, request);
            this.dialog = st.getDialog();
            // subscribe dialogs do not terminate on bye.
            this.dialog.terminateOnBye(false);
        }
        // Both 2xx response to SUBSCRIBE and NOTIFY need a Contact
        Address address = null;
        if (!isIpv6)
            address = protocolObjects.addressFactory.createAddress("Notifier <sip:127.0.0.1>");
        else
            address = protocolObjects.addressFactory.createAddress("Notifier <sip:[::1]>");
        ((SipURI) address.getURI()).setPort(sipProvider.getListeningPoint(ListeningPoint.UDP).getPort());
        ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
        response.addHeader(contactHeader);
        // Expires header is mandatory in 2xx responses to SUBSCRIBE
        ExpiresHeader expires = (ExpiresHeader) request.getHeader(ExpiresHeader.NAME);
        if (expires == null) {
            // rather short
            expires = protocolObjects.headerFactory.createExpiresHeader(30);
        }
        response.addHeader(expires);
        /*
			 * JvB: The SUBSCRIBE MUST be answered first. See RFC3265 3.1.6.2: 
			 * "[...] a NOTIFY message is always sent immediately after any 200-
			 * class response to a SUBSCRIBE request"
			 * 
			 *  Do this before creating the NOTIFY request below
			 */
        st.sendResponse(response);
        /*
			 * 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.
			 */
        if (sendNotify) {
            Request notifyRequest = dialog.createRequest("NOTIFY");
            // Mark the contact header, to check that the remote contact is updated
            // ((SipURI)contactHeader.getAddress().getURI()).setParameter("id","not");
            // Initial state is pending, second time we assume terminated (Expires==0)
            SubscriptionStateHeader sstate = protocolObjects.headerFactory.createSubscriptionStateHeader(expires.getExpires() != 0 ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
            allSubscriptionStates.add(sstate.getState().toLowerCase());
            // Need a reason for terminated
            if (sstate.getState().equalsIgnoreCase("terminated")) {
                sstate.setReasonCode("deactivated");
            }
            notifyRequest.addHeader(sstate);
            notifyRequest.setHeader(eventHeader);
            notifyRequest.setHeader(contactHeader);
            // notifyRequest.setHeader(routeHeader);
            ClientTransaction ct = sipProvider.getNewClientTransaction(notifyRequest);
            if (sstate.getState().equals(SubscriptionStateHeader.TERMINATED)) {
                Thread.sleep(timeToWaitBetweenSubsNotify);
            }
            // Let the other side know that the tx is pending acceptance
            // 
            dialog.sendRequest(ct);
            logger.info("NOTIFY Branch ID " + ((ViaHeader) request.getHeader(ViaHeader.NAME)).getParameter("branch"));
            logger.info("Dialog " + dialog);
            logger.info("Dialog state after pending NOTIFY: " + dialog.getState());
            if (expires.getExpires() != 0) {
                Thread myEventSource = new Thread(new MyEventSource(this, eventHeader));
                myEventSource.start();
            }
        }
    } catch (Throwable ex) {
        logger.info(ex.getMessage(), ex);
    }
}
Also used : ContactHeader(javax.sip.header.ContactHeader) Address(javax.sip.address.Address) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) EventHeader(javax.sip.header.EventHeader) SipURI(javax.sip.address.SipURI) Response(javax.sip.message.Response) ViaHeader(javax.sip.header.ViaHeader) ReferToHeader(javax.sip.header.ReferToHeader) ToHeader(javax.sip.header.ToHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) ServerTransaction(javax.sip.ServerTransaction) SipProvider(javax.sip.SipProvider)

Example 7 with SubscriptionStateHeader

use of javax.sip.header.SubscriptionStateHeader in project load-balancer by RestComm.

the class TestSipListener method processNotify.

public void processNotify(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
    SipProvider provider = (SipProvider) requestEvent.getSource();
    Request notify = requestEvent.getRequest();
    try {
        logger.info("subscriber:  got a notify count  " + this.notifyCount++);
        if (serverTransactionId == null) {
            logger.info("subscriber:  null TID.");
            serverTransactionId = provider.getNewServerTransaction(notify);
        }
        Dialog dialog = serverTransactionId.getDialog();
        // if ( dialog != subscriberDialog ) {
        // if (forkedDialog == null) {
        // forkedDialog = dialog;
        // } else  {
        // AbstractSubsnotifyTestCase.assertTrue("Dialog should be either the subscriber dialog ",
        // forkedDialog  == dialog);
        // }
        // }
        // 
        // this.dialogs.add(dialog);
        logger.info("Dialog State = " + dialog.getState());
        Response response = protocolObjects.messageFactory.createResponse(200, notify);
        // SHOULD add a Contact
        ContactHeader contact = (ContactHeader) contactHeader.clone();
        ((SipURI) contact.getAddress().getURI()).setParameter("id", "sub");
        response.addHeader(contact);
        logger.info("Transaction State = " + serverTransactionId.getState());
        serverTransactionId.sendResponse(response);
        logger.info("Dialog State = " + dialog.getState());
        SubscriptionStateHeader subscriptionState = (SubscriptionStateHeader) notify.getHeader(SubscriptionStateHeader.NAME);
        // Subscription is terminated?
        String state = subscriptionState.getState();
        allSubscriptionStates.add(state.toLowerCase());
        if (notify.getRawContent() != null) {
            this.lastMessageContent = new String(notify.getRawContent());
            allMessagesContent.add(new String(lastMessageContent));
        }
        if (state.equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
            if (subscriptionState.getReasonCode() == null) {
                dialog.delete();
            }
        } else if (state.equalsIgnoreCase(SubscriptionStateHeader.ACTIVE)) {
            if ("reg".equalsIgnoreCase(((EventHeader) notify.getHeader(EventHeader.NAME)).getEventType())) {
                if (sendByeBeforeTerminatingNotify) {
                    dialog.terminateOnBye(false);
                    sendBye();
                    Thread.sleep(1000);
                }
                logger.info("Subscriber: sending unSUBSCRIBE");
                // Else we end it ourselves
                Request unsubscribe = dialog.createRequest(Request.SUBSCRIBE);
                logger.info("dialog created:" + unsubscribe);
                // SHOULD add a Contact (done by dialog), lets mark it to test updates
                // ((SipURI) dialog.getLocalParty().getURI()).setParameter( "id", "unsub" );
                ExpiresHeader expires = protocolObjects.headerFactory.createExpiresHeader(0);
                unsubscribe.addHeader(expires);
                // JvB note : stack should do this!
                // copy
                unsubscribe.addHeader(notify.getHeader(EventHeader.NAME));
                // event
                // header
                logger.info("Sending Unsubscribe : " + unsubscribe);
                logger.info("unsubscribe dialog  " + dialog);
                ClientTransaction ct = sipProvider.getNewClientTransaction(unsubscribe);
                dialog.sendRequest(ct);
                if (sendByeAfterTerminatingNotify) {
                    Thread.sleep(1000);
                    sendBye();
                }
            } else if (sendByeBeforeTerminatingNotify) {
                sendBye();
            }
        } else {
            // pending
            logger.info("Subscriber: state now " + state);
        }
    } catch (Exception ex) {
        logger.error("Unexpected exception", ex);
    }
}
Also used : Response(javax.sip.message.Response) ContactHeader(javax.sip.header.ContactHeader) Dialog(javax.sip.Dialog) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) EventHeader(javax.sip.header.EventHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) SipURI(javax.sip.address.SipURI) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) TransactionDoesNotExistException(javax.sip.TransactionDoesNotExistException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) SipProvider(javax.sip.SipProvider)

Aggregations

SubscriptionStateHeader (javax.sip.header.SubscriptionStateHeader)7 Request (javax.sip.message.Request)7 SipURI (javax.sip.address.SipURI)5 Dialog (javax.sip.Dialog)4 SipProvider (javax.sip.SipProvider)4 Response (javax.sip.message.Response)4 ClientTransaction (javax.sip.ClientTransaction)3 ServerTransaction (javax.sip.ServerTransaction)3 Address (javax.sip.address.Address)3 ContactHeader (javax.sip.header.ContactHeader)3 EventHeader (javax.sip.header.EventHeader)3 ExpiresHeader (javax.sip.header.ExpiresHeader)3 FromHeader (javax.sip.header.FromHeader)3 ParseException (java.text.ParseException)2 InvalidArgumentException (javax.sip.InvalidArgumentException)2 SipException (javax.sip.SipException)2 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)2 TransactionUnavailableException (javax.sip.TransactionUnavailableException)2 ReferToHeader (javax.sip.header.ReferToHeader)2 ToHeader (javax.sip.header.ToHeader)2