AFTIPCServerSocket.java

  1. /*
  2.  * junixsocket
  3.  *
  4.  * Copyright 2009-2024 Christian Kohlschütter
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package org.newsclub.net.unix.tipc;

  19. import java.io.FileDescriptor;
  20. import java.io.IOException;
  21. import java.net.ServerSocket;
  22. import java.net.SocketException;

  23. import org.newsclub.net.unix.AFServerSocket;
  24. import org.newsclub.net.unix.AFServerSocketChannel;
  25. import org.newsclub.net.unix.AFSocket;
  26. import org.newsclub.net.unix.AFSocketAddress;
  27. import org.newsclub.net.unix.AFSocketImpl;
  28. import org.newsclub.net.unix.AFTIPCSocketAddress;

  29. /**
  30.  * The server part of an {@code AF_TIPC} socket.
  31.  *
  32.  * @author Christian Kohlschütter
  33.  */
  34. public final class AFTIPCServerSocket extends AFServerSocket<AFTIPCSocketAddress> {
  35.   AFTIPCServerSocket(FileDescriptor fdObj) throws IOException {
  36.     super(fdObj);
  37.   }

  38.   @Override
  39.   protected AFServerSocketChannel<AFTIPCSocketAddress> newChannel() {
  40.     return new AFTIPCServerSocketChannel(this);
  41.   }

  42.   @Override
  43.   public AFTIPCServerSocketChannel getChannel() {
  44.     return (AFTIPCServerSocketChannel) super.getChannel();
  45.   }

  46.   /**
  47.    * Returns a new, unbound AF_TIPC {@link ServerSocket}.
  48.    *
  49.    * @return The new, unbound {@link AFServerSocket}.
  50.    * @throws IOException if the operation fails.
  51.    */
  52.   public static AFTIPCServerSocket newInstance() throws IOException {
  53.     return (AFTIPCServerSocket) AFServerSocket.newInstance(AFTIPCServerSocket::new);
  54.   }

  55.   static AFTIPCServerSocket newInstance(FileDescriptor fdObj, int localPort, int remotePort)
  56.       throws IOException {
  57.     return (AFTIPCServerSocket) AFServerSocket.newInstance(AFTIPCServerSocket::new, fdObj,
  58.         localPort, remotePort);
  59.   }

  60.   /**
  61.    * Returns a new AF_TIPC {@link ServerSocket} that is bound to the given
  62.    * {@link AFTIPCSocketAddress}.
  63.    *
  64.    * @param addr The socket file to bind to.
  65.    * @return The new, bound {@link AFServerSocket}.
  66.    * @throws IOException if the operation fails.
  67.    */
  68.   public static AFTIPCServerSocket bindOn(final AFTIPCSocketAddress addr) throws IOException {
  69.     return (AFTIPCServerSocket) AFServerSocket.bindOn(AFTIPCServerSocket::new, addr);
  70.   }

  71.   /**
  72.    * Returns a new AF_TIPC {@link ServerSocket} that is bound to the given {@link AFSocketAddress}.
  73.    *
  74.    * @param addr The socket file to bind to.
  75.    * @param deleteOnClose If {@code true}, the socket file (if the address points to a file) will be
  76.    *          deleted upon {@link #close}.
  77.    * @return The new, bound {@link AFServerSocket}.
  78.    * @throws IOException if the operation fails.
  79.    */
  80.   public static AFTIPCServerSocket bindOn(final AFTIPCSocketAddress addr, boolean deleteOnClose)
  81.       throws IOException {
  82.     return (AFTIPCServerSocket) AFServerSocket.bindOn(AFTIPCServerSocket::new, addr, deleteOnClose);
  83.   }

  84.   /**
  85.    * Returns a new, <em>unbound</em> AF_TIPC {@link ServerSocket} that will always bind to the given
  86.    * address, regardless of any socket address used in a call to <code>bind</code>.
  87.    *
  88.    * @param forceAddr The address to use.
  89.    * @return The new, yet unbound {@link AFServerSocket}.
  90.    * @throws IOException if an exception occurs.
  91.    */
  92.   public static AFTIPCServerSocket forceBindOn(final AFTIPCSocketAddress forceAddr)
  93.       throws IOException {
  94.     return (AFTIPCServerSocket) AFServerSocket.forceBindOn(AFTIPCServerSocket::new, forceAddr);
  95.   }

  96.   @Override
  97.   protected AFSocketImpl<AFTIPCSocketAddress> newImpl(FileDescriptor fdObj) throws SocketException {
  98.     return new AFTIPCSocketImpl(fdObj);
  99.   }

  100.   @Override
  101.   protected AFSocket<AFTIPCSocketAddress> newSocketInstance() throws IOException {
  102.     return AFTIPCSocket.newInstance();
  103.   }

  104.   @Override
  105.   public AFTIPCSocket accept() throws IOException {
  106.     return (AFTIPCSocket) super.accept();
  107.   }
  108. }