001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2002-2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.common.net;
020:
021: import java.io.*;
022: import java.net.*;
023:
024: /**
025: * An object connection
026: */
027: public class ObjectConnection {
028: //-- attributes
029: private Socket socket;
030: private OutputStream output;
031: private InputStream input;
032: private ObjectListeningThread listener;
033:
034: /**
035: * Constructor
036: *
037: * @param socket the underlying socket
038: */
039: public ObjectConnection(Socket socket) {
040: this .listener = new ObjectListeningThread(this );
041: this .socket = socket;
042: try {
043: this .output = socket.getOutputStream();
044: this .input = socket.getInputStream();
045: } catch (Exception e) {
046: }
047: }
048:
049: /**
050: * Add an ObjectListener to this connection
051: *
052: * @param ol the listener
053: */
054: public void addObjectListener(ObjectListener ol) {
055: this .listener.addObjectListener(ol);
056: }
057:
058: /**
059: * Start the listening thread
060: */
061: public void listen() {
062: this .listener.start();
063: }
064:
065: /**
066: * Write an object
067: *
068: * @param o the object to write
069: */
070: public void write(Serializable o) throws IOException {
071: byte[] data = ObjectZipper.objectToBytes(o);
072: byte[] size = intToBytes(data.length);
073:
074: this .output.write(size);
075: this .output.write(data);
076: this .output.flush();
077: }
078:
079: /**
080: * Check if we have something to read
081: *
082: * @return true if we can read
083: */
084: public boolean readyToRead() {
085: try {
086: return this .socket.getInputStream().available() > 0;
087: } catch (Exception e) {
088: e.printStackTrace();
089: }
090:
091: return false;
092: }
093:
094: /**
095: * Read an object
096: *
097: * @return the object read
098: */
099: public Serializable read() throws IOException,
100: ClassNotFoundException {
101: byte[] size = new byte[4];
102: this .input.read(size);
103:
104: byte[] data = new byte[bytesToInt(size)];
105:
106: int length = 0;
107: while (length < data.length)
108: length += this .input.read(data, length, data.length
109: - length);
110:
111: return ObjectZipper.bytesToObject(data);
112: }
113:
114: /**
115: * Read a string
116: *
117: * @return the string read
118: */
119: public String readString() throws IOException,
120: ClassNotFoundException {
121: return (String) this .read();
122: }
123:
124: /**
125: * Close this connection
126: */
127: public void close() {
128: this .listener.close();
129:
130: try {
131: output.close();
132: input.close();
133: socket.close();
134: } catch (Exception e) {
135: }
136: }
137:
138: //-- utilities
139:
140: /**
141: * Convert 1 int to 4 bytes
142: */
143: protected static byte[] intToBytes(int value) {
144: byte[] bytes = new byte[4];
145:
146: for (int i = 0; i < bytes.length; i++) {
147: int offset = (bytes.length - i - 1) * 8;
148: int mask = 0xff << offset;
149: bytes[i] = (byte) (value >>> offset);
150: }
151:
152: return bytes;
153: }
154:
155: /**
156: * Convert 4 bytes to 1 int
157: */
158: protected static int bytesToInt(byte[] bytes) {
159: int value = 0;
160:
161: for (int i = 0; i < bytes.length; i++) {
162: value = value << 8;
163: value += bytes[i] & 0xff;
164: }
165:
166: return value;
167: }
168: }
|