1 24 25 package com.mckoi.database.jdbcserver; 26 27 import com.mckoi.database.DatabaseSystem; 28 import com.mckoi.database.Database; 29 import com.mckoi.debug.*; 30 import java.net.ServerSocket ; 31 import java.net.Socket ; 32 import java.net.InetAddress ; 33 import java.io.IOException ; 34 import java.util.HashMap ; 35 import java.util.ResourceBundle ; 36 37 43 44 public final class TCPServer { 45 46 50 private Database database; 51 52 56 private ConnectionPoolServer connection_pool; 57 58 61 private ServerSocket server_socket; 62 63 66 private InetAddress address; 67 68 71 private int port; 72 73 76 private String connection_pool_model; 77 78 81 public TCPServer(Database database) { 82 this.database = database; 83 } 84 85 88 public final DebugLogger Debug() { 89 return database.Debug(); 90 } 91 92 95 public int getJDBCPort() { 96 return port; 97 } 98 99 104 public boolean checkAvailable(InetAddress bind_address, int tcp_port) { 105 109 int port = tcp_port; 110 114 try { 115 server_socket = new ServerSocket (port, 50, bind_address); 117 server_socket.close(); 118 } 119 catch (IOException e) { 120 return false; 122 } 123 124 return true; 125 } 126 127 128 132 public void start(InetAddress bind_address, int tcp_port, 133 String connection_pool_model) { 134 135 this.address = bind_address; 136 this.port = tcp_port; 137 this.connection_pool_model = connection_pool_model; 138 139 if (connection_pool_model.equals("multi_threaded")) { 159 this.connection_pool = new MultiThreadedConnectionPoolServer(database); 160 } 161 else if (connection_pool_model.equals("single_threaded")) { 162 this.connection_pool = new SingleThreadedConnectionPoolServer(database); 163 } 164 165 try { 166 server_socket = new ServerSocket (port, 50, bind_address); 168 server_socket.setSoTimeout(0); 169 } 170 catch (IOException e) { 171 Debug().writeException(e); 172 Debug().write(Lvl.ERROR, this, 173 "Unable to start a server socket on port: " + port); 174 throw new Error (e.getMessage()); 175 } 176 177 Thread listen_thread = new Thread () { 179 public void run() { 180 try { 181 while(true) { 183 Socket s = server_socket.accept(); 184 portConnection(s); 185 } 186 } 187 catch (IOException e) { 188 Debug().writeException(Lvl.WARNING, e); 189 Debug().write(Lvl.WARNING, this, "Socket listen thread died."); 190 } 191 } 192 }; 193 194 listen_thread.setName("Mckoi - TCP/IP Socket Accept"); 196 197 listen_thread.start(); 198 199 } 200 201 204 private void portConnection(Socket socket) throws IOException { 205 String host_string = "TCP/" + socket.getInetAddress().getHostAddress() + 208 ":" + socket.getPort() + "@" + 209 socket.getLocalAddress().getHostAddress() + ":" + 210 socket.getLocalPort(); 211 JDBCDatabaseInterface db_interface = 216 new JDBCDatabaseInterface(database, host_string); 217 TCPJDBCServerConnection connection = 218 new TCPJDBCServerConnection(db_interface, socket, Debug()); 219 connection_pool.addConnection(connection); 222 } 223 224 227 public void close() { 228 if (server_socket != null) { 229 try { 230 server_socket.close(); 231 } 232 catch (IOException e) { 233 Debug().write(Lvl.ERROR, this, "Error closing JDBC Server."); 234 Debug().writeException(e); 235 } 236 } 237 connection_pool.close(); 238 } 239 240 243 public String toString() { 244 StringBuffer buf = new StringBuffer (); 245 buf.append("TCP JDBC Server ("); 246 buf.append(connection_pool_model); 247 buf.append(") on "); 248 if (address != null) { 249 buf.append(address.getHostAddress()); 250 buf.append(" "); 251 } 252 buf.append("port: "); 253 buf.append(getJDBCPort()); 254 return new String (buf); 255 } 256 257 } 258 | Popular Tags |