Refactor Session classes out of SAMv3Handler to their own files

This commit is contained in:
zzz
2016-01-03 16:56:44 +00:00
parent ab5d4b59fd
commit a141d50902
8 changed files with 176 additions and 135 deletions

View File

@@ -160,7 +160,7 @@ class SAMv3DatagramServer implements Handler {
String nick = tok.nextToken();
String dest = tok.nextToken();
SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if (rec!=null) {
Properties sprops = rec.getProps();
String pr = sprops.getProperty("PROTOCOL");
@@ -206,7 +206,7 @@ class SAMv3DatagramServer implements Handler {
// TODO too many allocations and copies. One here and one in Listener above.
byte[] data = new byte[is.available()];
is.read(data);
SAMv3Handler.Session sess = rec.getHandler().getSession();
Session sess = rec.getHandler().getSession();
if (sess != null)
sess.sendBytes(dest, data, proto, fromPort, toPort);
else

View File

@@ -18,7 +18,7 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress ;
import java.nio.ByteBuffer;
class SAMv3DatagramSession extends SAMDatagramSession implements SAMv3Handler.Session, SAMDatagramReceiver {
class SAMv3DatagramSession extends SAMDatagramSession implements Session, SAMDatagramReceiver {
private final SAMv3Handler handler;
private final SAMv3DatagramServer server;
@@ -46,7 +46,7 @@ class SAMv3DatagramSession extends SAMDatagramSession implements SAMv3Handler.Se
this.recv = this; // replacement
this.server = dgServer;
SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if (rec == null)
throw new SAMException("Record disappeared for nickname : \""+nick+"\"");

View File

@@ -23,7 +23,6 @@ import java.net.NoRouteToHostException;
import java.nio.channels.SocketChannel;
import java.nio.ByteBuffer;
import java.util.Properties;
import java.util.HashMap;
import net.i2p.I2PAppContext;
import net.i2p.I2PException;
@@ -57,13 +56,6 @@ class SAMv3Handler extends SAMv1Handler
private static final int FIRST_READ_TIMEOUT = 60*1000;
private static final int READ_TIMEOUT = 3*60*1000;
interface Session {
String getNick();
void close();
boolean sendBytes(String dest, byte[] data, int proto,
int fromPort, int toPort) throws DataFormatException, I2PSessionException;
}
/**
* Create a new SAM version 3 handler. This constructor expects
* that the SAM HELLO message has been still answered (and
@@ -104,121 +96,6 @@ class SAMv3Handler extends SAMv1Handler
{
return (verMajor == 3);
}
/**
* The values in the SessionsDB
*/
public static class SessionRecord
{
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_handler = handler ;
}
public SessionRecord( SessionRecord in )
{
m_dest = in.getDest();
m_props = in.getProps();
m_threadgroup = in.getThreadGroup();
m_handler = in.getHandler();
}
public String getDest()
{
return m_dest;
}
synchronized public Properties getProps()
{
Properties p = new Properties();
p.putAll(m_props);
return m_props;
}
public SAMv3Handler getHandler()
{
return m_handler ;
}
synchronized public ThreadGroup getThreadGroup()
{
return m_threadgroup ;
}
synchronized public void createThreadGroup(String name)
{
if (m_threadgroup == null)
m_threadgroup = new ThreadGroup(name);
}
}
/**
* basically a HashMap from String to SessionRecord
*/
public static class SessionsDB
{
private static final long serialVersionUID = 0x1;
static class ExistingIdException extends Exception {
private static final long serialVersionUID = 0x1;
}
static class ExistingDestException extends Exception {
private static final long serialVersionUID = 0x1;
}
private final HashMap<String, SessionRecord> map;
public SessionsDB() {
map = new HashMap<String, SessionRecord>() ;
}
/** @return success */
synchronized public boolean put( String nick, SessionRecord session )
throws ExistingIdException, ExistingDestException
{
if ( map.containsKey(nick) ) {
throw new ExistingIdException();
}
for ( SessionRecord r : map.values() ) {
if (r.getDest().equals(session.getDest())) {
throw new ExistingDestException();
}
}
if ( !map.containsKey(nick) ) {
session.createThreadGroup("SAM session "+nick);
map.put(nick, session) ;
return true ;
}
else
return false ;
}
/** @return true if removed */
synchronized public boolean del( String nick )
{
return map.remove(nick) != null;
}
synchronized public SessionRecord get(String nick)
{
return map.get(nick);
}
synchronized public boolean containsKey( String nick )
{
return map.containsKey(nick);
}
}
public String getClientIP()
{

View File

@@ -19,7 +19,7 @@ import net.i2p.util.Log;
* @author MKVore
*
*/
class SAMv3RawSession extends SAMRawSession implements SAMv3Handler.Session, SAMRawReceiver {
class SAMv3RawSession extends SAMRawSession implements Session, SAMRawReceiver {
private final String nick;
private final SAMv3Handler handler;
@@ -48,7 +48,7 @@ class SAMv3RawSession extends SAMRawSession implements SAMv3Handler.Session, SA
this.recv = this ; // replacement
this.server = dgServer;
SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if (rec == null)
throw new InterruptedIOException() ;
this.handler = rec.getHandler();

View File

@@ -43,7 +43,7 @@ import net.i2p.util.Log;
* @author mkvore
*/
class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handler.Session
class SAMv3StreamSession extends SAMStreamSession implements Session
{
private static final int BUFFER_SIZE = 1024 ;
@@ -81,7 +81,7 @@ class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handler.Sessi
this.nick = login ;
}
public static SAMv3Handler.SessionsDB getDB()
public static SessionsDB getDB()
{
return SAMv3Handler.sSessionsHash ;
}
@@ -135,7 +135,7 @@ class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handler.Sessi
I2PSocket i2ps = socketMgr.connect(d, opts);
SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if ( rec==null ) throw new InterruptedIOException() ;
@@ -193,7 +193,7 @@ class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handler.Sessi
_acceptors.decrementAndGet();
}
SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if ( rec==null || i2ps==null ) throw new InterruptedIOException() ;
@@ -223,7 +223,7 @@ class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handler.Sessi
*/
public void startForwardingIncoming(Properties props, boolean sendPorts) throws SAMException, InterruptedIOException
{
SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
boolean verbose = !Boolean.parseBoolean(props.getProperty("SILENT"));
if ( rec==null ) throw new InterruptedIOException() ;
@@ -450,7 +450,7 @@ class SAMv3StreamSession extends SAMStreamSession implements SAMv3Handler.Sessi
*/
public void stopForwardingIncoming() throws SAMException, InterruptedIOException
{
SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if ( rec==null ) throw new InterruptedIOException() ;

View File

@@ -0,0 +1,25 @@
package net.i2p.sam;
/*
* free (adj.): unencumbered; not under the control of others
* Written by human in 2004 and released into the public domain
* with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk.
*
*/
import net.i2p.client.I2PSessionException;
import net.i2p.data.DataFormatException;
/**
* A V3 session.
*
* @since 0.9.25 moved from SAMv3Handler
*/
interface Session {
String getNick();
void close();
boolean sendBytes(String dest, byte[] data, int proto,
int fromPort, int toPort) throws DataFormatException, I2PSessionException;
}

View File

@@ -0,0 +1,67 @@
package net.i2p.sam;
/*
* free (adj.): unencumbered; not under the control of others
* Written by human in 2004 and released into the public domain
* with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk.
*
*/
import java.util.Properties;
/**
* The values in the SessionsDB
*
* @since 0.9.25 moved from SAMv3Handler
*/
class SessionRecord {
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_handler = handler ;
}
public SessionRecord( SessionRecord in )
{
m_dest = in.getDest();
m_props = in.getProps();
m_threadgroup = in.getThreadGroup();
m_handler = in.getHandler();
}
public String getDest()
{
return m_dest;
}
synchronized public Properties getProps()
{
Properties p = new Properties();
p.putAll(m_props);
return m_props;
}
public SAMv3Handler getHandler()
{
return m_handler ;
}
synchronized public ThreadGroup getThreadGroup()
{
return m_threadgroup ;
}
synchronized public void createThreadGroup(String name)
{
if (m_threadgroup == null)
m_threadgroup = new ThreadGroup(name);
}
}

View File

@@ -0,0 +1,72 @@
package net.i2p.sam;
/*
* free (adj.): unencumbered; not under the control of others
* Written by human in 2004 and released into the public domain
* with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk.
*
*/
import java.util.HashMap;
/**
* basically a HashMap from String to SessionRecord
*
* @since 0.9.25 moved from SAMv3Handler
*/
class SessionsDB {
private static final long serialVersionUID = 0x1;
static class ExistingIdException extends Exception {
private static final long serialVersionUID = 0x1;
}
static class ExistingDestException extends Exception {
private static final long serialVersionUID = 0x1;
}
private final HashMap<String, SessionRecord> map;
public SessionsDB() {
map = new HashMap<String, SessionRecord>() ;
}
/** @return success */
synchronized public boolean put( String nick, SessionRecord session )
throws ExistingIdException, ExistingDestException
{
if ( map.containsKey(nick) ) {
throw new ExistingIdException();
}
for ( SessionRecord r : map.values() ) {
if (r.getDest().equals(session.getDest())) {
throw new ExistingDestException();
}
}
if ( !map.containsKey(nick) ) {
session.createThreadGroup("SAM session "+nick);
map.put(nick, session) ;
return true ;
}
else
return false ;
}
/** @return true if removed */
synchronized public boolean del( String nick )
{
return map.remove(nick) != null;
}
synchronized public SessionRecord get(String nick)
{
return map.get(nick);
}
synchronized public boolean containsKey( String nick )
{
return map.containsKey(nick);
}
}