Beispiel #1
0
 protected override void PreExecution()
 {
     _plugin.PreExecution();
 }
Beispiel #2
0
        /// <summary>
        /// Called by the execution engine threads to execute the internal plugin
        /// </summary>
        /// <param name="o"></param>
        internal void Execute(Object o)
        {
            var executionEngine = (ExecutionEngine)o;

            try
            {
                Stop = false;

                plugin.PreExecution();
                bool firstrun = true;

                while (true)
                {
                    resetEvent.WaitOne(10);
                    resetEvent.Reset();

                    //Check if we want to stop
                    if (Stop)
                    {
                        break;
                    }

                    // ################
                    // 0. If this is our first run and we are startable we start
                    // ################

                    //we are startable if we have NO input connectors
                    //Startable flag is deprecated now
                    if (firstrun && (InputConnectors.Count == 0 || HasOnlyOptionalUnconnectedInputs()))
                    {
                        firstrun = false;
                        try
                        {
                            PercentageFinished = 0;
                            GuiNeedsUpdate     = true;

                            Plugin.Execute();
                            executionEngine.ExecutionCounter++;

                            PercentageFinished = 1;
                            GuiNeedsUpdate     = true;
                        }
                        catch (Exception ex)
                        {
                            executionEngine.GuiLogMessage(
                                String.Format(Resources.PluginModel_Execute_An_error_occured_while_executing___0______1_, Name, ex.Message),
                                NotificationLevel.Error);
                            State          = PluginModelState.Error;
                            GuiNeedsUpdate = true;
                        }
                        continue;
                    }

                    var breakit       = false;
                    var atLeastOneNew = false;

                    // ################
                    // 1. Check if we may execute
                    // ################

                    //Check if all necessary inputs are set
                    foreach (ConnectorModel connectorModel in InputConnectors)
                    {
                        if (!connectorModel.IControl &&
                            (connectorModel.IsMandatory || connectorModel.InputConnections.Count > 0))
                        {
                            if (connectorModel.DataQueue.Count == 0 && connectorModel.LastData == null)
                            {
                                breakit = true;
                                continue;
                            }
                            if (connectorModel.DataQueue.Count > 0)
                            {
                                atLeastOneNew = true;
                            }
                        }
                    }

                    //Check if all outputs are free
                    foreach (ConnectorModel connectorModel in OutputConnectors)
                    {
                        if (!connectorModel.IControl)
                        {
                            foreach (ConnectionModel connectionModel in connectorModel.OutputConnections)
                            {
                                if (connectionModel.To.DataQueue.Count > 0)
                                {
                                    breakit = true;
                                }
                            }
                        }
                    }

                    //Gate is a special case: here we need all new data
                    if (PluginType.FullName.Equals("Gate.Gate"))
                    {
                        foreach (ConnectorModel connectorModel in InputConnectors)
                        {
                            if (connectorModel.InputConnections.Count > 0 && connectorModel.DataQueue.Count == 0)
                            {
                                breakit = true;
                            }
                        }
                    }

                    if (breakit || !atLeastOneNew)
                    {
                        continue;
                    }

                    // ################
                    //2. Fill all Inputs of the plugin, if this fails break the loop run
                    // ################
                    foreach (ConnectorModel connectorModel in InputConnectors)
                    {
                        try
                        {
                            if ((connectorModel.DataQueue.Count == 0 && connectorModel.LastData == null) || connectorModel.InputConnections.Count == 0)
                            {
                                continue;
                            }

                            object data;

                            if (connectorModel.DataQueue.Count > 0)
                            {
                                data = connectorModel.DataQueue.Dequeue();
                            }
                            else
                            {
                                continue;
                            }

                            if (data == null)
                            {
                                continue;
                            }

                            //Implicit conversions:

                            //Cast from BigInteger -> Integer
                            if ((connectorModel.ConnectorType.FullName == "System.Int32" ||
                                 connectorModel.ConnectorType.FullName == "System.Int64") &&
                                data.GetType().FullName == "System.Numerics.BigInteger")
                            {
                                try
                                {
                                    data = (int)((BigInteger)data);
                                }
                                catch (OverflowException)
                                {
                                    State = PluginModelState.Error;
                                    WorkspaceModel.ExecutionEngine.GuiLogMessage(String.Format(Resources.PluginModel_Execute_Number_of__0__too_big_for__1____2_, connectorModel.Name, Name, data), NotificationLevel.Error);
                                }
                            }
                            //Cast from Integer -> BigInteger
                            else if (connectorModel.ConnectorType.FullName == "System.Numerics.BigInteger" &&
                                     (data.GetType().FullName == "System.Int32" || data.GetType().FullName == "System.Int64"))
                            {
                                data = new BigInteger((int)data);
                            }
                            //Cast from System.Byte[] -> System.String (UTF8)
                            else if (connectorModel.ConnectorType.FullName == "System.String" && data.GetType().FullName == "System.Byte[]")
                            {
                                var encoding = new UTF8Encoding();
                                data = encoding.GetString((byte[])data);
                            }
                            //Cast from System.String (UTF8) -> System.Byte[]
                            else if (connectorModel.ConnectorType.FullName == "System.Byte[]" && data.GetType().FullName == "System.String")
                            {
                                var encoding = new UTF8Encoding();
                                data = encoding.GetBytes((string)data);
                            }
                            //Cast from System.String (UTF8) -> ICryptoolStream
                            else if (connectorModel.ConnectorType.FullName == "Cryptool.PluginBase.IO.ICryptoolStream" && data.GetType().FullName == "System.String")
                            {
                                var writer = new CStreamWriter();
                                var str    = (string)data;
                                if (str.Length > MaxStrStreamConversionLength)
                                {
                                    str = str.Substring(0, MaxStrStreamConversionLength);
                                }
                                var encoding = new UTF8Encoding();
                                writer.Write(encoding.GetBytes(str));
                                writer.Close();
                                data = writer;
                            }
                            //Cast from ICryptoolStream -> System.String (UTF8)
                            else if (connectorModel.ConnectorType.FullName == "System.String" && data.GetType().FullName == "Cryptool.PluginBase.IO.CStreamWriter")
                            {
                                var writer = (CStreamWriter)data;
                                using (var reader = writer.CreateReader())
                                {
                                    var buffer     = new byte[MaxStrStreamConversionLength];
                                    var readamount = reader.Read(buffer, 0, MaxStrStreamConversionLength);
                                    if (readamount > 0)
                                    {
                                        var encoding = new UTF8Encoding();
                                        var str      = encoding.GetString(buffer, 0, readamount);
                                        data = str;
                                    }
                                    else
                                    {
                                        data = String.Empty;
                                    }
                                }
                            }
                            //Cast to System.String
                            else if (connectorModel.ConnectorType.FullName == "System.String")
                            {
                                //Cast from System.Boolean -> System.String
                                if (data.GetType().FullName == "System.Boolean")
                                {
                                    data = ((Boolean)data).ToString();
                                }
                                //Cast from System.Int32 -> System.String
                                else if (data.GetType().FullName == "System.Int32")
                                {
                                    data = ((Int32)data).ToString();
                                }
                                //Cast from System.Int64 -> System.String
                                else if (data.GetType().FullName == "System.Int64")
                                {
                                    data = ((Int64)data).ToString();
                                }
                                //Cast from System.Numerics.BigInteger -> System.String
                                else if (data.GetType().FullName == "System.Numerics.BigInteger")
                                {
                                    data = ((BigInteger)data).ToString();
                                }
                            }

                            //now set the data
                            if (connectorModel.property == null)
                            {
                                connectorModel.property =
                                    Plugin.GetType().GetProperty(connectorModel.PropertyName);
                            }
                            connectorModel.property.SetValue(Plugin, data, null);
                        }
                        catch (Exception ex)
                        {
                            executionEngine.GuiLogMessage(
                                String.Format(Resources.PluginModel_Execute_An_error_occured_while_setting_value_of_connector___0___of___1_____2_, connectorModel.Name, Name, ex.Message), NotificationLevel.Error);
                            State          = PluginModelState.Error;
                            GuiNeedsUpdate = true;
                        }
                    }

                    // ################
                    //3. Execute
                    // ################
                    try
                    {
                        if (executionEngine.SleepTime > 0)
                        {
                            Thread.Sleep(executionEngine.SleepTime);
                        }

                        PercentageFinished = 0;
                        GuiNeedsUpdate     = true;

                        Plugin.Execute();
                        executionEngine.ExecutionCounter++;

                        if (plugin.GetAutoAssumeFullEndProgressAttribute())
                        {
                            PercentageFinished = 1;
                            GuiNeedsUpdate     = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        executionEngine.GuiLogMessage(
                            String.Format(Resources.PluginModel_Execute_An_error_occured_while_executing____0______1__, Name, ex.Message), NotificationLevel.Error);
                        State          = PluginModelState.Error;
                        GuiNeedsUpdate = true;
                    }

                    // ################
                    // 4. Set all connectorModels belonging to this pluginModel to inactive
                    // ################
                    foreach (ConnectorModel connectorModel in InputConnectors)
                    {
                        foreach (var connectionModel in connectorModel.InputConnections)
                        {
                            connectionModel.Active         = false;
                            connectionModel.GuiNeedsUpdate = true;
                        }
                    }

                    // ################
                    // 5. let all plugins before this check if it may execute
                    // ################
                    foreach (ConnectorModel connectorModel in InputConnectors)
                    {
                        foreach (ConnectionModel connectionModel in connectorModel.InputConnections)
                        {
                            connectionModel.From.PluginModel.resetEvent.Set();
                        }
                    }
                }
                plugin.PostExecution();
            }
            catch (Exception ex)
            {
                executionEngine.GuiLogMessage(
                    String.Format(Resources.PluginModel_Execute_An_error_occured_while_executing___0______1_, Name, ex.Message),
                    NotificationLevel.Error);
                State = PluginModelState.Error;
            }
        }