content: /*
   * Copyright 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.rfq.infra;
  
  import com.aeroncookbook.rfq.domain.instrument.Instruments;
  import com.aeroncookbook.rfq.domain.rfq.Rfqs;
  import com.aeroncookbook.rfq.domain.users.Users;
  import io.aeron.ExclusivePublication;
  import io.aeron.Image;
  import io.aeron.cluster.codecs.CloseReason;
  import io.aeron.cluster.service.ClientSession;
  import io.aeron.cluster.service.Cluster;
  import io.aeron.cluster.service.ClusteredService;
  import io.aeron.logbuffer.Header;
  import org.agrona.DirectBuffer;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  /**
   * The clustered service for the auction application.
   */
  public class AppClusteredService implements ClusteredService
  {
      private static final Logger LOGGER = LoggerFactory.getLogger(AppClusteredService.class);
      private final ClientSessions clientSessions = new ClientSessions();
      private final SessionMessageContextImpl context = new SessionMessageContextImpl(clientSessions);
      private final ClusterClientResponder clusterClientResponder = new ClusterClientResponderImpl(context);
      private final TimerManager timerManager = new TimerManager(context);
      private final Instruments instruments = new Instruments(clusterClientResponder);
      private final Users users = new Users();
      private final Rfqs rfqs = new Rfqs(context, instruments, users, clusterClientResponder, timerManager);
      private final SnapshotManager snapshotManager = new SnapshotManager(context);
      private final SbeAdapter sbeAdapter = new SbeAdapter(instruments, rfqs, clusterClientResponder);
  
      @Override
      public void onStart(final Cluster cluster, final Image snapshotImage)
      {
          snapshotManager.setIdleStrategy(cluster.idleStrategy());
          context.setIdleStrategy(cluster.idleStrategy());
          timerManager.setCluster(cluster);
          if (snapshotImage != null)
          {
              LOGGER.info("Snapshotting not supported");
          }
      }
  
      @Override
      public void onSessionOpen(final ClientSession session, final long timestamp)
      {
          LOGGER.info("Client session with id {} opened", session.id());
          context.setClusterTime(timestamp);
          clientSessions.addSession(session, timestamp);
      }
  
      @Override
      public void onSessionClose(final ClientSession session, final long timestamp, final CloseReason closeReason)
      {
          LOGGER.info("Client session with id {} closed", session.id());
          context.setClusterTime(timestamp);
          clientSessions.removeSession(session, timestamp);
      }
  
      @Override
      public void onSessionMessage(
          final ClientSession session,
          final long timestamp,
          final DirectBuffer buffer,
          final int offset,
          final int length,
          final Header header)
      {
          context.setSessionContext(session, timestamp);
          sbeAdapter.dispatch(buffer, offset, length);
      }
  
      @Override
      public void onTimerEvent(final long correlationId, final long timestamp)
      {
          context.setClusterTime(timestamp);
          timerManager.onTimerEvent(correlationId, timestamp);
      }
  
      @Override
      public void onTakeSnapshot(final ExclusivePublication snapshotPublication)
      {
          LOGGER.info("Snapshottting not implemented");
      }
  
      @Override
      public void onRoleChange(final Cluster.Role newRole)
      {
          LOGGER.info("Role change: {}", newRole);
      }
  
      @Override
      public void onTerminate(final Cluster cluster)
      {
          LOGGER.info("Terminating");
      }
  }
  