1 17 package org.alfresco.filesys.smb.server; 18 19 import java.net.InetAddress ; 20 import java.net.ServerSocket ; 21 import java.net.SocketException ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 31 public abstract class SessionSocketHandler implements Runnable 32 { 33 35 protected static Log logger = LogFactory.getLog("org.alfresco.smb.protocol"); 36 37 39 protected static final int LISTEN_BACKLOG = 10; 40 41 43 private SMBServer m_server; 44 45 47 private int m_port; 48 private InetAddress m_bindAddr; 49 50 52 private ServerSocket m_srvSock; 53 54 56 private boolean m_debug; 57 58 60 private boolean m_shutdown; 61 62 64 private String m_name; 65 66 68 private static int m_sessId; 69 70 79 public SessionSocketHandler(String name, SMBServer srv, int port, InetAddress bindAddr, boolean debug) 80 { 81 m_name = name; 82 m_server = srv; 83 m_port = port; 84 m_bindAddr = bindAddr; 85 m_debug = debug; 86 } 87 88 95 public SessionSocketHandler(String name, SMBServer srv, boolean debug) 96 { 97 m_name = name; 98 m_server = srv; 99 m_debug = debug; 100 } 101 102 107 public final String getName() 108 { 109 return m_name; 110 } 111 112 117 protected final SMBServer getServer() 118 { 119 return m_server; 120 } 121 122 127 protected final int getPort() 128 { 129 return m_port; 130 } 131 132 137 protected final boolean hasBindAddress() 138 { 139 return m_bindAddr != null ? true : false; 140 } 141 142 145 protected final InetAddress getBindAddress() 146 { 147 return m_bindAddr; 148 } 149 150 155 protected final synchronized int getNextSessionId() 156 { 157 return m_sessId++; 158 } 159 160 165 protected final boolean hasDebug() 166 { 167 return m_debug; 168 } 169 170 175 protected final ServerSocket getSocket() 176 { 177 return m_srvSock; 178 } 179 180 185 protected final void setSocket(ServerSocket sock) 186 { 187 m_srvSock = sock; 188 } 189 190 195 protected final boolean hasShutdown() 196 { 197 return m_shutdown; 198 } 199 200 203 protected final void clearShutdown() 204 { 205 m_shutdown = false; 206 } 207 208 211 public void shutdownRequest() 212 { 213 214 216 m_shutdown = true; 217 218 try 219 { 220 221 223 if (m_srvSock != null) 224 m_srvSock.close(); 225 } 226 catch (SocketException ex) 227 { 228 } 229 catch (Exception ex) 230 { 231 } 232 } 233 234 239 public void initialize() throws Exception 240 { 241 242 244 ServerSocket srvSock = null; 245 246 if (hasBindAddress()) 247 srvSock = new ServerSocket (getPort(), LISTEN_BACKLOG, getBindAddress()); 248 else 249 srvSock = new ServerSocket (getPort(), LISTEN_BACKLOG); 250 setSocket(srvSock); 251 252 254 if (logger.isDebugEnabled() && hasDebug()) 255 logger.debug("[SMB] Binding " + getName() + " session handler to local address : " 256 + (hasBindAddress() ? getBindAddress().getHostAddress() : "ALL")); 257 } 258 259 262 public abstract void run(); 263 264 269 public String toString() 270 { 271 StringBuffer str = new StringBuffer (); 272 273 str.append("["); 274 str.append(getName()); 275 str.append(","); 276 str.append(getServer().getServerName()); 277 str.append(","); 278 str.append(getBindAddress() != null ? getBindAddress().getHostAddress() : "<All>"); 279 str.append(":"); 280 str.append(getPort()); 281 str.append("]"); 282 283 return str.toString(); 284 } 285 } 286 | Popular Tags |