KickJava   Java API By Example, From Geeks To Geeks.

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


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: DxClient.java,v 1.3 2003/03/12 12:30:23 per_nyfelt Exp $
8

9 package org.ozoneDB.DxLib.net;
10
11 import java.io.*;
12 import java.net.*;
13 import org.ozoneDB.DxLib.*;
14 import org.ozoneDB.io.stream.ResolvingObjectInputStream;
15
16
17 /**
18  * DxClient stellt ein Ende einer Socketverbindung dar, an die DxCompatibles
19  * gesendet oder empfangen werden koennen. am anderen Ende der Verbindung
20  * sollte entweder ein DxServer oder DxMultiServer sein.
21  *
22  *
23  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
24  * @version $Revision: 1.3 $Date: 2003/03/12 12:30:23 $
25  */

26 public class DxClient extends DxObject {
27     
28     /**
29      * The size of the stream buffers. When all data fits into this buffer
30      * performance increases. TODO: change this into a configurable setting, so
31      * db users can change to their needs.
32      */

33     protected final static int buffSize = 8192;
34     
35     protected Socket sock;
36     
37     protected ObjectInputStream in;
38     
39     protected ObjectOutputStream out;
40     
41     
42     public DxClient() {
43     }
44     
45     
46     public DxClient( String JavaDoc host, int port ) throws IOException {
47         sock = new Socket( host, port );
48         init();
49         
50         onConnect();
51     }
52     
53     
54     public DxClient( Socket s ) throws IOException {
55         sock = s;
56         init();
57         
58         onConnect();
59     }
60     
61     
62     protected void init() throws IOException {
63         // this buffer size values seems to work for a localhost connections
64
// which is the fastest possible case; we rely on OS setting however
65
// since the kernel may better know what is a good buffer size; with
66
// many connections this may lead to memory issues
67

68         // sock.setSendBufferSize( buffSize * 10 );
69
// sock.setReceiveBufferSize( buffSize * 10 );
70
// System.out.println( sock.getSendBufferSize() + ", " + sock.getReceiveBufferSize() );
71

72         out = new ObjectOutputStream( new BufferedOutputStream( sock.getOutputStream(), buffSize ) );
73         // send the header data
74
out.flush();
75         
76         in = new ResolvingObjectInputStream( new BufferedInputStream( sock.getInputStream(), buffSize ) );
77     }
78     
79     
80     /**
81      * Diese Methode wird ausgefuehrt, wenn Verbindung zum Server aufgenommen
82      * wird. Sie kann ueberschrieben werden, um ein Verbindungsprotokoll zu
83      * implementieren.
84      */

85     public void onConnect() throws IOException {
86     }
87     
88     
89     /**
90      * Diese Methode wird analog zu onConnect() beim schliessen der Verbindung
91      * aufgenommen.
92      */

93     public void onDeconnect() throws IOException {
94     }
95     
96     
97     public void send( Object JavaDoc obj ) throws IOException {
98         try {
99             out.writeObject( obj );
100         }
101         finally {
102             out.flush();
103             out.reset();
104         }
105     }
106     
107     
108     public Object JavaDoc receive() throws IOException, ClassNotFoundException JavaDoc {
109         return in.readObject();
110     }
111     
112     
113     /**
114      * prueft ob daten im input stream liegen
115      */

116     public boolean objectAvailable() {
117         try {
118             return in.available() > 0;
119         } catch (IOException e) {
120             return false;
121         }
122     }
123     
124     
125     public synchronized void close() throws IOException {
126         onDeconnect();
127         // in.close();
128
in = null;
129         // out.flush();
130
// out.close();
131
out = null;
132         sock.close();
133         sock = null;
134     }
135     
136     
137     public ObjectInputStream inputStream() {
138         return in;
139     }
140     
141     
142     public ObjectOutputStream outputStream() {
143         return out;
144     }
145 }
146
Popular Tags