01: /*
02: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
03:
04: This program is free software; you can redistribute it and/or
05: modify it under the terms of the GNU General Public License
06: as published by the Free Software Foundation; either version 2
07: of the License, or (at your option) any later version.
08:
09: This program is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with this program; if not, write to the Free Software
16: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17: */
18:
19: package org.homedns.dade.jcgrid.util;
20:
21: import java.io.*;
22: import java.net.*;
23: import java.util.zip.*;
24:
25: import org.homedns.dade.jcgrid.message.*;
26:
27: public class GridMessageFlatChannel implements GridMessageChannel {
28: private Socket socket;
29: private ObjectInputStream objIn;
30: private ObjectOutputStream objOut;
31:
32: public GridMessageFlatChannel(Socket s, boolean isServer)
33: throws IOException {
34: socket = s;
35:
36: InputStream is = socket.getInputStream();
37: OutputStream os = socket.getOutputStream();
38:
39: if (isServer) {
40: objIn = new ObjectInputStream(is);
41: objOut = new ObjectOutputStream(os);
42: } else {
43: objOut = new ObjectOutputStream(os);
44: objIn = new ObjectInputStream(is);
45: }
46: }
47:
48: public GridMessage recv() throws IOException,
49: ClassNotFoundException {
50: GridMessage msg = (GridMessage) objIn.readObject();
51:
52: return msg;
53: }
54:
55: public GridMessage recv(int timeout) throws IOException,
56: ClassNotFoundException {
57: socket.setSoTimeout(timeout);
58: GridMessage msg = (GridMessage) objIn.readObject();
59: socket.setSoTimeout(0);
60:
61: return msg;
62: }
63:
64: public void send(GridMessage msg) throws IOException,
65: ClassNotFoundException {
66: objOut.writeObject(msg);
67: objOut.flush();
68:
69: // In order to avoid a well know out of memory problem
70: // of ObjectOutputStream class
71:
72: objOut.reset();
73: }
74:
75: public void close() throws IOException {
76: objIn.close();
77: objOut.close();
78: socket.close();
79: }
80: }
|