Beispiel #1
0
        public override Message OnReceiveMessage(Message message)
        {
            if (message.GetType() != typeof(Response))
            {
                throw new NotImplementedException();
            }

            var response = (Response)message;

            lock (ReceivedResponses) {
                // if duplicated response, ignore
                if (ReceivedResponses.ContainsKey(response.Request.SeqNum))
                {
                    return(null);
                }

                ReceivedResponses.Add(response.Request.SeqNum, response);
            }

            // if it receives at least one response for a blocking request then unblock ( can only send one and then block, so it will always unblock properly? )
            if (response.Request.GetType() == typeof(ReadRequest) ||
                response.Request.GetType() == typeof(TakeRequest))
            {
                if (RequestSemaphore.TryGetValue(response.Request.SeqNum, out var semaphore))
                {
                    semaphore.Release();
                    return(null);
                }

                throw new NotImplementedException("No semaphore was allocated.");
            }
            return(null);
        }