/** * */ package com.jotuntech.sketcher.client; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.util.concurrent.ArrayBlockingQueue; import com.jotuntech.sketcher.common.User; /** * Client connection * * @author Thor Harald Johansen */ public class Connection { /** Return values for Command */ public final static int SEND_NONE = 0; /** Send to nobody */ public final static int SEND_SELF = 1; /** Send to self only */ public final static int SEND_OTHERS = 2; /** Send to others only */ public final static int SEND_ALL = 3; /** Send to self and others */ public final static long PING_INTERVAL = 10000; /** Socket channel */ private SocketChannel channel; /** User to which connection belongs. */ private User user; /** Input buffer */ private ByteBuffer inputBuffer; /** Output buffer */ private ByteBuffer outputBuffer; /** Send queue */ private ArrayBlockingQueue sendQueue = new ArrayBlockingQueue(98304); /** Time of death */ private long timeOfDeath; /** Last ping */ private long lastPing; public Connection() { } public Connection(SocketChannel channel) throws IOException { /** Store socket channel. */ this.channel = channel; /** Allocate transmission buffers */ inputBuffer = ByteBuffer.allocate(65538); outputBuffer = ByteBuffer.allocate(65538); /** Disable time of death */ timeOfDeath = Long.MAX_VALUE; } /** Set user for connection */ public void setUser(User user) { this.user = user; } /** Get user for connection */ public User getUser() { return user; } public void setChannel(SocketChannel channel) { this.channel = channel; } public SocketChannel getChannel() { return channel; } public ByteBuffer getInputBuffer() { return inputBuffer; } public ByteBuffer getOutputBuffer() { return outputBuffer; } public ArrayBlockingQueue getSendQueue() { return sendQueue; } public void setTimeOfDeath(long timeOfDeath) { this.timeOfDeath = timeOfDeath; } public boolean hasTimeOfDeath() { return timeOfDeath != Long.MAX_VALUE; } public boolean isTimeOfDeath() { return timeOfDeath != Long.MAX_VALUE && System.currentTimeMillis() >= timeOfDeath; } public void setLastPing(long lastPing) { this.lastPing = lastPing; } public long getLastPing() { return lastPing; } }