コード例 #1
0
        private void ExecuteAction(DoseTest.Action action)
        {
            switch (action.ActionKind.Val)
            {
            case DoseTest.ActionEnum.Enumeration.Reset:
                if (m_isActive)
                {
                    m_testConnection.Close();

                    Dispatcher oldDispatcher = m_testDispatcher; //keep this for a while, so we get a new dispatcher address.
                    m_testDispatcher = new Dispatcher(m_testDispatchEvent);
                    if (oldDispatcher != null)                   //add a check to avoid a warning from mono
                    {
                        oldDispatcher = null;
                    }

                    StopHandler oldStopHandler = m_testStopHandler;      //keep this for a while, so we get a new stopHandler address.
                    m_testStopHandler = new StopHandler();
                    if (oldStopHandler != null)
                    {
                        oldStopHandler = null;
                    }

                    m_testConnection.Open(m_testConnectionName, m_instanceString, m_defaultContext, null, m_testDispatcher);
                    using (Safir.Dob.EntityProxy ep = m_controlConnection.Read(m_partnerEntityId))
                    {
                        DoseTest.Partner partner = ep.Entity as DoseTest.Partner;
                        if (partner.Incarnation.IsNull())
                        {
                            partner.Incarnation.Val = 0;
                        }
                        else
                        {
                            partner.Incarnation.Val = partner.Incarnation.Val + 1;
                        }
                        m_controlConnection.SetChanges(partner, m_partnerEntityId.InstanceId, new Safir.Dob.Typesystem.HandlerId(m_instance));
                    }
                    Consumer[] oldCons = m_consumers;     //keep these for a while, so we get new consumer addresses.
                    m_consumers = new Consumer[3];

                    for (int i = 0; i < 3; ++i)
                    {
                        m_consumers[i] = new Consumer(i, m_testConnectionName, m_instanceString);
                    }

                    if (oldCons != null)    //avoid warning...
                    {
                        oldCons = null;
                    }

                    foreach (KeyValuePair <Safir.Dob.CallbackId.Enumeration, List <DoseTest.Action> > cbActions
                             in m_callbackActions)
                    {
                        cbActions.Value.Clear();
                    }
                }
                break;

            case DoseTest.ActionEnum.Enumeration.CheckReferences:
                if (m_isActive)
                {
                    if (m_consumers != null)
                    {
                        m_consumers = null;
                    }

                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();

                    // After releasing the executor's references and a garabage collection, there should be no
                    // Consumer instances
                    if (Consumer.instanceCount != 0)
                    {
                        Logger.Instance.WriteLine("Expected 0 consumer instances, but there is " + Consumer.instanceCount);
                    }

                    // restore consumers
                    m_consumers = new Consumer[3];
                    for (int i = 0; i < 3; ++i)
                    {
                        m_consumers[i] = new Consumer(i, m_testConnectionName, m_instanceString);
                    }
                }
                break;

            case DoseTest.ActionEnum.Enumeration.CloseAndCheckReferences:
                if (m_isActive)
                {
                    m_testConnection.Close();

                    if (m_consumers != null)
                    {
                        m_consumers = null;
                    }

                    if (m_testDispatcher != null)
                    {
                        m_testDispatcher = null;
                    }

                    if (m_testStopHandler != null)
                    {
                        m_testStopHandler = null;
                    }

                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();

                    // After releasing the executor's references and a garabage collection, there should be no
                    // Consumer instances and no Dispatcher instances
                    if (Consumer.instanceCount != 0)
                    {
                        Logger.Instance.WriteLine("Expected 0 consumer instances, but there is " + Consumer.instanceCount);
                    }
                    if (Dispatcher.instanceCount != 0)
                    {
                        Logger.Instance.WriteLine("Expected 0 dispatcher instances, but there is " + Dispatcher.instanceCount);
                    }
                    if (StopHandler.instanceCount != 0)
                    {
                        Logger.Instance.WriteLine("Expected 0 stopHandler instances, but there is " + StopHandler.instanceCount);
                    }

                    // Restore dispatcher
                    m_testDispatcher = new Dispatcher(m_testDispatchEvent);

                    m_testConnection.Open(m_testConnectionName, m_instanceString, 0, null, m_testDispatcher);

                    // Restore consumers
                    m_consumers = new Consumer[3];
                    for (int i = 0; i < 3; ++i)
                    {
                        m_consumers[i] = new Consumer(i, m_testConnectionName, m_instanceString);
                    }

                    // Restore stopHandler
                    m_testStopHandler = new StopHandler();
                }
                break;

            case DoseTest.ActionEnum.Enumeration.RunGarbageCollector:
                if (m_isActive)
                {
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                }
                break;

            case DoseTest.ActionEnum.Enumeration.Open:
            {
                if (m_isActive)
                {
                    System.Int32 context = m_defaultContext;
                    if (!action.Context.IsNull())
                    {
                        context = action.Context.Val;
                    }

                    string connName = m_testConnectionName;
                    if (!action.ConnectionName.IsNull())
                    {
                        connName = action.ConnectionName.Val;
                    }
                    m_testConnection.Open(connName, m_instanceString, context, m_testStopHandler, m_testDispatcher);
                }
            }
            break;

            case DoseTest.ActionEnum.Enumeration.Close:
            {
                if (m_isActive)
                {
                    m_testConnection.Close();
                }
            }
            break;

            case DoseTest.ActionEnum.Enumeration.InhibitDispatch:
                if (m_isActive)
                {
                    m_dispatchTestConnection = !action.Inhibit.Val;
                    Logger.Instance.WriteLine("InhibitDispatch set to " + m_dispatchTestConnection);
                }
                break;

            case DoseTest.ActionEnum.Enumeration.InhibitOutgoingTraffic:
                if (m_isActive)
                {
                    byte success;
                    InhibitOutgoingTraffic(ByteOf(action.Inhibit.Val), out success);
                    Logger.Instance.WriteLine("InhibitOutgoingTraffic set to " + ByteOf(action.Inhibit.Val));
                }
                break;

            case DoseTest.ActionEnum.Enumeration.Print:
                if (m_isActive)
                {
                    Logger.Instance.WriteLine(action.PrintString.Val);
                }
                break;

            case DoseTest.ActionEnum.Enumeration.ResetCallbackActions:
                foreach (KeyValuePair <Safir.Dob.CallbackId.Enumeration, List <DoseTest.Action> > cbActions
                         in m_callbackActions)
                {
                    cbActions.Value.Clear();
                }
                break;

            case DoseTest.ActionEnum.Enumeration.Sleep:
            {
                if (m_isActive)
                {
                    System.Console.WriteLine("Sleeping " + action.SleepDuration.Val + " seconds");
                    System.Threading.Thread.Sleep((int)(action.SleepDuration.Val * 1000.0));
                }
            }
            break;

            default:
                Logger.Instance.WriteLine("Got unexpected action " + action.ActionKind.Val);
                break;
            }
        }
コード例 #2
0
        void HandleSequencerState(DoseTest.Sequencer sequencerState)
        {
            bool activate = sequencerState != null && sequencerState.Partners[m_instance].Val.Equals(m_identifier);

            //bool activate = true;

            if (activate == m_isActive)
            {
                //already active or not active
                return;
            }

            if (activate)
            {
                m_defaultContext = sequencerState.Context.Val;
                System.Console.WriteLine("Activating (default context is " + m_defaultContext + ")");


                m_controlConnection.RegisterEntityHandler(m_partnerEntityId.TypeId,
                                                          new Safir.Dob.Typesystem.HandlerId(m_instance),
                                                          Safir.Dob.InstanceIdPolicy.Enumeration.HandlerDecidesInstanceId,
                                                          this);
                m_controlConnection.RegisterServiceHandler(DoseTest.Dump.ClassTypeId,
                                                           new Safir.Dob.Typesystem.HandlerId(m_instance),
                                                           this);

                m_actionReceiver.Open();

                DoseTest.Partner partner = new DoseTest.Partner();
                partner.Identifier.Val = m_identifier;

                partner.Port.Val = m_actionReceiver.getPort();

                try
                {
                    Safir.Dob.Typesystem.InstanceId instance = new Safir.Dob.Typesystem.InstanceId(Safir.Dob.ThisNodeParameters.NodeNumber);
                    Safir.Dob.Typesystem.EntityId   eid      = new Safir.Dob.Typesystem.EntityId(Safir.Dob.NodeInfo.ClassTypeId, instance);
                    using (Safir.Dob.EntityProxy ep = m_controlConnection.Read(eid))
                    {
                        partner.Address.Val = ((Safir.Dob.NodeInfo)ep.Entity).IpAddress.Val;
                    }
                }
                catch (Safir.Dob.NotFoundException e)
                {
                    System.Console.WriteLine("Not found exception: " + e);
                }

                m_controlConnection.SetAll(partner, m_partnerEntityId.InstanceId,
                                           new Safir.Dob.Typesystem.HandlerId(m_instance));

                m_isActive = true;
            }
            else
            {
                System.Console.WriteLine("Deactivating");
                m_actionReceiver.Close();

                m_testConnection.Close();

                m_controlConnection.Delete(m_partnerEntityId, new Safir.Dob.Typesystem.HandlerId(m_instance));
                m_controlConnection.UnregisterHandler(m_partnerEntityId.TypeId, new Safir.Dob.Typesystem.HandlerId(m_instance));

                m_controlConnection.UnregisterHandler(DoseTest.Dump.ClassTypeId, new Safir.Dob.Typesystem.HandlerId(m_instance));
                m_isActive = false;
            }
        }