📄 iosession.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */package org.apache.mina.core.session;import java.net.SocketAddress;import java.util.Set;import org.apache.mina.core.filterchain.IoFilter;import org.apache.mina.core.filterchain.IoFilterChain;import org.apache.mina.core.future.CloseFuture;import org.apache.mina.core.future.ReadFuture;import org.apache.mina.core.future.WriteFuture;import org.apache.mina.core.service.IoAcceptor;import org.apache.mina.core.service.IoConnector;import org.apache.mina.core.service.IoHandler;import org.apache.mina.core.service.IoService;import org.apache.mina.core.service.TransportMetadata;import org.apache.mina.core.write.WriteRequest;import org.apache.mina.core.write.WriteRequestQueue;/** * A handle which represents connection between two end-points regardless of * transport types. * <p/> * {@link IoSession} provides user-defined attributes. User-defined attributes * are application-specific data which are associated with a session. * It often contains objects that represents the state of a higher-level protocol * and becomes a way to exchange data between filters and handlers. * <p/> * <h3>Adjusting Transport Type Specific Properties</h3> * <p/> * You can simply downcast the session to an appropriate subclass. * </p> * <p/> * <h3>Thread Safety</h3> * <p/> * {@link IoSession} is thread-safe. But please note that performing * more than one {@link #write(Object)} calls at the same time will * cause the {@link IoFilter#filterWrite(IoFilter.NextFilter,IoSession,WriteRequest)} * to be executed simultaneously, and therefore you have to make sure the * {@link IoFilter} implementations you're using are thread-safe, too. * </p> * <p/> * <h3>Equality of Sessions</h3> * TODO : The getId() method is totally wrong. We can't base * a method which is designed to create a unique ID on the hashCode method. * {@link #equals(Object)} and {@link #hashCode()} shall not be overriden * to the default behavior that is defined in {@link Object}. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 720462 $, $Date: 2008-11-25 11:39:07 +0100 (Tue, 25 Nov 2008) $ */public interface IoSession { /** * @return a unique identifier for this session. Every session has its own * ID which is different from each other. * * TODO : The way it's implemented does not guarantee that the contract is * respected. It uses the HashCode() method which don't guarantee the key * unicity. */ long getId(); /** * @return the {@link IoService} which provides I/O service to this session. */ IoService getService(); /** * @return the {@link IoHandler} which handles this session. */ IoHandler getHandler(); /** * @return the configuration of this session. */ IoSessionConfig getConfig(); /** * @return the filter chain that only affects this session. */ IoFilterChain getFilterChain(); /** * TODO Add method documentation */ WriteRequestQueue getWriteRequestQueue(); /** * @return the {@link TransportMetadata} that this session runs on. */ TransportMetadata getTransportMetadata(); /** * TODO This javadoc is wrong. The return tag should be short. * * @return a {@link ReadFuture} which is notified when a new message is * received, the connection is closed or an exception is caught. This * operation is especially useful when you implement a client application. * TODO : Describe here how we enable this feature. * However, please note that this operation is disabled by default and * throw {@link IllegalStateException} because all received events must be * queued somewhere to support this operation, possibly leading to memory * leak. This means you have to keep calling {@link #read()} once you * enabled this operation. To enable this operation, please call * {@link IoSessionConfig#setUseReadOperation(boolean)} with <tt>true</tt>. * * @throws IllegalStateException if * {@link IoSessionConfig#setUseReadOperation(boolean) useReadOperation} * option has not been enabled. */ ReadFuture read(); /** * Writes the specified <code>message</code> to remote peer. This * operation is asynchronous; {@link IoHandler#messageSent(IoSession,Object)} * will be invoked when the message is actually sent to remote peer. * You can also wait for the returned {@link WriteFuture} if you want * to wait for the message actually written. */ WriteFuture write(Object message); /** * (Optional) Writes the specified <tt>message</tt> to the specified <tt>destination</tt>. * This operation is asynchronous; {@link IoHandler#messageSent(IoSession, Object)} * will be invoked when the message is actually sent to remote peer. You can * also wait for the returned {@link WriteFuture} if you want to wait for * the message actually written. * <p> * When you implement a client that receives a broadcast message from a server * such as DHCP server, the client might need to send a response message for the * broadcast message the server sent. Because the remote address of the session * is not the address of the server in case of broadcasting, there should be a * way to specify the destination when you write the response message. * This interface provides {@link #write(Object, SocketAddress)} method so you * can specify the destination. * * @param destination <tt>null</tt> if you want the message sent to the * default remote address * * @throws UnsupportedOperationException if this operation is not supported */ WriteFuture write(Object message, SocketAddress destination); /** * Closes this session immediately or after all queued write requests * are flushed. This operation is asynchronous. Wait for the returned * {@link CloseFuture} if you want to wait for the session actually closed. * * @param immediately {@code true} to close this session immediately * (i.e. {@link #close()}). * {@code false} to close this session after all queued * write requests are flushed (i.e. {@link #closeOnFlush()}). */ CloseFuture close(boolean immediately); /** * Closes this session after all queued write requests * are flushed. This operation is asynchronous. Wait for the returned * {@link CloseFuture} if you want to wait for the session actually closed. * @deprecated use {@link IoSession#close(boolean)} */ @Deprecated CloseFuture close(); /** * Returns an attachment of this session. * This method is identical with <tt>getAttribute( "" )</tt>. * * @deprecated Use {@link #getAttribute(Object)} instead. */ @Deprecated Object getAttachment(); /** * Sets an attachment of this session. * This method is identical with <tt>setAttribute( "", attachment )</tt>. * * @return Old attachment. <tt>null</tt> if it is new. * @deprecated Use {@link #setAttribute(Object, Object)} instead. */ @Deprecated Object setAttachment(Object attachment); /** * Returns the value of the user-defined attribute of this session. * * @param key the key of the attribute * @return <tt>null</tt> if there is no attribute with the specified key */ Object getAttribute(Object key); /** * Returns the value of user defined attribute associated with the * specified key. If there's no such attribute, the specified default * value is associated with the specified key, and the default value is * returned. This method is same with the following code except that the * operation is performed atomically. * <pre> * if (containsAttribute(key)) { * return getAttribute(key); * } else { * setAttribute(key, defaultValue); * return defaultValue; * } * </pre> */ Object getAttribute(Object key, Object defaultValue); /** * Sets a user-defined attribute. * * @param key the key of the attribute * @param value the value of the attribute * @return The old value of the attribute. <tt>null</tt> if it is new. */ Object setAttribute(Object key, Object value); /** * Sets a user defined attribute without a value. This is useful when * you just want to put a 'mark' attribute. Its value is set to * {@link Boolean#TRUE}. * * @param key the key of the attribute * @return The old value of the attribute. <tt>null</tt> if it is new. */ Object setAttribute(Object key); /** * Sets a user defined attribute if the attribute with the specified key * is not set yet. This method is same with the following code except * that the operation is performed atomically. * <pre> * if (containsAttribute(key)) { * return getAttribute(key); * } else { * return setAttribute(key, value); * } * </pre> */ Object setAttributeIfAbsent(Object key, Object value); /** * Sets a user defined attribute without a value if the attribute with * the specified key is not set yet. This is useful when you just want to * put a 'mark' attribute. Its value is set to {@link Boolean#TRUE}. * This method is same with the following code except that the operation * is performed atomically. * <pre> * if (containsAttribute(key)) { * return getAttribute(key); // might not always be Boolean.TRUE. * } else { * return setAttribute(key); * } * </pre> */ Object setAttributeIfAbsent(Object key); /** * Removes a user-defined attribute with the specified key. * * @return The old value of the attribute. <tt>null</tt> if not found. */ Object removeAttribute(Object key); /** * Removes a user defined attribute with the specified key if the current * attribute value is equal to the specified value. This method is same * with the following code except that the operation is performed * atomically. * <pre> * if (containsAttribute(key) && getAttribute(key).equals(value)) { * removeAttribute(key); * return true; * } else { * return false; * } * </pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -