KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > DxLib > net > DxAsyncClient


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: DxAsyncClient.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
8

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 JavaDoc {
16     boolean cont;
17     Thread JavaDoc _thread;
18     
19     
20     private void init( boolean externalThread ) {
21         cont = true;
22         if (!externalThread) {
23             _thread = new Thread JavaDoc( this );
24             _thread.setPriority( Thread.NORM_PRIORITY );
25             _thread.setDaemon( true );
26             _thread.start();
27         }
28     }
29     
30     
31     public DxAsyncClient( String JavaDoc 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 JavaDoc thread() {
44         return _thread;
45     }
46     
47     
48     public void run() {
49         try {
50             while (cont) {
51                 handleEvent( receive() );
52             }
53         } catch (Exception JavaDoc 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 JavaDoc event );
69 }
70
Popular Tags