forked from I2P_Developers/i2p.i2p
SAM:
- Classes static/private - Fields private/final - Remove unused fields - Remove shadowing fields - Remove dup method overrides - Remove static Logs - Remove unnecessary field initialization - Atomics - Findbugs
This commit is contained in:
@@ -27,8 +27,7 @@ import net.i2p.util.Log;
|
||||
*/
|
||||
public class SAMDatagramSession extends SAMMessageSession {
|
||||
|
||||
private final static Log _log = new Log(SAMDatagramSession.class);
|
||||
public static int DGRAM_SIZE_MAX = 31*1024;
|
||||
public static final int DGRAM_SIZE_MAX = 31*1024;
|
||||
|
||||
// FIXME make final after fixing SAMv3DatagramSession override
|
||||
protected SAMDatagramReceiver recv;
|
||||
|
||||
@@ -25,7 +25,7 @@ import net.i2p.util.Log;
|
||||
*/
|
||||
public abstract class SAMHandler implements Runnable {
|
||||
|
||||
private final static Log _log = new Log(SAMHandler.class);
|
||||
protected final Log _log;
|
||||
|
||||
protected I2PAppThread thread = null;
|
||||
protected SAMBridge bridge = null;
|
||||
@@ -53,6 +53,7 @@ public abstract class SAMHandler implements Runnable {
|
||||
*/
|
||||
protected SAMHandler(SocketChannel s,
|
||||
int verMajor, int verMinor, Properties i2cpProps) throws IOException {
|
||||
_log = new Log(getClass());
|
||||
socket = s;
|
||||
|
||||
this.verMajor = verMajor;
|
||||
@@ -136,7 +137,7 @@ public abstract class SAMHandler implements Runnable {
|
||||
try {
|
||||
writeBytes(ByteBuffer.wrap(str.getBytes("ISO-8859-1")), out);
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
//_log.debug("Caught IOException", e);
|
||||
return false;
|
||||
}
|
||||
return true ;
|
||||
|
||||
@@ -23,7 +23,6 @@ import net.i2p.util.Log;
|
||||
*/
|
||||
public class SAMHandlerFactory {
|
||||
|
||||
private final static Log _log = new Log(SAMHandlerFactory.class);
|
||||
|
||||
/**
|
||||
* Return the right SAM handler depending on the protocol version
|
||||
@@ -37,11 +36,12 @@ public class SAMHandlerFactory {
|
||||
public static SAMHandler createSAMHandler(SocketChannel s, Properties i2cpProps) throws SAMException {
|
||||
String line;
|
||||
StringTokenizer tok;
|
||||
Log log = new Log(SAMHandlerFactory.class);
|
||||
|
||||
try {
|
||||
line = DataHelper.readLine(s.socket().getInputStream());
|
||||
if (line == null) {
|
||||
_log.debug("Connection closed by client");
|
||||
log.debug("Connection closed by client");
|
||||
return null;
|
||||
}
|
||||
tok = new StringTokenizer(line.trim(), " ");
|
||||
@@ -94,7 +94,7 @@ public class SAMHandlerFactory {
|
||||
s.write(ByteBuffer.wrap(("HELLO REPLY RESULT=OK VERSION="
|
||||
+ ver + "\n").getBytes("ISO-8859-1")));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
_log.error("Caught UnsupportedEncodingException ("
|
||||
log.error("Caught UnsupportedEncodingException ("
|
||||
+ e.getMessage() + ")");
|
||||
throw new SAMException("Character encoding error: "
|
||||
+ e.getMessage());
|
||||
@@ -120,11 +120,11 @@ public class SAMHandlerFactory {
|
||||
handler = new SAMv3Handler(s, verMajor, verMinor, i2cpProps);
|
||||
break;
|
||||
default:
|
||||
_log.error("BUG! Trying to initialize the wrong SAM version!");
|
||||
log.error("BUG! Trying to initialize the wrong SAM version!");
|
||||
throw new SAMException("BUG! (in handler instantiation)");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
_log.error("Error creating the handler for version "+verMajor, e);
|
||||
log.error("Error creating the handler for version "+verMajor, e);
|
||||
throw new SAMException("IOException caught during SAM handler instantiation");
|
||||
}
|
||||
return handler;
|
||||
|
||||
@@ -32,7 +32,7 @@ import net.i2p.util.Log;
|
||||
*/
|
||||
public abstract class SAMMessageSession {
|
||||
|
||||
private final static Log _log = new Log(SAMMessageSession.class);
|
||||
protected final Log _log;
|
||||
|
||||
private I2PSession session = null;
|
||||
|
||||
@@ -48,10 +48,8 @@ public abstract class SAMMessageSession {
|
||||
* @throws I2PSessionException
|
||||
*/
|
||||
protected SAMMessageSession(String dest, Properties props) throws IOException, DataFormatException, I2PSessionException {
|
||||
ByteArrayInputStream bais;
|
||||
|
||||
bais = new ByteArrayInputStream(Base64.decode(dest));
|
||||
|
||||
_log = new Log(getClass());
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(dest));
|
||||
initSAMMessageSession(bais, props);
|
||||
}
|
||||
|
||||
@@ -65,11 +63,11 @@ public abstract class SAMMessageSession {
|
||||
* @throws I2PSessionException
|
||||
*/
|
||||
protected SAMMessageSession(InputStream destStream, Properties props) throws IOException, DataFormatException, I2PSessionException {
|
||||
_log = new Log(getClass());
|
||||
initSAMMessageSession(destStream, props);
|
||||
}
|
||||
|
||||
private void initSAMMessageSession (InputStream destStream, Properties props) throws IOException, DataFormatException, I2PSessionException {
|
||||
|
||||
_log.debug("Initializing SAM message-based session");
|
||||
|
||||
handler = new SAMMessageSessionHandler(destStream, props);
|
||||
|
||||
@@ -23,7 +23,6 @@ import net.i2p.util.Log;
|
||||
*/
|
||||
public class SAMRawSession extends SAMMessageSession {
|
||||
|
||||
private final static Log _log = new Log(SAMRawSession.class);
|
||||
public static final int RAW_SIZE_MAX = 32*1024;
|
||||
|
||||
// FIXME make final after fixing SAMv3DatagramSession override
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import net.i2p.I2PException;
|
||||
import net.i2p.client.I2PClient;
|
||||
@@ -47,7 +48,7 @@ import net.i2p.util.Log;
|
||||
*/
|
||||
public class SAMStreamSession {
|
||||
|
||||
private final static Log _log = new Log(SAMStreamSession.class);
|
||||
protected final Log _log;
|
||||
|
||||
protected final static int SOCKET_HANDLER_BUF_SIZE = 32768;
|
||||
|
||||
@@ -57,17 +58,15 @@ public class SAMStreamSession {
|
||||
|
||||
protected final I2PSocketManager socketMgr;
|
||||
|
||||
private final Object handlersMapLock = new Object();
|
||||
/** stream id (Long) to SAMStreamSessionSocketReader */
|
||||
private final HashMap<Integer,SAMStreamSessionSocketReader> handlersMap = new HashMap<Integer,SAMStreamSessionSocketReader>();
|
||||
/** stream id (Long) to StreamSender */
|
||||
private final HashMap<Integer,StreamSender> sendersMap = new HashMap<Integer,StreamSender>();
|
||||
|
||||
private final Object idLock = new Object();
|
||||
private int lastNegativeId = 0;
|
||||
private final AtomicInteger lastNegativeId = new AtomicInteger();;
|
||||
|
||||
// Can we create outgoing connections?
|
||||
protected boolean canCreate = false;
|
||||
protected boolean canCreate;
|
||||
|
||||
/**
|
||||
* should we flush every time we get a STREAM SEND, or leave that up to
|
||||
@@ -75,8 +74,8 @@ public class SAMStreamSession {
|
||||
*/
|
||||
protected final boolean forceFlush;
|
||||
|
||||
public static String PROP_FORCE_FLUSH = "sam.forceFlush";
|
||||
public static String DEFAULT_FORCE_FLUSH = "false";
|
||||
public static final String PROP_FORCE_FLUSH = "sam.forceFlush";
|
||||
public static final String DEFAULT_FORCE_FLUSH = "false";
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session.
|
||||
@@ -108,7 +107,7 @@ public class SAMStreamSession {
|
||||
public SAMStreamSession(InputStream destStream, String dir,
|
||||
Properties props, SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
||||
this.recv = recv;
|
||||
|
||||
_log = new Log(getClass());
|
||||
_log.debug("SAM STREAM session instantiated");
|
||||
|
||||
Properties allprops = (Properties) System.getProperties().clone();
|
||||
@@ -304,7 +303,7 @@ public class SAMStreamSession {
|
||||
return 0;
|
||||
}
|
||||
|
||||
synchronized (handlersMapLock) {
|
||||
synchronized (handlersMap) {
|
||||
handlersMap.put(Integer.valueOf(id), reader);
|
||||
sendersMap.put(Integer.valueOf(id), sender);
|
||||
}
|
||||
@@ -319,9 +318,7 @@ public class SAMStreamSession {
|
||||
|
||||
/* Create an unique id, either positive or negative */
|
||||
private int createUniqueId() {
|
||||
synchronized (idLock) {
|
||||
return --lastNegativeId;
|
||||
}
|
||||
return lastNegativeId.decrementAndGet();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -331,12 +328,12 @@ public class SAMStreamSession {
|
||||
* @return SAM StreamSender handler
|
||||
*/
|
||||
protected SAMStreamSessionSocketReader getSocketReader ( int id ) {
|
||||
synchronized (handlersMapLock) {
|
||||
synchronized (handlersMap) {
|
||||
return handlersMap.get(Integer.valueOf(id));
|
||||
}
|
||||
}
|
||||
private StreamSender getSender(int id) {
|
||||
synchronized (handlersMapLock) {
|
||||
synchronized (handlersMap) {
|
||||
return sendersMap.get(Integer.valueOf(id));
|
||||
}
|
||||
}
|
||||
@@ -348,7 +345,7 @@ public class SAMStreamSession {
|
||||
* @return True if in use
|
||||
*/
|
||||
protected boolean checkSocketHandlerId ( int id ) {
|
||||
synchronized (handlersMapLock) {
|
||||
synchronized (handlersMap) {
|
||||
return (!(handlersMap.get(Integer.valueOf(id)) == null));
|
||||
}
|
||||
}
|
||||
@@ -359,10 +356,10 @@ public class SAMStreamSession {
|
||||
* @param id Handler id to be removed
|
||||
*/
|
||||
protected void removeSocketHandler ( int id ) {
|
||||
SAMStreamSessionSocketReader reader = null;
|
||||
StreamSender sender = null;
|
||||
SAMStreamSessionSocketReader reader;
|
||||
StreamSender sender;
|
||||
|
||||
synchronized (handlersMapLock) {
|
||||
synchronized (handlersMap) {
|
||||
reader = handlersMap.remove(Integer.valueOf(id));
|
||||
sender = sendersMap.remove(Integer.valueOf(id));
|
||||
}
|
||||
@@ -384,7 +381,7 @@ public class SAMStreamSession {
|
||||
Set<Integer> keySet;
|
||||
Iterator<Integer> iter;
|
||||
|
||||
synchronized (handlersMapLock) {
|
||||
synchronized (handlersMap) {
|
||||
keySet = handlersMap.keySet();
|
||||
iter = keySet.iterator();
|
||||
|
||||
@@ -498,13 +495,13 @@ public class SAMStreamSession {
|
||||
*/
|
||||
public class SAMStreamSessionSocketReader implements Runnable {
|
||||
|
||||
protected I2PSocket i2pSocket = null;
|
||||
protected final I2PSocket i2pSocket;
|
||||
|
||||
protected final Object runningLock = new Object();
|
||||
|
||||
protected volatile boolean stillRunning = true;
|
||||
|
||||
protected int id;
|
||||
protected final int id;
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session socket reader
|
||||
@@ -513,7 +510,10 @@ public class SAMStreamSession {
|
||||
* @param id Unique id assigned to the handler
|
||||
* @throws IOException
|
||||
*/
|
||||
public SAMStreamSessionSocketReader ( I2PSocket s, int id ) throws IOException {}
|
||||
public SAMStreamSessionSocketReader ( I2PSocket s, int id ) throws IOException {
|
||||
i2pSocket = s;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop a SAM STREAM session socket reader thread immediately.
|
||||
@@ -541,9 +541,6 @@ public class SAMStreamSession {
|
||||
public SAMv1StreamSessionSocketReader ( I2PSocket s, int id ) throws IOException {
|
||||
super(s, id);
|
||||
_log.debug("Instantiating new SAM STREAM session socket reader");
|
||||
|
||||
i2pSocket = s;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -611,8 +608,15 @@ public class SAMStreamSession {
|
||||
* Lets us push data through the stream without blocking, (even after exceeding
|
||||
* the I2PSocket's buffer)
|
||||
*/
|
||||
protected class StreamSender implements Runnable {
|
||||
public StreamSender ( I2PSocket s, int id ) throws IOException {}
|
||||
protected static abstract class StreamSender implements Runnable {
|
||||
|
||||
protected final int _id;
|
||||
protected final I2PSocket i2pSocket;
|
||||
|
||||
public StreamSender ( I2PSocket s, int id ) throws IOException {
|
||||
_id = id;
|
||||
i2pSocket = s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send bytes through the SAM STREAM session socket sender
|
||||
@@ -621,47 +625,42 @@ public class SAMStreamSession {
|
||||
* @param size Count of bytes to send
|
||||
* @throws IOException if the client didnt provide enough data
|
||||
*/
|
||||
public void sendBytes ( InputStream in, int size ) throws IOException {}
|
||||
public abstract void sendBytes ( InputStream in, int size ) throws IOException;
|
||||
|
||||
|
||||
/**
|
||||
* Stop a SAM STREAM session socket sender thread immediately
|
||||
*
|
||||
*/
|
||||
public void stopRunning() {}
|
||||
public abstract void stopRunning();
|
||||
|
||||
/**
|
||||
* Stop a SAM STREAM session socket sender gracefully: stop the
|
||||
* sender thread once all pending data has been sent.
|
||||
*/
|
||||
public void shutDownGracefully() {}
|
||||
public abstract void shutDownGracefully();
|
||||
|
||||
public void run() {}
|
||||
public abstract void run();
|
||||
}
|
||||
|
||||
protected StreamSender newStreamSender ( I2PSocket s, int id ) throws IOException {
|
||||
return new v1StreamSender ( s, id ) ;
|
||||
return new V1StreamSender ( s, id ) ;
|
||||
}
|
||||
|
||||
protected class v1StreamSender extends StreamSender
|
||||
private class V1StreamSender extends StreamSender
|
||||
{
|
||||
private List<ByteArray> _data;
|
||||
private int _id;
|
||||
private ByteCache _cache;
|
||||
private OutputStream _out = null;
|
||||
private final List<ByteArray> _data;
|
||||
private final ByteCache _cache;
|
||||
private final OutputStream _out;
|
||||
private volatile boolean _stillRunning, _shuttingDownGracefully;
|
||||
private final Object runningLock = new Object();
|
||||
private I2PSocket i2pSocket = null;
|
||||
|
||||
public v1StreamSender ( I2PSocket s, int id ) throws IOException {
|
||||
public V1StreamSender ( I2PSocket s, int id ) throws IOException {
|
||||
super ( s, id );
|
||||
_data = new ArrayList<ByteArray>(1);
|
||||
_id = id;
|
||||
_cache = ByteCache.getInstance(4, 32*1024);
|
||||
_out = s.getOutputStream();
|
||||
_stillRunning = true;
|
||||
_shuttingDownGracefully = false;
|
||||
i2pSocket = s;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,7 +34,7 @@ import net.i2p.util.Log;
|
||||
*/
|
||||
public class SAMUtils {
|
||||
|
||||
private final static Log _log = new Log(SAMUtils.class);
|
||||
//private final static Log _log = new Log(SAMUtils.class);
|
||||
|
||||
/**
|
||||
* Generate a random destination key
|
||||
@@ -43,7 +43,7 @@ public class SAMUtils {
|
||||
* @param pub Stream used to write the public key (may be null)
|
||||
*/
|
||||
public static void genRandomKey(OutputStream priv, OutputStream pub) {
|
||||
_log.debug("Generating random keys...");
|
||||
//_log.debug("Generating random keys...");
|
||||
try {
|
||||
I2PClient c = I2PClientFactory.createClient();
|
||||
Destination d = c.createDestination(priv);
|
||||
@@ -161,7 +161,7 @@ public class SAMUtils {
|
||||
|
||||
pos = token.indexOf("=");
|
||||
if (pos == -1) {
|
||||
_log.debug("Error in params format");
|
||||
//_log.debug("Error in params format");
|
||||
throw new SAMException("Bad formatting for param [" + token + "]");
|
||||
}
|
||||
param = token.substring(0, pos);
|
||||
@@ -179,9 +179,9 @@ public class SAMUtils {
|
||||
value.setLength(0);
|
||||
}
|
||||
|
||||
if (_log.shouldLog(Log.DEBUG)) {
|
||||
_log.debug("Parsed properties: " + dumpProperties(props));
|
||||
}
|
||||
//if (_log.shouldLog(Log.DEBUG)) {
|
||||
// _log.debug("Parsed properties: " + dumpProperties(props));
|
||||
//}
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.nio.channels.SocketChannel;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Properties;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import net.i2p.I2PException;
|
||||
import net.i2p.client.I2PClient;
|
||||
@@ -35,20 +36,17 @@ import net.i2p.util.Log;
|
||||
* @author human
|
||||
*/
|
||||
public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramReceiver, SAMStreamReceiver {
|
||||
protected int verMajorId = 1;
|
||||
protected int verMinorId = 0;
|
||||
|
||||
private final static Log _log = new Log(SAMv1Handler.class);
|
||||
protected SAMRawSession rawSession;
|
||||
protected SAMDatagramSession datagramSession;
|
||||
protected SAMStreamSession streamSession;
|
||||
|
||||
protected SAMRawSession rawSession = null;
|
||||
protected SAMDatagramSession datagramSession = null;
|
||||
protected SAMStreamSession streamSession = null;
|
||||
protected SAMRawSession getRawSession() {return rawSession ;}
|
||||
protected SAMDatagramSession getDatagramSession() {return datagramSession ;}
|
||||
protected SAMStreamSession getStreamSession() {return streamSession ;}
|
||||
|
||||
protected final long _id;
|
||||
protected static volatile long __id = 0;
|
||||
private static final AtomicLong __id = new AtomicLong();
|
||||
|
||||
/**
|
||||
* Create a new SAM version 1 handler. This constructor expects
|
||||
@@ -78,7 +76,7 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatag
|
||||
*/
|
||||
public SAMv1Handler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps) throws SAMException, IOException {
|
||||
super(s, verMajor, verMinor, i2cpProps);
|
||||
_id = ++__id;
|
||||
_id = __id.incrementAndGet();
|
||||
_log.debug("SAM version 1 handler instantiated");
|
||||
|
||||
if ( ! verifVersion() ) {
|
||||
|
||||
@@ -24,8 +24,6 @@ import net.i2p.util.Log;
|
||||
public class SAMv2Handler extends SAMv1Handler implements SAMRawReceiver, SAMDatagramReceiver, SAMStreamReceiver
|
||||
{
|
||||
|
||||
private final static Log _log = new Log ( SAMv2Handler.class );
|
||||
|
||||
|
||||
/**
|
||||
* Create a new SAM version 2 handler. This constructor expects
|
||||
|
||||
@@ -40,8 +40,6 @@ import net.i2p.util.Log;
|
||||
public class SAMv2StreamSession extends SAMStreamSession
|
||||
{
|
||||
|
||||
private final static Log _log = new Log ( SAMv2StreamSession.class );
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session.
|
||||
*
|
||||
@@ -139,12 +137,12 @@ public class SAMv2StreamSession extends SAMStreamSession
|
||||
* @author mkvore
|
||||
*/
|
||||
|
||||
public class StreamConnector implements Runnable
|
||||
private class StreamConnector implements Runnable
|
||||
{
|
||||
|
||||
private int id;
|
||||
private Destination dest ;
|
||||
private I2PSocketOptions opts ;
|
||||
private final int id;
|
||||
private final Destination dest ;
|
||||
private final I2PSocketOptions opts ;
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session socket reader
|
||||
@@ -231,7 +229,7 @@ public class SAMv2StreamSession extends SAMStreamSession
|
||||
@Override
|
||||
protected StreamSender newStreamSender ( I2PSocket s, int id ) throws IOException
|
||||
{
|
||||
return new v2StreamSender ( s, id ) ;
|
||||
return new V2StreamSender ( s, id ) ;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -241,29 +239,23 @@ public class SAMv2StreamSession extends SAMStreamSession
|
||||
return new SAMv2StreamSessionSocketReader(s,id);
|
||||
}
|
||||
|
||||
protected class v2StreamSender extends StreamSender
|
||||
private class V2StreamSender extends StreamSender
|
||||
|
||||
{
|
||||
private final List<ByteArray> _data;
|
||||
private int _dataSize;
|
||||
private final int _id;
|
||||
private final ByteCache _cache;
|
||||
private final OutputStream _out;
|
||||
private volatile boolean _stillRunning, _shuttingDownGracefully;
|
||||
private final Object runningLock = new Object();
|
||||
private final I2PSocket i2pSocket;
|
||||
|
||||
public v2StreamSender ( I2PSocket s, int id ) throws IOException
|
||||
public V2StreamSender ( I2PSocket s, int id ) throws IOException
|
||||
{
|
||||
super ( s, id );
|
||||
_data = new ArrayList<ByteArray> ( 1 );
|
||||
_dataSize = 0;
|
||||
_id = id;
|
||||
_cache = ByteCache.getInstance ( 10, 32 * 1024 );
|
||||
_out = s.getOutputStream();
|
||||
_stillRunning = true;
|
||||
_shuttingDownGracefully = false;
|
||||
i2pSocket = s;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -489,9 +481,6 @@ public class SAMv2StreamSession extends SAMStreamSession
|
||||
public SAMv2StreamSessionSocketReader ( I2PSocket s, int id ) throws IOException
|
||||
{
|
||||
super ( s, id );
|
||||
nolimit = false ;
|
||||
limit = 0 ;
|
||||
totalReceived = 0 ;
|
||||
}
|
||||
|
||||
public void setLimit ( long limit, boolean nolimit )
|
||||
@@ -505,6 +494,7 @@ public class SAMv2StreamSession extends SAMStreamSession
|
||||
_log.debug ( "new limit set for socket reader " + id + " : " + (nolimit ? "NOLIMIT" : limit + " bytes" ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
_log.debug ( "run() called for socket reader " + id );
|
||||
|
||||
@@ -19,12 +19,10 @@ import java.nio.ByteBuffer;
|
||||
|
||||
public class SAMv3DatagramSession extends SAMDatagramSession implements SAMv3Handler.Session, SAMDatagramReceiver {
|
||||
|
||||
private final static Log _log = new Log ( SAMv3DatagramSession.class );
|
||||
|
||||
final SAMv3Handler handler;
|
||||
final SAMv3Handler.DatagramServer server;
|
||||
final String nick;
|
||||
final SocketAddress clientAddress;
|
||||
private final SAMv3Handler handler;
|
||||
private final SAMv3Handler.DatagramServer server;
|
||||
private final String nick;
|
||||
private final SocketAddress clientAddress;
|
||||
|
||||
public String getNick() { return nick; }
|
||||
|
||||
|
||||
@@ -43,23 +43,8 @@ import net.i2p.util.I2PAppThread;
|
||||
|
||||
public class SAMv3Handler extends SAMv1Handler
|
||||
{
|
||||
private final static Log _log = new Log ( SAMv3Handler.class );
|
||||
|
||||
protected SAMv3RawSession rawSession = null ;
|
||||
protected SAMv3DatagramSession datagramSession = null ;
|
||||
protected SAMv3StreamSession streamSession = null ;
|
||||
|
||||
protected SAMRawSession getRawSession() {
|
||||
return rawSession ;
|
||||
}
|
||||
protected SAMDatagramSession getDatagramSession() {
|
||||
return datagramSession ;
|
||||
}
|
||||
protected SAMStreamSession getStreamSession() {
|
||||
return streamSession ;
|
||||
}
|
||||
|
||||
protected Session session = null ;
|
||||
private Session session;
|
||||
|
||||
interface Session {
|
||||
String getNick();
|
||||
@@ -103,25 +88,27 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
return (verMajor == 3 && verMinor == 0) ;
|
||||
}
|
||||
|
||||
static public class DatagramServer {
|
||||
public static class DatagramServer {
|
||||
|
||||
private static DatagramServer _instance = null ;
|
||||
private static DatagramChannel server = null ;
|
||||
private static DatagramServer _instance;
|
||||
private static DatagramChannel server;
|
||||
|
||||
public static DatagramServer getInstance() throws IOException {
|
||||
return getInstance(new Properties());
|
||||
}
|
||||
|
||||
public static DatagramServer getInstance(Properties props) throws IOException {
|
||||
if (_instance==null) {
|
||||
_instance = new DatagramServer(props);
|
||||
synchronized(DatagramServer.class) {
|
||||
if (_instance==null)
|
||||
_instance = new DatagramServer(props);
|
||||
return _instance ;
|
||||
}
|
||||
return _instance ;
|
||||
}
|
||||
|
||||
public DatagramServer(Properties props) throws IOException {
|
||||
if (server==null) {
|
||||
server = DatagramChannel.open();
|
||||
synchronized(DatagramServer.class) {
|
||||
if (server==null)
|
||||
server = DatagramChannel.open();
|
||||
}
|
||||
|
||||
String host = props.getProperty(SAMBridge.PROP_DATAGRAM_HOST, SAMBridge.DEFAULT_DATAGRAM_HOST);
|
||||
@@ -143,7 +130,7 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
|
||||
static class Listener implements Runnable {
|
||||
|
||||
final DatagramChannel server;
|
||||
private final DatagramChannel server;
|
||||
|
||||
public Listener(DatagramChannel server)
|
||||
{
|
||||
@@ -171,9 +158,9 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
}
|
||||
}
|
||||
|
||||
public static class MessageDispatcher implements Runnable
|
||||
private static class MessageDispatcher implements Runnable
|
||||
{
|
||||
final ByteArrayInputStream is;
|
||||
private final ByteArrayInputStream is;
|
||||
|
||||
public MessageDispatcher(byte[] buf)
|
||||
{
|
||||
@@ -181,23 +168,19 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
}
|
||||
|
||||
public void run() {
|
||||
String header = null ;
|
||||
String nick ;
|
||||
String dest ;
|
||||
String version ;
|
||||
|
||||
try {
|
||||
header = DataHelper.readLine(is).trim();
|
||||
String header = DataHelper.readLine(is).trim();
|
||||
StringTokenizer tok = new StringTokenizer(header, " ");
|
||||
if (tok.countTokens() != 3) {
|
||||
// This is not a correct message, for sure
|
||||
_log.debug("Error in message format");
|
||||
//_log.debug("Error in message format");
|
||||
// FIXME log? throw?
|
||||
return;
|
||||
}
|
||||
version = tok.nextToken();
|
||||
String version = tok.nextToken();
|
||||
if (!"3.0".equals(version)) return ;
|
||||
nick = tok.nextToken();
|
||||
dest = tok.nextToken();
|
||||
String nick = tok.nextToken();
|
||||
String dest = tok.nextToken();
|
||||
|
||||
byte[] data = new byte[is.available()];
|
||||
is.read(data);
|
||||
@@ -205,23 +188,24 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
if (rec!=null) {
|
||||
rec.getHandler().session.sendBytes(dest,data);
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
} catch (Exception e) {
|
||||
// FIXME log? throw?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class SessionRecord
|
||||
{
|
||||
protected final String m_dest ;
|
||||
protected final Properties m_props ;
|
||||
protected ThreadGroup m_threadgroup ;
|
||||
protected final SAMv3Handler m_handler ;
|
||||
private final String m_dest ;
|
||||
private final Properties m_props ;
|
||||
private ThreadGroup m_threadgroup ;
|
||||
private final SAMv3Handler m_handler ;
|
||||
|
||||
public SessionRecord( String dest, Properties props, SAMv3Handler handler )
|
||||
{
|
||||
m_dest = dest;
|
||||
m_props = new Properties() ;
|
||||
m_props.putAll(props);
|
||||
m_threadgroup = null ;
|
||||
m_handler = handler ;
|
||||
}
|
||||
|
||||
@@ -269,7 +253,7 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
static final long serialVersionUID = 0x1 ;
|
||||
}
|
||||
|
||||
final HashMap<String, SessionRecord> map;
|
||||
private final HashMap<String, SessionRecord> map;
|
||||
|
||||
public SessionsDB() {
|
||||
map = new HashMap<String, SessionRecord>() ;
|
||||
@@ -315,16 +299,15 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
}
|
||||
}
|
||||
|
||||
public static SessionsDB sSessionsHash = new SessionsDB() ;
|
||||
public static final SessionsDB sSessionsHash = new SessionsDB() ;
|
||||
|
||||
public String getClientIP()
|
||||
{
|
||||
return this.socket.socket().getInetAddress().getHostAddress();
|
||||
}
|
||||
|
||||
boolean stolenSocket = false ;
|
||||
|
||||
boolean streamForwardingSocket = false ;
|
||||
private boolean stolenSocket;
|
||||
private boolean streamForwardingSocket;
|
||||
|
||||
public void stealSocket()
|
||||
{
|
||||
@@ -423,7 +406,7 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
{
|
||||
if (this.getStreamSession()!=null) {
|
||||
try {
|
||||
this.streamSession.stopForwardingIncoming();
|
||||
((SAMv3StreamSession)streamSession).stopForwardingIncoming();
|
||||
} catch (SAMException e) {
|
||||
_log.error("Error while stopping forwarding connections: " + e.getMessage());
|
||||
} catch (InterruptedIOException e) {
|
||||
@@ -540,15 +523,18 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
|
||||
if (style.equals("RAW")) {
|
||||
DatagramServer.getInstance(i2cpProps);
|
||||
rawSession = newSAMRawSession(nick);
|
||||
this.session = rawSession ;
|
||||
SAMv3RawSession v3 = newSAMRawSession(nick);
|
||||
rawSession = v3;
|
||||
this.session = v3;
|
||||
} else if (style.equals("DATAGRAM")) {
|
||||
DatagramServer.getInstance(i2cpProps);
|
||||
datagramSession = newSAMDatagramSession(nick);
|
||||
this.session = datagramSession ;
|
||||
SAMv3DatagramSession v3 = newSAMDatagramSession(nick);
|
||||
datagramSession = v3;
|
||||
this.session = v3;
|
||||
} else if (style.equals("STREAM")) {
|
||||
streamSession = newSAMStreamSession(nick);
|
||||
this.session = streamSession ;
|
||||
SAMv3StreamSession v3 = newSAMStreamSession(nick);
|
||||
streamSession = v3;
|
||||
this.session = v3;
|
||||
} else {
|
||||
_log.debug("Unrecognized SESSION STYLE: \"" + style +"\"");
|
||||
return writeString("SESSION STATUS RESULT=I2P_ERROR MESSAGE=\"Unrecognized SESSION STYLE\"\n");
|
||||
@@ -585,19 +571,19 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
/**
|
||||
* @throws NPE if login nickname is not registered
|
||||
*/
|
||||
SAMv3StreamSession newSAMStreamSession(String login )
|
||||
private SAMv3StreamSession newSAMStreamSession(String login )
|
||||
throws IOException, DataFormatException, SAMException
|
||||
{
|
||||
return new SAMv3StreamSession( login ) ;
|
||||
}
|
||||
|
||||
SAMv3RawSession newSAMRawSession(String login )
|
||||
private SAMv3RawSession newSAMRawSession(String login )
|
||||
throws IOException, DataFormatException, SAMException, I2PSessionException
|
||||
{
|
||||
return new SAMv3RawSession( login ) ;
|
||||
}
|
||||
|
||||
SAMv3DatagramSession newSAMDatagramSession(String login )
|
||||
private SAMv3DatagramSession newSAMDatagramSession(String login )
|
||||
throws IOException, DataFormatException, SAMException, I2PSessionException
|
||||
{
|
||||
return new SAMv3DatagramSession( login ) ;
|
||||
@@ -691,7 +677,7 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
props.remove("DESTINATION");
|
||||
|
||||
try {
|
||||
streamSession.connect( this, dest, props );
|
||||
((SAMv3StreamSession)streamSession).connect( this, dest, props );
|
||||
return true ;
|
||||
} catch (DataFormatException e) {
|
||||
_log.debug("Invalid destination in STREAM CONNECT message");
|
||||
@@ -718,7 +704,7 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
try {
|
||||
try {
|
||||
streamForwardingSocket = true ;
|
||||
streamSession.startForwardingIncoming(props);
|
||||
((SAMv3StreamSession)streamSession).startForwardingIncoming(props);
|
||||
notifyStreamResult( true, "OK", null );
|
||||
return true ;
|
||||
} catch (SAMException e) {
|
||||
@@ -736,7 +722,7 @@ public class SAMv3Handler extends SAMv1Handler
|
||||
try {
|
||||
try {
|
||||
notifyStreamResult(verbose, "OK", null);
|
||||
streamSession.accept(this, verbose);
|
||||
((SAMv3StreamSession)streamSession).accept(this, verbose);
|
||||
return true ;
|
||||
} catch (InterruptedIOException e) {
|
||||
_log.debug("STREAM ACCEPT failed: " + e.getMessage());
|
||||
|
||||
@@ -20,11 +20,10 @@ import net.i2p.util.Log;
|
||||
*/
|
||||
public class SAMv3RawSession extends SAMRawSession implements SAMv3Handler.Session, SAMRawReceiver {
|
||||
|
||||
final String nick;
|
||||
final SAMv3Handler handler;
|
||||
final SAMv3Handler.DatagramServer server;
|
||||
private final static Log _log = new Log ( SAMv3DatagramSession.class );
|
||||
final SocketAddress clientAddress;
|
||||
private final String nick;
|
||||
private final SAMv3Handler handler;
|
||||
private final SAMv3Handler.DatagramServer server;
|
||||
private final SocketAddress clientAddress;
|
||||
|
||||
public String getNick() { return nick; }
|
||||
|
||||
|
||||
@@ -37,14 +37,12 @@ import java.nio.channels.SocketChannel;
|
||||
public class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handler.Session
|
||||
{
|
||||
|
||||
private final static Log _log = new Log ( SAMv3StreamSession.class );
|
||||
private static final int BUFFER_SIZE = 1024 ;
|
||||
|
||||
protected static final int BUFFER_SIZE = 1024 ;
|
||||
|
||||
protected final Object socketServerLock = new Object();
|
||||
protected I2PServerSocket socketServer = null;
|
||||
private final Object socketServerLock = new Object();
|
||||
private I2PServerSocket socketServer;
|
||||
|
||||
protected final String nick ;
|
||||
private final String nick ;
|
||||
|
||||
public String getNick() {
|
||||
return nick ;
|
||||
@@ -210,12 +208,12 @@ public class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handle
|
||||
(new Thread(rec.getThreadGroup(), new I2PAppThread(forwarder, "SAMStreamForwarder"), "SAMStreamForwarder")).start();
|
||||
}
|
||||
|
||||
public class SocketForwarder extends Thread
|
||||
private static class SocketForwarder extends Thread
|
||||
{
|
||||
final String host;
|
||||
final int port;
|
||||
final SAMv3StreamSession session;
|
||||
final boolean verbose;
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final SAMv3StreamSession session;
|
||||
private final boolean verbose;
|
||||
|
||||
SocketForwarder(String host, int port, SAMv3StreamSession session, boolean verbose) {
|
||||
this.host = host ;
|
||||
@@ -248,12 +246,6 @@ public class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handle
|
||||
catch ( IOException e ) {
|
||||
continue ;
|
||||
}
|
||||
if (clientServerSock==null) {
|
||||
try {
|
||||
i2ps.close();
|
||||
} catch (IOException ee) {}
|
||||
continue ;
|
||||
}
|
||||
|
||||
// build pipes between both sockets
|
||||
try {
|
||||
@@ -280,11 +272,11 @@ public class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handle
|
||||
}
|
||||
}
|
||||
|
||||
public static class Pipe extends Thread
|
||||
private static class Pipe extends Thread
|
||||
{
|
||||
final ReadableByteChannel in ;
|
||||
final WritableByteChannel out ;
|
||||
final ByteBuffer buf ;
|
||||
private final ReadableByteChannel in ;
|
||||
private final WritableByteChannel out ;
|
||||
private final ByteBuffer buf ;
|
||||
|
||||
public Pipe(ReadableByteChannel in, WritableByteChannel out, String name)
|
||||
{
|
||||
|
||||
@@ -18,7 +18,7 @@ public class RouterVersion {
|
||||
/** deprecated */
|
||||
public final static String ID = "Monotone";
|
||||
public final static String VERSION = CoreVersion.VERSION;
|
||||
public final static long BUILD = 18;
|
||||
public final static long BUILD = 19;
|
||||
|
||||
/** for example "-test" */
|
||||
public final static String EXTRA = "";
|
||||
|
||||
Reference in New Issue
Block a user