001package arcommon.net;
002
003import arcommon.MyThread;
004
005public abstract class LineServerThread extends MyThread {
006    private LineServer lineServer;
007
008    public LineServerThread(int port) {
009        lineServer = new LineServer(port);
010    }
011    
012    protected void doLoop() {
013        LineConnection connection = lineServer.getConnection();
014        if (!lineServer.isListening()) {
015            requestToStop();
016            onStopListen();
017            return ;
018        }
019        
020        if (connection == null) {
021            throw new RuntimeException("connection == null");
022        }
023        
024        onClientConnect(connection);
025    }
026    
027    protected abstract void onClientConnect(LineConnection connection);
028    protected abstract void onStopListen();
029    
030    public boolean isListening() {
031        return lineServer.isListening();
032    }
033    
034    public void close() {
035        lineServer.close();
036    }
037}