View Javadoc

1   /**
2    *Copyright [2009-2010] [dennis zhuang(killme2008@gmail.com)]
3    *Licensed under the Apache License, Version 2.0 (the "License");
4    *you may not use this file except in compliance with the License. 
5    *You may obtain a copy of the License at 
6    *             http://www.apache.org/licenses/LICENSE-2.0 
7    *Unless required by applicable law or agreed to in writing, 
8    *software distributed under the License is distributed on an "AS IS" BASIS, 
9    *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
10   *either express or implied. See the License for the specific language governing permissions and limitations under the License
11   */
12  /**
13   *Copyright [2009-2010] [dennis zhuang(killme2008@gmail.com)]
14   *Licensed under the Apache License, Version 2.0 (the "License");
15   *you may not use this file except in compliance with the License.
16   *You may obtain a copy of the License at
17   *             http://www.apache.org/licenses/LICENSE-2.0
18   *Unless required by applicable law or agreed to in writing,
19   *software distributed under the License is distributed on an "AS IS" BASIS,
20   *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
21   *either express or implied. See the License for the specific language governing permissions and limitations under the License
22   */
23  package com.google.code.yanf4j.core;
24  
25  import java.net.InetAddress;
26  import java.net.InetSocketAddress;
27  import java.nio.ByteOrder;
28  
29  /**
30   * Abstract connection
31   * 
32   * @author dennis
33   * 
34   */
35  public interface Session {
36  
37  	public enum SessionStatus {
38  		NULL, READING, WRITING, IDLE, INITIALIZE, CLOSING, CLOSED
39  	}
40  
41  	/**
42  	 * Start session
43  	 */
44  	public void start();
45  
46  	/**
47  	 * Write a message,if you don't care when the message is written
48  	 * 
49  	 * @param packet
50  	 */
51  	public void write(Object packet);
52  
53  	/**
54  	 * Check if session is closed
55  	 * 
56  	 * @return
57  	 */
58  	public boolean isClosed();
59  
60  	/**
61  	 * Close session
62  	 */
63  	public void close();
64  
65  	/**
66  	 * Return the remote end's InetSocketAddress
67  	 * 
68  	 * @return
69  	 */
70  	public InetSocketAddress getRemoteSocketAddress();
71  
72  	public InetAddress getLocalAddress();
73  
74  	/**
75  	 * Return true if using blocking write
76  	 * 
77  	 * @return
78  	 */
79  	public boolean isUseBlockingWrite();
80  
81  	/**
82  	 * Set if using blocking write
83  	 * 
84  	 * @param useBlockingWrite
85  	 */
86  	public void setUseBlockingWrite(boolean useBlockingWrite);
87  
88  	/**
89  	 * Return true if using blocking read
90  	 * 
91  	 * @return
92  	 */
93  	public boolean isUseBlockingRead();
94  
95  	public void setUseBlockingRead(boolean useBlockingRead);
96  
97  	/**
98  	 * Flush the write queue,this method may be no effect if OP_WRITE is
99  	 * running.
100 	 */
101 	public void flush();
102 
103 	/**
104 	 * Return true if session is expired,session is expired beacause you set the
105 	 * sessionTimeout,if since session's last operation form now is over this
106 	 * vlaue,isExpired return true,and Handler.onExpired() will be invoked.
107 	 * 
108 	 * @return
109 	 */
110 	public boolean isExpired();
111 
112 	/**
113 	 * Check if session is idle
114 	 * 
115 	 * @return
116 	 */
117 	public boolean isIdle();
118 
119 	/**
120 	 * Return current encoder
121 	 * 
122 	 * @return
123 	 */
124 	public CodecFactory.Encoder getEncoder();
125 
126 	/**
127 	 * Set encoder
128 	 * 
129 	 * @param encoder
130 	 */
131 	public void setEncoder(CodecFactory.Encoder encoder);
132 
133 	/**
134 	 * Return current decoder
135 	 * 
136 	 * @return
137 	 */
138 
139 	public CodecFactory.Decoder getDecoder();
140 
141 	public void setDecoder(CodecFactory.Decoder decoder);
142 
143 	/**
144 	 * Return true if allow handling read and write concurrently,default is
145 	 * true.
146 	 * 
147 	 * @return
148 	 */
149 	public boolean isHandleReadWriteConcurrently();
150 
151 	public void setHandleReadWriteConcurrently(
152 			boolean handleReadWriteConcurrently);
153 
154 	/**
155 	 * Return the session read buffer's byte order,big end or little end.
156 	 * 
157 	 * @return
158 	 */
159 	public ByteOrder getReadBufferByteOrder();
160 
161 	public void setReadBufferByteOrder(ByteOrder readBufferByteOrder);
162 
163 	/**
164 	 * Set a attribute attched with this session
165 	 * 
166 	 * @param key
167 	 * @param value
168 	 */
169 	public void setAttribute(String key, Object value);
170 
171 	/**
172 	 * Remove attribute
173 	 * 
174 	 * @param key
175 	 */
176 	public void removeAttribute(String key);
177 
178 	/**
179 	 * Return attribute associated with key
180 	 * 
181 	 * @param key
182 	 * @return
183 	 */
184 	public Object getAttribute(String key);
185 
186 	/**
187 	 * Clear attributes
188 	 */
189 	public void clearAttributes();
190 
191 	/**
192 	 * Return the bytes in write queue,there bytes is in memory.Use this method
193 	 * to controll writing speed.
194 	 * 
195 	 * @return
196 	 */
197 	public long getScheduleWritenBytes();
198 
199 	/**
200 	 * Return last operation timestamp,operation include read,write,idle
201 	 * 
202 	 * @return
203 	 */
204 	public long getLastOperationTimeStamp();
205 
206 	/**
207 	 * return true if it is a loopback connection
208 	 * 
209 	 * @return
210 	 */
211 	public boolean isLoopbackConnection();
212 
213 	public long getSessionIdleTimeout();
214 
215 	public void setSessionIdleTimeout(long sessionIdleTimeout);
216 
217 	public long getSessionTimeout();
218 
219 	public void setSessionTimeout(long sessionTimeout);
220 
221 	public Object setAttributeIfAbsent(String key, Object value);
222 
223 	public Handler getHandler();
224 
225 }