1 9 package org.ozoneDB.DxLib.net; 10 11 import java.io.*; 12 import java.net.*; 13 14 15 public abstract class DxAsyncClient extends DxClient implements Runnable { 16 boolean cont; 17 Thread _thread; 18 19 20 private void init( boolean externalThread ) { 21 cont = true; 22 if (!externalThread) { 23 _thread = new Thread ( this ); 24 _thread.setPriority( Thread.NORM_PRIORITY ); 25 _thread.setDaemon( true ); 26 _thread.start(); 27 } 28 } 29 30 31 public DxAsyncClient( String host, int port, boolean externalThread ) throws IOException{ 32 super( host, port ); 33 init( externalThread ); 34 } 35 36 37 public DxAsyncClient( Socket s, boolean externalThread ) throws IOException{ 38 super( s ); 39 init( externalThread ); 40 } 41 42 43 public Thread thread() { 44 return _thread; 45 } 46 47 48 public void run() { 49 try { 50 while (cont) { 51 handleEvent( receive() ); 52 } 53 } catch (Exception e) { 54 System.out.println( "DxAsyncClient: " + e ); 55 } 56 } 57 58 59 public synchronized void close() throws IOException { 60 cont = false; 61 if (_thread != null) { 62 _thread.stop(); 63 } 64 super.close(); 65 } 66 67 68 public abstract void handleEvent( Object event ); 69 } 70 | Popular Tags |