查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.net.SocketAddress;
23  
24  import org.apache.mina.core.future.ConnectFuture;
25  import org.apache.mina.core.session.IoSession;
26  import org.apache.mina.core.session.IoSessionInitializer;
27  
28  /**
29   * Connects to endpoint, communicates with the server, and fires events to
30   * {@link IoHandler}s.
31   * <p>
32   * Please refer to
33   * <a href="../../../../../xref-examples/org/apache/mina/examples/netcat/Main.html">NetCat</a>
34   * example.
35   * <p>
36   * You should connect to the desired socket address to start communication,
37   * and then events for incoming connections will be sent to the specified
38   * default {@link IoHandler}.
39   * <p>
40   * Threads connect to endpoint start automatically when
41   * {@link #connect(SocketAddress)} is invoked, and stop when all
42   * connection attempts are finished.
43   *
44   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
45   */
46  public interface IoConnector extends IoService {
47      /**
48       * @return the connect timeout in seconds.  The default value is 1 minute.
49       * 
50       * @deprecated
51       */
52      int getConnectTimeout();
53  
54      /**
55       * @return the connect timeout in milliseconds.  The default value is 1 minute.
56       */
57      long getConnectTimeoutMillis();
58  
59      /**
60       * Sets the connect timeout in seconds.  The default value is 1 minute.
61       * 
62       * @deprecated
63       * @param connectTimeout The time out for the connection
64       */
65      void setConnectTimeout(int connectTimeout);
66  
67      /**
68       * Sets the connect timeout in milliseconds.  The default value is 1 minute.
69       * 
70       * @param connectTimeoutInMillis The time out for the connection
71       */
72      void setConnectTimeoutMillis(long connectTimeoutInMillis);
73  
74      /**
75       * @return the default remote address to connect to when no argument
76       * is specified in {@link #connect()} method.
77       */
78      SocketAddress getDefaultRemoteAddress();
79  
80      /**
81       * Sets the default remote address to connect to when no argument is
82       * specified in {@link #connect()} method.
83       * 
84       * @param defaultRemoteAddress The default remote address
85       */
86      void setDefaultRemoteAddress(SocketAddress defaultRemoteAddress);
87  
88      /**
89       * @return the default local address
90       */
91      SocketAddress getDefaultLocalAddress();
92  
93      /**
94       * Sets the default local address
95       * 
96       * @param defaultLocalAddress The default local address
97       */
98      void setDefaultLocalAddress(SocketAddress defaultLocalAddress);
99  
100     /**
101      * Connects to the {@link #setDefaultRemoteAddress(SocketAddress) default
102      * remote address}.
103      * 
104      * @return the {@link ConnectFuture} instance which is completed when the
105      *         connection attempt initiated by this call succeeds or fails.
106      * @throws IllegalStateException
107      *             if no default remoted address is set.
108      */
109     ConnectFuture connect();
110 
111     /**
112      * Connects to the {@link #setDefaultRemoteAddress(SocketAddress) default
113      * remote address} and invokes the <code>ioSessionInitializer</code> when
114      * the IoSession is created but before {@link IoHandler#sessionCreated(IoSession)}
115      * is invoked.  There is <em>no</em> guarantee that the <code>ioSessionInitializer</code>
116      * will be invoked before this method returns.
117      * 
118      * @param sessionInitializer  the callback to invoke when the {@link IoSession} object is created
119      * @return the {@link ConnectFuture} instance which is completed when the
120      *         connection attempt initiated by this call succeeds or fails.
121      * 
122      * @throws IllegalStateException if no default remote address is set.
123      */
124     ConnectFuture connect(IoSessionInitializer<? extends ConnectFuture> sessionInitializer);
125 
126     /**
127      * Connects to the specified remote address.
128      * 
129      * @param remoteAddress The remote address to connect to
130      * @return the {@link ConnectFuture} instance which is completed when the
131      *         connection attempt initiated by this call succeeds or fails.
132      */
133     ConnectFuture connect(SocketAddress remoteAddress);
134 
135     /**
136      * Connects to the specified remote address and invokes
137      * the <code>ioSessionInitializer</code> when the IoSession is created but before
138      * {@link IoHandler#sessionCreated(IoSession)} is invoked.  There is <em>no</em>
139      * guarantee that the <code>ioSessionInitializer</code> will be invoked before
140      * this method returns.
141      * 
142      * @param remoteAddress  the remote address to connect to
143      * @param sessionInitializer  the callback to invoke when the {@link IoSession} object is created
144      * 
145      * @return the {@link ConnectFuture} instance which is completed when the
146      *         connection attempt initiated by this call succeeds or fails.
147      */
148     ConnectFuture connect(SocketAddress remoteAddress, IoSessionInitializer<? extends ConnectFuture> sessionInitializer);
149 
150     /**
151      * Connects to the specified remote address binding to the specified local address.
152      *
153      * @param remoteAddress The remote address to connect
154      * @param localAddress The local address to bind
155      * 
156      * @return the {@link ConnectFuture} instance which is completed when the
157      *         connection attempt initiated by this call succeeds or fails.
158      */
159     ConnectFuture connect(SocketAddress remoteAddress, SocketAddress localAddress);
160 
161     /**
162      * Connects to the specified remote address binding to the specified local
163      * address and and invokes the <code>ioSessionInitializer</code> when the
164      * IoSession is created but before {@link IoHandler#sessionCreated(IoSession)}
165      * is invoked.  There is <em>no</em> guarantee that the <code>ioSessionInitializer</code>
166      * will be invoked before this method returns.
167      * 
168      * @param remoteAddress  the remote address to connect to
169      * @param localAddress  the local interface to bind to
170      * @param sessionInitializer  the callback to invoke when the {@link IoSession} object is created
171      *
172      * @return the {@link ConnectFuture} instance which is completed when the
173      *         connection attempt initiated by this call succeeds or fails.
174      */
175     ConnectFuture connect(SocketAddress remoteAddress, SocketAddress localAddress,
176             IoSessionInitializer<? extends ConnectFuture> sessionInitializer);
177 }