001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.core;
025:
026: import java.io.*;
027:
028: import org.myoodb.*;
029: import org.myoodb.util.*;
030: import org.myoodb.exception.*;
031: import org.myoodb.core.command.*;
032:
033: abstract interface AbstractConnectionInterface {
034: public void send(Object obj) throws IOException;
035:
036: public Object receive(int timeout) throws IOException,
037: ClassNotFoundException, TimeoutException;
038:
039: public void close() throws IOException;
040:
041: public boolean isConnected();
042: }
043:
044: public abstract class AbstractConnection implements
045: AbstractConnectionInterface, java.lang.Comparable {
046: private static volatile int s_bufferSize = 8192;
047:
048: protected Long m_identifier;
049: protected AbstractDatabase m_db;
050:
051: protected static ObjectInputStream getObjectInputStream(
052: InputStream inputStream, AbstractDatabase db)
053: throws IOException {
054: return new FastObjectInputStream(new BufferedInputStream(
055: inputStream, getBufferSize()), db);
056: }
057:
058: protected static ObjectOutputStream getObjectOutputStream(
059: OutputStream outputStream) throws IOException {
060: return new FastObjectOutputStream(new BufferedOutputStream(
061: outputStream, getBufferSize()));
062: }
063:
064: public static void setBufferSize(int bufferSize) {
065: s_bufferSize = bufferSize;
066: }
067:
068: public static int getBufferSize() {
069: return s_bufferSize;
070: }
071:
072: public AbstractConnection(AbstractDatabase db, Long id) {
073: m_db = db;
074: m_identifier = id;
075: }
076:
077: protected ObjectInputStream getObjectInputStream(
078: InputStream inputStream) throws IOException {
079: return getObjectInputStream(inputStream, m_db);
080: }
081:
082: final public long getIdentifier() {
083: return m_identifier;
084: }
085:
086: final public AbstractDatabase getDatabase() {
087: return m_db;
088: }
089:
090: final public int hashCode() {
091: return (m_identifier != null) ? m_identifier.hashCode() : super
092: .hashCode();
093: }
094:
095: final public boolean equals(Object obj) {
096: return (m_identifier != null) ? m_identifier.equals(obj)
097: : super .equals(obj);
098: }
099:
100: final public int compareTo(Object obj) {
101: return (m_identifier != null)
102: && (obj instanceof AbstractConnection) ? m_identifier
103: .compareTo(((AbstractConnection) obj).m_identifier)
104: : -1;
105: }
106: }
|