Exemple #1
0
        public void StartServer()
        {
            TcpServerChannel tcpServerChannel = new TcpServerChannel("TcpServerChannel", 9988);

            IntervalActivator activator1 = new IntervalActivator("Activator1", 5000);

            byte[] command1Bytes = new byte[] { 0x01, 0x16 };
            activator1.AddCommand(new ByteArrayCommand("command1", tcpServerChannel, command1Bytes));

            SingleFormatterByteArrayInterpreter interpreter = new SingleFormatterByteArrayInterpreter("Interpreter");

            interpreter.SetHeaders(new byte[] { 0x55, 0xAA });
            interpreter.SetTailers(new byte[] { 0xAA, 0x55 });

            ByteArrayFormatter formatter = new ByteArrayFormatter("Formatter1");

            formatter.AddItem(new ByteArrayStringItem("NodeName", 1, 6, Encoding.ASCII));
            formatter.AddItem(new ByteArrayInt32Item("NodeId", 0));
            formatter.AddItem(new ByteArrayInt16Item("Temperature", 2));
            formatter.AddItem(new ByteArrayDoubleItem("Longitude", 3));
            formatter.AddItem(new ByteArrayByteItem("Reserved", 4));

            interpreter.AddFormatter(formatter);

            ByteArrayAdapter baa = new ByteArrayAdapter("ByteArrayAdapter", tcpServerChannel, interpreter, Program.ShowEnvelop);

            baa.SetupFinished += activator1.StartProcess;

            baa.Setup();
        }
        private IActivator GetActivator(XElement element, object channel)
        {
            IActivator activator = null;

            string activatorType = GetAttribute(element, "Type");
            string activatorName = GetAttribute(element, "Name");

            if (activatorType == "OneTimeActivator")
            {
                activator = new OneTimeActivator(activatorName);
            }
            else if (activatorType == "IntervalActivator")
            {
                if (element.Attribute("ExecuteInterval") != null && !string.IsNullOrEmpty(element.Attribute("ExecuteInterval").Value))
                {
                    activator = new IntervalActivator(activatorName, int.Parse(element.Attribute("ExecuteInterval").Value));
                }
                else
                {
                    activator = new IntervalActivator(activatorName);
                }
            }
            else
            {
                throw new UnknownElementException("Unknown activator type:" + activatorType);
            }

            if (element.Element("Command") == null)
            {
                throw new ElementNotFoundException("Element Command cannot be found");
            }

            foreach (var commandElement in element.Elements("Command"))
            {
                ICommand command = GetCommand(commandElement, channel);
                activator.AddCommand(command);
            }

            _adapterObjects[_currentAdapterName].Add(activator.Name, activator);

            return(activator);
        }