Exemplo n.º 1
0
        /// <summary>
        /// Performs IMRU iterations on update side
        /// </summary>
        /// <returns></returns>
        protected override byte[] TaskBody(byte[] memento)
        {
            UpdateResult <TMapInput, TResult> updateResult = null;

            try
            {
                updateResult = _updateTask.Initialize();
            }
            catch (Exception e)
            {
                HandleTaskAppException(e);
            }

            while (!_cancellationSource.IsCancellationRequested && updateResult.HasMapInput)
            {
                using (
                    var message = new MapInputWithControlMessage <TMapInput>(updateResult.MapInput,
                                                                             MapControlMessage.AnotherRound))
                {
                    _dataAndControlMessageSender.Send(message);
                }

                if (_invokeGc)
                {
                    Logger.Log(Level.Verbose, "Calling Garbage Collector");
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }

                var input = _dataReceiver.Reduce(_cancellationSource);

                try
                {
                    updateResult = _updateTask.Update(input);
                    if (updateResult.HasResult)
                    {
                        _resultHandler.HandleResult(updateResult.Result);
                        _done = true;
                    }
                }
                catch (Exception e)
                {
                    HandleTaskAppException(e);
                }
            }

            if (!_cancellationSource.IsCancellationRequested)
            {
                MapInputWithControlMessage <TMapInput> stopMessage =
                    new MapInputWithControlMessage <TMapInput>(MapControlMessage.Stop);
                _dataAndControlMessageSender.Send(stopMessage);
            }

            if (_done)
            {
                return(ByteUtilities.StringToByteArrays(TaskManager.UpdateTaskCompleted));
            }
            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Performs IMRU iterations on update side
        /// </summary>
        /// <param name="memento"></param>
        /// <returns></returns>
        public byte[] Call(byte[] memento)
        {
            var updateResult = _updateTask.Initialize();
            int iterNo       = 0;

            while (updateResult.HasMapInput && !_taskCloseCoordinator.ShouldCloseTask())
            {
                iterNo++;

                using (
                    var message = new MapInputWithControlMessage <TMapInput>(updateResult.MapInput,
                                                                             MapControlMessage.AnotherRound))
                {
                    _dataAndControlMessageSender.Send(message);
                }

                var input = _dataReceiver.Reduce();

                if (_invokeGC)
                {
                    Logger.Log(Level.Verbose, "Calling Garbage Collector");
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }

                updateResult = _updateTask.Update(input);

                if (updateResult.HasResult)
                {
                    _resultHandler.HandleResult(updateResult.Result);
                }
            }

            if (!_taskCloseCoordinator.ShouldCloseTask())
            {
                MapInputWithControlMessage <TMapInput> stopMessage =
                    new MapInputWithControlMessage <TMapInput>(MapControlMessage.Stop);
                _dataAndControlMessageSender.Send(stopMessage);
            }

            _resultHandler.Dispose();
            _taskCloseCoordinator.SignalTaskStopped();
            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Performs IMRU iterations on update side
        /// </summary>
        /// <param name="memento"></param>
        /// <returns></returns>
        public byte[] Call(byte[] memento)
        {
            var updateResult = _updateTask.Initialize();
            int iterNo       = 0;

            try
            {
                while (updateResult.HasMapInput && !_cancellationSource.IsCancellationRequested)
                {
                    iterNo++;

                    using (
                        var message = new MapInputWithControlMessage <TMapInput>(updateResult.MapInput,
                                                                                 MapControlMessage.AnotherRound))
                    {
                        _dataAndControlMessageSender.Send(message);
                    }

                    var input = _dataReceiver.Reduce(_cancellationSource);

                    if (_invokeGC)
                    {
                        Logger.Log(Level.Verbose, "Calling Garbage Collector");
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }

                    updateResult = _updateTask.Update(input);

                    if (updateResult.HasResult)
                    {
                        _resultHandler.HandleResult(updateResult.Result);
                    }
                }
                if (!_cancellationSource.IsCancellationRequested)
                {
                    MapInputWithControlMessage <TMapInput> stopMessage =
                        new MapInputWithControlMessage <TMapInput>(MapControlMessage.Stop);
                    _dataAndControlMessageSender.Send(stopMessage);
                }
            }
            catch (OperationCanceledException e)
            {
                Logger.Log(Level.Warning,
                           "Received OperationCanceledException in UpdateTaskHost with message: {0}.",
                           e.Message);
            }
            catch (IOException e)
            {
                Logger.Log(Level.Error, "Received IOException in UpdateTaskHost with message: {0}.", e.Message);
                if (!_cancellationSource.IsCancellationRequested)
                {
                    throw new IMRUTaskGroupCommunicationException(TaskManager.TaskGroupCommunicationError);
                }
            }
            catch (TcpClientConnectionException e)
            {
                Logger.Log(Level.Error,
                           "Received TcpClientConnectionException in UpdateTaskHost with message: {0}.",
                           e.Message);
                if (!_cancellationSource.IsCancellationRequested)
                {
                    throw new IMRUTaskGroupCommunicationException(TaskManager.TaskGroupCommunicationError);
                }
            }
            _resultHandler.Dispose();
            _taskCloseCoordinator.SignalTaskStopped();
            Logger.Log(Level.Info, "UpdateTaskHost returned with cancellation token {0}.", _cancellationSource.IsCancellationRequested);
            return(null);
        }