content: /*
   * Copyright 2019-2023 Adaptive Financial Consulting Ltd.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * https://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.aeroncookbook.cluster.rsm.node;
  
  import io.aeron.cluster.ClusteredMediaDriver;
  import io.aeron.cluster.service.ClusteredServiceContainer;
  import io.aeron.samples.cluster.ClusterConfig;
  import org.agrona.ErrorHandler;
  import org.agrona.concurrent.ShutdownSignalBarrier;
  
  import java.util.List;
  
  public class RsmCluster
  {
      @SuppressWarnings("try")
      public static void main(final String[] args)
      {
          final ClusterConfig clusterConfig = ClusterConfig.create(
              0, List.of("localhost"), List.of("localhost"), 9000, new RsmClusteredService());
  
          clusterConfig.mediaDriverContext().errorHandler(errorHandler("Media Driver"));
          clusterConfig.archiveContext().errorHandler(errorHandler("Archive"));
          clusterConfig.aeronArchiveContext().errorHandler(errorHandler("Aeron Archive"));
          clusterConfig.consensusModuleContext().errorHandler(errorHandler("Consensus Module"));
          clusterConfig.clusteredServiceContext().errorHandler(errorHandler("Clustered Service"));
          clusterConfig.consensusModuleContext().ingressChannel("aeron:udp?endpoint=localhost:9010|term-length=64k");
          clusterConfig.consensusModuleContext().deleteDirOnStart(false); //true to always start fresh
  
          try (ShutdownSignalBarrier barrier = new ShutdownSignalBarrier();
              ClusteredMediaDriver clusteredMediaDriver = ClusteredMediaDriver.launch(
                  clusterConfig.mediaDriverContext().terminationHook(barrier::signalAll),
                  clusterConfig.archiveContext(),
                  clusterConfig.consensusModuleContext().terminationHook(barrier::signalAll));
              ClusteredServiceContainer container = ClusteredServiceContainer.launch(
                  clusterConfig.clusteredServiceContext().terminationHook(barrier::signalAll)))
          {
              System.out.println("Started Cluster Node...");
              System.out.println("Cluster directory is " + clusterConfig.consensusModuleContext().clusterDir());
              barrier.await();
              System.out.println("Exiting");
          }
      }
  
      private static ErrorHandler errorHandler(final String context)
      {
          return (Throwable throwable) ->
          {
              System.err.println(context);
              throwable.printStackTrace(System.err);
          };
      }
  }
  