AcceptPortImpl.java 5.51 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20
package org.jivesoftware.net.spi;

import org.jivesoftware.net.policies.BasicAcceptPolicy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.*;

import org.jivesoftware.net.*;
21
import org.jivesoftware.messenger.JiveGlobals;
Matt Tucker's avatar
Matt Tucker committed
22 23 24 25
import org.jivesoftware.util.Log;

public class AcceptPortImpl implements AcceptPort {

26
    private String context;
Matt Tucker's avatar
Matt Tucker committed
27 28 29 30 31 32 33 34 35 36
    private InetSocketAddress address;
    private boolean secure;
    private AcceptPolicy policy = new BasicAcceptPolicy(true);
    private long startTime = -1;
    private ConnectionMonitor acceptMonitor = new TransientConnectionMonitor();
    private ConnectionMonitor connectMonitor = new TransientConnectionMonitor();
    private ConnectionMonitor disconnectMonitor = new TransientConnectionMonitor();
    private AcceptThread acceptThread = null;
    private ConnectionManager conManager;

37 38
    public AcceptPortImpl(String context, ConnectionManager connectionManager) {
        this.context = context;
Matt Tucker's avatar
Matt Tucker committed
39
        conManager = connectionManager;
40
        String hostname = JiveGlobals.getProperty(context + ".hostname");
Matt Tucker's avatar
Matt Tucker committed
41 42 43 44 45 46 47 48 49 50 51
        int port = JiveGlobals.getIntProperty(context + ".portnumber", -1);
        if (port == -1) {
            Log.error("No port number set for " + context);
            return;
        }
        if (hostname == null) {
            address = new InetSocketAddress(port);
        }
        else {
            address = new InetSocketAddress(hostname, port);
        }
Matt Tucker's avatar
Matt Tucker committed
52 53
    }

54 55 56 57
    public AcceptPortImpl(String context, ConnectionManager connectionManager,
            InetSocketAddress bindAddress)
    {
        this.context = context;
Matt Tucker's avatar
Matt Tucker committed
58 59 60 61 62
        conManager = connectionManager;
        address = bindAddress;
        savePort();
    }

63 64 65 66
    public void setContext(String context) {
        this.context = context;
    }

Matt Tucker's avatar
Matt Tucker committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    public boolean isSecure() {
        return secure;
    }

    public void setSecure(boolean securePort) {
        this.secure = securePort;
        savePort();
    }

    public InetSocketAddress getInetSocketAddress() {
        return address;
    }

    public void setInetSocketAddress(InetSocketAddress bindAddress) {
        this.address = bindAddress;
        savePort();
    }

    public long getUptime() {
        long uptime = -1;
        if (startTime > 0){
            uptime = System.currentTimeMillis() - startTime;
        }
        return uptime;
    }

    public void open()
            throws IOException, SecurityException, IllegalArgumentException {
        if (acceptThread == null){
            acceptThread = new AcceptThread();
            acceptThread.init();
            acceptThread.start();
            startTime = System.currentTimeMillis();
        }
    }

    public void close() throws IOException {
        if (acceptThread != null){
            acceptThread.close();
            acceptThread = null;
        }
    }

    public void remove() throws IOException {
111
        JiveGlobals.deleteProperty(context);
Matt Tucker's avatar
Matt Tucker committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        if (startTime > 0){
            close();
        }
    }

    public AcceptPolicy getAcceptPolicy() {
        return policy;
    }

    public ConnectionMonitor getAcceptMonitor() {
        return acceptMonitor;
    }

    public ConnectionMonitor getConnectMonitor() {
        return connectMonitor;
    }

    public ConnectionMonitor getDisconnectMonitor() {
        return disconnectMonitor;
    }

133 134 135 136
    public void savePort(){
        if (address != null){
            JiveGlobals.setProperty(context + ".hostname",address.getHostName());
            JiveGlobals.setProperty(context + ".portnumber",Integer.toString(address.getPort()));
Matt Tucker's avatar
Matt Tucker committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        }
    }

    private class AcceptThread extends Thread{
        AcceptThread(){
            super("Accept thread - " + address.toString());
            setDaemon(true);
        }
        private ServerSocketChannel server;
        private boolean running = false;

        void init() throws IOException {
            server = ServerSocketChannel.open();
            server.socket().bind(address);
            server.configureBlocking(true);
        }

        public void run(){
            running = true;
            while (running){
                try {
                    SocketChannel socket = server.accept();
                    Connection conn = new SocketChannelConnection(socket);
                    acceptMonitor.addSample(conn);
161
                    if (policy.evaluate(conn)) {
Matt Tucker's avatar
Matt Tucker committed
162 163
                        conManager.addConnection(conn);
                        connectMonitor.addSample(conn);
164 165
                    }
                    else {
Matt Tucker's avatar
Matt Tucker committed
166 167
                        disconnectMonitor.addSample(conn);
                    }
168 169
                }
                catch (ClosedChannelException ce){
Matt Tucker's avatar
Matt Tucker committed
170 171
                    startTime = -1;
                    running = false;
172 173
                }
                catch (NotYetBoundException ce){
Matt Tucker's avatar
Matt Tucker committed
174
                    throw new IllegalStateException("Must init thread before running");
175 176
                }
                catch (IOException e) {
Matt Tucker's avatar
Matt Tucker committed
177 178 179 180 181 182 183 184 185 186
                    Log.error(e);
                }
            }
        }
        void close(){
            if (running){
                running = false;
                startTime = -1;
                try {
                    server.close();
187 188
                }
                catch (IOException e) {
Matt Tucker's avatar
Matt Tucker committed
189 190 191 192 193 194
                    //
                }
            }
        }
    }
}