disapproval of revision '7ed18fd4c3a5430150a2d76bfe202bc491115974'
This commit is contained in:
@ -2,7 +2,6 @@ package net.i2p.client.streaming;
|
||||
|
||||
import java.net.ConnectException;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import net.i2p.I2PException;
|
||||
|
||||
/**
|
||||
@ -10,7 +9,6 @@ import net.i2p.I2PException;
|
||||
*
|
||||
*/
|
||||
public interface I2PServerSocket {
|
||||
|
||||
/**
|
||||
* Closes the socket.
|
||||
*/
|
||||
@ -26,21 +24,8 @@ public interface I2PServerSocket {
|
||||
* @throws I2PException if there is a problem with reading a new socket
|
||||
* from the data available (aka the I2PSession closed, etc)
|
||||
* @throws ConnectException if the I2PServerSocket is closed
|
||||
* @throws SocketTimeoutException
|
||||
*/
|
||||
public I2PSocket accept() throws I2PException, ConnectException, SocketTimeoutException;
|
||||
|
||||
/**
|
||||
* Set Sock Option accept timeout
|
||||
* @param x
|
||||
*/
|
||||
public void setSoTimeout(long x);
|
||||
|
||||
/**
|
||||
* Get Sock Option accept timeout
|
||||
* @return timeout
|
||||
*/
|
||||
public long getSoTimeout();
|
||||
public I2PSocket accept() throws I2PException, ConnectException;
|
||||
|
||||
/**
|
||||
* Access the manager which is coordinating the server socket
|
||||
|
@ -17,33 +17,19 @@ import net.i2p.util.Log;
|
||||
*
|
||||
*/
|
||||
class I2PServerSocketImpl implements I2PServerSocket {
|
||||
|
||||
private final static Log _log = new Log(I2PServerSocketImpl.class);
|
||||
private I2PSocketManager mgr;
|
||||
/** list of sockets waiting for the client to accept them */
|
||||
private List pendingSockets = Collections.synchronizedList(new ArrayList(4));
|
||||
|
||||
/** have we been closed */
|
||||
private volatile boolean closing = false;
|
||||
|
||||
/** lock on this when accepting a pending socket, and wait on it for notification of acceptance */
|
||||
private Object socketAcceptedLock = new Object();
|
||||
/** lock on this when adding a new socket to the pending list, and wait on it accordingly */
|
||||
private Object socketAddedLock = new Object();
|
||||
|
||||
/**
|
||||
* Set Sock Option accept timeout stub, does nothing
|
||||
* @param x
|
||||
*/
|
||||
public void setSoTimeout(long x) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Sock Option accept timeout stub, does nothing
|
||||
* @return timeout
|
||||
*/
|
||||
public long getSoTimeout() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public I2PServerSocketImpl(I2PSocketManager mgr) {
|
||||
this.mgr = mgr;
|
||||
}
|
||||
@ -61,22 +47,19 @@ class I2PServerSocketImpl implements I2PServerSocket {
|
||||
* @throws ConnectException if the I2PServerSocket is closed
|
||||
*/
|
||||
public I2PSocket accept() throws I2PException, ConnectException {
|
||||
if(_log.shouldLog(Log.DEBUG)) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("accept() called, pending: " + pendingSockets.size());
|
||||
}
|
||||
|
||||
I2PSocket ret = null;
|
||||
|
||||
while ( (ret == null) && (!closing) ){
|
||||
while (pendingSockets.size() <= 0) {
|
||||
if(closing) {
|
||||
throw new ConnectException("I2PServerSocket closed");
|
||||
}
|
||||
if (closing) throw new ConnectException("I2PServerSocket closed");
|
||||
try {
|
||||
synchronized(socketAddedLock) {
|
||||
socketAddedLock.wait();
|
||||
}
|
||||
} catch(InterruptedException ie) {
|
||||
}
|
||||
} catch (InterruptedException ie) {}
|
||||
}
|
||||
synchronized (pendingSockets) {
|
||||
if (pendingSockets.size() > 0) {
|
||||
@ -90,9 +73,8 @@ class I2PServerSocketImpl implements I2PServerSocket {
|
||||
}
|
||||
}
|
||||
|
||||
if(_log.shouldLog(Log.DEBUG)) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("TIMING: handed out accept result " + ret.hashCode());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -106,13 +88,12 @@ class I2PServerSocketImpl implements I2PServerSocket {
|
||||
* or the socket was closed
|
||||
*/
|
||||
public boolean addWaitForAccept(I2PSocket s, long timeoutMs) {
|
||||
if(_log.shouldLog(Log.DEBUG)) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("addWaitForAccept [new socket arrived [" + s.toString() + "], pending: " + pendingSockets.size());
|
||||
}
|
||||
|
||||
if (closing) {
|
||||
if(_log.shouldLog(Log.WARN)) {
|
||||
if (_log.shouldLog(Log.WARN))
|
||||
_log.warn("Already closing the socket");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -129,16 +110,14 @@ class I2PServerSocketImpl implements I2PServerSocket {
|
||||
while (pendingSockets.contains(s)) {
|
||||
long now = clock.now();
|
||||
if (now >= end) {
|
||||
if(_log.shouldLog(Log.INFO)) {
|
||||
if (_log.shouldLog(Log.INFO))
|
||||
_log.info("Expired while waiting for accept (time elapsed =" + (now - start) + "ms) for socket " + s.toString());
|
||||
}
|
||||
pendingSockets.remove(s);
|
||||
return false;
|
||||
}
|
||||
if (closing) {
|
||||
if(_log.shouldLog(Log.WARN)) {
|
||||
if (_log.shouldLog(Log.WARN))
|
||||
_log.warn("Server socket closed while waiting for accept");
|
||||
}
|
||||
pendingSockets.remove(s);
|
||||
return false;
|
||||
}
|
||||
@ -147,13 +126,11 @@ class I2PServerSocketImpl implements I2PServerSocket {
|
||||
synchronized (socketAcceptedLock) {
|
||||
socketAcceptedLock.wait(remaining);
|
||||
}
|
||||
} catch(InterruptedException ie) {
|
||||
}
|
||||
} catch (InterruptedException ie) {}
|
||||
}
|
||||
long now = clock.now();
|
||||
if(_log.shouldLog(Log.DEBUG)) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.info("Socket accepted after " + (now-start) + "ms for socket " + s.toString());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -169,7 +146,5 @@ class I2PServerSocketImpl implements I2PServerSocket {
|
||||
}
|
||||
}
|
||||
|
||||
public I2PSocketManager getManager() {
|
||||
return mgr;
|
||||
}
|
||||
public I2PSocketManager getManager() { return mgr; }
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Properties;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
@ -21,7 +20,6 @@ import net.i2p.util.Log;
|
||||
*
|
||||
*/
|
||||
public class StreamSinkServer {
|
||||
|
||||
private Log _log;
|
||||
private String _sinkDir;
|
||||
private String _destFile;
|
||||
@ -38,7 +36,6 @@ public class StreamSinkServer {
|
||||
public StreamSinkServer(String sinkDir, String ourDestFile) {
|
||||
this(sinkDir, ourDestFile, null, -1, 3);
|
||||
}
|
||||
|
||||
public StreamSinkServer(String sinkDir, String ourDestFile, String i2cpHost, int i2cpPort, int handlers) {
|
||||
_sinkDir = sinkDir;
|
||||
_destFile = ourDestFile;
|
||||
@ -55,15 +52,13 @@ public class StreamSinkServer {
|
||||
*/
|
||||
public void runServer() {
|
||||
I2PSocketManager mgr = null;
|
||||
if(_i2cpHost != null) {
|
||||
if (_i2cpHost != null)
|
||||
mgr = I2PSocketManagerFactory.createManager(_i2cpHost, _i2cpPort, new Properties());
|
||||
} else {
|
||||
else
|
||||
mgr = I2PSocketManagerFactory.createManager();
|
||||
}
|
||||
Destination dest = mgr.getSession().getMyDestination();
|
||||
if(_log.shouldLog(Log.INFO)) {
|
||||
if (_log.shouldLog(Log.INFO))
|
||||
_log.info("Listening for connections on: " + dest.calculateHash().toBase64());
|
||||
}
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(_destFile);
|
||||
@ -75,12 +70,7 @@ public class StreamSinkServer {
|
||||
_log.error("Error formatting the destination", dfe);
|
||||
return;
|
||||
} finally {
|
||||
if(fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch(IOException ioe) {
|
||||
}
|
||||
}
|
||||
if (fos != null) try { fos.close(); } catch (IOException ioe) {}
|
||||
}
|
||||
|
||||
I2PServerSocket sock = mgr.getServerSocket();
|
||||
@ -101,28 +91,22 @@ public class StreamSinkServer {
|
||||
*
|
||||
*/
|
||||
private class ClientRunner implements Runnable {
|
||||
|
||||
private I2PServerSocket _socket;
|
||||
|
||||
public ClientRunner(I2PServerSocket socket) {
|
||||
_socket = socket;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
I2PSocket socket = _socket.accept();
|
||||
if(socket != null) {
|
||||
if (socket != null)
|
||||
handle(socket);
|
||||
}
|
||||
} catch (I2PException ie) {
|
||||
_log.error("Error accepting connection", ie);
|
||||
return;
|
||||
} catch (ConnectException ce) {
|
||||
_log.error("Connection already dropped", ce);
|
||||
return;
|
||||
} catch(SocketTimeoutException ste) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -131,14 +115,12 @@ public class StreamSinkServer {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
File sink = new File(_sinkDir);
|
||||
if(!sink.exists()) {
|
||||
if (!sink.exists())
|
||||
sink.mkdirs();
|
||||
}
|
||||
File cur = File.createTempFile("clientSink", ".dat", sink);
|
||||
fos = new FileOutputStream(cur);
|
||||
if(_log.shouldLog(Log.DEBUG)) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Writing to " + cur.getAbsolutePath());
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
_log.error("Error creating sink", ioe);
|
||||
return;
|
||||
@ -153,28 +135,17 @@ public class StreamSinkServer {
|
||||
while ( (read = in.read(buf)) != -1) {
|
||||
//_fos.write(buf, 0, read);
|
||||
written += read;
|
||||
if(_log.shouldLog(Log.DEBUG)) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("read and wrote " + read + " (" + written + ")");
|
||||
}
|
||||
}
|
||||
fos.write(("written: [" + written + "]\n").getBytes());
|
||||
long lifetime = System.currentTimeMillis() - start;
|
||||
_log.info("Got EOF from client socket [written=" + written + " lifetime=" + lifetime + "]");
|
||||
} catch (IOException ioe) {
|
||||
_log.error("Error writing the sink", ioe);
|
||||
} finally {
|
||||
if(fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch(IOException ioe) {
|
||||
}
|
||||
}
|
||||
if(sock != null) {
|
||||
try {
|
||||
sock.close();
|
||||
} catch(IOException ioe) {
|
||||
}
|
||||
}
|
||||
if (fos != null) try { fos.close(); } catch (IOException ioe) {}
|
||||
if (sock != null) try { sock.close(); } catch (IOException ioe) {}
|
||||
_log.debug("Client socket closed");
|
||||
}
|
||||
}
|
||||
@ -203,8 +174,7 @@ public class StreamSinkServer {
|
||||
if (args.length == 5) {
|
||||
try {
|
||||
handlers = Integer.parseInt(args[4]);
|
||||
} catch(NumberFormatException nfe) {
|
||||
}
|
||||
} catch (NumberFormatException nfe) {}
|
||||
}
|
||||
try {
|
||||
int port = Integer.parseInt(args[1]);
|
||||
@ -216,8 +186,7 @@ public class StreamSinkServer {
|
||||
default:
|
||||
System.out.println("Usage: StreamSinkServer [i2cpHost i2cpPort] sinkDir ourDestFile [handlers]");
|
||||
}
|
||||
if(server != null) {
|
||||
if (server != null)
|
||||
server.runServer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user