1 11 12 package org.jivesoftware.messenger.net; 13 14 import org.jivesoftware.messenger.ConnectionManager; 15 import org.jivesoftware.messenger.ServerPort; 16 import org.jivesoftware.util.LocaleUtils; 17 import org.jivesoftware.util.Log; 18 19 import java.io.IOException ; 20 import java.net.InetAddress ; 21 import java.net.ServerSocket ; 22 import java.net.Socket ; 23 24 30 public class SocketAcceptThread extends Thread { 31 32 35 public static final int DEFAULT_PORT = 5222; 36 37 40 public static final int DEFAULT_COMPONENT_PORT = 10015; 41 42 45 public static final int DEFAULT_SERVER_PORT = 5269; 46 47 50 private ServerPort serverPort; 51 52 55 private InetAddress bindInterface; 56 57 60 private boolean notTerminated = true; 61 62 65 ServerSocket serverSocket; 66 67 private ConnectionManager connManager; 68 69 public SocketAcceptThread(ConnectionManager connManager, ServerPort serverPort) 70 throws IOException { 71 super("Socket Listener at port " + serverPort.getPort()); 72 this.connManager = connManager; 73 this.serverPort = serverPort; 74 String interfaceName = serverPort.getInterfaceName(); 75 bindInterface = null; 76 if (interfaceName != null) { 77 if (interfaceName.trim().length() > 0) { 78 bindInterface = InetAddress.getByName(interfaceName); 79 } 80 } 81 serverSocket = new ServerSocket (serverPort.getPort(), -1, bindInterface); 82 } 83 84 89 public int getPort() { 90 return serverPort.getPort(); 91 } 92 93 98 public ServerPort getServerPort() { 99 return serverPort; 100 } 101 102 105 public void shutdown() { 106 notTerminated = false; 107 108 try { 109 ServerSocket sSock = serverSocket; 110 serverSocket = null; 111 if (sSock != null) { 112 sSock.close(); 113 } 114 } 115 catch (IOException e) { 116 } 118 119 } 120 121 125 public void run() { 126 while (notTerminated) { 127 try { 128 Socket sock = serverSocket.accept(); 129 if (sock != null) { 130 Log.debug("Connect " + sock.toString()); 131 connManager.addSocket(sock, false, serverPort); 132 } 133 } 134 catch (IOException ie) { 135 if (notTerminated) { 136 Log.error(LocaleUtils.getLocalizedString("admin.error.accept"), 137 ie); 138 } 139 } 140 catch (Exception e) { 141 Log.error(LocaleUtils.getLocalizedString("admin.error.accept"), e); 142 } 143 } 144 145 try { 146 ServerSocket sSock = serverSocket; 147 serverSocket = null; 148 if (sSock != null) { 149 sSock.close(); 150 } 151 } 152 catch (IOException e) { 153 } 155 } 156 } 157 | Popular Tags |