📄 channelresponse.java
字号:
/* * Copyright (c) 2003, The Regents of the University of California, through * Lawrence Berkeley National Laboratory (subject to receipt of any required * approvals from the U.S. Dept. of Energy). All rights reserved. */package gov.lbl.dsd.sea.nio.event;import gov.lbl.dsd.sea.nio.NetAgent;import gov.lbl.dsd.sea.nio.util.NioUtil;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;/** * Response from an agent to a prior {@link ChannelRequest.Register}or * {@link ChannelRequest.Close} or {@link ChannelRequest.WriteData}request; * Contains the agent this response originates from, as well as the selection key * of the channel the request/response are associated with (retrievable via * <code>key.channel()</code>), as well as an optional exception that may * have been thrown by the agent in attempting to handle the associated prior * request. * * @author whoschek@lbl.gov * @author $Author: hoschek3 $ * @version $Revision: 1.13 $, $Date: 2004/07/28 19:49:20 $ */public abstract class ChannelResponse { protected NetAgent agent; protected SelectionKey key; protected IOException exception; protected ChannelResponse(NetAgent agent, SelectionKey key, IOException exception) { this.agent = agent; this.key = key; this.exception = exception; } public NetAgent getAgent() { return this.agent; } public SelectionKey getKey() { return this.key; } public IOException getException() { return this.exception; } public String toString() { return this.getClass().getName() + ": agent=" + this.getAgent() + ", key=" + this.getKey() + ", exception=" + this.getException(); } /** * Indicates response is to a prior {@link ChannelRequest.Register}request; * The interest ops supplied to the original request can be retrieved from * the response via <code>response.getInterestOps()</code>. (This is * necessary because <code>key.interestOps()</code> may block on a lock held * by the NIO Selector.select() operation, potentially leading to ugly race * deadlocks). */ public static class Registered extends ChannelResponse { protected int interestOps; public Registered(NetAgent agent, SelectionKey key, IOException exception, int interestOps) { super(agent, key, exception); this.interestOps = interestOps; } public int getInterestOps() { return this.interestOps; } public String toString() { return super.toString() + ", interestOps=" + NioUtil.toString(this.getInterestOps()); } } /** * Indicates that an agent has accepted a new channel from a network client * (originally initiated by a {@link ChannelRequest.Register}request containing * {@link java.nio.channels.SelectionKey} OP_ACCEPT ops). */ public static class Accepted extends ChannelResponse { public Accepted(NetAgent agent, SelectionKey key, IOException exception) { super(agent, key, exception); } } /** * Indicates that an agent has connected to a network server (originally * initiated by a {@link ChannelRequest.Register}request containing * {@link java.nio.channels.SelectionKey} OP_CONNECT ops). */ public static class Connected extends ChannelResponse { public Connected(NetAgent agent, SelectionKey key, IOException exception) { super(agent, key, exception); } } /** * Indicates that an agent has closed a channel (originally initiated by a * {@link ChannelRequest.Close}request). */ public static class Closed extends ChannelResponse { public Closed(NetAgent agent, SelectionKey key, IOException exception) { super(agent, key, exception); } } /** * Contains a buffer with the non-blocking data read from the given channel * (originally initiated by a prior {@link ChannelRequest.Register}request * containing {@link java.nio.channels.SelectionKey}OP_READ ops); The data * has <code>buffer.remaining()</code> bytes, and is contained between * indexes <code>0 == buffer.position()</code> and <code>buffer.limit()</code>, * as usual with NIO; A buffer with <code>!buffer.hasRemaining()</code> * indicates that end-of-stream has been reached for the given channel. * Once your application has completely processed the contained buffer, you * can for efficiency consider reusing it (e.g. via a buffer pool). */ public static class Read extends ChannelResponse { protected ByteBuffer buffer; public Read(NetAgent agent, SelectionKey key, IOException exception, ByteBuffer buffer) { super(agent, key, exception); this.buffer = buffer; } public ByteBuffer getBuffer() { return this.buffer; } public String toString() { return super.toString() + ", buffer=" + this.getBuffer(); } } /** * Contains a buffer with the data fully written to the given * channel (originally initiated by a prior {@link ChannelRequest.WriteData} * request). Once the buffer has been fully written you * can for efficiency consider reusing it (e.g. via a buffer pool). The contained * buffer is IDENTICAL to the buffer originally handed to the agent via a * {@link ChannelRequest.WriteData}. Caution: DO NOT modify the buffer in * any way until it has been fully written! Failure to observe this rule can * cause MAJOR data corruption. */ public static class Write extends ChannelResponse { protected ByteBuffer buffer; public Write(NetAgent agent, SelectionKey key, IOException exception, ByteBuffer buffer) { super(agent, key, exception); this.buffer = buffer; } public ByteBuffer getBuffer() { return this.buffer; } public String toString() { return super.toString() + ", buffer=" + this.getBuffer(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -