Class P4JServer

java.lang.Object
lu.kbra.p4j.socket.server.P4JServer
All Implemented Interfaces:
Closeable, AutoCloseable, Runnable, P4JInstance, P4JInstance.P4JServerInstance, EventDispatcher

public class P4JServer extends Object implements P4JInstance.P4JServerInstance, EventDispatcher, Closeable, Runnable
  • Field Details

    • MAX_PACKET_SIZE

      public static int MAX_PACKET_SIZE
  • Constructor Details

    • P4JServer

      public P4JServer(CodecManager codecManager, EncryptionManager encryptionManager, CompressionManager compressionManager)
      Default constructor for a P4JServer, creates a default ClientManager bound to this server instance.
      Parameters:
      codecManager - the server codec manager
      encryptionManager - the server encryption manager
      compressionManager - the server compression manager
  • Method Details

    • bind

      public void bind(InetSocketAddress inetSocketAddress) throws IOException
      Binds the current server to the local address.
      Parameters:
      inetSocketAddress - the local address to bind to
      Throws:
      IOException - if the ServerSocketChannel or Selector cannot be opened or bound
      P4JServerException - if the server is already bound
    • broadcast

      public void broadcast(Function<ServerClient,S2CPacket<?>> packetSupplier)
      Sends the packet provided by the supplier to all the connected clients.
    • broadcast

      public void broadcast(List<S2CPacket<?>> packets)
      Sends the packets to all the connected clients.
    • broadcast

      public void broadcast(S2CPacket<?> packet)
      Sends the packet to all the connected clients.
    • broadcastIf

      public void broadcastIf(Function<ServerClient,S2CPacket<?>> packetSupplier, Predicate<ServerClient> condition)
      Iterates over all the connected clients and sends the packet provided by the supplier, if the predicate's condition is met.
    • broadcastIf

      public void broadcastIf(List<S2CPacket<?>> packets, Predicate<ServerClient> condition)
      Iterates over all the connected clients and sends the specified packet(s) if the predicate's condition is met.
    • broadcastIf

      public void broadcastIf(S2CPacket<?> packet, Predicate<ServerClient> condition)
      Iterates over all the connected clients and sends the specified packet if the predicate's condition is met.
    • close

      public void close()
      Closes the server socket.
      The server will no longer accept new client connections, all clients will be forcefully disconnected and the local port is released.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      P4JServerException - if the server socket is already closed
    • disconnectAll

      public void disconnectAll()
    • dispatchEvent

      public void dispatchEvent(P4JEvent event)
    • getClientManager

      public ClientManager getClientManager()
    • getCodec

      public CodecManager getCodec()
    • getCompression

      public CompressionManager getCompression()
    • getConnectedClients

      public Collection<ServerClient> getConnectedClients()
    • getEncryption

      public EncryptionManager getEncryption()
    • getEndPoint

      public final P4JEndPoint getEndPoint()
      Specified by:
      getEndPoint in interface P4JInstance
      Specified by:
      getEndPoint in interface P4JInstance.P4JServerInstance
    • getEventManager

      public EventManager getEventManager()
    • getLocalInetSocketAddress

      public InetSocketAddress getLocalInetSocketAddress()
    • getPackets

      public PacketManager getPackets()
    • getPort

      public int getPort()
      Returns:
      the local port bound to the server or -1 if the server is closed
    • getServerStatus

      public ServerStatus getServerStatus()
    • getState

      public Thread.State getState()
    • isAlive

      public final boolean isAlive()
    • join

      public final void join() throws InterruptedException
      Throws:
      InterruptedException
    • join

      public final void join(long millis) throws InterruptedException
      Throws:
      InterruptedException
    • join

      public final void join(long millis, int nanos) throws InterruptedException
      Throws:
      InterruptedException
    • registerPacket

      public void registerPacket(Class<?> p, int id)
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • setAccepting

      public void setAccepting()
      Sets the server socket in client accept mode.
      The server will accept all future incoming client connections.
      Throws:
      P4JServerException - if the server socket is closed
    • setClientManager

      public void setClientManager(ClientManager clientManager)
    • setCodec

      public void setCodec(CodecManager codec)
    • setCompression

      public void setCompression(CompressionManager compression)
    • setEncryption

      public void setEncryption(EncryptionManager encryption)
    • setEventManager

      public void setEventManager(EventManager eventManager)
    • setPackets

      public void setPackets(PacketManager packets)
    • setRefusing

      public void setRefusing()
      Sets the server socket in client refuse mode.
      The server will refuse all future incoming client connections but keep current connections alive.
      Throws:
      P4JServerException - if the server socket is closed.
    • toString

      public String toString()
      Overrides:
      toString in class Object