1 65 66 67 package org.hsqldb; 68 69 import java.io.BufferedOutputStream ; 70 import java.io.DataInputStream ; 71 import java.io.IOException ; 72 import java.net.Socket ; 73 74 import org.hsqldb.lib.ArrayUtil; 75 import org.hsqldb.rowio.RowInputBinary; 76 import org.hsqldb.rowio.RowOutputBinary; 77 78 83 102 class ServerConnection implements Runnable { 103 104 boolean keepAlive; 105 private String user; 106 private String password; 107 int dbID; 108 private volatile Session session; 109 private Socket socket; 110 private Server server; 111 private DataInputStream dataInput; 112 private BufferedOutputStream dataOutput; 113 private static int mCurrentThread = 0; 114 private int mThread; 115 static final int BUFFER_SIZE = 0x1000; 116 final byte[] mainBuffer = new byte[BUFFER_SIZE]; 117 RowOutputBinary rowOut = new RowOutputBinary(BUFFER_SIZE); 118 RowInputBinary rowIn = new RowInputBinary(rowOut); 119 Thread runnerThread; 120 121 130 ServerConnection(Socket socket, Server server) { 131 132 this.socket = socket; 133 this.server = server; 134 135 synchronized (ServerConnection.class) { 136 mThread = mCurrentThread++; 137 } 138 139 synchronized (server.serverConnSet) { 140 server.serverConnSet.add(this); 141 } 142 } 143 144 148 void signalClose() { 149 150 keepAlive = false; 151 152 if (!Thread.currentThread().equals(runnerThread)) { 153 close(); 154 } 155 } 156 157 160 private void close() { 161 162 if (session != null) { 163 session.close(); 164 } 165 166 session = null; 167 168 try { 170 socket.close(); 171 } catch (IOException e) {} 172 173 synchronized (server.serverConnSet) { 174 server.serverConnSet.remove(this); 175 } 176 } 177 178 181 private void init() { 182 183 runnerThread = Thread.currentThread(); 184 keepAlive = true; 185 186 try { 187 socket.setTcpNoDelay(true); 188 189 dataInput = new DataInputStream (socket.getInputStream()); 190 dataOutput = new BufferedOutputStream (socket.getOutputStream()); 191 192 Result resultIn = Result.read(rowIn, dataInput); 193 Result resultOut; 194 195 try { 196 dbID = server.getDBID(resultIn.subSubString); 197 user = resultIn.getMainString(); 198 password = resultIn.getSubString(); 199 200 if (!server.isSilent()) { 201 server.printWithThread(mThread 202 + ":trying to connect user " 203 + user); 204 } 205 206 session = DatabaseManager.newSession(dbID, 207 resultIn.getMainString(), 208 resultIn.getSubString()); 209 resultOut = new Result(ResultConstants.UPDATECOUNT); 210 resultOut.databaseID = session.getDatabase().databaseID; 211 resultOut.sessionID = session.getId(); 212 } catch (HsqlException e) { 213 session = null; 214 resultOut = new Result(e, null); 215 } catch (RuntimeException e) { 216 session = null; 217 resultOut = new Result(e, null); 218 } 219 220 Result.write(resultOut, rowOut, dataOutput); 221 222 return; 223 } catch (Exception e) { 224 server.printWithThread(mThread + ":couldn't connect " + user); 225 } 226 227 close(); 228 } 229 230 234 public void run() { 235 236 init(); 237 238 if (session != null) { 239 try { 240 while (keepAlive) { 241 Result resultIn = Result.read(rowIn, dataInput); 242 243 server.printRequest(mThread, resultIn); 244 245 Result resultOut; 246 247 if (resultIn.mode == ResultConstants.HSQLRESETSESSION) { 248 resultOut = resetSession(); 249 } else { 250 resultOut = session.execute(resultIn); 251 } 252 253 Result.write(resultOut, rowOut, dataOutput); 254 rowOut.setBuffer(mainBuffer); 255 rowIn.resetRow(mainBuffer.length); 256 } 257 } catch (IOException e) { 258 259 server.printWithThread(mThread + ":disconnected " + user); 261 } catch (HsqlException e) { 262 263 server.printStackTrace(e); 265 } 266 267 close(); 268 } 269 } 270 271 275 private Result resetSession() { 276 277 Result resultOut; 278 279 if (!server.isSilent()) { 280 server.printWithThread(mThread + ":trying to connect user " 281 + user); 282 } 283 284 try { 285 session.close(); 286 287 session = DatabaseManager.newSession(dbID, user, password); 288 resultOut = new Result(ResultConstants.UPDATECOUNT); 289 resultOut.databaseID = session.getDatabase().databaseID; 290 resultOut.sessionID = session.getId(); 291 } catch (HsqlException e) { 292 session = null; 293 resultOut = new Result(e, null); 294 } 295 296 return resultOut; 297 } 298 299 306 String getConnectionThreadName() { 307 return "HSQLDB Connection @" + Integer.toString(hashCode(), 16); 308 } 309 } 310 | Popular Tags |