Exemplo n.º 1
0
        /// <summary>
        /// Create the layer
        /// </summary>
        /// <param name="logger">The logger to use when creating</param>
        /// <returns>The network layer</returns>
        public override INetworkLayer CreateLayer(Logger logger)
        {
            if (Script == null)
            {
                throw new InvalidOperationException(String.Format(Properties.Resources.ScriptNetworkLayerFactory_SpecifyScript, Description));
            }

            if (ClassName == null)
            {
                throw new InvalidOperationException(String.Format(Properties.Resources.ScriptNetworkLayerFactory_SpecifyClassName, Description));
            }

            object obj = Script.Container.GetInstance(ClassName);

            INetworkLayer layer = obj as INetworkLayer;

            if (layer == null)
            {
                IDataStreamParser parser = obj as IDataStreamParser;
                if (parser != null)
                {
                    layer = new ParserNetworkLayer(DynamicScriptContainer.Create(Script.Container, ClassName), logger);
                }
            }

            if (layer == null)
            {
                throw new InvalidOperationException(String.Format(Properties.Resources.ScriptNetworkLayerFactory_InvalidType, Script.Name));
            }

            IPersistNode persist = layer as IPersistNode;

            if (persist != null)
            {
                persist.SetState(new DynamicConfigObject(_config), logger);
            }

            return(layer);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected override BasePipelineNode OnCreate(Logger logger, NetGraph graph, Dictionary <string, object> stateDictionary)
        {
            BasePipelineNode node = null;

            if (Container != null)
            {
                try
                {
                    if (String.IsNullOrWhiteSpace(ClassName))
                    {
                        throw new ArgumentException(String.Format(CANAPE.Scripting.Properties.Resources.DynamicNodeFactory_MustSpecifyClassName, Label));
                    }

                    // Determine what type of object we create, if it is an BasePipelineNode then return as is,
                    // if it is a parser then create the parsing pipeline node to contain it
                    object o = ScriptUtils.GetInstance(Container, ClassName);
                    if (o is BasePipelineNode)
                    {
                        node = o as BasePipelineNode;
                        if (State != null)
                        {
                            IPersistNode persist = node as IPersistNode;
                            if (persist != null)
                            {
                                persist.SetState(State, logger);
                            }
                        }
                    }
                    else if (o is IDataStreamParser)
                    {
                        // If we are selecting the whole packet, then we convert a binary stream
                        if (SelectionPath == "/")
                        {
                            DynamicBinaryStreamPipelineNode stmNode = new DynamicBinaryStreamPipelineNode();
                            stmNode.Container = DynamicScriptContainer.Create(Container, ClassName);
                            stmNode.State     = State;

                            node = stmNode;
                        }
                        else
                        {
                            DynamicStreamPipelineNode dynNode = new DynamicStreamPipelineNode();

                            dynNode.Container = DynamicScriptContainer.Create(Container, ClassName);
                            dynNode.State     = State;

                            node = dynNode;
                        }
                    }
                    else if (o is IDataArrayParser)
                    {
                        DynamicArrayPipelineNode dynNode = new DynamicArrayPipelineNode();

                        dynNode.Container = DynamicScriptContainer.Create(Container, ClassName);
                        dynNode.State     = State;

                        node = dynNode;
                    }
                    else if (o is IDataStringParser)
                    {
                        DynamicStringPipelineNode dynNode = new DynamicStringPipelineNode();

                        dynNode.Container = DynamicScriptContainer.Create(Container, ClassName);
                        dynNode.State     = State;

                        node = dynNode;
                    }
                    else if (o == null)
                    {
                        throw new InvalidOperationException(String.Format(CANAPE.Scripting.Properties.Resources.DynamicNodeFactory_CannotCreateType, ClassName, Label));
                    }
                    else
                    {
                        throw new InvalidOperationException(String.Format(CANAPE.Scripting.Properties.Resources.DynamicNodeFactory_InvalidNodeType, o.GetType().Name, Label));
                    }

                    node.Enabled = Enabled;
                }
                catch (EndOfStreamException)
                {
                    // End of stream
                }
                catch (ThreadAbortException)
                {
                }
            }
            else
            {
                throw new ArgumentException(String.Format(CANAPE.Scripting.Properties.Resources.DynamicNodeFactory_NoScriptSpecified, Label));
            }

            return(node);
        }