Advertisement
Chiddix

ChatServer

May 25th, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package me.rabrg.chat.net;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetSocketAddress;
  5. import java.nio.ByteBuffer;
  6. import java.nio.channels.SelectionKey;
  7. import java.nio.channels.Selector;
  8. import java.nio.channels.ServerSocketChannel;
  9. import java.nio.channels.SocketChannel;
  10. import java.nio.charset.StandardCharsets;
  11.  
  12. public final class ChatServer implements Runnable {
  13.  
  14.     private final InetSocketAddress inetSocketAddress;
  15.  
  16.     private final ServerSocketChannel serverSocketChannel;
  17.  
  18.     private final Selector selector;
  19.  
  20.     private volatile boolean running;
  21.  
  22.     protected ChatServer(final InetSocketAddress inetSocketAddress) throws IOException {
  23.         this.inetSocketAddress = inetSocketAddress;
  24.        
  25.         selector = Selector.open();
  26.        
  27.         serverSocketChannel = ServerSocketChannel.open();
  28.         serverSocketChannel.bind(inetSocketAddress);
  29.         serverSocketChannel.configureBlocking(false);
  30.         serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
  31.     }
  32.  
  33.     public void start() {
  34.         running = true;
  35.         new Thread(this).start();
  36.     }
  37.  
  38.     @Override
  39.     public void run() {
  40.         try {
  41.             while(running) {
  42.                 selector.selectNow();
  43.                 for (final SelectionKey selectionKey : selector.selectedKeys()) {
  44.                     if (selectionKey.isValid()) {
  45.                         if (selectionKey.isAcceptable()) {
  46.                             final SocketChannel socketChannel = serverSocketChannel.accept();
  47.                             socketChannel.configureBlocking(false);
  48.                             socketChannel.register(selector, SelectionKey.OP_READ);
  49.                             sendMessage(selectionKey, "Welcome to the server."); // TODO: message constant
  50.                         }
  51.                         if (selectionKey.isReadable()) {
  52.                             final ByteBuffer messageByteBuffer = ByteBuffer.allocate(512);
  53.                             ((SocketChannel) selectionKey.channel()).read(messageByteBuffer);
  54.                             final String message = new String(messageByteBuffer.array(), StandardCharsets.UTF_8);
  55.                             broadcastMessage(message);
  56.                         }
  57.                     }
  58.                 }
  59.             }
  60.         } catch (final IOException e) {
  61.             e.printStackTrace();
  62.         }
  63.     }
  64.  
  65.     public void broadcastMessage(final String message) throws IOException {
  66.         final ByteBuffer messageByteBuffer = ByteBuffer.wrap(message.getBytes());
  67.         for (final SelectionKey selectionKey : selector.keys()) {
  68.             if (selectionKey.isValid()) {
  69.                 sendMessage(selectionKey, (ByteBuffer) messageByteBuffer.rewind());
  70.             }
  71.         }
  72.     }
  73.  
  74.     public void sendMessage(final SelectionKey selectionKey, final String message) throws IOException {
  75.         sendMessage(selectionKey, ByteBuffer.wrap(message.getBytes()));
  76.     }
  77.  
  78.     public void sendMessage(final SelectionKey selectionKey, final ByteBuffer message) throws IOException {
  79.         System.out.println(message);
  80.         ((SocketChannel) selectionKey.channel()).write(message);
  81.     }
  82.  
  83.     public void stop() {
  84.         running = false;
  85.     }
  86.  
  87.     public InetSocketAddress getInetSocketAddress() {
  88.         return inetSocketAddress;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement