View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.service;
21  
22  import java.io.IOException;
23  import java.net.SocketAddress;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.mina.core.session.IoSession;
28  
29  /**
30   * Accepts incoming connection, communicates with clients, and fires events to
31   * {@link IoHandler}s.
32   * <p>
33   * Please refer to
34   * <a href="../../../../../xref-examples/org/apache/mina/examples/echoserver/Main.html">EchoServer</a>
35   * example.
36   * <p>
37   * You should bind to the desired socket address to accept incoming
38   * connections, and then events for incoming connections will be sent to
39   * the specified default {@link IoHandler}.
40   * <p>
41   * Threads accept incoming connections start automatically when
42   * {@link #bind()} is invoked, and stop when {@link #unbind()} is invoked.
43   *
44   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
45   */
46  public interface IoAcceptor extends IoService {
47      /**
48       * Returns the local address which is bound currently.  If more than one
49       * address are bound, only one of them will be returned, but it's not
50       * necessarily the firstly bound address.
51       */
52      SocketAddress getLocalAddress();
53  
54      /**
55       * Returns a {@link Set} of the local addresses which are bound currently.
56       */
57      Set<SocketAddress> getLocalAddresses();
58  
59      /**
60       * Returns the default local address to bind when no argument is specified
61       * in {@link #bind()} method.  Please note that the default will not be
62       * used if any local address is specified.  If more than one address are
63       * set, only one of them will be returned, but it's not necessarily the
64       * firstly specified address in {@link #setDefaultLocalAddresses(List)}.
65       * 
66       */
67      SocketAddress getDefaultLocalAddress();
68  
69      /**
70       * Returns a {@link List} of the default local addresses to bind when no
71       * argument is specified in {@link #bind()} method.  Please note that the
72       * default will not be used if any local address is specified.
73       */
74      List<SocketAddress> getDefaultLocalAddresses();
75  
76      /**
77       * Sets the default local address to bind when no argument is specified in
78       * {@link #bind()} method.  Please note that the default will not be used
79       * if any local address is specified.
80       */
81      void setDefaultLocalAddress(SocketAddress localAddress);
82  
83      /**
84       * Sets the default local addresses to bind when no argument is specified
85       * in {@link #bind()} method.  Please note that the default will not be
86       * used if any local address is specified.
87       */
88      void setDefaultLocalAddresses(SocketAddress firstLocalAddress, SocketAddress... otherLocalAddresses);
89  
90      /**
91       * Sets the default local addresses to bind when no argument is specified
92       * in {@link #bind()} method.  Please note that the default will not be
93       * used if any local address is specified.
94       */
95      void setDefaultLocalAddresses(Iterable<? extends SocketAddress> localAddresses);
96  
97      /**
98       * Sets the default local addresses to bind when no argument is specified
99       * in {@link #bind()} method.  Please note that the default will not be
100      * used if any local address is specified.
101      */
102     void setDefaultLocalAddresses(List<? extends SocketAddress> localAddresses);
103 
104     /**
105     * Returns <tt>true</tt> if and only if all clients are closed when this
106     * acceptor unbinds from all the related local address (i.e. when the
107     * service is deactivated).
108     */
109     boolean isCloseOnDeactivation();
110 
111     /**
112      * Sets whether all client sessions are closed when this acceptor unbinds
113      * from all the related local addresses (i.e. when the service is
114      * deactivated).  The default value is <tt>true</tt>.
115      */
116     void setCloseOnDeactivation(boolean closeOnDeactivation);
117 
118     /**
119      * Binds to the default local address(es) and start to accept incoming
120      * connections.
121      *
122      * @throws IOException if failed to bind
123      */
124     void bind() throws IOException;
125 
126     /**
127      * Binds to the specified local addresses and start to accept incoming
128      * connections. If no address is given, bind on the default local address.
129      * 
130      * @param addresses The SocketAddresses to bind to 
131      *
132      * @throws IOException if failed to bind
133      */
134     void bind(SocketAddress... addresses) throws IOException;
135 
136     /**
137      * Binds to the specified local addresses and start to accept incoming
138      * connections.
139      *
140      * @throws IOException if failed to bind
141      */
142     void bind(Iterable<? extends SocketAddress> localAddresses) throws IOException;
143 
144     /**
145      * Unbinds from all local addresses that this service is bound to and stops
146      * to accept incoming connections.  All managed connections will be closed
147      * if {@link #setCloseOnDeactivation(boolean) disconnectOnUnbind} property
148      * is <tt>true</tt>.  This method returns silently if no local address is
149      * bound yet.
150      */
151     void unbind();
152 
153     /**
154      * Unbinds from the specified local address and stop to accept incoming
155      * connections.  All managed connections will be closed if
156      * {@link #setCloseOnDeactivation(boolean) disconnectOnUnbind} property is
157      * <tt>true</tt>.  This method returns silently if the default local
158      * address is not bound yet.
159      */
160     void unbind(SocketAddress localAddress);
161 
162     /**
163      * Unbinds from the specified local addresses and stop to accept incoming
164      * connections.  All managed connections will be closed if
165      * {@link #setCloseOnDeactivation(boolean) disconnectOnUnbind} property is
166      * <tt>true</tt>.  This method returns silently if the default local
167      * addresses are not bound yet.
168      */
169     void unbind(SocketAddress firstLocalAddress, SocketAddress... otherLocalAddresses);
170 
171     /**
172      * Unbinds from the specified local addresses and stop to accept incoming
173      * connections.  All managed connections will be closed if
174      * {@link #setCloseOnDeactivation(boolean) disconnectOnUnbind} property is
175      * <tt>true</tt>.  This method returns silently if the default local
176      * addresses are not bound yet.
177      */
178     void unbind(Iterable<? extends SocketAddress> localAddresses);
179 
180     /**
181      * (Optional) Returns an {@link IoSession} that is bound to the specified
182      * <tt>localAddress</tt> and the specified <tt>remoteAddress</tt> which
183      * reuses the local address that is already bound by this service.
184      * <p>
185      * This operation is optional.  Please throw {@link UnsupportedOperationException}
186      * if the transport type doesn't support this operation.  This operation is
187      * usually implemented for connectionless transport types.
188      *
189      * @throws UnsupportedOperationException if this operation is not supported
190      * @throws IllegalStateException if this service is not running.
191      * @throws IllegalArgumentException if this service is not bound to the
192      *                                  specified <tt>localAddress</tt>.
193      */
194     IoSession newSession(SocketAddress remoteAddress, SocketAddress localAddress);
195 }