예제 #1
0
        private void EngineStateReportEventHandler(TELEGRAPH_ENGINE_STATE State, TelegraphEngine EngineItem)
        {
            switch (State)
            {
            case TELEGRAPH_ENGINE_STATE.ENGINE_START:
                m_Adapter.WriteLogLine("Telegraph engine starts working..");
                break;

            case TELEGRAPH_ENGINE_STATE.ENGINE_DISPOSED:
                EngineItem.EngineStateReportEvent -= new EngineStateReport(EngineStateReportEventHandler);
                m_Adapter.WriteLogLine("Telegraph engine is disposed.");
                if (m_tEngine == EngineItem)
                {
                    m_tEngine = null;
                }

                break;

            case TELEGRAPH_ENGINE_STATE.ENGINE_STOPED:
                //EngineItem.EngineStateReportEvent -= new EngineStateReport(EngineStateReportEventHandler);
                m_Adapter.WriteLogLine("Telegraph engine stopped.");

                break;

            case TELEGRAPH_ENGINE_STATE.ENGINE_STOPPING:
                m_Adapter.WriteLogLine("Requesting stopping telegraph engine...");
                break;
            }
        }
        private void EngineStateReportEventHandler(TELEGRAPH_ENGINE_STATE State, TelegraphEngine EngineItem)
        {
            switch (State)
            {
            case TELEGRAPH_ENGINE_STATE.ENGINE_DISPOSED:
                if (null != m_Adapter)
                {
                    m_Adapter.WriteLogLine("Telegraph engine disposed.");
                }
                if (EngineItem == m_Engine)
                {
                    m_Engine.EngineStateReportEvent -= new EngineStateReport(EngineStateReportEventHandler);
                    m_Engine = null;
                }
                break;

            case TELEGRAPH_ENGINE_STATE.ENGINE_STOPED:
                if (null != m_Adapter)
                {
                    m_Adapter.WriteLogLine("Telegrapp engine stopped. Try to fetch next telegraph group.");
                }
                //Thread.Sleep(5);
                FetchNextTelegraphGroup();
                break;

            case TELEGRAPH_ENGINE_STATE.ENGINE_START:
                if (null != m_Adapter)
                {
                    m_Adapter.WriteLogLine("Telegraph engine get start.");
                }
                break;

            case TELEGRAPH_ENGINE_STATE.ENGINE_STOPPING:
                if (null != m_Adapter)
                {
                    m_Adapter.WriteLogLine("Stopping Telegraph engine...");
                }
                break;

            case TELEGRAPH_ENGINE_STATE.ENGINE_WORKING:
                break;
            }
        }
        public void Dispose()
        {
            if (!m_bDisposed)
            {
                m_bDisposed = true;

                try
                {
                    if (null != m_Engine)
                    {
                        m_Engine.EngineStateReportEvent -= new EngineStateReport(EngineStateReportEventHandler);
                        m_Engine.Dispose();
                        m_Engine = null;
                    }
                }
                catch (Exception Err)
                {
                    Err.ToString();
                }

                GC.SuppressFinalize(this);
            }
        }
        private void FetchNextTelegraphGroup()
        {
            Object tObj = new Object();

            lock (tObj)
            {
                Telegraph[]     tTelegraphGroup = null;
                TelegraphEngine tEngine         = null;

                do
                {
                    lock (((ICollection)m_TelegrapGroupQueue).SyncRoot)
                    {
                        if (m_TelegrapGroupQueue.Count > 0)
                        {
                            tTelegraphGroup = m_TelegrapGroupQueue.Dequeue();
                            if (null == tTelegraphGroup)
                            {
                                continue;
                            }
                            if (0 == tTelegraphGroup.Length)
                            {
                                continue;
                            }

                            tEngine = tTelegraphGroup[0].CreateTelegraphEngine();
                            if (null == tEngine)
                            {
                                continue;
                            }
                            tEngine.ParentAdapter = m_Adapter;
                            tEngine.Priority      = ThreadPriority.Normal;

                            m_WaitTelegraphGroup.Reset();
                            break;
                        }
                        else
                        {
                            if (null != m_Adapter)
                            {
                                m_Adapter.WriteLogLine("Adapter enter idle state.");
                            }
                        }
                    }

                    switch (WaitHandle.WaitAny(m_WaitHandlers))
                    {
                    case 0:
                        break;

                    case 1:
                        m_IsWorking        = false;
                        m_Engine.IsWorking = false;
                        m_Engine.EngineStateReportEvent -= new EngineStateReport(EngineStateReportEventHandler);
                        m_Engine = null;
                        return;
                    }
                }while (true);


                if (null == m_Engine)
                {
                    m_Engine = tEngine;
                    m_Engine.EngineStateReportEvent += new EngineStateReport(EngineStateReportEventHandler);
                }
                else
                {
                    if (m_Engine.Type != tEngine.Type)
                    {
                        m_Engine.EngineStateReportEvent -= new EngineStateReport(EngineStateReportEventHandler);
                        m_Engine.Dispose();
                        if (null != m_Adapter)
                        {
                            m_Adapter.WriteLogLine("Telegraph engine changed.");
                        }
                        m_Engine = tEngine;
                    }
                }

                m_Engine.TryToSendTelegraphs(tTelegraphGroup);
                m_IsWorking = true;
                if (null != m_Adapter)
                {
                    m_Adapter.WriteLogLine("Adapter enter working state.");
                }
                m_Engine.IsWorking = true;
            }
        }