Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var nodeConfig = NodeConfiguration.GetDefault();

            using (var core = new CrossStitchCore(nodeConfig))
            {
                core.MessageBus.Subscribe <NodeAddedToClusterEvent>(s => s.WithChannelName(NodeAddedToClusterEvent.EventName).Invoke(NodeAdded));
                core.MessageBus.Subscribe <NodeRemovedFromClusterEvent>(s => s.WithChannelName(NodeRemovedFromClusterEvent.EventName).Invoke(NodeRemoved));
                core.MessageBus.Subscribe <ObjectReceivedEvent <NodeStatus> >(b => b
                                                                              .WithChannelName(ReceivedEvent.ReceivedEventName(NodeStatus.BroadcastEvent))
                                                                              .Invoke(ReceiveNodeStatus));
                core.MessageBus.TimerSubscribe(1, b => b.Invoke(m => SendPing(core)));

                core.AddModule(new BackplaneModule(core));
                core.AddModule(new LoggingModule(core, Common.Logging.LogManager.GetLogger("CrossStitch")));

                core.Start();
                core.Log.LogInformation("Started MASTER node {0}", core.NodeId);

                Console.ReadKey();

                core.Log.LogInformation("Stopping node {0}", core.NodeId);
                core.Stop();
            }
        }
Ejemplo n.º 2
0
        public void Start()
        {
            _data.Initialize();
            _subscriptions = new SubscriptionCollection(_messageBus);
            _cacheThreadId = _messageBus.ThreadPool.StartDedicatedWorker();

            // On startup, publish the node status and get info from the backplane
            _subscriptions.Subscribe <CoreEvent>(b => b
                                                 .WithChannelName(CoreEvent.ChannelInitialized)
                                                 .Invoke(m => GenerateAndPublishNodeStatus()));
            _subscriptions.Subscribe <BackplaneEvent>(b => b
                                                      .WithChannelName(BackplaneEvent.ChannelNetworkIdChanged)
                                                      .Invoke(m => _service.SetNetworkNodeId(m.Data)));
            _subscriptions.Subscribe <BackplaneEvent>(b => b
                                                      .WithChannelName(BackplaneEvent.ChannelSetZones)
                                                      .Invoke(m => _service.SetClusterZones((m.Data ?? string.Empty).Split(','))));

            // Publish the status of the node every 60 seconds
            int timerTickMultiple = (_configuration.StatusBroadcastIntervalMinutes * 60) / Timer.MessageTimerModule.TimerIntervalSeconds;

            _subscriptions.TimerSubscribe(timerTickMultiple, b => b
                                          .Invoke(t => GenerateAndPublishNodeStatus())
                                          .OnWorkerThread());

            // TODO: Publish NodeStatus to cluster when Modules or StitchInstances change

            _subscriptions.Listen <StitchSummaryRequest, List <StitchSummary> >(b => b
                                                                                .OnDefaultChannel()
                                                                                .Invoke(_service.GetStitchSummaries));

            _subscriptions.Subscribe <ClusterMemberEvent>(b => b
                                                          .WithChannelName(ClusterMemberEvent.EnteringEvent)
                                                          .Invoke(SendNodeStatusToNewClusterNode));

            // Save node status from other nodes
            _subscriptions.Subscribe <ObjectReceivedEvent <NodeStatus> >(b => b
                                                                         .WithChannelName(ReceivedEvent.ChannelReceived)
                                                                         .Invoke(m => _service.SaveNodeStatus(m.Object)));

            // Subscribe to events for caching stitch status
            _subscriptions.Subscribe <ObjectReceivedEvent <NodeStatus> >(b => b
                                                                         .WithChannelName(ReceivedEvent.ChannelReceived)
                                                                         .Invoke(m => _data.StitchCache.AddNodeStatus(m, m.Object))
                                                                         .OnThread(_cacheThreadId));
            _subscriptions.Subscribe <StitchInstanceEvent>(b => b
                                                           .WithChannelName(StitchInstanceEvent.ChannelCreated)
                                                           .Invoke(_service.HandleLocalStitchCreated)
                                                           .OnThread(_cacheThreadId));
            _subscriptions.Subscribe <ObjectReceivedEvent <StitchInstanceEvent> >(b => b
                                                                                  .WithChannelName(ReceivedEvent.ReceivedEventName(StitchInstanceEvent.ChannelCreated))
                                                                                  .Invoke(m => _service.HandleRemoteStitchCreated(m, m.Object))
                                                                                  .OnThread(_cacheThreadId));
            _subscriptions.Subscribe <StitchInstanceEvent>(b => b
                                                           .WithChannelName(StitchInstanceEvent.ChannelDeleted)
                                                           .Invoke(_service.HandleLocalStitchDeleted)
                                                           .OnThread(_cacheThreadId));
            _subscriptions.Subscribe <ObjectReceivedEvent <StitchInstanceEvent> >(b => b
                                                                                  .WithChannelName(ReceivedEvent.ReceivedEventName(StitchInstanceEvent.ChannelDeleted))
                                                                                  .Invoke(m => _service.HandleRemoteStitchDeleted(m, m.Object))
                                                                                  .OnThread(_cacheThreadId));

            // TODO: On Stitch Started/Stopped we should publish notification to the cluster so other Master nodes can update their
            // caches.

            // Upload package files and distribute to all nodes
            _subscriptions.Listen <PackageFileUploadRequest, PackageFileUploadResponse>(l => l
                                                                                        .OnDefaultChannel()
                                                                                        .Invoke(UploadPackageFile));
            // Create new stitch instances
            _subscriptions.Listen <CreateInstanceRequest, CreateInstanceResponse>(l => l
                                                                                  .OnDefaultChannel()
                                                                                  .Invoke(_service.CreateNewInstances));
            _subscriptions.Subscribe <ObjectReceivedEvent <CreateInstanceRequest> >(b => b
                                                                                    .WithChannelName(ReceivedEvent.ChannelReceived)
                                                                                    .Invoke(m => _service.CreateNewInstanceFromRemote(m, m.Object)));

            // Handle incoming commands
            _subscriptions.Listen <CommandRequest, CommandResponse>(b => b
                                                                    .OnDefaultChannel()
                                                                    .Invoke(_service.DispatchCommandRequest));
            _subscriptions.Subscribe <ObjectReceivedEvent <CommandRequest> >(b => b
                                                                             .WithChannelName(ReceivedEvent.ChannelReceived)
                                                                             .Invoke(ore => _service.ReceiveCommandFromRemote(ore, ore.Object)));

            // Handle incoming command receipt messages
            _subscriptions.Subscribe <ObjectReceivedEvent <CommandReceipt> >(b => b
                                                                             .WithChannelName(ReceivedEvent.ChannelReceived)
                                                                             .Invoke(m => _service.ReceiveReceiptFromRemote(m, m.Object)));

            // Route StitchDataMessage to the correct node
            _subscriptions.Subscribe <StitchDataMessage>(b => b
                                                         .OnDefaultChannel()
                                                         .Invoke(_service.EnrichStitchDataMessageWithAddress));
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var nodeConfig = NodeConfiguration.GetDefault();

            using (var core = new CrossStitchCore(nodeConfig))
            {
                core.MessageBus.Subscribe <NodeAddedToClusterEvent>(s => s.WithTopic(NodeAddedToClusterEvent.EventName).Invoke(NodeAdded));
                core.MessageBus.Subscribe <NodeRemovedFromClusterEvent>(s => s.WithTopic(NodeRemovedFromClusterEvent.EventName).Invoke(NodeRemoved));
                core.MessageBus.Subscribe <ObjectReceivedEvent <NodeStatus> >(b => b
                                                                              .WithTopic(ReceivedEvent.ReceivedEventName(NodeStatus.BroadcastEvent))
                                                                              .Invoke(ReceiveNodeStatus));
                core.MessageBus.TimerSubscribe("tick", 1, b => b.Invoke(m => SendPing(core)));

                core.AddModule(new BackplaneModule(core));
                var logger = new LoggerFactory().AddConsole(LogLevel.Debug).CreateLogger <Program>();
                core.AddModule(new LoggingModule(core, logger));

                core.Start();
                core.Log.LogInformation("Started MASTER node {0}", core.NodeId);

                Console.ReadKey();

                core.Log.LogInformation("Stopping node {0}", core.NodeId);
                core.Stop();
            }
        }