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.ipc.agents;
  
  import io.aeron.Aeron;
  import io.aeron.Publication;
  import io.aeron.Subscription;
  import io.aeron.driver.MediaDriver;
  import io.aeron.driver.ThreadingMode;
  import org.agrona.CloseHelper;
  import org.agrona.concurrent.AgentRunner;
  import org.agrona.concurrent.BusySpinIdleStrategy;
  import org.agrona.concurrent.IdleStrategy;
  import org.agrona.concurrent.ShutdownSignalBarrier;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  public class StartHere
  {
      private static final Logger LOGGER = LoggerFactory.getLogger(StartHere.class);
  
      public static void main(final String[] args)
      {
          final String channel = "aeron:ipc";
          final int stream = 10;
          final int sendCount = 10_000_000;
          final IdleStrategy idleStrategySend = new BusySpinIdleStrategy();
          final IdleStrategy idleStrategyReceive = new BusySpinIdleStrategy();
          final ShutdownSignalBarrier barrier = new ShutdownSignalBarrier();
  
          //construct Media Driver, cleaning up media driver folder on start/stop
          final MediaDriver.Context mediaDriverCtx = new MediaDriver.Context()
              .dirDeleteOnStart(true)
              .threadingMode(ThreadingMode.SHARED)
              .sharedIdleStrategy(new BusySpinIdleStrategy())
              .dirDeleteOnShutdown(true);
          final MediaDriver mediaDriver = MediaDriver.launchEmbedded(mediaDriverCtx);
  
          //construct Aeron, pointing at the media driver's folder
          final Aeron.Context aeronCtx = new Aeron.Context()
              .aeronDirectoryName(mediaDriver.aeronDirectoryName());
          final Aeron aeron = Aeron.connect(aeronCtx);
  
          LOGGER.info("Dir {}", mediaDriver.aeronDirectoryName());
  
          //construct the subs and pubs
          final Subscription subscription = aeron.addSubscription(channel, stream);
          final Publication publication = aeron.addPublication(channel, stream);
  
          //construct the agents
          final SendAgent sendAgent = new SendAgent(publication, sendCount);
          final ReceiveAgent receiveAgent = new ReceiveAgent(subscription, barrier, sendCount);
  
          //construct agent runners
          final AgentRunner sendAgentRunner = new AgentRunner(idleStrategySend,
              Throwable::printStackTrace, null, sendAgent);
          final AgentRunner receiveAgentRunner = new AgentRunner(idleStrategyReceive,
              Throwable::printStackTrace, null, receiveAgent);
          LOGGER.info("starting");
          //start the runners
          AgentRunner.startOnThread(sendAgentRunner);
          AgentRunner.startOnThread(receiveAgentRunner);
  
          //wait for the final item to be received before closing
          barrier.await();
  
          //close the resources
          CloseHelper.closeAll(receiveAgentRunner, sendAgentRunner, aeron, mediaDriver, barrier);
      }
  }
  