Exemplo n.º 1
0
        private void HandleAction(DoseTest.Action action)
        {
            if (!action.Partner.IsNull() && action.Partner.Val != new Safir.Dob.Typesystem.ChannelId(m_instance))
            {
                // Not meant for this partner
                return;
            }

            if (action.Consumer.IsNull())
            {                                       //No consumer set, meant for the executor.
                if (action.ActionCallback.IsNull()) //it is a normal action
                {
                    ExecuteAction(action);
                }
                else if (m_isActive)
                {
                    AddCallbackAction(action);
                }
            }
            else if (m_isActive)
            {
                Consumer theConsumer = m_consumers[action.Consumer.Val];

                if (action.ActionCallback.IsNull()) //it is a normal action
                {
                    theConsumer.ExecuteAction(action);
                }
                else
                {
                    theConsumer.AddCallbackAction(action);
                }
            }
        }
Exemplo n.º 2
0
 public DoseTest.Action getData()
 {
     DoseTest.Action action = null;
     lock (m_actionLock)
     {
         action   = m_action;
         m_action = null;
     }
     return(action);
 }
Exemplo n.º 3
0
        public void ReadCallback(IAsyncResult ar)
        {
            try
            {
                // Read data from the client socket.
                int read = m_socket.EndReceive(ar);
                if (read > 0)
                {   // Data was read from the client socket.
                    //Get pointer to data
                    System.IntPtr blobPointer = Marshal.AllocHGlobal(m_bufferHeader.Length);
                    Marshal.Copy(m_bufferHeader, 0, blobPointer, m_bufferHeader.Length);
                    System.Int32 blobSize = Safir.Dob.Typesystem.BlobOperations.GetSize(blobPointer);

                    //Free memory
                    Marshal.FreeHGlobal(blobPointer);

                    //create buffer for blobdata
                    byte[] blobData = new byte[blobSize];
                    m_bufferHeader.CopyTo(blobData, 0);

                    //Read the remaining data
                    int recievedSize = BLOB_HEADER_SIZE;
                    while (recievedSize < blobSize)
                    {
                        recievedSize += m_socket.Receive(blobData, recievedSize, blobSize - recievedSize, SocketFlags.None);
                    }

                    //Create object action
                    DoseTest.Action action = (DoseTest.Action)Safir.Dob.Typesystem.Serialization.ToObject(blobData);
                    System.Console.WriteLine("Got action " + action.ActionKind.Val);

                    lock (m_actionLock)
                    {
                        m_action = action;
                        m_dataReady.Set();
                    }
                }
                else
                {
                    System.Console.WriteLine("Error in reading header.");
                    Close();
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Error in ReadCallback: " + e);
            }
        }
Exemplo n.º 4
0
        public void Run()
        {
            // Seems that subsequent garbage collections will execute faster after the first one so we start with
            // a GC here.
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();

            m_controlConnection.Open(m_controlConnectionName, m_instanceString, 0, this, m_controlDispatcher);
            m_controlConnection.SubscribeEntity(DoseTest.Sequencer.ClassTypeId, this);

            System.Console.WriteLine(m_identifier + ":" + m_instance + " Started");
            System.Threading.AutoResetEvent[] waitHandles = new System.Threading.AutoResetEvent[]
            {
                m_controlDispatchEvent,
                m_testDispatchEvent,
                m_stopEvent,
                m_dataReceivedEvent
            };

            while (!m_isDone)
            {
                // Wait for initiation to finish
                int which = System.Threading.WaitHandle.WaitAny(waitHandles, TimeSpan.FromMilliseconds(1000), false);
                switch (which)
                {
                case 0:
                    try
                    {
                        m_controlConnection.Dispatch();
                    }
                    catch (Safir.Dob.Typesystem.Exception exc)
                    {
                        Logger.Instance.WriteLine("Caught Exception when Dispatching controlConnection: " +
                                                  exc.GetType().Name);
                        System.Console.WriteLine("Exception info: " + exc);
                    }
                    catch (Safir.Dob.Typesystem.FundamentalException exc)
                    {
                        Logger.Instance.WriteLine("Caught FundamentalException when Dispatching controlConnection: " +
                                                  exc.GetType().Name);
                        System.Console.WriteLine("Exception info: " + exc);
                    }

                    break;

                case 1:
                    if (m_dispatchTestConnection && m_isActive)
                    {
                        try
                        {
                            ExecuteCallbackActions(Safir.Dob.CallbackId.Enumeration.OnDoDispatch);
                            foreach (Consumer consumer in m_consumers)
                            {
                                consumer.ExecuteCallbackActions(Safir.Dob.CallbackId.Enumeration.OnDoDispatch);
                            }

                            m_testConnection.Dispatch();
                        }
                        catch (Safir.Dob.Typesystem.Exception exc)
                        {
                            Logger.Instance.WriteLine("Caught Exception when Dispatching testConnection: " + exc.GetType().Name);
                            System.Console.WriteLine("Exception info: " + exc);
                        }
                        catch (Safir.Dob.Typesystem.FundamentalException exc)
                        {
                            Logger.Instance.WriteLine("Caught FundamentalException when Dispatching testConnection: " + exc.GetType().Name);
                            System.Console.WriteLine("Exception info: " + exc);
                        }
                    }
                    break;

                case 2:
                    Logger.Instance.WriteLine("Got stop order");
                    ExecuteCallbackActions(Safir.Dob.CallbackId.Enumeration.OnStopOrder);
                    m_isDone = true;
                    break;

                case 3:
                    DoseTest.Action action         = m_actionReceiver.getData();
                    bool            actionAfterAck = action.ActionKind.Val == DoseTest.ActionEnum.Enumeration.Sleep;

                    if (!actionAfterAck)
                    {
                        HandleAction(action);
                    }

                    m_actionReceiver.ActionHandled();

                    if (actionAfterAck)
                    {
                        HandleAction(action);
                    }
                    break;
                }
            }

            //we apparently have to close the connection to not leave a dispatch thread running in the bg.
            m_testConnection.Close();
            m_controlConnection.Close();
        }
Exemplo n.º 5
0
 void AddCallbackAction(DoseTest.Action action)
 {
     m_callbackActions[action.ActionCallback.Val].Add(action);
 }
Exemplo n.º 6
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;
            }
        }