001package jmri.jmrix.ipocs;
002
003import java.nio.channels.AsynchronousServerSocketChannel;
004import java.nio.channels.AsynchronousSocketChannel;
005import java.nio.channels.CompletionHandler;
006
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 *
012 * @author Fredrik Elestedt Copyright (C) 2020
013 * @since 4.21.2
014 */
015public class IpocsSocketAcceptor implements CompletionHandler<AsynchronousSocketChannel, Object> {
016  private final static Logger log = LoggerFactory.getLogger(IpocsSocketAcceptor.class);
017  private final AsynchronousServerSocketChannel serverSocket;
018  private final IpocsPortController portController;
019
020  public IpocsSocketAcceptor(IpocsPortController portController, AsynchronousServerSocketChannel serverSocket) {
021    this.serverSocket = serverSocket;
022    this.portController = portController;
023  }
024
025  @Override
026  public void completed(AsynchronousSocketChannel result, Object attachment) {
027    serverSocket.accept(null, this);
028    log.info("Client connected");
029    IpocsClientHandler c = new IpocsClientHandler(result);
030    c.addClientListener(portController);
031  }
032
033  @Override
034  public void failed(Throwable exc, Object attachment) {
035    log.error("Unable to accept socket");
036  }
037
038}