Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 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
package org.apache.sshd.common.io.nio2;
20
 
21
import java.io.IOException;
22
import java.net.SocketAddress;
23
import java.nio.channels.AsynchronousChannelGroup;
24
import java.nio.channels.AsynchronousSocketChannel;
25
 
26
import org.apache.sshd.common.FactoryManager;
27
import org.apache.sshd.common.future.DefaultSshFuture;
28
import org.apache.sshd.common.io.IoConnectFuture;
29
import org.apache.sshd.common.io.IoConnector;
30
import org.apache.sshd.common.io.IoHandler;
31
import org.apache.sshd.common.io.IoSession;
32
 
33
/**
34
 */
35
public class Nio2Connector extends Nio2Service implements IoConnector {
36
 
37
    public Nio2Connector(FactoryManager manager, IoHandler handler, AsynchronousChannelGroup group) {
38
        super(manager, handler, group);
39
    }
40
 
41
    public IoConnectFuture connect(SocketAddress address) {
42
        logger.debug("Connecting to {}", address);
43
        final IoConnectFuture future = new DefaultIoConnectFuture(null);
44
        try {
45
            final AsynchronousSocketChannel socket = AsynchronousSocketChannel.open(group);
46
            socket.connect(address, null, new Nio2CompletionHandler<Void, Object>() {
47
                protected void onCompleted(Void result, Object attachment) {
48
                    try {
49
                        Nio2Session session = new Nio2Session(Nio2Connector.this, handler, socket);
50
                        handler.sessionCreated(session);
51
                        sessions.put(session.getId(), session);
52
                        future.setSession(session);
53
                        session.startReading();
54
                    } catch (Throwable e) {
55
                        try {
56
                            socket.close();
57
                        } catch (IOException t) {
58
                            // Ignore
59
                        }
60
                        future.setException(e);
61
                    }
62
                }
63
                protected void onFailed(final Throwable exc, final Object attachment) {
64
                    future.setException(exc);
65
                }
66
            });
67
        } catch (IOException exc) {
68
            future.setException(exc);
69
        }
70
        return future;
71
    }
72
 
73
    static class DefaultIoConnectFuture extends DefaultSshFuture<IoConnectFuture> implements IoConnectFuture {
74
        DefaultIoConnectFuture(Object lock) {
75
            super(lock);
76
        }
77
        public IoSession getSession() {
78
            Object v = getValue();
79
            return v instanceof IoSession ? (IoSession) v : null;
80
        }
81
        public Throwable getException() {
82
            Object v = getValue();
83
            return v instanceof Throwable ? (Throwable) v : null;
84
        }
85
        public boolean isConnected() {
86
            return getValue() instanceof IoSession;
87
        }
88
        public void setSession(IoSession session) {
89
            setValue(session);
90
        }
91
        public void setException(Throwable exception) {
92
            setValue(exception);
93
        }
94
    }
95
 
96
}