Advertisement
Guest User

Untitled

a guest
Jun 5th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. /**
  2.  * @author Dalton (Palidino)
  3.  */
  4. package com.palidino.nio;
  5.  
  6. import java.io.IOException;
  7. import java.nio.ByteBuffer;
  8. import java.nio.channels.SelectionKey;
  9. import java.nio.channels.SocketChannel;
  10. import java.util.Queue;
  11. import java.util.concurrent.ConcurrentLinkedQueue;
  12.  
  13. public class Session {
  14.     private SocketChannel socketChannel;
  15.     private SelectionKey selectionKey;
  16.     private String remoteAddress;
  17.     private long lastRead;
  18.     private Queue<WriteEvent> writeEvents = new ConcurrentLinkedQueue<>();
  19.     private Object attachment;
  20.  
  21.     public Session(SocketChannel socketChannel, String remoteAddress, SelectionKey selectionKey) {
  22.         this.socketChannel = socketChannel;
  23.         this.remoteAddress = remoteAddress;
  24.         this.selectionKey = selectionKey;
  25.         selectionKey.attach(this);
  26.         updateLastRead();
  27.     }
  28.  
  29.     public void write(byte[] bytes) {
  30.         write(bytes, 0, bytes.length, null);
  31.     }
  32.  
  33.     public void write(byte[] bytes, WriteEventHandler handler) {
  34.         write(bytes, 0, bytes.length, handler);
  35.     }
  36.  
  37.     public void write(byte[] bytes, int offset, int length) {
  38.         write(bytes, 0, bytes.length, null);
  39.     }
  40.  
  41.     public void write(byte[] bytes, int offset, int length, WriteEventHandler handler) {
  42.         addWriteEvent(new WriteEvent(bytes, offset, length, handler));
  43.     }
  44.  
  45.     public void write(ByteBuffer buffer) {
  46.         write(buffer, null);
  47.     }
  48.  
  49.     public void write(ByteBuffer buffer, WriteEventHandler handler) {
  50.         addWriteEvent(new WriteEvent(buffer, handler));
  51.     }
  52.  
  53.     private void addWriteEvent(WriteEvent writeEvent) {
  54.         writeEvents.offer(writeEvent);
  55.         if (selectionKey.isValid()) {
  56.             selectionKey.selector().wakeup();
  57.         }
  58.     }
  59.  
  60.     public void close() {
  61.         try {
  62.             socketChannel.close();
  63.         } catch (IOException ioe) {
  64.         }
  65.     }
  66.  
  67.     public boolean isOpen() {
  68.         return socketChannel.isOpen();
  69.     }
  70.  
  71.     public SocketChannel getSocketChannel() {
  72.         return socketChannel;
  73.     }
  74.  
  75.     public String getRemoteAddress() {
  76.         return remoteAddress;
  77.     }
  78.  
  79.     public long getLastRead() {
  80.         return lastRead;
  81.     }
  82.  
  83.     public boolean idleTimeout(int timeoutMillis) {
  84.         return timeoutMillis > 0 && System.currentTimeMillis() - lastRead > timeoutMillis;
  85.     }
  86.  
  87.     public void setAttachment(Object attachment) {
  88.         this.attachment = attachment;
  89.     }
  90.  
  91.     public Object getAttachment() {
  92.         return attachment;
  93.     }
  94.  
  95.     SelectionKey getSelectionKey() {
  96.         return selectionKey;
  97.     }
  98.  
  99.     void updateLastRead() {
  100.         lastRead = System.currentTimeMillis();
  101.     }
  102.  
  103.     Queue<WriteEvent> getWriteEvents() {
  104.         return writeEvents;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement