001: package it.unimi.dsi.mg4j.index.remote;
002:
003: /*
004: * MG4J: Managing Gigabytes for Java
005: *
006: * Copyright (C) 2006-2007 Sebastiano Vigna
007: *
008: * This library is free software; you can redistribute it and/or modify it
009: * under the terms of the GNU Lesser General Public License as published by the Free
010: * Software Foundation; either version 2.1 of the License, or (at your option)
011: * any later version.
012: *
013: * This library is distributed in the hope that it will be useful, but
014: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
015: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
016: * for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023:
024: import it.unimi.dsi.fastutil.longs.AbstractLongList;
025: import it.unimi.dsi.fastutil.longs.LongList;
026: import it.unimi.dsi.Util;
027:
028: import java.io.EOFException;
029: import java.io.IOException;
030: import java.io.Serializable;
031: import java.net.Socket;
032: import java.net.SocketAddress;
033:
034: import org.apache.log4j.Logger;
035:
036: /** A class accessing remotely a {@link it.unimi.dsi.fastutil.longs.LongList}.
037: *
038: * @author Alessandro Arrabito
039: * @author Sebastiano Vigna
040: */
041: public class RemoteOffsetList extends AbstractLongList implements
042: Serializable {
043: static final long serialVersionUID = 2L;
044:
045: /** A remote server connection, lazily initialised at the first remote call. */
046: private transient RemoteIndexServerConnection connection;
047: /** The address of the socket assigned to the server thread. */
048: private SocketAddress address;
049: /** The size of the list, cached locally. */
050: private int size;
051:
052: /** Creates a new remote long list.
053: * @param addr the address of the socket assigned to the server thread.
054: * @param size the size of the list.
055: */
056: public RemoteOffsetList(final SocketAddress addr, final int size) {
057: address = addr;
058: this .size = size;
059: }
060:
061: public long getLong(int index) {
062: try {
063: if (connection == null)
064: connection = new RemoteIndexServerConnection(address,
065: IndexServer.GET_OFFSET_LIST);
066: connection.outputStream.writeInt(index);
067: connection.outputStream.flush();
068: return connection.inputStream.readLong();
069: } catch (IOException e) {
070: throw new RuntimeException(e);
071: }
072: }
073:
074: public int size() {
075: return size;
076: }
077:
078: public static class ServerThread extends
079: it.unimi.dsi.mg4j.index.remote.ServerThread {
080: private final static boolean DEBUG = false;
081: private final static Logger LOGGER = Util
082: .getLogger(ServerThread.class);
083: /** The remoted list. */
084: private final LongList list;
085:
086: public ServerThread(final Socket socket, final LongList list)
087: throws IOException {
088: super (socket);
089: this .list = list;
090: }
091:
092: public void run() {
093: try {
094: int index;
095: for (;;) {
096: index = inputStream.readInt();
097: if (DEBUG)
098: LOGGER.debug("Received request for index "
099: + index);
100: outputStream.writeLong(list.getLong(index));
101: outputStream.flush();
102: }
103: } catch (EOFException e) {
104: LOGGER.warn("The socket has been closed");
105: } catch (Exception e) {
106: LOGGER.fatal(e, e);
107: }
108: }
109: }
110: }
|