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.mina;
20
 
21
import java.net.SocketAddress;
22
 
23
import org.apache.mina.core.buffer.IoBuffer;
24
import org.apache.mina.core.future.IoFuture;
25
import org.apache.mina.core.future.IoFutureListener;
26
import org.apache.mina.core.future.WriteFuture;
27
import org.apache.sshd.common.Closeable;
28
import org.apache.sshd.common.SshException;
29
import org.apache.sshd.common.future.DefaultCloseFuture;
30
import org.apache.sshd.common.future.DefaultSshFuture;
31
import org.apache.sshd.common.io.IoService;
32
import org.apache.sshd.common.io.IoSession;
33
import org.apache.sshd.common.io.IoWriteFuture;
34
import org.apache.sshd.common.util.Buffer;
35
import org.apache.sshd.common.util.CloseableUtils;
36
 
37
/**
38
 */
39
public class MinaSession extends CloseableUtils.AbstractInnerCloseable implements IoSession {
40
 
41
    private final MinaService service;
42
    private final org.apache.mina.core.session.IoSession session;
43
 
44
    public MinaSession(MinaService service, org.apache.mina.core.session.IoSession session) {
45
        this.service = service;
46
        this.session = session;
47
    }
48
 
49
    public org.apache.mina.core.session.IoSession getSession() {
50
        return session;
51
    }
52
 
53
    public void suspend() {
54
        session.suspendRead();
55
        session.suspendWrite();
56
    }
57
 
58
    public Object getAttribute(Object key) {
59
        return session.getAttribute(key);
60
    }
61
 
62
    public Object setAttribute(Object key, Object value) {
63
        return session.setAttribute(key, value);
64
    }
65
 
66
    public SocketAddress getRemoteAddress() {
67
        return session.getRemoteAddress();
68
    }
69
 
70
    public SocketAddress getLocalAddress() {
71
        return session.getLocalAddress();
72
    }
73
 
74
    public long getId() {
75
        return session.getId();
76
    }
77
 
78
    public WriteFuture write(byte[] data, int offset, int len) {
79
        IoBuffer buffer = IoBuffer.wrap(data, offset, len);
80
        return session.write(buffer);
81
    }
82
 
83
    @Override
84
    protected Closeable getInnerCloseable() {
85
        return new Closeable() {
86
            public boolean isClosing() {
87
                return session.isClosing();
88
            }
89
            public boolean isClosed() {
90
                return !session.isConnected();
91
            }
92
            public org.apache.sshd.common.future.CloseFuture close(boolean immediately) {
93
                final DefaultCloseFuture future = new DefaultCloseFuture(lock);
94
                session.close(false).addListener(new IoFutureListener<IoFuture>() {
95
                    public void operationComplete(IoFuture f) {
96
                        future.setValue(true);
97
                    }
98
                });
99
                return future;
100
            }
101
        };
102
    }
103
 
104
    public IoWriteFuture write(Buffer buffer) {
105
        class Future extends DefaultSshFuture<IoWriteFuture> implements IoWriteFuture {
106
            Future(Object lock) {
107
                super(lock);
108
            }
109
 
110
            public void verify() throws SshException {
111
                try {
112
                    await();
113
                }
114
                catch (InterruptedException e) {
115
                    throw new SshException("Interrupted", e);
116
                }
117
                if (!isWritten()) {
118
                    throw new SshException("Write failed", getException());
119
                }
120
            }
121
 
122
            public boolean isWritten() {
123
                return getValue() instanceof Boolean;
124
            }
125
 
126
            public void setWritten() {
127
                setValue(Boolean.TRUE);
128
            }
129
 
130
            public Throwable getException() {
131
                Object v = getValue();
132
                return v instanceof Throwable ? (Throwable) v : null;
133
            }
134
 
135
            public void setException(Throwable exception) {
136
                if (exception == null) {
137
                    throw new IllegalArgumentException("exception");
138
                }
139
                setValue(exception);
140
            }
141
        }
142
        final Future future = new Future(null);
143
        session.write(MinaSupport.asIoBuffer(buffer)).addListener(new IoFutureListener<WriteFuture>() {
144
            public void operationComplete(WriteFuture cf) {
145
                if (cf.getException() != null) {
146
                    future.setException(cf.getException());
147
                } else {
148
                    future.setWritten();
149
                }
150
            }
151
        });
152
        return future;
153
    }
154
 
155
    public IoService getService() {
156
        return service;
157
    }
158
 
159
    public String toString() {
160
        return getClass().getSimpleName() + "[local=" + session.getLocalAddress() + ", remote=" + session.getRemoteAddress() + "]";
161
    }
162
}