Exemplo n.º 1
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, response.Session);
            }
            else
            {
                // remote namespace
                keyId = new Exchange.KeyID(response.ID, response.Source, response.Session);
            }

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

            Exchange exchange;

            if (_exchangesByToken.TryGetValue(keyToken, out exchange))
            {
                //  We need to play games if this is multicast
                if (exchange.CurrentRequest.IsMulticast)
                {
                    Exchange newExchange = new Exchange(exchange)
                    {
                        Request = exchange.Request
                    };
                    exchange = newExchange;
                }

                // There is an exchange with the given token
                Exchange prev = _deduplicator.FindPrevious(keyId, exchange);
                if (prev != null)
                {
                    // (and thus it holds: prev == exchange)
                    _Log.Info(m => m($"Duplicate response for open exchange: {response}"));
                    response.Duplicate = true;
                }
                else
                {
                    keyId = new Exchange.KeyID(exchange.CurrentRequest.ID, null, response.Session);
                    _Log.Debug(m => m($"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
                    _Log.Warn(m => m($"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)
                    {
                        _Log.Info(m => m($"Duplicate response for completed exchange: {response}"));
                        response.Duplicate = true;
                        return(prev);
                    }
                }
                else
                {
                    _Log.Info(m => m($"Ignoring unmatchable piggy-backed response from {response.Source}: {response}"));
                }

                // ignore response
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public Exchange ReceiveRequest(Request request)
        {
            /*
             * This request could be
             *  - Complete origin request => deliver with new exchange
             *  - One origin block        => deliver with ongoing exchange
             *  - Complete duplicate request or one duplicate block (because client got no ACK)
             *      =>
             *      if ACK got lost => resend ACK
             *      if ACK+response got lost => resend ACK+response
             *      if nothing has been sent yet => do nothing
             * (Retransmission is supposed to be done by the retransm. layer)
             */

            Exchange.KeyID keyId = new Exchange.KeyID(request.ID, request.Source, request.Session);

            /*
             * The differentiation between the case where there is a Block1 or
             * Block2 option and the case where there is none has the advantage that
             * all exchanges that do not need blockwise transfer have simpler and
             * faster code than exchanges with blockwise transfer.
             */
            if (!request.HasOption(OptionType.Block1) && !request.HasOption(OptionType.Block2))
            {
                Exchange exchange = new Exchange(request, Origin.Remote);
                Exchange previous = _deduplicator.FindPrevious(keyId, exchange);
                if (previous == null)
                {
                    exchange.Completed += OnExchangeCompleted;
                    return(exchange);
                }
                else
                {
                    if (_Log.IsInfoEnabled)
                    {
                        _Log.Info("Duplicate request: " + request);
                    }
                    request.Duplicate = true;
                    return(previous);
                }
            }
            else
            {
                Exchange.KeyUri keyUri = new Exchange.KeyUri(request, request.Source);

                if (_Log.IsDebugEnabled)
                {
                    _Log.Debug("Looking up ongoing exchange for " + keyUri);
                }

                Exchange ongoing;
                if (_ongoingExchanges.TryGetValue(keyUri, out ongoing))
                {
                    Exchange prev = _deduplicator.FindPrevious(keyId, ongoing);
                    if (prev != null)
                    {
                        if (_Log.IsInfoEnabled)
                        {
                            _Log.Info("Duplicate ongoing request: " + request);
                        }
                        request.Duplicate = true;
                    }
                    else
                    {
                        // the exchange is continuing, we can (i.e., must) clean up the previous response
                        if (ongoing.CurrentResponse.Type != MessageType.ACK && !ongoing.CurrentResponse.HasOption(OptionType.Observe))
                        {
                            keyId = new Exchange.KeyID(ongoing.CurrentResponse.ID, null, ongoing.CurrentResponse.Session);
                            if (_Log.IsDebugEnabled)
                            {
                                _Log.Debug("Ongoing exchange got new request, cleaning up " + keyId);
                            }
                            _exchangesByID.Remove(keyId);
                        }
                    }
                    return(ongoing);
                }
                else
                {
                    // We have no ongoing exchange for that request block.

                    /*
                     * Note the difficulty of the following code: The first message
                     * of a blockwise transfer might arrive twice due to a
                     * retransmission. The new Exchange must be inserted in both the
                     * hash map 'ongoing' and the deduplicator. They must agree on
                     * which exchange they store!
                     */

                    Exchange exchange = new Exchange(request, Origin.Remote);
                    Exchange previous = _deduplicator.FindPrevious(keyId, exchange);
                    if (previous == null)
                    {
                        if (_Log.IsDebugEnabled)
                        {
                            _Log.Debug("New ongoing request, storing " + keyUri + " for " + request);
                        }
                        exchange.Completed       += OnExchangeCompleted;
                        _ongoingExchanges[keyUri] = exchange;
                        return(exchange);
                    }
                    else
                    {
                        if (_Log.IsInfoEnabled)
                        {
                            _Log.Info("Duplicate initial request: " + request);
                        }
                        request.Duplicate = true;
                        return(previous);
                    }
                } // if ongoing
            }     // if blockwise
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public void SendResponse(Exchange exchange, Response response)
        {
            if (response.ID == Message.None)
            {
                response.ID = System.Threading.Interlocked.Increment(ref _currentID) % (1 << 16);
            }

            /*
             * The response is a CON or NON or ACK and must be prepared for these
             * - CON => ACK / RST // we only care to stop retransmission
             * - NON => RST // we only care for observe
             * - ACK => nothing!
             * If this response goes lost, we must be prepared to get the same
             * CON/NON request with same MID again. We then find the corresponding
             * exchange and the ReliabilityLayer resends this response.
             */

            // If this is a CON notification we now can forget all previous NON notifications
            if (response.Type == MessageType.CON || response.Type == MessageType.ACK)
            {
                ObserveRelation relation = exchange.Relation;
                if (relation != null)
                {
                    RemoveNotificatoinsOf(relation);
                }
            }

            // Blockwise transfers are identified by URI and remote endpoint
            if (response.HasOption(OptionType.Block2))
            {
                Request request = exchange.CurrentRequest;

                Exchange.KeyUri keyUri = new Exchange.KeyUri(request, response.Destination);

                // Observe notifications only send the first block, hence do not store them as ongoing
                if (exchange.ResponseBlockStatus != null && !response.HasOption(OptionType.Observe))
                {
                    // Remember ongoing blockwise GET requests
                    if (Utils.Put(_ongoingExchanges, keyUri, exchange) == null)
                    {
                        if (_Log.IsDebugEnabled)
                        {
                            _Log.Debug("Ongoing Block2 started late, storing " + keyUri + " for " + request);
                        }
                    }
                    else
                    {
                        if (_Log.IsDebugEnabled)
                        {
                            _Log.Debug("Ongoing Block2 continued, storing " + keyUri + " for " + request);
                        }
                    }
                }
                else
                {
                    if (_Log.IsDebugEnabled)
                    {
                        _Log.Debug("Ongoing Block2 completed, cleaning up " + keyUri + " for " + request);
                    }
                    Exchange exc;
                    _ongoingExchanges.TryRemove(keyUri, out exc);
                }
            }

            // Insert CON and NON to match ACKs and RSTs to the exchange
            // Do not insert ACKs and RSTs.
            if (response.Type == MessageType.CON || response.Type == MessageType.NON)
            {
                Exchange.KeyID keyID = new Exchange.KeyID(response.ID, null, response.Session);
                _exchangesByID[keyID] = exchange;
            }

            // Only CONs and Observe keep the exchange active
            if (response.Type != MessageType.CON && response.Last)
            {
                exchange.Complete = true;
            }
        }
Exemplo n.º 4
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, 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, response.Session);
                    //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)))
                {
#if INCLUDE_OSCOAP
                    byte[] oscoapValue = null;
                    if (request.HasOption(OptionType.Oscoap))
                    {
                        oscoapValue = request.Oscoap.RawValue;
                    }
                    Exchange.KeyUri uriKey = new Exchange.KeyUri(request.URI, oscoapValue, request.Source);
#else
                    Exchange.KeyUri uriKey = new Exchange.KeyUri(request.URI, request.Source);
#endif
                    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);
                }
            }
        }
Exemplo n.º 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, response.Session);
            }
            else
            {
                // remote namespace
                keyId = new Exchange.KeyID(response.ID, response.Source, response.Session);
            }

            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, response.Session);
                    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);
            }
        }
 /// <inheritdoc/>
 public void DeliverRequest(Exchange exchange)
 {
     exchange.SendReject();
 }