2008年(3500)
分类:
2008-05-04 19:28:09
Toolkit kit = Toolkit.getDefaultToolkit(); Image image; image = kit.createImage (new XImageSource (_reply)); image = kit.createImage (new XImageSource (_post)); image = kit.createImage (new XImageSource (_reload)); image = kit.createImage (new XImageSource (_catchup)); image = kit.createImage (new XImageSource (_back10)); image = kit.createImage (new XImageSource (_reset)); image = kit.createImage (new XImageSource (_faq));
Image image; image = getImage ("reply.gif"); image = getImage ("post.gif"); image = getImage ("reload.gif"); image = getImage ("catchup.gif"); image = getImage ("back10.gif"); image = getImage ("reset.gif"); image = getImage ("faq.gif");
ImageIcon icon = new ImageIcon ( kit.createImage (new XImageSource (_reply))); JButton button = new JButton (icon, "Reply");
Worker worker; WorkerThread w; for ( int i = 0; i < _max; i ) { worker = (Worker)_workerClass.newInstance(); w = new WorkerThread ("Worker#" i, worker); w.start(); _waiting.push (w); }
synchronized void wake (Object data) { _data = data; notify(); } synchronized public void run(){ boolean stop = false; while (!stop){ if ( _data == null ){ try{ wait(); }catch (InterruptedException e){ e.printStackTrace(); continue; } } if ( _data != null ){ _worker.run(_data); } _data = null; stop = !(_push (this)); } }
public void performWork (Object data) throws InstantiationException{ WorkerThread w = null; synchronized (_waiting){ if ( _waiting.empty() ){ try{ w = new WorkerThread ("additional worker", (Worker)_workerClass.newInstance()); w.start(); }catch (Exception e){ throw new InstantiationException ( "Problem creating instance of Worker.class: " e.getMessage()); } }else{ w = (WorkerThread)_waiting.pop(); } } w.wake (data); }为了表明线程池类代码在运转,参看下一节中关于HttpServer类的防火墙隧道技术的讨论。
try{8888 _pool = new Pool (poolSize, HttpServerWorker.class); }catch (Exception e){ e.printStackTrace(); throw new InternalError (e.getMessage()); }
try{ Socket s = _serverSocket.accept(); Hashtable data = new Hashtable(); data.put ("Socket", s); data.put ("HttpServer", this); _pool.performWork (data); }catch (Exception e){ e.printStackTrace(); }88
synchronized public byte[] send (byte data[]) throws IOException { byte buffer[]; // Establish a connection URL url = new URL (_url); URLConnection connection = url.openConnection(); connection.setUseCaches (false); connection.setDoOutput(true); // Write out the data DataOutputStream dataOut = new DataOutputStream ( new BufferedOutputStream ( connection.getOutputStream())); dataOut.writeInt (HttpClient.DATA); dataOut.writeInt (data.length); dataOut.write (data); dataOut.flush(); dataOut.close(); int length; DataInputStream input = new DataInputStream ( new BufferedInputStream ( connection.getInputStream())); int type = input.readInt(); if ( type == HttpClient.DATA ) { length = input.readInt(); buffer = new byte[length]; input.readFully (buffer); } else { buffer = null; throw new IOException ("Unknown Response Type"); } input.close(); return buffer; }
public void run(Object data) { Socket socket = (Socket)((Hashtable)data).get ("Socket"); HttpServer server = (HttpServer)((Hashtable)data).get ("HttpServer"); try { DataInputStream input = new DataInputStream (new BufferedInputStream(socket.getInputStream())); String line = input.readLine(); if (line.toUpperCase().startsWith ("POST") ){ for ( ; (line=input.readLine()).length() > 0; ) ; int type = input.readInt() switch (type){ case HttpClient.DATA :{ int length = input.readInt(); byte buffer[] = new byte[length]; input.readFully (buffer); ByteArrayOutputStream dataOut = new ByteArrayOutputStream(); server.notifyListener (new ByteArrayInputStream(buffer), dataOut); DataOutputStream output = new DataOutputStream( new BufferedOutputStream(socket.getOutputStream())); output.writeBytes (_httpResponse); output.writeInt (HttpClient.DATA); output.writeInt (dataOut.toByteArray().length); output.write (dataOut.toByteArray()); output.flush(); input.close(); output.close(); socket.close(); break; } default :{ System.err.println ("Invalid type: " type); } } } else { System.err.println ("Invalid HTTP request: " line); } } catch (IOException e) { e.printStackTrace(); try { socket.close(); } catch (Exception e) { } } }
connection = _sendPending(); DataOutputStream output = new DataOutputStream (connection.getOutputStream()); output.writeInt (PENDING); output.flush(); output.close(); output = null; int code = ((HttpURLConnection)connection).getResponseCode(); switch (code) { case 200: { // HTTP_OK DataInputStream input = new DataInputStream(connection.getInputStream()); length = input.readInt(); byte buffer[] = new byte[length]; if ( length >= 0 ) { input.readFully(buffer); _listener.service( new ByteArrayInputStream(buffer)); } else { System.err.println("Invalid length: " length); } buffer = null; input.close(); input = null; break; } case 504: { // HTTP_GATEWAY_TIMEOUT connection = _sendPending(); break; } default: { // OTHER HTTP_SERVER ERRORS System.out.println ("Invalid code:" code); } }
case HttpClient.PENDING :{ server.addClient (socket); break; }
synchronized void addClient (Socket s){ _clients.addElement(s); }
synchronized public void send (byte data[]) { Enumeration elements = _clients.elements(); while ( elements.hasMoreElements() ) { Socket s = (Socket)elements.nextElement(); try { DataOutputStream output = new DataOutputStream(new BufferedOutputStream(s.getOutputStream())); int length; writeResponse (output); output.writeInt (data.length); output.write (data); output.flush(); output.close(); } catch (IOException e) { e.printStackTrace(); } finally{ try{ s.close(); }catch (IOException e){ e.printStackTrace(); } } } _clients.removeAllElements(); }