Handles asynchronous responses
Example #1
0
        public override FutureResponse AsyncRequest(Command command)
        {
            int commandId = GetNextCommandId();

            command.CommandId        = commandId;
            command.ResponseRequired = true;
            FutureResponse future     = new FutureResponse();
            Exception      priorError = null;

            lock (requestMap.SyncRoot)
            {
                priorError = this.error;
                if (priorError == null)
                {
                    requestMap[commandId] = future;
                }
            }

            if (priorError != null)
            {
                BrokerError brError = new BrokerError();
                brError.Message = priorError.Message;
                ExceptionResponse response = new ExceptionResponse();
                response.Exception = brError;
                future.Response    = response;
                throw priorError;
            }

            next.Oneway(command);

            return(future);
        }
Example #2
0
        public override FutureResponse AsyncRequest(Command command)
        {
            int commandId = GetNextCommandId();

            command.CommandId = commandId;
            command.ResponseRequired = true;
            FutureResponse future = new FutureResponse();
	        Exception priorError = null;
	        lock(requestMap.SyncRoot) 
			{
	            priorError = this.error;
	            if(priorError == null) 
				{
		            requestMap[commandId] = future;
	            }
	        }
	
	        if(priorError != null) 
			{
				BrokerError brError = new BrokerError();
				brError.Message = priorError.Message;
				ExceptionResponse response = new ExceptionResponse();
				response.Exception = brError;
	            future.Response = response;
	            throw priorError;
	        }
			
            next.Oneway(command);

			return future;
        }
Example #3
0
        protected override void OnCommand(ITransport sender, Command command)
        {
            if (command.IsResponse)
            {
                Response       response      = (Response)command;
                int            correlationId = response.CorrelationId;
                FutureResponse future        = (FutureResponse)requestMap[correlationId];

                if (future != null)
                {
                    requestMap.Remove(correlationId);
                    future.Response = response;

                    if (response is ExceptionResponse)
                    {
                        ExceptionResponse er          = response as ExceptionResponse;
                        BrokerError       brokerError = er.Exception;
                        BrokerException   exception   = new BrokerException(brokerError);
                        this.exceptionHandler(this, exception);
                    }
                }
                else
                {
                    if (Tracer.IsDebugEnabled)
                    {
                        Tracer.Debug("Unknown response ID: " + response.CommandId + " for response: " + response);
                    }
                }
            }
            else
            {
                this.commandHandler(sender, command);
            }
        }
Example #4
0
        protected override void OnCommand(ITransport sender, Command command)
        {
            if (command.IsResponse)
            {
                Response       response      = (Response)command;
                int            correlationId = response.CorrelationId;
                FutureResponse future        = (FutureResponse)requestMap[correlationId];

                if (future != null)
                {
                    requestMap.Remove(correlationId);
                    future.Response = response;
                }
                else
                {
                    if (Tracer.IsDebugEnabled)
                    {
                        Tracer.Debug("Unknown response ID: " + response.CorrelationId + " for response: " + response);
                    }
                }
            }
            else
            {
                this.commandHandler(sender, command);
            }
        }
Example #5
0
        public override async Task <Response> RequestAsync(Command command, TimeSpan timeout)
        {
            FutureResponse future = AsyncRequest(command);

            if (timeout.TotalMilliseconds > 0)
            {
                using (CancellationTokenSource ct = new CancellationTokenSource(timeout))
                {
                    using (ct.Token.Register(() =>
                    {
                        if (future.TrySetException(new RequestTimedOutException(timeout)))
                        {
                            requestMap.Remove(command.CommandId);
                        }
                    }, false))
                    {
                        return(await future.Task);
                    }
                }
            }
            else
            {
                return(await future.Task);
            }
        }
Example #6
0
        public override Response Request(Command command, TimeSpan timeout)
        {
            FutureResponse future = AsyncRequest(command);

            future.ResponseTimeout = timeout;
            Response response = future.Response;

            return(response);
        }
        public override FutureResponse AsyncRequest(Command command)
        {
            int commandId = GetNextCommandId();

            command.CommandId        = commandId;
            command.ResponseRequired = true;
            FutureResponse future = new FutureResponse();

            requestMap[commandId] = future;
            next.Oneway(command);
            return(future);
        }
        protected override void OnException(ITransport sender, Exception command)
        {
            base.OnException(sender, command);

            foreach (DictionaryEntry entry in requestMap)
            {
                FutureResponse    value    = (FutureResponse)entry.Value;
                ExceptionResponse response = new ExceptionResponse();
                BrokerError       error    = new BrokerError();

                error.Message      = command.Message;
                response.Exception = error;
                value.Response     = response;
            }

            requestMap.Clear();
        }
Example #9
0
        public override Response Request(Command command, TimeSpan timeout)
        {
            FutureResponse future = AsyncRequest(command);

            future.ResponseTimeout = timeout;
            Response response = future.Response;

            if (response != null && response is ExceptionResponse)
            {
                ExceptionResponse er          = response as ExceptionResponse;
                BrokerError       brokerError = er.Exception;

                if (brokerError == null)
                {
                    throw new BrokerException();
                }
                else
                {
                    throw new BrokerException(brokerError);
                }
            }

            return(response);
        }