All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.it.unimi.dsi.big.mg4j.index.remote.RemoteIndexServerConnection Maven / Gradle / Ivy

Go to download

MG4J (Managing Gigabytes for Java) is a free full-text search engine for large document collections written in Java. The big version is a fork of the original MG4J that can handle more than 2^31 terms and documents.

The newest version!
package it.unimi.dsi.big.mg4j.index.remote;

/*		 
 * MG4J: Managing Gigabytes for Java (big)
 *
 * Copyright (C) 2006-2011 Sebastiano Vigna 
 *
 *  This library is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as published by the Free
 *  Software Foundation; either version 3 of the License, or (at your option)
 *  any later version.
 *
 *  This library is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, see .
 *
 */

import it.unimi.dsi.Util;
import it.unimi.dsi.fastutil.io.FastBufferedInputStream;
import it.unimi.dsi.fastutil.io.FastBufferedOutputStream;

import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;

import org.apache.log4j.Logger;

/** An index server connection.
 * 
 * 

Instances of this class provide a convenient way to gather all fields * related to a server connection (streams, socket, etc.). * * @author Alessandro Arrabito * @author Sebastiano Vigna */ public class RemoteIndexServerConnection implements Closeable { private static final Logger LOGGER = Util.getLogger( RemoteIndexServerConnection.class ); public final Socket socket; /** The stream from the server. */ public final DataInputStream inputStream; /** The stream towards the server. */ public final DataOutputStream outputStream; /** Creates a remote index-server connection from a given address and command. * *

Immediately after establishing the connection, command is sent * to the server, so the stream will be connected to a thread providing a specific service. See {@link IndexServer}. * * @param address the server address. * @param command the first command sent to the server ({@link IndexServer#GET_CLIENT_INPUT_STREAM}, * {@link IndexServer#GET_INDEX_READER}, …). */ public RemoteIndexServerConnection( final SocketAddress address, final byte command ) throws IOException { socket = new Socket(); socket.connect( address ); inputStream = new DataInputStream( new FastBufferedInputStream( socket.getInputStream() ) ); outputStream = new DataOutputStream( new FastBufferedOutputStream( socket.getOutputStream() ) ); outputStream.writeByte( command ); outputStream.flush(); } /** Closes the underlying socket. */ public synchronized void close() throws IOException { socket.close(); } protected void finalize() throws Throwable { try { if ( ! socket.isClosed() ) { LOGGER.warn( "This " + this.getClass().getName() + " [" + toString() + "] should have been closed." ); close(); } } finally { super.finalize(); } } public String toString() { return this.getClass().getName() + ":" + socket.toString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy