Search in sources :

Example 11 with Hop

use of javax.sip.address.Hop in project XobotOS by xamarin.

the class SipProviderImpl method transactionErrorEvent.

/**
     * Invoked when an error has ocurred with a transaction. Propagate up to the
     * listeners.
     *
     * @param transactionErrorEvent
     *            Error event.
     */
public void transactionErrorEvent(SIPTransactionErrorEvent transactionErrorEvent) {
    SIPTransaction transaction = (SIPTransaction) transactionErrorEvent.getSource();
    if (transactionErrorEvent.getErrorID() == SIPTransactionErrorEvent.TRANSPORT_ERROR) {
        // There must be a way to inform the TU here!!
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logDebug("TransportError occured on " + transaction);
        }
        // Treat this like a timeout event. (Suggestion from Christophe).
        Object errorObject = transactionErrorEvent.getSource();
        Timeout timeout = Timeout.TRANSACTION;
        TimeoutEvent ev = null;
        if (errorObject instanceof SIPServerTransaction) {
            ev = new TimeoutEvent(this, (ServerTransaction) errorObject, timeout);
        } else {
            SIPClientTransaction clientTx = (SIPClientTransaction) errorObject;
            Hop hop = clientTx.getNextHop();
            if (sipStack.getRouter() instanceof RouterExt) {
                ((RouterExt) sipStack.getRouter()).transactionTimeout(hop);
            }
            ev = new TimeoutEvent(this, (ClientTransaction) errorObject, timeout);
        }
        // Handling transport error like timeout
        this.handleEvent(ev, (SIPTransaction) errorObject);
    } else if (transactionErrorEvent.getErrorID() == SIPTransactionErrorEvent.TIMEOUT_ERROR) {
        // This is a timeout event.
        Object errorObject = transactionErrorEvent.getSource();
        Timeout timeout = Timeout.TRANSACTION;
        TimeoutEvent ev = null;
        if (errorObject instanceof SIPServerTransaction) {
            ev = new TimeoutEvent(this, (ServerTransaction) errorObject, timeout);
        } else {
            SIPClientTransaction clientTx = (SIPClientTransaction) errorObject;
            Hop hop = clientTx.getNextHop();
            if (sipStack.getRouter() instanceof RouterExt) {
                ((RouterExt) sipStack.getRouter()).transactionTimeout(hop);
            }
            ev = new TimeoutEvent(this, (ClientTransaction) errorObject, timeout);
        }
        this.handleEvent(ev, (SIPTransaction) errorObject);
    } else if (transactionErrorEvent.getErrorID() == SIPTransactionErrorEvent.TIMEOUT_RETRANSMIT) {
        // This is a timeout retransmit event.
        // We should never get this if retransmit filter is
        // enabled (ie. in that case the stack should handle.
        // all retransmits.
        Object errorObject = transactionErrorEvent.getSource();
        Transaction tx = (Transaction) errorObject;
        if (tx.getDialog() != null)
            InternalErrorHandler.handleException("Unexpected event !", this.sipStack.getStackLogger());
        Timeout timeout = Timeout.RETRANSMIT;
        TimeoutEvent ev = null;
        if (errorObject instanceof SIPServerTransaction) {
            ev = new TimeoutEvent(this, (ServerTransaction) errorObject, timeout);
        } else {
            ev = new TimeoutEvent(this, (ClientTransaction) errorObject, timeout);
        }
        this.handleEvent(ev, (SIPTransaction) errorObject);
    }
}
Also used : SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) TimeoutEvent(javax.sip.TimeoutEvent) ServerTransaction(javax.sip.ServerTransaction) Transaction(javax.sip.Transaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) SIPTransaction(gov.nist.javax.sip.stack.SIPTransaction) ClientTransaction(javax.sip.ClientTransaction) RouterExt(gov.nist.javax.sip.address.RouterExt) Timeout(javax.sip.Timeout) SIPTransaction(gov.nist.javax.sip.stack.SIPTransaction) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) ClientTransaction(javax.sip.ClientTransaction) Hop(javax.sip.address.Hop) EventObject(java.util.EventObject) ServerTransaction(javax.sip.ServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction)

Example 12 with Hop

use of javax.sip.address.Hop in project XobotOS by xamarin.

the class TLSMessageChannel method processMessage.

/**
     * Gets invoked by the parser as a callback on successful message parsing (i.e. no parser
     * errors).
     *
     * @param sipMessage Message to process (this calls the application for processing the
     *        message).
     *
     * Jvb: note that this code is identical to TCPMessageChannel, refactor some day
     */
public void processMessage(SIPMessage sipMessage) throws Exception {
    try {
        if (sipMessage.getFrom() == null || sipMessage.getTo() == null || sipMessage.getCallId() == null || sipMessage.getCSeq() == null || sipMessage.getViaHeaders() == null) {
            String badmsg = sipMessage.encode();
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logError("bad message " + badmsg);
                sipStack.getStackLogger().logError(">>> Dropped Bad Msg");
            }
            return;
        }
        ViaList viaList = sipMessage.getViaHeaders();
        if (sipMessage instanceof SIPRequest) {
            Via v = (Via) viaList.getFirst();
            // the peer address and tag it appropriately.
            Hop hop = sipStack.addressResolver.resolveAddress(v.getHop());
            this.peerProtocol = v.getTransport();
            try {
                this.peerAddress = mySock.getInetAddress();
                // JvB: if sender added 'rport', must always set received
                if (v.hasParameter(Via.RPORT) || !hop.getHost().equals(this.peerAddress.getHostAddress())) {
                    v.setParameter(Via.RECEIVED, this.peerAddress.getHostAddress());
                }
                // @@@ hagai
                // JvB: technically, may only do this when Via already contains
                // rport
                v.setParameter(Via.RPORT, Integer.toString(this.peerPort));
            } catch (java.text.ParseException ex) {
                InternalErrorHandler.handleException(ex);
            }
            // Use this for outgoing messages as well.
            if (!this.isCached) {
                ((TLSMessageProcessor) this.messageProcessor).cacheMessageChannel(this);
                this.isCached = true;
                String key = IOHandler.makeKey(mySock.getInetAddress(), this.peerPort);
                sipStack.ioHandler.putSocket(key, mySock);
            }
        }
        // Foreach part of the request header, fetch it and process it
        long receptionTime = System.currentTimeMillis();
        if (sipMessage instanceof SIPRequest) {
            // This is a request - process the request.
            SIPRequest sipRequest = (SIPRequest) sipMessage;
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("----Processing Message---");
            }
            if (this.sipStack.getStackLogger().isLoggingEnabled(ServerLogger.TRACE_MESSAGES)) {
                sipStack.serverLogger.logMessage(sipMessage, this.getPeerHostPort().toString(), this.messageProcessor.getIpAddress().getHostAddress() + ":" + this.messageProcessor.getPort(), false, receptionTime);
            }
            // if it is too long.
            if (sipStack.getMaxMessageSize() > 0 && sipRequest.getSize() + (sipRequest.getContentLength() == null ? 0 : sipRequest.getContentLength().getContentLength()) > sipStack.getMaxMessageSize()) {
                SIPResponse sipResponse = sipRequest.createResponse(SIPResponse.MESSAGE_TOO_LARGE);
                byte[] resp = sipResponse.encodeAsBytes(this.getTransport());
                this.sendMessage(resp, false);
                throw new Exception("Message size exceeded");
            }
            // Stack could not create a new server request interface.
            // maybe not enough resources.
            ServerRequestInterface sipServerRequest = sipStack.newSIPServerRequest(sipRequest, this);
            if (sipServerRequest != null) {
                try {
                    sipServerRequest.processRequest(sipRequest, this);
                } finally {
                    if (sipServerRequest instanceof SIPTransaction) {
                        SIPServerTransaction sipServerTx = (SIPServerTransaction) sipServerRequest;
                        if (!sipServerTx.passToListener())
                            ((SIPTransaction) sipServerRequest).releaseSem();
                    }
                }
            } else {
                SIPResponse response = sipRequest.createResponse(Response.SERVICE_UNAVAILABLE);
                RetryAfter retryAfter = new RetryAfter();
                // Be a good citizen and send a decent response code back.
                try {
                    retryAfter.setRetryAfter((int) (10 * (Math.random())));
                    response.setHeader(retryAfter);
                    this.sendMessage(response);
                } catch (Exception e) {
                // IGNore
                }
                if (sipStack.isLoggingEnabled())
                    sipStack.getStackLogger().logWarning("Dropping message -- could not acquire semaphore");
            }
        } else {
            SIPResponse sipResponse = (SIPResponse) sipMessage;
            try {
                sipResponse.checkHeaders();
            } catch (ParseException ex) {
                if (sipStack.isLoggingEnabled())
                    sipStack.getStackLogger().logError("Dropping Badly formatted response message >>> " + sipResponse);
                return;
            }
            // If it is too large dump it silently.
            if (sipStack.getMaxMessageSize() > 0 && sipResponse.getSize() + (sipResponse.getContentLength() == null ? 0 : sipResponse.getContentLength().getContentLength()) > sipStack.getMaxMessageSize()) {
                if (sipStack.isLoggingEnabled())
                    this.sipStack.getStackLogger().logDebug("Message size exceeded");
                return;
            }
            ServerResponseInterface sipServerResponse = sipStack.newSIPServerResponse(sipResponse, this);
            if (sipServerResponse != null) {
                try {
                    if (sipServerResponse instanceof SIPClientTransaction && !((SIPClientTransaction) sipServerResponse).checkFromTag(sipResponse)) {
                        if (sipStack.isLoggingEnabled())
                            sipStack.getStackLogger().logError("Dropping response message with invalid tag >>> " + sipResponse);
                        return;
                    }
                    sipServerResponse.processResponse(sipResponse, this);
                } finally {
                    if (sipServerResponse instanceof SIPTransaction && !((SIPTransaction) sipServerResponse).passToListener()) {
                        // Note that the semaphore is released in event
                        // scanner if the
                        // request is actually processed by the Listener.
                        ((SIPTransaction) sipServerResponse).releaseSem();
                    }
                }
            } else {
                sipStack.getStackLogger().logWarning("Could not get semaphore... dropping response");
            }
        }
    } finally {
    }
}
Also used : Hop(javax.sip.address.Hop) ParseException(java.text.ParseException) ParseException(java.text.ParseException) ParseException(java.text.ParseException)

Aggregations

Hop (javax.sip.address.Hop)12 ParseException (java.text.ParseException)8 IOException (java.io.IOException)7 Via (gov.nist.javax.sip.header.Via)6 SIPRequest (gov.nist.javax.sip.message.SIPRequest)6 SipException (javax.sip.SipException)6 InvalidArgumentException (javax.sip.InvalidArgumentException)4 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)3 ClientTransaction (javax.sip.ClientTransaction)3 SIPMessage (gov.nist.javax.sip.message.SIPMessage)2 MessageChannel (gov.nist.javax.sip.stack.MessageChannel)2 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)2 SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)2 SIPTransaction (gov.nist.javax.sip.stack.SIPTransaction)2 ListIterator (java.util.ListIterator)2 DialogDoesNotExistException (javax.sip.DialogDoesNotExistException)2 ObjectInUseException (javax.sip.ObjectInUseException)2 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)2 SipURI (javax.sip.address.SipURI)2 URI (javax.sip.address.URI)2