Exemplo n.º 1
0
        /// <summary>
        /// Handle task status messages.
        /// </summary>
        /// <param name="taskStatusProto"></param>
        private void Handle(TaskStatusProto taskStatusProto)
        {
            LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Received task {0} status {1}", taskStatusProto.task_id, taskStatusProto.state));
            string taskId    = taskStatusProto.task_id;
            string contextId = taskStatusProto.context_id;
            State  taskState = taskStatusProto.state;

            if (taskState == State.INIT)
            {
                EvaluatorContext evaluatorContext = GetEvaluatorContext(contextId);
                _runningTask = new RunningTaskImpl(this, taskId, evaluatorContext);
                // this.dispatcher.onNext(RunningTask.class, this.runningTask);
            }
            else if (taskState == State.SUSPEND)
            {
                EvaluatorContext evaluatorContext = GetEvaluatorContext(contextId);
                _runningTask = null;
                byte[] message = taskStatusProto.result != null ? taskStatusProto.result : null;
                LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + evaluatorContext + message.ToString());
                //this.dispatcher.onNext(SuspendedTask.class, new SuspendedTaskImpl(evaluatorContext, message, taskId));
            }
            else if (taskState == State.DONE)
            {
                EvaluatorContext evaluatorContext = GetEvaluatorContext(contextId);
                _runningTask = null;
                byte[] message = taskStatusProto.result != null ? taskStatusProto.result : null;
                LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + evaluatorContext + message.ToString());
                //this.dispatcher.onNext(CompletedTask.class, new CompletedTaskImpl(evaluatorContext, message, taskId));
            }
            else if (taskState == State.FAILED)
            {
                _runningTask = null;
                //EvaluatorContext evaluatorContext = GetEvaluatorContext(contextId);
                //FailedTask failedTask = taskStatusProto.result != null ?
                //    new FailedTask(taskId, ByteUtilities.ByteArrarysToString(taskStatusProto.result), Optional<IActiveContext>.Of(evaluatorContext)) :
                //    new FailedTask(taskId, "Failed task: " + taskState, Optional<IActiveContext>.Of(evaluatorContext));
                //LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + failedTask.ToString());
                //this.dispatcher.onNext(FailedTask.class, taskException);
            }
            else if (taskStatusProto.task_message.Count > 0)
            {
                if (_runningTask != null)
                {
                    var e = new InvalidOperationException("runningTask must be null when there are multiple task messages");
                    Exceptions.Throw(e, LOGGER);
                }
                foreach (TaskStatusProto.TaskMessageProto taskMessageProto in taskStatusProto.task_message)
                {
                    LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + taskMessageProto.ToString());
                    //        this.dispatcher.onNext(TaskMessage.class,
                    //new TaskMessageImpl(taskMessageProto.getMessage().toByteArray(),
                    //    taskId, contextId, taskMessageProto.getSourceId()));
                }
            }
        }
Exemplo n.º 2
0
 private void RemoveEvaluatorContext(EvaluatorContext context)
 {
     _activeContexts.Remove(context);
     _activeContextIds.Remove(context.Id);
 }
Exemplo n.º 3
0
 private void AddEvaluatorContext(EvaluatorContext context)
 {
     _activeContexts.Add(context);
     _activeContextIds.Add(context.Id);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Handle a context status update
        /// </summary>
        /// <param name="contextStatusProto"></param>
        /// <param name="notifyClientOnNewActiveContext"></param>
        private void Handle(ContextStatusProto contextStatusProto, bool notifyClientOnNewActiveContext)
        {
            string            contextId = contextStatusProto.context_id;
            Optional <string> parentId  = contextStatusProto.parent_id != null ?
                                          Optional <string> .Of(contextStatusProto.parent_id) : Optional <string> .Empty();

            if (ContextStatusProto.State.READY == contextStatusProto.context_state)
            {
                if (!_activeContextIds.Contains(contextId))
                {
                    EvaluatorContext evaluatorContext = new EvaluatorContext(this, contextId, parentId);
                    AddEvaluatorContext(evaluatorContext);
                    if (notifyClientOnNewActiveContext)
                    {
                        LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + evaluatorContext.ToString());
                        //TODO
                        //dispatcher.onNext(ActiveContext.class, context);
                    }
                }
                foreach (ContextStatusProto.ContextMessageProto contextMessageProto in contextStatusProto.context_message)
                {
                    byte[] message  = contextMessageProto.message;
                    string sourceId = contextMessageProto.source_id;
                    LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + sourceId + message);
                    //        this.dispatcher.onNext(ContextMessage.class,
                    //new ContextMessageImpl(theMessage, contextID, sourceID));
                }
            }
            else
            {
                if (!_activeContextIds.Contains(contextId))
                {
                    if (ContextStatusProto.State.FAIL == contextStatusProto.context_state)
                    {
                        AddEvaluatorContext(new EvaluatorContext(this, contextId, parentId));
                    }
                    else
                    {
                        var e = new InvalidOperationException("unknown context signaling state " + contextStatusProto.context_state);
                        Exceptions.Throw(e, LOGGER);
                    }
                }
            }

            EvaluatorContext context       = GetEvaluatorContext(contextId);
            EvaluatorContext parentContext = context.ParentId.IsPresent() ?
                                             GetEvaluatorContext(context.ParentId.Value) : null;

            RemoveEvaluatorContext(context);

            if (ContextStatusProto.State.FAIL == contextStatusProto.context_state)
            {
                // TODO
                Exception reason = new InvalidOperationException(ByteUtilities.ByteArrarysToString(contextStatusProto.error));
                Optional <IActiveContext> optionalParentContext = (null == parentContext) ?
                                                                  Optional <IActiveContext> .Empty() : Optional <IActiveContext> .Of(parentContext);

                LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + reason.ToString() + optionalParentContext);
                // TODO
                //this.dispatcher.onNext(FailedContext.class,
                //context.getFailedContext(optionalParentContext, reason));
            }
            else if (ContextStatusProto.State.DONE == contextStatusProto.context_state)
            {
                if (null != parentContext)
                {
                    // TODO
                    //this.dispatcher.onNext(ClosedContext.class, context.getClosedContext(parentContext));
                }
                else
                {
                    LOGGER.Log(Level.Info, "Root context closed. Evaluator closed will trigger final shutdown.");
                }
            }
            else
            {
                var e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unknown context state {0} for context {1}", contextStatusProto.context_state, contextId));
                Exceptions.Throw(e, LOGGER);
            }
        }