| Home >> All >> com >> telefonicasoluciones >> search >> [ server Javadoc ] |
Source code: com/telefonicasoluciones/search/server/HLServer.java
1 package com.telefonicasoluciones.search.server; 2 3 4 5 import java.io.*; 6 7 import java.net.*; 8 9 10 11 public class HLServer { 12 13 private HLThreadFIFO idleWorkers; 14 15 private static final int DEFAULT_PORT = 8421; 16 17 private HLWorker[] workers; 18 19 private ServerSocket ss; 20 21 private static File config; 22 23 private Thread internalThread; 24 25 private volatile boolean noStopRequested; 26 27 28 29 public HLServer(int port, int numberOfWorkers) throws IOException { 30 31 if (port == 0) port = DEFAULT_PORT; 32 33 HLHandler hlh = null; 34 35 try { 36 37 hlh = new HLHandler(new HLConfig(config.getCanonicalPath())); 38 39 } catch(IOException _ex) { 40 41 System.out.println("Config file not found ("+config.getAbsolutePath()+")"); 42 43 System.exit(1); 44 45 } catch(HLConfigException _ex) { 46 47 System.out.println("Config file not found ("+config.getAbsolutePath()+")"); 48 49 System.exit(1); 50 51 } catch(Exception _ex) { 52 53 System.out.println("Config file not found ("+config.getAbsolutePath()+")"); 54 55 System.exit(1); 56 57 } 58 59 /* Permite un máximo de 10 sockets 60 61 * en cola. 62 63 */ 64 65 ss = new ServerSocket(port, 10); 66 67 numberOfWorkers = Math.max(1, numberOfWorkers); 68 69 idleWorkers = new HLThreadFIFO(numberOfWorkers); 70 71 workers = new HLWorker[numberOfWorkers]; 72 73 for (int i=numberOfWorkers; --i>=0; ) { 74 75 /* 76 77 * Cada worker tiene una referencia a FIFO 78 79 * podrá añadirse a sí mismo cuando esté listo 80 81 * para gestionar una petición. 82 83 */ 84 85 workers[i] = new HLWorker(idleWorkers, hlh); 86 87 } 88 89 noStopRequested = true; 90 91 Runnable r = new Runnable() { 92 93 public void run() { 94 95 try { 96 97 runWork(); 98 99 } catch (Exception _ex) { 100 101 _ex.printStackTrace(); 102 103 } 104 105 } 106 107 }; 108 109 internalThread = new Thread(r); 110 111 internalThread.start(); 112 113 } 114 115 116 117 private void runWork() { 118 119 System.out.println("\r\nARTLIGHT 1.4 Server\r\n"); 120 121 try { 122 123 System.out.println("Config file: "+config.getCanonicalPath()); 124 125 } catch(IOException _ex) {} 126 127 System.out.println("Total Memory: "+Runtime.getRuntime().totalMemory()); 128 129 System.out.println("Free Memory: "+Runtime.getRuntime().freeMemory()); 130 131 while (noStopRequested) { 132 133 try { 134 135 Socket s = ss.accept(); 136 137 if (idleWorkers.isEmpty()) { 138 139 BufferedWriter writer = 140 141 new BufferedWriter( 142 143 new OutputStreamWriter( 144 145 s.getOutputStream())); 146 147 writer.write("Artlight1.4 server is too busy\r\n"); 148 149 writer.flush(); 150 151 writer.close(); 152 153 writer = null; 154 155 } else { 156 157 HLWorker worker = (HLWorker) idleWorkers.remove(); 158 159 worker.processRequest(s); 160 161 } 162 163 } catch (IOException _ex) { 164 165 if (noStopRequested) { 166 167 _ex.printStackTrace(); 168 169 } 170 171 } catch (InterruptedException _ex) { 172 173 Thread.currentThread().interrupt(); 174 175 } 176 177 } 178 179 if(ss!=null) { 180 181 try { ss.close(); } catch (IOException _ex) {} 182 183 ss = null; 184 185 } 186 187 } 188 189 public void stopRequest() { 190 191 noStopRequested = false; 192 193 internalThread.interrupt(); 194 195 for(int i=workers.length; --i>=0; ) { 196 197 workers[i].stopRequest(); 198 199 } 200 201 } 202 203 public boolean isAlive() { 204 205 return internalThread.isAlive(); 206 207 } 208 209 public static void main(String[] args) { 210 211 int port = 0; 212 213 int workers = 10; 214 215 for(int i=0; i<args.length; i++) { 216 217 if(args[i].indexOf("port")!=-1) { 218 219 try { 220 221 port = Integer.parseInt(args[i].substring(args[i].indexOf("=")+1,args[i].length())); 222 223 } catch (NumberFormatException e) {} 224 225 } else if(args[i].indexOf("config-file")!=-1) { 226 227 config = new File(args[i].substring(args[i].indexOf("=")+1,args[i].length())); 228 229 if(!config.exists()) { 230 231 System.out.println("\r\nConfiguration file not found ("+config.getAbsolutePath()+")"); 232 233 break; 234 235 } 236 237 } else if(args[i].indexOf("workers")!=-1) { 238 239 try { 240 241 workers = Integer.parseInt(args[i].substring(args[i].indexOf("=")+1,args[i].length())); 242 243 } catch (NumberFormatException _ex) {} 244 245 } else if(args[i].equals("start")) { 246 247 try { 248 249 new HLServer(port,workers); 250 251 } catch (IOException _ex) { 252 253 _ex.printStackTrace(System.out); 254 255 } 256 257 } else if(args[i].equals("stop")) { 258 259 // nothing yet 260 261 } 262 263 } 264 265 } 266 267 } 268