SocketAcceptThread.java 3.91 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
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
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13
package org.jivesoftware.messenger.net;

14
import org.jivesoftware.messenger.ConnectionManager;
15
import org.jivesoftware.messenger.ServerPort;
Matt Tucker's avatar
Matt Tucker committed
16 17
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
18

Matt Tucker's avatar
Matt Tucker committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Implements a network front end with a dedicated thread reading
 * each incoming socket.
 *
 * @author Iain Shigeoka
 */
public class SocketAcceptThread extends Thread {

    /**
33
     * The default XMPP port for clients.
Matt Tucker's avatar
Matt Tucker committed
34 35 36 37
     */
    public static final int DEFAULT_PORT = 5222;

    /**
38
     * The default XMPP port for external components.
Matt Tucker's avatar
Matt Tucker committed
39
     */
40 41 42 43 44 45 46 47 48 49 50
    public static final int DEFAULT_COMPONENT_PORT = 10015;

    /**
     * The default XMPP port for server2server communication.
     */
    public static final int DEFAULT_SERVER_PORT = 5269;

    /**
     * Holds information about the port on which the server will listen for connections.
     */
    private ServerPort serverPort;
Matt Tucker's avatar
Matt Tucker committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

    /**
     * Interface to bind to.
     */
    private InetAddress bindInterface;

    /**
     * True while this thread should continue running.
     */
    private boolean notTerminated = true;

    /**
     * socket that listens for connections.
     */
    ServerSocket serverSocket;

    private ConnectionManager connManager;

69 70
    public SocketAcceptThread(ConnectionManager connManager, ServerPort serverPort)
            throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
71
        super("Socket Listener at port " + serverPort.getPort());
Matt Tucker's avatar
Matt Tucker committed
72
        this.connManager = connManager;
73 74
        this.serverPort = serverPort;
        String interfaceName = serverPort.getInterfaceName();
Matt Tucker's avatar
Matt Tucker committed
75 76
        bindInterface = null;
        if (interfaceName != null) {
77 78
            if (interfaceName.trim().length() > 0) {
                bindInterface = InetAddress.getByName(interfaceName);
Matt Tucker's avatar
Matt Tucker committed
79 80
            }
        }
81
        serverSocket = new ServerSocket(serverPort.getPort(), -1, bindInterface);
Matt Tucker's avatar
Matt Tucker committed
82 83 84 85 86 87 88 89
    }

    /**
     * Retrieve the port this server socket is bound to.
     *
     * @return the port the socket is bound to.
     */
    public int getPort() {
90
        return serverPort.getPort();
Matt Tucker's avatar
Matt Tucker committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    }

    /**
     * Unblock the thread and force it to terminate.
     */
    public void shutdown() {
        notTerminated = false;

        try {
            ServerSocket sSock = serverSocket;
            serverSocket = null;
            if (sSock != null) {
                sSock.close();
            }
        }
        catch (IOException e) {
            // we don't care, no matter what, the socket should be dead
        }

    }

    /**
     * About as simple as it gets.  The thread spins around an accept
     * call getting sockets and handing them to the SocketManager.
     */
    public void run() {
117 118 119 120 121
        while (notTerminated) {
            try {
                Socket sock = serverSocket.accept();
                if (sock != null) {
                    Log.debug("Connect " + sock.toString());
122
                    connManager.addSocket(sock, false, serverPort);
Matt Tucker's avatar
Matt Tucker committed
123
                }
124 125 126 127 128
            }
            catch (IOException ie) {
                if (notTerminated) {
                    Log.error(LocaleUtils.getLocalizedString("admin.error.accept"),
                            ie);
Matt Tucker's avatar
Matt Tucker committed
129 130
                }
            }
131 132 133
            catch (Exception e) {
                Log.error(LocaleUtils.getLocalizedString("admin.error.accept"), e);
            }
Matt Tucker's avatar
Matt Tucker committed
134
        }
135

Matt Tucker's avatar
Matt Tucker committed
136 137 138 139 140 141 142 143 144 145 146 147
        try {
            ServerSocket sSock = serverSocket;
            serverSocket = null;
            if (sSock != null) {
                sSock.close();
            }
        }
        catch (IOException e) {
            // we don't care, no matter what, the socket should be dead
        }
    }
}