Пример #1
0
        static void Main(string[] args)
        {
            log4net.Config.BasicConfigurator.Configure();
            Tracer.Initialize(new TracerManager());

            AppDomain.CurrentDomain.UnhandledException += (s, e) => tracer.Error(e.ExceptionObject);
            TaskScheduler.UnobservedTaskException += (s, e) => tracer.Error(e.Exception);

            try
            {
                short port = 1055;
                if (args.Length > 0)
                    port = short.Parse(args[0]);

                ServiceRegistration.Start(port);

                var devices = new DeviceRegistry();
                var topics = new Dictionary<string, TopicType>();
                var stream = new EventStream();
                var state = new SystemState();
                var impulseStore = new FileImpulseStore("Store\\Impulses");
                var commandStore = new FileCommandStore("Store\\Commands");

                SetupTracing(stream);

                // Hook up event stream consumers that perform orthogonal operations.
                new ClockImpulses(Clock.Default).Connect(stream);
                new CommandToBytes().Connect(stream);
                new SensedToImpulse(Sensorium.Clock.Default, topics).Connect(stream);
                new SetSystemState(state).Connect(stream);
                // Hook up stores
                new StoreCommands(commandStore).Connect(stream);
                new StoreImpulses(impulseStore).Connect(stream);
                
                var brain = new Brain(stream, devices, topics, state, Clock.Default);

                if (File.Exists("Server.cfg"))
                {
                    var setup = Setup.Read("Server.cfg", File.ReadAllText("Server.cfg"));
                    Console.WriteLine("Applying configuration file:");
                    Console.WriteLine(setup.ToString(true));
                    foreach (var topic in setup.Topics)
                    {
                        topics[topic.Key] = topic.Value;
                    }
                    foreach (var device in setup.DeviceTypes)
                    {
                        devices.Register(device.Type, device.Commands.ToArray());
                    }
                    foreach (var behavior in setup.Behaviors)
                    {
                        brain.Behave(behavior);
                    }
                }

                var server = new ReactiveListener(port);
                server.Connections.Subscribe(socket =>
                {
                    Console.WriteLine("New socket connected {0}", socket.GetHashCode());

                    var binary = new BinaryChannel(socket);
                    var message = new MessageChannel(binary);
                    var device = new TcpDevice(brain, message, Clock.Default);

                    connectedDevices.Add(device);
                    device.Disconnected += (sender, e) => socket.Dispose();
                    socket.Disconnected += (sender, e) =>
                        {
                            Console.WriteLine("Socket disconnected {0}", sender.GetHashCode());
                            connectedDevices.Remove(device);
                            device.Dispose();
                        };
                    socket.Disposed += (sender, e) =>
                        {
                            Console.WriteLine("Socket disposed {0}", sender.GetHashCode());
                            connectedDevices.Remove(device);
                            device.Dispose();
                        };
                });

                server.Start();

                Console.WriteLine("Define topic:");
                Console.WriteLine("  topic [void|bool|number|string] [name]");
                Console.WriteLine("Define device:");
                Console.WriteLine("  device [type] [comma-separated list of topic commands the device can receive]");
                Console.WriteLine("Define behavior:");
                Console.WriteLine("  behave [when then expression]");

                Console.WriteLine("Press Enter to exit");

                string line = null;
                while ((line = Console.ReadLine()) != "")
                {
                    if (line == Environment.NewLine)
                        return;

                    if (line.StartsWith("topic"))
                    {
                        var topic = TopicParser.Parse(line);
                        topics[topic.Item1] = topic.Item2;
                        Console.WriteLine("Registered topic '{0}' of type {1}", topic.Item1, topic.Item2);
                    }
                    else if (line.StartsWith("device"))
                    {
                        var device = DeviceParser.Parse(line);
                        devices.Register(device.Item1, device.Item2.ToArray());
                        Console.WriteLine("Registered device type '{0}' to receive commands {1}", device.Item1, string.Join(", ", device.Item2.Select(s => "'" + s + "'")));
                    }
                    else if (line.StartsWith("behave "))
                    {
                        try
                        {
                            brain.Behave(line.Substring(7));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed: {0}", e);
            }
        }
Пример #2
0
        private void Run(Setup setup)
        {
            var stream = new EventStream();
            stream.Of<IEventPattern<IDevice, IImpulse>>().Subscribe(x => Tracer.Get<BrainExercise>().Info("Impulse from {0}: {1}", x.Sender.Id, x.EventArgs));
            stream.Of<ICommand<float>>().Subscribe(x => Tracer.Get<BrainExercise>().Info("Command: {0}", x));
            stream.Of<ICommand<bool>>().Subscribe(x => Tracer.Get<BrainExercise>().Info("Command: {0}", x));
            stream.Of<ICommand<string>>().Subscribe(x => Tracer.Get<BrainExercise>().Info("Command: {0}", x));
            stream.Of<ICommand<Unit>>().Subscribe(x => Tracer.Get<BrainExercise>().Info("Command: {0}", x));

            var devices = Mock.Of<IDeviceRegistry>();
            var topics = setup.Topics;
            var state = new SystemState();

            // Hook up event stream consumers that perform orthogonal operations.
            new ClockImpulses(Sensorium.Clock.Default).Connect(stream);
            new CommandToBytes().Connect(stream);
            new SensedToImpulse(Sensorium.Clock.Default, topics).Connect(stream);
            new SetSystemState(state).Connect(stream);

            var brain = new Brain(stream, devices, topics, state, Sensorium.Clock.Default);
            var evaluator = CodeEvaluator.Create();
            var anyDeviceType = Guid.NewGuid().ToString();
            var anyDevice = Mock.Of<IDevice>(d => d.Id == Guid.NewGuid().ToString() && d.Type == anyDeviceType);

            // Arrange behaviors
            foreach (var when in setup.Statements)
            {
                brain.Behave(when);
            }

            var tab = "    ";
            // Arrange verifications
            var verifications = SetupVerifications(setup, stream, evaluator, tab);

            // Act by issuing messages.
            SendGivenMessages(setup, stream, anyDeviceType, anyDevice);

            // Assert verifications succeeded.
            verifications.ForEach(v => Assert.True(v.Succeeded, "Verification failed for: " + v.Expression));
        }