Example #1
0
        private void WaitServiceEnded()
        {
            var msg = _communicator.GetMessage <IDTRequest>();

            if (msg != null)
            {
                bool ok = msg.ServiceRequest == 0;

                if (ok)
                {
                    //Richiesta finita
                    _loopStatus    = LoopStatus.NoActiveService;
                    _forceService0 = true;
                    OnDiagnosticMessage(String.Format("Service {0} ended", _request.GetType().Name));
                }
                else
                {
                    //Devo spedire che la richiesta è completata
                    var reply = _request.GetCompletedReply();
                    Debug.Assert(reply != null);
                    _communicator.QueueMessage(reply);
                }

                if (_communicator.IsConnected == false)
                {
                    Log.Info("WAIT SERVICE ---> PERSA COMUNICAZIONE CON FIRAP");
                    //   _loopStatus = LoopStatus.NoActiveService;
                }
            }
        }
Example #2
0
        private void ServiceRunningState()
        {
            var msg = _communicator.GetMessage <IDTRequest>();

            if (msg != null)
            {
                bool abort = msg.ServiceRequest == 0;
                if (abort)
                {
                    _request.Abort();
                }
            }

            _request.Executing();

            if (msg != null && _request.Completed == false)
            {
                //  Log.Info("contrololo se franco mi restituisce 1");
                //Devo spedire una risposta
                var reply = _request.GetProgressReply();
                OnDiagnosticMessage(String.Format("sending progress {0}", _request.GetType().Name));
                if (reply != null)
                {
                    _communicator.QueueMessage(reply);
                }
            }
            else if (_request.Completed == true)
            {
                this._loopStatus = LoopStatus.WaitingServiceEnded;
            }

            /* else if (msg == null) // A.B. aggiunto per uscire dal loop wait se cade la comunicazione
             * {
             *   this._loopStatus = LoopStatus.NoActiveService;
             * }*/
            //else if (_communicator != null && _communicator.IsConnected)// A.B. 18.09.2014 aggiunto per uscire dal loop wait se cade la comunicazione
            //{
            //    this._loopStatus = LoopStatus.NoActiveService;
            //}

            if (_communicator.IsConnected == false)
            {
                Log.Info("RUNNING SERVICE ---> PERSA COMUNICAZIONE CON FIRAP");
                //(°°) 13-10-2014 inserita condizione di reset a ciclo 0 quando perdo la comunicazione con DSA
                _loopStatus = LoopStatus.NoActiveService;
            }
        }
Example #3
0
        /// <summary>
        /// 读取实体属性遍历时,为每个属性生成内容时使用的模板
        /// </summary>
        /// <returns></returns>
        protected byte[] ReadDtoSeeTemplate()
        {
            List <byte> templateContent = new List <byte>(); //标签模式下读取到的内容全部放到这里,直到标签结束时调用对应的方法
            List <byte> buffer          = new List <byte>(); //所有判断模式下,数据读取到的数据都将暂存在这块

            LoopStatus status  = LoopStatus.文本读取;
            byte       current = 0;

            while (_templateReader.BaseStream.Position < _templateReader.BaseStream.Length)
            {
                current = _templateReader.ReadByte();
                if (current == '\n')
                {
                    _lineNumber++;
                    _columnNumber = 1;
                }
                else
                {
                    _columnNumber++;
                }
                switch (status)
                {
                case LoopStatus.文本读取:
                {
                    //文本模式下,需要时刻判断是否有标签开始
                    if (current == _dtoSeeTemplateBeginSign[0])
                    {
                        status = LoopStatus.标签头判断;
                        buffer.Clear();
                        buffer.Add(current);
                    }
                    else
                    {
                        WriteData(current);
                    }
                }
                break;

                case LoopStatus.标签头判断:
                {
                    //判断标签头状态,如果

                    if (current == _dtoSeeTemplateBeginSign[buffer.Count])
                    {
                        buffer.Add(current);
                        //如果缓存的信息已经和标签头完全匹配,则进入模板内容读取阶段
                        if (_dtoSeeTemplateBeginSign.Length == buffer.Count && _dtoSeeTemplateBeginSign.Except(buffer).IsNullOrEmpty())
                        {
                            status = LoopStatus.标签内容读取;
                        }
                    }
                    else        //和标签头不匹配,将缓存的内容写入到流中,并将状态回滚到文本模式
                    {
                        buffer.Add(current);
                        WriteData(buffer.ToArray());
                        status = LoopStatus.文本读取;
                        buffer.Clear();
                    }
                }
                break;

                case LoopStatus.标签内容读取:
                {
                    //标签模式下,判断是否是标签结束标记
                    if (current == _dtoSeeTemplateEndSign[0])
                    {
                        status = LoopStatus.标签尾判断;
                        buffer.Clear();
                        buffer.Add(current);
                    }
                    else
                    {
                        templateContent.Add(current);
                    }
                }
                break;

                case LoopStatus.标签尾判断:
                {
                    //判断是否满足标签结束要求,当完全匹配标签尾时,将状态跳转到文本模式,


                    if (current == _dtoSeeTemplateEndSign[buffer.Count])
                    {
                        buffer.Add(current);
                        //如果缓存的信息已经和标签尾完全匹配,则进入标签内容处理阶段
                        if (_dtoSeeTemplateEndSign.Length == buffer.Count && _dtoSeeTemplateEndSign.Except(buffer).IsNullOrEmpty())
                        {
                            return(templateContent.ToArray());
                        }
                    }
                    else        //和标签尾不匹配,将缓存的内容写入到标签内容中,并将状态回滚到标签内容模式
                    {
                        buffer.Add(current);
                        templateContent.AddRange(buffer);
                        status = LoopStatus.标签内容读取;
                        buffer.Clear();
                    }
                }
                break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            throw new Exception($"模板内容有误!文件:{_templatePath}");
        }
Example #4
0
        /// <summary>
        /// 主循环,从头到尾扫描模板流。遇到 {@ 时直接调用相关的解释方法,然后解析接下来的内容.直到遇到 @} 后返回到主循环中.
        /// </summary>
        protected void MainLoop(BinaryReader reader, BinaryWriter writer, bool tracePos = true)
        {
            List <byte> tagContent = new List <byte>(); //标签模式下读取到的内容全部放到这里,直到标签结束时调用对应的方法
            List <byte> buffer     = new List <byte>(); //所有判断模式下,数据读取到的数据都将暂存在这块

            LoopStatus status  = LoopStatus.文本读取;
            byte       current = 0;

            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                current = reader.ReadByte();
                if (tracePos)
                {
                    if (current == '\n')
                    {
                        _lineNumber++;
                        _columnNumber = 1;
                    }
                    else
                    {
                        _columnNumber++;
                    }
                }
                switch (status)
                {
                case LoopStatus.文本读取:
                {
                    //文本模式下,需要时刻判断是否有标签开始
                    if (current == _tagBeginSign[0])
                    {
                        status = LoopStatus.标签头判断;
                        buffer.Clear();
                        buffer.Add(current);
                    }
                    else
                    {
                        writer.Write(current);
                    }
                }
                break;

                case LoopStatus.标签头判断:
                {
                    //判断标签头状态,如果

                    if (current == _tagBeginSign[buffer.Count])
                    {
                        buffer.Add(current);
                        //如果缓存的信息已经和标签头完全匹配,则进入标签内容读取阶段
                        if (_tagBeginSign.Length == buffer.Count && _tagBeginSign.Except(buffer).IsNullOrEmpty())
                        {
                            status = LoopStatus.标签内容读取;
                        }
                    }
                    else        //和标签头不匹配,将缓存的内容写入到流中,并将状态回滚到文本模式
                    {
                        //假设起始标记为:{{@,结束标记为:@}},那么遇到{{{@A@}}}时,需要特殊处理
                        //如果当前的字符不能和之前已存放的数据一起组合成起始标记,则 将buffer中最前方的一个字符视为普通文本并写入到流中,
                        buffer.Add(current);
                        do
                        {
                            WriteData(buffer[0]);
                            buffer.RemoveAt(0);
                        } while (StartWith(_tagBeginSign, buffer) == false);

                        if (buffer.Count == 0)
                        {
                            //buffer.Add(current);
                            writer.Write(buffer.ToArray());
                            status = LoopStatus.文本读取;
                            buffer.Clear();
                        }
                    }
                }
                break;

                case LoopStatus.标签内容读取:
                {
                    //标签模式下,判断是否是标签结束标记
                    if (current == _tagEndSign[0])
                    {
                        status = LoopStatus.标签尾判断;
                        buffer.Clear();
                        buffer.Add(current);
                    }
                    else
                    {
                        tagContent.Add(current);
                    }
                }
                break;

                case LoopStatus.标签尾判断:
                {
                    //判断是否满足标签结束要求,当完全匹配标签尾时,将状态跳转到文本模式,


                    if (current == _tagEndSign[buffer.Count])
                    {
                        buffer.Add(current);
                        //如果缓存的信息已经和标签尾完全匹配,则进入标签内容处理阶段
                        if (_tagEndSign.Length == buffer.Count && _tagEndSign.Except(buffer).IsNullOrEmpty())
                        {
                            HandleTag(Encoding.GetString(tagContent.ToArray()));
                            tagContent.Clear();
                            status = LoopStatus.文本读取;
                        }
                    }
                    else        //和标签尾不匹配,将缓存的内容写入到标签内容中,并将状态回滚到标签内容模式
                    {
                        tagContent.AddRange(buffer.ToArray());
                        buffer.Clear();
                        status = LoopStatus.标签内容读取;
                    }
                }
                break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            writer.Flush();
        }
Example #5
0
        protected override void Func(object param)
        {
            _forceService0 = false;
            _loopStatus    = LoopStatus.NoActiveService;

            _communicator.Start();

            OnDiagnosticMessage("Staring main loop");

            long?avrCycleTime = null;

            while (ShouldStop(5) == false)
            {
                _cycleTimeST.Restart();

                this.Cycle();



                switch (_loopStatus)
                {
                case LoopStatus.NoActiveService:
                {
                    var req = CheckForNewRequest();

                    if (req != null)
                    {
                        if (req.ServiceNumber != 0)
                        {
                            _request    = req;
                            _loopStatus = LoopStatus.RunningService;
                            _request.Start();
                            OnDiagnosticMessage(String.Format("Started service {0}", _request.GetType().Name));
                        }
                        else
                        {
                            //Service 0 is special
                            req.Start();
                            var rep = req.GetCompletedReply();
                            Debug.Assert(rep != null);
                            //                                    OnDiagnosticMessage(String.Format("Received service {0}", req.GetType().Name));
                            _communicator.QueueMessage(rep);
                        }
                    }
                }
                break;

                case LoopStatus.RunningService:
                {
                    ServiceRunningState();
                }
                break;

                case LoopStatus.WaitingServiceEnded:
                {
                    WaitServiceEnded();
                }
                break;
                }

                if (avrCycleTime == null)
                {
                    avrCycleTime = _cycleTimeST.ElapsedMilliseconds;
                }
                else
                {
                    avrCycleTime = (_cycleTimeST.ElapsedMilliseconds + avrCycleTime.Value) / 2;
                }
            }

            _communicator.AskStop();
            _communicator.WaitStop();
        }
Example #6
0
 /// <summary>
 /// Modifica o status do loop
 /// </summary>
 /// <param name="newStatus"></param>
 private void ChangeCurStatus(LoopStatus newStatus)
 {
     _curLoopStatus = newStatus;
 }