Search in sources :

Example 1 with MessageChannel

use of gov.nist.javax.sip.stack.MessageChannel in project XobotOS by xamarin.

the class SipProviderImpl method sendResponse.

/*
     * (non-Javadoc)
     *
     * @see javax.sip.SipProvider#sendResponse(javax.sip.message.Response)
     */
public void sendResponse(Response response) throws SipException {
    if (!sipStack.isAlive())
        throw new SipException("Stack is stopped");
    SIPResponse sipResponse = (SIPResponse) response;
    Via via = sipResponse.getTopmostVia();
    if (via == null)
        throw new SipException("No via header in response!");
    SIPServerTransaction st = (SIPServerTransaction) sipStack.findTransaction((SIPMessage) response, true);
    if (st != null && st.getState() != TransactionState.TERMINATED && this.isAutomaticDialogSupportEnabled()) {
        throw new SipException("Transaction exists -- cannot send response statelessly");
    }
    String transport = via.getTransport();
    // check to see if Via has "received paramaeter". If so
    // set the host to the via parameter. Else set it to the
    // Via host.
    String host = via.getReceived();
    if (host == null)
        host = via.getHost();
    // Symmetric nat support
    int port = via.getRPort();
    if (port == -1) {
        port = via.getPort();
        if (port == -1) {
            if (transport.equalsIgnoreCase("TLS"))
                port = 5061;
            else
                port = 5060;
        }
    }
    // for correct management of IPv6 addresses.
    if (host.indexOf(":") > 0)
        if (host.indexOf("[") < 0)
            host = "[" + host + "]";
    Hop hop = sipStack.getAddressResolver().resolveAddress(new HopImpl(host, port, transport));
    try {
        ListeningPointImpl listeningPoint = (ListeningPointImpl) this.getListeningPoint(transport);
        if (listeningPoint == null)
            throw new SipException("whoopsa daisy! no listening point found for transport " + transport);
        MessageChannel messageChannel = sipStack.createRawMessageChannel(this.getListeningPoint(hop.getTransport()).getIPAddress(), listeningPoint.port, hop);
        messageChannel.sendMessage(sipResponse);
    } catch (IOException ex) {
        throw new SipException(ex.getMessage());
    }
}
Also used : SIPResponse(gov.nist.javax.sip.message.SIPResponse) MessageChannel(gov.nist.javax.sip.stack.MessageChannel) Hop(javax.sip.address.Hop) SIPMessage(gov.nist.javax.sip.message.SIPMessage) IOException(java.io.IOException) SipException(javax.sip.SipException) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) ListeningPoint(javax.sip.ListeningPoint) HopImpl(gov.nist.javax.sip.stack.HopImpl) Via(gov.nist.javax.sip.header.Via)

Example 2 with MessageChannel

use of gov.nist.javax.sip.stack.MessageChannel in project XobotOS by xamarin.

the class SipProviderImpl method sendRequest.

/*
     * (non-Javadoc)
     *
     * @see javax.sip.SipProvider#sendRequest(javax.sip.message.Request)
     */
public void sendRequest(Request request) throws SipException {
    if (!sipStack.isAlive())
        throw new SipException("Stack is stopped.");
    // message.
    if (((SIPRequest) request).getRequestLine() != null && request.getMethod().equals(Request.ACK)) {
        Dialog dialog = sipStack.getDialog(((SIPRequest) request).getDialogId(false));
        if (dialog != null && dialog.getState() != null) {
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logWarning("Dialog exists -- you may want to use Dialog.sendAck() " + dialog.getState());
        }
    }
    Hop hop = sipStack.getRouter((SIPRequest) request).getNextHop(request);
    if (hop == null)
        throw new SipException("could not determine next hop!");
    SIPRequest sipRequest = (SIPRequest) request;
    // Null request is used to send default proxy keepalive messages.
    if ((!sipRequest.isNullRequest()) && sipRequest.getTopmostVia() == null)
        throw new SipException("Invalid SipRequest -- no via header!");
    try {
        /*
             * JvB: Via branch should already be OK, dont touch it here? Some
             * apps forward statelessly, and then it's not set. So set only when
             * not set already, dont overwrite CANCEL branch here..
             */
        if (!sipRequest.isNullRequest()) {
            Via via = sipRequest.getTopmostVia();
            String branch = via.getBranch();
            if (branch == null || branch.length() == 0) {
                via.setBranch(sipRequest.getTransactionId());
            }
        }
        MessageChannel messageChannel = null;
        if (this.listeningPoints.containsKey(hop.getTransport().toUpperCase()))
            messageChannel = sipStack.createRawMessageChannel(this.getListeningPoint(hop.getTransport()).getIPAddress(), this.getListeningPoint(hop.getTransport()).getPort(), hop);
        if (messageChannel != null) {
            messageChannel.sendMessage((SIPMessage) sipRequest, hop);
        } else {
            throw new SipException("Could not create a message channel for " + hop.toString());
        }
    } catch (IOException ex) {
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logException(ex);
        }
        throw new SipException("IO Exception occured while Sending Request", ex);
    } catch (ParseException ex1) {
        InternalErrorHandler.handleException(ex1);
    } finally {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("done sending " + request.getMethod() + " to hop " + hop);
    }
}
Also used : MessageChannel(gov.nist.javax.sip.stack.MessageChannel) SIPDialog(gov.nist.javax.sip.stack.SIPDialog) Dialog(javax.sip.Dialog) Hop(javax.sip.address.Hop) IOException(java.io.IOException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) SIPRequest(gov.nist.javax.sip.message.SIPRequest) Via(gov.nist.javax.sip.header.Via)

Example 3 with MessageChannel

use of gov.nist.javax.sip.stack.MessageChannel in project XobotOS by xamarin.

the class SipProviderImpl method getNewServerTransaction.

/*
     * (non-Javadoc)
     *
     * @see javax.sip.SipProvider#getNewServerTransaction(javax.sip.message.Request)
     */
public ServerTransaction getNewServerTransaction(Request request) throws TransactionAlreadyExistsException, TransactionUnavailableException {
    if (!sipStack.isAlive())
        throw new TransactionUnavailableException("Stack is stopped");
    SIPServerTransaction transaction = null;
    SIPRequest sipRequest = (SIPRequest) request;
    try {
        sipRequest.checkHeaders();
    } catch (ParseException ex) {
        throw new TransactionUnavailableException(ex.getMessage(), ex);
    }
    if (request.getMethod().equals(Request.ACK)) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logError("Creating server transaction for ACK -- makes no sense!");
        throw new TransactionUnavailableException("Cannot create Server transaction for ACK ");
    }
    /*
         * Got a notify.
         */
    if (sipRequest.getMethod().equals(Request.NOTIFY) && sipRequest.getFromTag() != null && sipRequest.getToTag() == null) {
        SIPClientTransaction ct = sipStack.findSubscribeTransaction(sipRequest, (ListeningPointImpl) this.getListeningPoint());
        /* Issue 104 */
        if (ct == null && !sipStack.deliverUnsolicitedNotify) {
            throw new TransactionUnavailableException("Cannot find matching Subscription (and gov.nist.javax.sip.DELIVER_UNSOLICITED_NOTIFY not set)");
        }
    }
    if (!sipStack.acquireSem()) {
        throw new TransactionUnavailableException("Transaction not available -- could not acquire stack lock");
    }
    try {
        if (sipStack.isDialogCreated(sipRequest.getMethod())) {
            if (sipStack.findTransaction((SIPRequest) request, true) != null)
                throw new TransactionAlreadyExistsException("server transaction already exists!");
            transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
            if (transaction == null)
                throw new TransactionUnavailableException("Transaction not available");
            if (transaction.getOriginalRequest() == null)
                transaction.setOriginalRequest(sipRequest);
            try {
                sipStack.addTransaction(transaction);
            } catch (IOException ex) {
                throw new TransactionUnavailableException("Error sending provisional response");
            }
            // So I can handle timeouts.
            transaction.addEventListener(this);
            if (isAutomaticDialogSupportEnabled()) {
                // If automatic dialog support is enabled then
                // this tx gets his own dialog.
                String dialogId = sipRequest.getDialogId(true);
                SIPDialog dialog = sipStack.getDialog(dialogId);
                if (dialog == null) {
                    dialog = sipStack.createDialog(transaction);
                }
                transaction.setDialog(dialog, sipRequest.getDialogId(true));
                if (sipRequest.getMethod().equals(Request.INVITE) && this.isDialogErrorsAutomaticallyHandled()) {
                    sipStack.putInMergeTable(transaction, sipRequest);
                }
                dialog.addRoute(sipRequest);
                if (dialog.getRemoteTag() != null && dialog.getLocalTag() != null) {
                    this.sipStack.putDialog(dialog);
                }
            }
        } else {
            if (isAutomaticDialogSupportEnabled()) {
                /*
                     * Under automatic dialog support, dialog is tied into a transaction. You cannot
                     * create a server tx except for dialog creating transactions. After that, all
                     * subsequent transactions are created for you by the stack.
                     */
                transaction = (SIPServerTransaction) sipStack.findTransaction((SIPRequest) request, true);
                if (transaction != null)
                    throw new TransactionAlreadyExistsException("Transaction exists! ");
                transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
                if (transaction == null)
                    throw new TransactionUnavailableException("Transaction not available!");
                if (transaction.getOriginalRequest() == null)
                    transaction.setOriginalRequest(sipRequest);
                // Map the transaction.
                try {
                    sipStack.addTransaction(transaction);
                } catch (IOException ex) {
                    throw new TransactionUnavailableException("Could not send back provisional response!");
                }
                // If there is a dialog already assigned then just update the
                // dialog state.
                String dialogId = sipRequest.getDialogId(true);
                SIPDialog dialog = sipStack.getDialog(dialogId);
                if (dialog != null) {
                    dialog.addTransaction(transaction);
                    dialog.addRoute(sipRequest);
                    transaction.setDialog(dialog, sipRequest.getDialogId(true));
                }
            } else {
                transaction = (SIPServerTransaction) sipStack.findTransaction((SIPRequest) request, true);
                if (transaction != null)
                    throw new TransactionAlreadyExistsException("Transaction exists! ");
                transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
                if (transaction != null) {
                    if (transaction.getOriginalRequest() == null)
                        transaction.setOriginalRequest(sipRequest);
                    // Map the transaction.
                    sipStack.mapTransaction(transaction);
                    // If there is a dialog already assigned then just
                    // assign the dialog to the transaction.
                    String dialogId = sipRequest.getDialogId(true);
                    SIPDialog dialog = sipStack.getDialog(dialogId);
                    if (dialog != null) {
                        dialog.addTransaction(transaction);
                        dialog.addRoute(sipRequest);
                        transaction.setDialog(dialog, sipRequest.getDialogId(true));
                    }
                    return transaction;
                } else {
                    // tx does not exist so create the tx.
                    MessageChannel mc = (MessageChannel) sipRequest.getMessageChannel();
                    transaction = sipStack.createServerTransaction(mc);
                    if (transaction == null)
                        throw new TransactionUnavailableException("Transaction unavailable -- too many servrer transactions");
                    transaction.setOriginalRequest(sipRequest);
                    sipStack.mapTransaction(transaction);
                    // If there is a dialog already assigned then just
                    // assign the dialog to the transaction.
                    String dialogId = sipRequest.getDialogId(true);
                    SIPDialog dialog = sipStack.getDialog(dialogId);
                    if (dialog != null) {
                        dialog.addTransaction(transaction);
                        dialog.addRoute(sipRequest);
                        transaction.setDialog(dialog, sipRequest.getDialogId(true));
                    }
                    return transaction;
                }
            }
        }
        return transaction;
    } finally {
        sipStack.releaseSem();
    }
}
Also used : SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) MessageChannel(gov.nist.javax.sip.stack.MessageChannel) SIPDialog(gov.nist.javax.sip.stack.SIPDialog) TransactionAlreadyExistsException(javax.sip.TransactionAlreadyExistsException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException) IOException(java.io.IOException) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Aggregations

MessageChannel (gov.nist.javax.sip.stack.MessageChannel)3 IOException (java.io.IOException)3 Via (gov.nist.javax.sip.header.Via)2 SIPRequest (gov.nist.javax.sip.message.SIPRequest)2 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)2 SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)2 ParseException (java.text.ParseException)2 SipException (javax.sip.SipException)2 Hop (javax.sip.address.Hop)2 SIPMessage (gov.nist.javax.sip.message.SIPMessage)1 SIPResponse (gov.nist.javax.sip.message.SIPResponse)1 HopImpl (gov.nist.javax.sip.stack.HopImpl)1 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)1 Dialog (javax.sip.Dialog)1 ListeningPoint (javax.sip.ListeningPoint)1 TransactionAlreadyExistsException (javax.sip.TransactionAlreadyExistsException)1 TransactionUnavailableException (javax.sip.TransactionUnavailableException)1