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.io.IOException;
22
import java.net.Socket;
23
import java.net.SocketAddress;
24
import java.util.Collection;
25
import java.util.Set;
26
 
27
import org.apache.mina.core.service.IoAcceptor;
28
import org.apache.mina.core.service.IoHandler;
29
import org.apache.mina.core.service.IoProcessor;
30
import org.apache.mina.core.service.IoService;
31
import org.apache.mina.core.session.IoSessionConfig;
32
import org.apache.mina.transport.socket.nio.NioSession;
33
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
34
import org.apache.sshd.common.FactoryManager;
35
 
36
/**
37
 */
38
public class MinaAcceptor extends MinaService implements org.apache.sshd.common.io.IoAcceptor, IoHandler {
39
 
40
    protected volatile IoAcceptor acceptor;
41
    // Acceptor
42
    protected int backlog = 50;
43
    protected boolean reuseAddress = true;
44
    protected IoSessionConfig sessionConfig;
45
 
46
    public MinaAcceptor(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) {
47
        super(manager, handler, ioProcessor);
48
    }
49
 
50
    protected IoAcceptor createAcceptor() {
51
        NioSocketAcceptor acceptor = new NioSocketAcceptor(ioProcessor);
52
        acceptor.setCloseOnDeactivation(false);
53
        acceptor.setReuseAddress(reuseAddress);
54
        acceptor.setBacklog(backlog);
55
 
56
        // MINA itself forces our socket receive buffer to 1024 bytes
57
        // by default, despite what the operating system defaults to.
58
        // This limits us to about 3 MB/s incoming data transfer.  By
59
        // forcing back to the operating system default we can get a
60
        // decent transfer rate again.
61
        //
62
        final Socket s = new Socket();
63
        try {
64
            try {
65
                acceptor.getSessionConfig().setReceiveBufferSize(s.getReceiveBufferSize());
66
            } finally {
67
                s.close();
68
            }
69
        } catch (IOException e) {
70
            log.warn("cannot adjust SO_RCVBUF back to system default", e);
71
        }
72
        if (sessionConfig != null) {
73
            acceptor.getSessionConfig().setAll(sessionConfig);
74
        }
75
        return acceptor;
76
    }
77
 
78
    protected IoAcceptor getAcceptor() {
79
        if (acceptor == null) {
80
            synchronized (this) {
81
                if (acceptor == null) {
82
                    acceptor = createAcceptor();
83
                    acceptor.setHandler(this);
84
                }
85
            }
86
        }
87
        return acceptor;
88
    }
89
 
90
    @Override
91
    protected IoService getIoService() {
92
        return getAcceptor();
93
    }
94
 
95
    public void bind(Collection<? extends SocketAddress> addresses) throws IOException {
96
        getAcceptor().bind(addresses);
97
    }
98
 
99
    public void bind(SocketAddress address) throws IOException {
100
        getAcceptor().bind(address);
101
    }
102
 
103
    public void unbind() {
104
        getAcceptor().unbind();
105
    }
106
 
107
    public void unbind(Collection<? extends SocketAddress> addresses) {
108
        getAcceptor().unbind(addresses);
109
    }
110
 
111
    public void unbind(SocketAddress address) {
112
        getAcceptor().unbind(address);
113
    }
114
 
115
    public Set<SocketAddress> getBoundAddresses() {
116
        return getAcceptor().getLocalAddresses();
117
    }
118
 
119
}