1 9 package org.ozoneDB.DxLib.net; 10 11 import java.io.*; 12 import java.net.*; 13 import org.ozoneDB.DxLib.*; 14 15 25 public abstract class DxMultiServer extends DxListBag { 26 27 protected ServerSocket serverSocket; 28 29 protected Acceptor acceptor; 30 31 protected Thread acceptThread; 32 33 protected ThreadGroup threadGroup; 34 35 36 public DxMultiServer( int port ) throws IOException{ 37 serverSocket = new ServerSocket( port ); 38 acceptor = new Acceptor( this ); 39 threadGroup = new ThreadGroup ( getClass().getName() ); 40 acceptThread = newThread( acceptor ); 41 } 42 43 44 47 public ThreadGroup threadGroup() { 48 return threadGroup; 49 } 50 51 52 59 public Thread newThread( Runnable run ) { 60 return new Thread ( threadGroup(), run ); 61 } 62 63 64 69 public synchronized DxMultiServerClient newClient( Socket sock ) { 70 try { 71 return new DxMultiServerClient( sock, this ); 72 } catch (Exception e) { 73 return null; 74 } 75 } 76 77 78 79 public void accept() { 80 if (!acceptThread.isAlive()) { 81 acceptThread.start(); 82 } 83 } 84 85 86 public void close() throws IOException { 87 acceptThread.stop(); 88 DxIterator it = iterator(); 89 while (it.next() != null) { 90 ((DxMultiServerClient)it.object()).close(); 91 } 92 serverSocket.close(); 93 } 94 95 96 public synchronized void removeClient( DxMultiServerClient client ) { 97 DxIterator it = iterator(); 98 DxMultiServerClient cl; 99 while ((cl = (DxMultiServerClient)it.next()) != null) { 100 if (cl == client) { 101 it.removeObject(); 102 cl.close(); 103 return; 104 } 105 } 106 throw new RuntimeException ( "removeClient(): no such client." ); 107 } 108 109 110 115 public abstract void handleClientEvent( DxMultiServerClient client, Object event ); 116 117 118 public abstract void handleClientException( DxMultiServerClient client, Exception e ); 119 } 120 | Popular Tags |