01: // You can redistribute this software and/or modify it under the terms of
02: // the Ozone Library License version 1 published by ozone-db.org.
03: //
04: // The original code and portions created by SMB are
05: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
06: //
07: // $Id: DxAsyncClient.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
08:
09: package org.ozoneDB.DxLib.net;
10:
11: import org.ozoneDB.DxLib.*;
12: import java.io.*;
13: import java.net.*;
14:
15: public abstract class DxAsyncClient extends DxClient implements
16: Runnable {
17: boolean cont;
18: Thread _thread;
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: public DxAsyncClient(String host, int port, boolean externalThread)
31: throws IOException {
32: super (host, port);
33: init(externalThread);
34: }
35:
36: public DxAsyncClient(Socket s, boolean externalThread)
37: throws IOException {
38: super (s);
39: init(externalThread);
40: }
41:
42: public Thread thread() {
43: return _thread;
44: }
45:
46: public void run() {
47: try {
48: while (cont) {
49: handleEvent(receive());
50: }
51: } catch (Exception e) {
52: System.out.println("DxAsyncClient: " + e);
53: }
54: }
55:
56: public synchronized void close() throws IOException {
57: cont = false;
58: if (_thread != null) {
59: _thread.stop();
60: }
61: super .close();
62: }
63:
64: public abstract void handleEvent(Object event);
65: }
|