Class ChatServer
- java.lang.Object
-
- naga.examples.ChatServer
-
- All Implemented Interfaces:
ServerSocketObserver
public class ChatServer extends java.lang.Object implements ServerSocketObserver
Creates a very simple chat server.Run using
java naga.examples.ChatServer [port]
- Author:
- Christoffer Lerno
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
acceptFailed(java.io.IOException exception)
Called by the NIOService on the NIO thread when an accept fails on the socket.void
broadcast(naga.examples.ChatServer.User sender, java.lang.String string)
EventMachine
getEventMachine()
static void
main(java.lang.String... args)
Runs the echo server.void
newConnection(NIOSocket nioSocket)
Called by the NIOService on the NIO thread when a new connection has been accepted by the socket.void
serverSocketDied(java.lang.Exception exception)
Called by the NIOService on the NIO thread when the server socket is closed.
-
-
-
Method Detail
-
acceptFailed
public void acceptFailed(java.io.IOException exception)
Description copied from interface:ServerSocketObserver
Called by the NIOService on the NIO thread when an accept fails on the socket.Note: Since this is a direct callback on the NIO thread, this method will suspend IO on all other connections until the method returns. It is therefore strongly recommended that the implementation of this method returns as quickly as possible to avoid blocking IO.
- Specified by:
acceptFailed
in interfaceServerSocketObserver
- Parameters:
exception
- the reason for the failure, never null.
-
serverSocketDied
public void serverSocketDied(java.lang.Exception exception)
Description copied from interface:ServerSocketObserver
Called by the NIOService on the NIO thread when the server socket is closed.Note: Since this is a direct callback on the NIO thread, this method will suspend IO on all other connections until the method returns. It is therefore strongly recommended that the implementation of this method returns as quickly as possible to avoid blocking IO.
- Specified by:
serverSocketDied
in interfaceServerSocketObserver
- Parameters:
exception
- the exception that caused the close, or null if this was caused by an explicitclose()
on the NIOServerSocket.
-
newConnection
public void newConnection(NIOSocket nioSocket)
Description copied from interface:ServerSocketObserver
Called by the NIOService on the NIO thread when a new connection has been accepted by the socket.The normal behaviour would be for the observer to assign a reader and a writer to the socket, and then finally invoke
NIOSocket#listen(SocketObserver)
on the socket.Note: Since this is a direct callback on the NIO thread, this method will suspend IO on all other connections until the method returns. It is therefore strongly recommended that the implementation of this method returns as quickly as possible to avoid blocking IO.
- Specified by:
newConnection
in interfaceServerSocketObserver
- Parameters:
nioSocket
- the socket that was accepted.
-
broadcast
public void broadcast(naga.examples.ChatServer.User sender, java.lang.String string)
-
main
public static void main(java.lang.String... args)
Runs the echo server.- Parameters:
args
- command line arguments, assumed to be a 1 length string containing a port.
-
getEventMachine
public EventMachine getEventMachine()
-
-