Пример #1
0
        /// <inheritdoc/>
        public void SendRequest(Exchange exchange, Request request)
        {
            if (request.ID == Message.None)
            {
                request.ID = System.Threading.Interlocked.Increment(ref _currentID) % (1 << 16);
            }

            /*
             * The request is a CON or NON and must be prepared for these responses
             * - CON  => ACK/RST/ACK+response/CON+response/NON+response
             * - NON => RST/CON+response/NON+response
             * If this request goes lost, we do not get anything back.
             */

            Exchange.KeyID    keyID    = new Exchange.KeyID(request.ID, request.Destination);
            Exchange.KeyToken keyToken = new Exchange.KeyToken(request.Token, request.Destination);

            exchange.Completed += OnExchangeCompleted;

            if (log.IsDebugEnabled)
            {
                log.Debug("Stored open request by " + keyID + ", " + keyToken);
            }

            _exchangesByID[keyID]       = exchange;
            _exchangesByToken[keyToken] = exchange;
        }
Пример #2
0
        private void OnExchangeCompleted(Object sender, EventArgs e)
        {
            Exchange exchange = (Exchange)sender;

            /*
             * Logging in this method leads to significant performance loss.
             * Uncomment logging code only for debugging purposes.
             */

            if (exchange.Origin == Origin.Local)
            {
                // this endpoint created the Exchange by issuing a request
                Request           request  = exchange.Request;
                Exchange.KeyToken keyToken = new Exchange.KeyToken(exchange.CurrentRequest.Token, request.Destination);
                Exchange.KeyID    keyID    = new Exchange.KeyID(request.ID, request.Destination);

                if (log.IsDebugEnabled)
                {
                    log.Debug("Exchange completed: Cleaning up " + keyToken);
                }

                _exchangesByToken.Remove(keyToken);
                // in case an empty ACK was lost
                _exchangesByID.Remove(keyID);
            }
            else
            {
                // this endpoint created the Exchange to respond a request
                Request request = exchange.CurrentRequest;
                if (request != null)
                {
                    // TODO: We can optimize this and only do it, when the request really had blockwise transfer
                    Exchange.KeyUri uriKey = new Exchange.KeyUri(request.URI, request.Source);
                    //if (log.IsDebugEnabled)
                    //    log.Debug("++++++++++++++++++Remote ongoing completed, cleaning up "+uriKey);
                    _ongoingExchanges.Remove(uriKey);
                }

                // TODO: What if the request is only a block?
                // TODO: This should only happen if the transfer was blockwise

                Response response = exchange.Response;
                if (response != null)
                {
                    // only response MIDs are stored for ACK and RST, no reponse Tokens
                    Exchange.KeyID midKey = new Exchange.KeyID(response.ID, response.Destination);
                    //if (log.IsDebugEnabled)
                    //    log.Debug("++++++++++++++++++Remote ongoing completed, cleaning up " + midKey);
                    _exchangesByID.Remove(midKey);
                }

                // Remove all remaining NON-notifications if this exchange is an observe relation
                ObserveRelation relation = exchange.Relation;
                if (relation != null)
                {
                    RemoveNotificatoinsOf(relation);
                }
            }
        }
Пример #3
0
        private void OnExchangeCompleted(Object sender, EventArgs e)
        {
            Exchange exchange = (Exchange)sender;

            /*
             * Logging in this method leads to significant performance loss.
             * Uncomment logging code only for debugging purposes.
             */

            if (exchange.Origin == Origin.Local)
            {
                // this endpoint created the Exchange by issuing a request
                Exchange.KeyID    keyID    = new Exchange.KeyID(exchange.CurrentRequest.ID, null);
                Exchange.KeyToken keyToken = new Exchange.KeyToken(exchange.CurrentRequest.Token);

                if (log.IsDebugEnabled)
                {
                    log.Debug("Exchange completed: Cleaning up " + keyToken);
                }

                _exchangesByToken.Remove(keyToken);
                // in case an empty ACK was lost
                _exchangesByID.Remove(keyID);
            }
            else // Origin.Remote
            {
                // this endpoint created the Exchange to respond a request

                Response response = exchange.CurrentResponse;
                if (response != null && response.Type != MessageType.ACK)
                {
                    // only response MIDs are stored for ACK and RST, no reponse Tokens
                    Exchange.KeyID midKey = new Exchange.KeyID(response.ID, null);
                    //if (log.IsDebugEnabled)
                    //    log.Debug("Remote ongoing completed, cleaning up " + midKey);
                    _exchangesByID.Remove(midKey);
                }

                Request request = exchange.CurrentRequest;
                if (request != null && (request.HasOption(OptionType.Block1) || response.HasOption(OptionType.Block2)))
                {
                    Exchange.KeyUri uriKey = new Exchange.KeyUri(request.URI, request.Source);
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Remote ongoing completed, cleaning up " + uriKey);
                    }
                    Exchange exc;
                    _ongoingExchanges.TryRemove(uriKey, out exc);
                }

                // Remove all remaining NON-notifications if this exchange is an observe relation
                ObserveRelation relation = exchange.Relation;
                if (relation != null)
                {
                    RemoveNotificatoinsOf(relation);
                }
            }
        }
Пример #4
0
        /// <inheritdoc/>
        public Exchange ReceiveResponse(Response response)
        {
            /*
             * This response could be
             * - The first CON/NON/ACK+response => deliver
             * - Retransmitted CON (because client got no ACK)
             *      => resend ACK
             */

            Exchange.KeyID    keyId    = new Exchange.KeyID(response.ID, response.Source);
            Exchange.KeyToken keyToken = new Exchange.KeyToken(response.Token, response.Source);

            Exchange exchange;

            if (_exchangesByToken.TryGetValue(keyToken, out exchange))
            {
                // There is an exchange with the given token
                Exchange prev = _deduplicator.FindPrevious(keyId, exchange);
                if (prev != null)
                {
                    // (and thus it holds: prev == exchange)
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Duplicate response " + response);
                    }
                    response.Duplicate = true;
                }
                else
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Exchange got reply: Cleaning up " + keyId);
                    }
                    _exchangesByID.Remove(keyId);
                }

                if (response.Type == MessageType.ACK && exchange.CurrentRequest.ID != response.ID)
                {
                    // The token matches but not the MID. This is a response for an older exchange
                    if (log.IsWarnEnabled)
                    {
                        log.Warn("Token matches but not MID: Expected " + exchange.CurrentRequest.ID + " but was " + response.ID);
                    }
                    // ignore response
                    return(null);
                }
                else
                {
                    // this is a separate response that we can deliver
                    return(exchange);
                }
            }
            else
            {
                // There is no exchange with the given token.
                if (response.Type != MessageType.ACK)
                {
                    Exchange prev = _deduplicator.Find(keyId);
                    if (prev != null)
                    {
                        response.Duplicate = true;
                        return(prev);
                    }
                }
                // ignore response
                return(null);
            }
        }
Пример #5
0
        /// <inheritdoc/>
        public Exchange ReceiveResponse(Response response)
        {
            /*
             * This response could be
             * - The first CON/NON/ACK+response => deliver
             * - Retransmitted CON (because client got no ACK)
             *      => resend ACK
             */

            Exchange.KeyID keyId;
            if (response.Type == MessageType.ACK)
            {
                // own namespace
                keyId = new Exchange.KeyID(response.ID, null);
            }
            else
            {
                // remote namespace
                keyId = new Exchange.KeyID(response.ID, response.Source);
            }

            Exchange.KeyToken keyToken = new Exchange.KeyToken(response.Token);

            Exchange exchange;

            if (_exchangesByToken.TryGetValue(keyToken, out exchange))
            {
                // There is an exchange with the given token
                Exchange prev = _deduplicator.FindPrevious(keyId, exchange);
                if (prev != null)
                {
                    // (and thus it holds: prev == exchange)
                    if (log.IsInfoEnabled)
                    {
                        log.Info("Duplicate response for open exchange: " + response);
                    }
                    response.Duplicate = true;
                }
                else
                {
                    keyId = new Exchange.KeyID(exchange.CurrentRequest.ID, null);
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Exchange got response: Cleaning up " + keyId);
                    }
                    _exchangesByID.Remove(keyId);
                }

                if (response.Type == MessageType.ACK && exchange.CurrentRequest.ID != response.ID)
                {
                    // The token matches but not the MID. This is a response for an older exchange
                    if (log.IsWarnEnabled)
                    {
                        log.Warn("Possible MID reuse before lifetime end: " + response.TokenString + " expected MID " + exchange.CurrentRequest.ID + " but received " + response.ID);
                    }
                }

                return(exchange);
            }
            else
            {
                // There is no exchange with the given token.
                if (response.Type != MessageType.ACK)
                {
                    // only act upon separate responses
                    Exchange prev = _deduplicator.Find(keyId);
                    if (prev != null)
                    {
                        if (log.IsInfoEnabled)
                        {
                            log.Info("Duplicate response for completed exchange: " + response);
                        }
                        response.Duplicate = true;
                        return(prev);
                    }
                }
                else
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info("Ignoring unmatchable piggy-backed response from " + response.Source + ": " + response);
                    }
                }
                // ignore response
                return(null);
            }
        }