* Added support for SESSION CREATE STYLE=STREAM DIRECTION={CREATE|RECEIVE|BOTH}
* M-x untabify (human)
This commit is contained in:
@ -43,8 +43,8 @@ public abstract class SAMHandler implements Runnable {
|
||||
*
|
||||
*/
|
||||
public void startHandling() {
|
||||
thread = new I2PThread(this, "SAMHandler");
|
||||
thread.start();
|
||||
thread = new I2PThread(this, "SAMHandler");
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,13 +61,13 @@ public abstract class SAMHandler implements Runnable {
|
||||
* @param data A byte array to be written
|
||||
*/
|
||||
protected void writeBytes(byte[] data) throws IOException {
|
||||
synchronized (socketWLock) {
|
||||
if (socketOS == null) {
|
||||
socketOS = socket.getOutputStream();
|
||||
}
|
||||
socketOS.write(data);
|
||||
socketOS.flush();
|
||||
}
|
||||
synchronized (socketWLock) {
|
||||
if (socketOS == null) {
|
||||
socketOS = socket.getOutputStream();
|
||||
}
|
||||
socketOS.write(data);
|
||||
socketOS.flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -80,14 +80,14 @@ public abstract class SAMHandler implements Runnable {
|
||||
* @return True is the string was successfully written, false otherwise
|
||||
*/
|
||||
protected boolean writeString(String str) {
|
||||
try {
|
||||
writeBytes(str.getBytes("ISO-8859-1"));
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
writeBytes(str.getBytes("ISO-8859-1"));
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,9 +95,9 @@ public abstract class SAMHandler implements Runnable {
|
||||
*
|
||||
*/
|
||||
public void stopHandling() {
|
||||
synchronized (stopLock) {
|
||||
stopHandler = true;
|
||||
}
|
||||
synchronized (stopLock) {
|
||||
stopHandler = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,9 +106,9 @@ public abstract class SAMHandler implements Runnable {
|
||||
* @return True if the handler should be stopped, false otherwise
|
||||
*/
|
||||
protected boolean shouldStop() {
|
||||
synchronized (stopLock) {
|
||||
return stopHandler;
|
||||
}
|
||||
synchronized (stopLock) {
|
||||
return stopHandler;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -119,6 +119,6 @@ public abstract class SAMHandler implements Runnable {
|
||||
public abstract String toString();
|
||||
|
||||
public final void run() {
|
||||
handle();
|
||||
handle();
|
||||
}
|
||||
}
|
||||
|
@ -35,145 +35,145 @@ public class SAMHandlerFactory {
|
||||
* @return A SAM protocol handler
|
||||
*/
|
||||
public static SAMHandler createSAMHandler(Socket s) throws SAMException {
|
||||
BufferedReader br;
|
||||
StringTokenizer tok;
|
||||
BufferedReader br;
|
||||
StringTokenizer tok;
|
||||
|
||||
try {
|
||||
br = new BufferedReader(new InputStreamReader(s.getInputStream(),
|
||||
"ISO-8859-1"));
|
||||
tok = new StringTokenizer(br.readLine(), " ");
|
||||
} catch (IOException e) {
|
||||
throw new SAMException("Error reading from socket: "
|
||||
+ e.getMessage());
|
||||
} catch (Exception e) {
|
||||
throw new SAMException("Unexpected error: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
try {
|
||||
br = new BufferedReader(new InputStreamReader(s.getInputStream(),
|
||||
"ISO-8859-1"));
|
||||
tok = new StringTokenizer(br.readLine(), " ");
|
||||
} catch (IOException e) {
|
||||
throw new SAMException("Error reading from socket: "
|
||||
+ e.getMessage());
|
||||
} catch (Exception e) {
|
||||
throw new SAMException("Unexpected error: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
|
||||
// Message format: HELLO VERSION MIN=v1 MAX=v2
|
||||
if (tok.countTokens() != 4) {
|
||||
throw new SAMException("Bad format in HELLO message");
|
||||
}
|
||||
if (!tok.nextToken().equals("HELLO")) {
|
||||
throw new SAMException("Bad domain in HELLO message");
|
||||
}
|
||||
{
|
||||
String opcode;
|
||||
if (!(opcode = tok.nextToken()).equals("VERSION")) {
|
||||
throw new SAMException("Unrecognized HELLO message opcode: \""
|
||||
+ opcode + "\"");
|
||||
}
|
||||
}
|
||||
// Message format: HELLO VERSION MIN=v1 MAX=v2
|
||||
if (tok.countTokens() != 4) {
|
||||
throw new SAMException("Bad format in HELLO message");
|
||||
}
|
||||
if (!tok.nextToken().equals("HELLO")) {
|
||||
throw new SAMException("Bad domain in HELLO message");
|
||||
}
|
||||
{
|
||||
String opcode;
|
||||
if (!(opcode = tok.nextToken()).equals("VERSION")) {
|
||||
throw new SAMException("Unrecognized HELLO message opcode: \""
|
||||
+ opcode + "\"");
|
||||
}
|
||||
}
|
||||
|
||||
Properties props;
|
||||
props = SAMUtils.parseParams(tok);
|
||||
if (props == null) {
|
||||
throw new SAMException("No parameters in HELLO VERSION message");
|
||||
}
|
||||
Properties props;
|
||||
props = SAMUtils.parseParams(tok);
|
||||
if (props == null) {
|
||||
throw new SAMException("No parameters in HELLO VERSION message");
|
||||
}
|
||||
|
||||
String minVer = props.getProperty("MIN");
|
||||
if (minVer == null) {
|
||||
throw new SAMException("Missing MIN parameter in HELLO VERSION message");
|
||||
}
|
||||
String minVer = props.getProperty("MIN");
|
||||
if (minVer == null) {
|
||||
throw new SAMException("Missing MIN parameter in HELLO VERSION message");
|
||||
}
|
||||
|
||||
String maxVer = props.getProperty("MAX");
|
||||
if (maxVer == null) {
|
||||
throw new SAMException("Missing MAX parameter in HELLO VERSION message");
|
||||
}
|
||||
String maxVer = props.getProperty("MAX");
|
||||
if (maxVer == null) {
|
||||
throw new SAMException("Missing MAX parameter in HELLO VERSION message");
|
||||
}
|
||||
|
||||
String ver = chooseBestVersion(minVer, maxVer);
|
||||
if (ver == null) {
|
||||
// Let's answer negatively
|
||||
try {
|
||||
OutputStream out = s.getOutputStream();
|
||||
out.write("HELLO REPLY RESULT=NOVERSION\n".getBytes("ISO-8859-1"));
|
||||
return null;
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
_log.error("Caught UnsupportedEncodingException ("
|
||||
+ e.getMessage() + ")");
|
||||
throw new SAMException("Character encoding error: "
|
||||
+ e.getMessage());
|
||||
} catch (IOException e) {
|
||||
throw new SAMException("Error reading from socket: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
}
|
||||
String ver = chooseBestVersion(minVer, maxVer);
|
||||
if (ver == null) {
|
||||
// Let's answer negatively
|
||||
try {
|
||||
OutputStream out = s.getOutputStream();
|
||||
out.write("HELLO REPLY RESULT=NOVERSION\n".getBytes("ISO-8859-1"));
|
||||
return null;
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
_log.error("Caught UnsupportedEncodingException ("
|
||||
+ e.getMessage() + ")");
|
||||
throw new SAMException("Character encoding error: "
|
||||
+ e.getMessage());
|
||||
} catch (IOException e) {
|
||||
throw new SAMException("Error reading from socket: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Let's answer positively
|
||||
try {
|
||||
OutputStream out = s.getOutputStream();
|
||||
out.write(("HELLO REPLY RESULT=OK VERSION="
|
||||
+ ver + "\n").getBytes("ISO-8859-1"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
_log.error("Caught UnsupportedEncodingException ("
|
||||
+ e.getMessage() + ")");
|
||||
throw new SAMException("Character encoding error: "
|
||||
+ e.getMessage());
|
||||
} catch (IOException e) {
|
||||
throw new SAMException("Error writing to socket: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
// Let's answer positively
|
||||
try {
|
||||
OutputStream out = s.getOutputStream();
|
||||
out.write(("HELLO REPLY RESULT=OK VERSION="
|
||||
+ ver + "\n").getBytes("ISO-8859-1"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
_log.error("Caught UnsupportedEncodingException ("
|
||||
+ e.getMessage() + ")");
|
||||
throw new SAMException("Character encoding error: "
|
||||
+ e.getMessage());
|
||||
} catch (IOException e) {
|
||||
throw new SAMException("Error writing to socket: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
|
||||
// ...and instantiate the right SAM handler
|
||||
int verMajor = getMajor(ver);
|
||||
int verMinor = getMinor(ver);
|
||||
SAMHandler handler;
|
||||
switch (verMajor) {
|
||||
case 1:
|
||||
handler = new SAMv1Handler(s, verMajor, verMinor);
|
||||
break;
|
||||
default:
|
||||
_log.error("BUG! Trying to initialize the wrong SAM version!");
|
||||
throw new SAMException("BUG triggered! (handler instantiation)");
|
||||
}
|
||||
// ...and instantiate the right SAM handler
|
||||
int verMajor = getMajor(ver);
|
||||
int verMinor = getMinor(ver);
|
||||
SAMHandler handler;
|
||||
switch (verMajor) {
|
||||
case 1:
|
||||
handler = new SAMv1Handler(s, verMajor, verMinor);
|
||||
break;
|
||||
default:
|
||||
_log.error("BUG! Trying to initialize the wrong SAM version!");
|
||||
throw new SAMException("BUG triggered! (handler instantiation)");
|
||||
}
|
||||
|
||||
return handler;
|
||||
return handler;
|
||||
}
|
||||
|
||||
/* Return the best version we can use, or null on failure */
|
||||
private static String chooseBestVersion(String minVer, String maxVer) {
|
||||
int minMajor = getMajor(minVer), minMinor = getMinor(minVer);
|
||||
int maxMajor = getMajor(maxVer), maxMinor = getMinor(maxVer);
|
||||
int minMajor = getMajor(minVer), minMinor = getMinor(minVer);
|
||||
int maxMajor = getMajor(maxVer), maxMinor = getMinor(maxVer);
|
||||
|
||||
// Consistency checks
|
||||
if ((minMajor == -1) || (minMinor == -1)
|
||||
|| (maxMajor == -1) || (maxMinor == -1)) {
|
||||
return null;
|
||||
}
|
||||
if (minMajor > maxMajor) {
|
||||
return null;
|
||||
} else if ((minMajor == maxMajor) && (minMinor > maxMinor)) {
|
||||
return null;
|
||||
}
|
||||
// Consistency checks
|
||||
if ((minMajor == -1) || (minMinor == -1)
|
||||
|| (maxMajor == -1) || (maxMinor == -1)) {
|
||||
return null;
|
||||
}
|
||||
if (minMajor > maxMajor) {
|
||||
return null;
|
||||
} else if ((minMajor == maxMajor) && (minMinor > maxMinor)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ((minMajor >= 1) && (minMinor >= 0)) {
|
||||
return "1.0";
|
||||
}
|
||||
|
||||
return null;
|
||||
if ((minMajor >= 1) && (minMinor >= 0)) {
|
||||
return "1.0";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Get the major protocol version from a string */
|
||||
private static int getMajor(String ver) {
|
||||
try {
|
||||
String major = ver.substring(0, ver.indexOf("."));
|
||||
return Integer.parseInt(major);
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
String major = ver.substring(0, ver.indexOf("."));
|
||||
return Integer.parseInt(major);
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the minor protocol version from a string */
|
||||
private static int getMinor(String ver) {
|
||||
try {
|
||||
String major = ver.substring(ver.indexOf(".") + 1);
|
||||
return Integer.parseInt(major);
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
String major = ver.substring(ver.indexOf(".") + 1);
|
||||
return Integer.parseInt(major);
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -54,57 +54,78 @@ public class SAMStreamSession {
|
||||
private Object idLock = new Object();
|
||||
private int lastNegativeId = 0;
|
||||
|
||||
// Can we create outgoing connections?
|
||||
private boolean canCreate = false;
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session.
|
||||
*
|
||||
* @param dest Base64-encoded destination (private key)
|
||||
* @param dir Session direction ("RECEIVE", "CREATE" or "BOTH")
|
||||
* @param props Properties to setup the I2P session
|
||||
* @param recv Object that will receive incoming data
|
||||
*/
|
||||
public SAMStreamSession(String dest, Properties props,
|
||||
SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
||||
ByteArrayInputStream bais;
|
||||
public SAMStreamSession(String dest, String dir, Properties props,
|
||||
SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
||||
ByteArrayInputStream bais;
|
||||
|
||||
bais = new ByteArrayInputStream(Base64.decode(dest));
|
||||
bais = new ByteArrayInputStream(Base64.decode(dest));
|
||||
|
||||
initSAMStreamSession(bais, props, recv);
|
||||
initSAMStreamSession(bais, dir, props, recv);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session.
|
||||
*
|
||||
* @param destStream Input stream containing the destination keys
|
||||
* @param dir Session direction ("RECEIVE", "CREATE" or "BOTH")
|
||||
* @param props Properties to setup the I2P session
|
||||
* @param recv Object that will receive incoming data
|
||||
*/
|
||||
public SAMStreamSession(InputStream destStream, Properties props,
|
||||
SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
||||
initSAMStreamSession(destStream, props, recv);
|
||||
public SAMStreamSession(InputStream destStream, String dir,
|
||||
Properties props, SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
||||
initSAMStreamSession(destStream, dir, props, recv);
|
||||
}
|
||||
|
||||
private void initSAMStreamSession(InputStream destStream, Properties props,
|
||||
SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException{
|
||||
this.recv = recv;
|
||||
private void initSAMStreamSession(InputStream destStream, String dir,
|
||||
Properties props, SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException{
|
||||
this.recv = recv;
|
||||
|
||||
_log.debug("SAM STREAM session instantiated");
|
||||
_log.debug("SAM STREAM session instantiated");
|
||||
|
||||
Properties allprops = new Properties();
|
||||
allprops.putAll(System.getProperties());
|
||||
allprops.putAll(props);
|
||||
allprops.putAll(props);
|
||||
|
||||
// FIXME: we should setup I2CP host and port, too
|
||||
_log.debug("Creating I2PSocketManager...");
|
||||
// FIXME: we should setup I2CP host and port, too
|
||||
_log.debug("Creating I2PSocketManager...");
|
||||
socketMgr = I2PSocketManagerFactory.createManager(destStream,
|
||||
"127.0.0.1",
|
||||
7654, allprops);
|
||||
if (socketMgr == null) {
|
||||
throw new SAMException("Error creating I2PSocketManager");
|
||||
}
|
||||
"127.0.0.1",
|
||||
7654, allprops);
|
||||
if (socketMgr == null) {
|
||||
throw new SAMException("Error creating I2PSocketManager");
|
||||
}
|
||||
|
||||
server = new SAMStreamSessionServer();
|
||||
Thread t = new I2PThread(server, "SAMStreamSessionServer");
|
||||
boolean canReceive = false;
|
||||
if (dir.equals("BOTH")) {
|
||||
canCreate = true;
|
||||
canReceive = true;
|
||||
} else if (dir.equals("CREATE")) {
|
||||
canCreate = true;
|
||||
} else if (dir.equals("RECEIVE")) {
|
||||
canReceive = true;
|
||||
} else {
|
||||
_log.error("BUG! Wrong direction passed to SAMStreamSession: "
|
||||
+ dir);
|
||||
throw new SAMException("BUG! Wrong direction specified!");
|
||||
}
|
||||
|
||||
t.start();
|
||||
if (canReceive) {
|
||||
server = new SAMStreamSessionServer();
|
||||
Thread t = new I2PThread(server, "SAMStreamSessionServer");
|
||||
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,7 +134,7 @@ public class SAMStreamSession {
|
||||
* @return The SAM STREAM session Destination.
|
||||
*/
|
||||
public Destination getDestination() {
|
||||
return socketMgr.getSession().getMyDestination();
|
||||
return socketMgr.getSession().getMyDestination();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,25 +144,30 @@ public class SAMStreamSession {
|
||||
* @param dest Base64-encoded Destination to connect to
|
||||
* @param props Options to be used for connection
|
||||
*/
|
||||
public boolean connect(int id, String dest, Properties props) throws I2PException, DataFormatException {
|
||||
if (checkSocketHandlerId(id)) {
|
||||
_log.debug("The specified id (" + id + ") is already in use");
|
||||
return false;
|
||||
}
|
||||
public boolean connect(int id, String dest, Properties props) throws I2PException, DataFormatException, SAMInvalidDirectionException {
|
||||
if (!canCreate) {
|
||||
_log.debug("Trying to create an outgoing connection using a receive-only session");
|
||||
throw new SAMInvalidDirectionException("Trying to create connections through a receive-only session");
|
||||
}
|
||||
|
||||
Destination d = new Destination();
|
||||
d.fromBase64(dest);
|
||||
if (checkSocketHandlerId(id)) {
|
||||
_log.debug("The specified id (" + id + ") is already in use");
|
||||
return false;
|
||||
}
|
||||
|
||||
// FIXME: we should config I2PSocketOptions here
|
||||
Destination d = new Destination();
|
||||
d.fromBase64(dest);
|
||||
|
||||
// FIXME: we should config I2PSocketOptions here
|
||||
I2PSocketOptions opts = new I2PSocketOptions();
|
||||
opts.setConnectTimeout(60 * 1000);
|
||||
|
||||
_log.debug("Connecting new I2PSocket...");
|
||||
I2PSocket i2ps = socketMgr.connect(d, opts);
|
||||
_log.debug("Connecting new I2PSocket...");
|
||||
I2PSocket i2ps = socketMgr.connect(d, opts);
|
||||
|
||||
createSocketHandler(i2ps, id);
|
||||
createSocketHandler(i2ps, id);
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -152,15 +178,15 @@ public class SAMStreamSession {
|
||||
* @return True if the data was sent, false otherwise
|
||||
*/
|
||||
public boolean sendBytes(int id, byte[] data) {
|
||||
Destination d = new Destination();
|
||||
SAMStreamSessionSocketHandler handler = getSocketHandler(id);
|
||||
|
||||
if (handler == null) {
|
||||
_log.error("Trying to send bytes through inexistent handler " +id);
|
||||
return false;
|
||||
}
|
||||
Destination d = new Destination();
|
||||
SAMStreamSessionSocketHandler handler = getSocketHandler(id);
|
||||
|
||||
if (handler == null) {
|
||||
_log.error("Trying to send bytes through inexistent handler " +id);
|
||||
return false;
|
||||
}
|
||||
|
||||
return handler.sendBytes(data);
|
||||
return handler.sendBytes(data);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -168,9 +194,11 @@ public class SAMStreamSession {
|
||||
*
|
||||
*/
|
||||
public void close() {
|
||||
server.stopRunning();
|
||||
removeAllSocketHandlers();
|
||||
recv.stopStreamReceiving();
|
||||
if (server != null) {
|
||||
server.stopRunning();
|
||||
}
|
||||
removeAllSocketHandlers();
|
||||
recv.stopStreamReceiving();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -179,13 +207,13 @@ public class SAMStreamSession {
|
||||
* @param id Connection id
|
||||
*/
|
||||
public boolean closeConnection(int id) {
|
||||
if (!checkSocketHandlerId(id)) {
|
||||
_log.debug("The specified id (" + id + ") does not exist!");
|
||||
return false;
|
||||
}
|
||||
removeSocketHandler(id);
|
||||
if (!checkSocketHandlerId(id)) {
|
||||
_log.debug("The specified id (" + id + ") does not exist!");
|
||||
return false;
|
||||
}
|
||||
removeSocketHandler(id);
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -197,34 +225,34 @@ public class SAMStreamSession {
|
||||
* @return An id associated to the socket handler
|
||||
*/
|
||||
private int createSocketHandler(I2PSocket s, int id) {
|
||||
SAMStreamSessionSocketHandler handler;
|
||||
if (id == 0) {
|
||||
id = createUniqueId();
|
||||
}
|
||||
SAMStreamSessionSocketHandler handler;
|
||||
if (id == 0) {
|
||||
id = createUniqueId();
|
||||
}
|
||||
|
||||
try {
|
||||
handler = new SAMStreamSessionSocketHandler(s, id);
|
||||
} catch (IOException e) {
|
||||
_log.error("IOException when creating SAM STREAM session socket handler", e);
|
||||
recv.stopStreamReceiving();
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
handler = new SAMStreamSessionSocketHandler(s, id);
|
||||
} catch (IOException e) {
|
||||
_log.error("IOException when creating SAM STREAM session socket handler", e);
|
||||
recv.stopStreamReceiving();
|
||||
return 0;
|
||||
}
|
||||
|
||||
synchronized (handlersMapLock) {
|
||||
handlersMap.put(new Integer(id), handler);
|
||||
}
|
||||
synchronized (handlersMapLock) {
|
||||
handlersMap.put(new Integer(id), handler);
|
||||
}
|
||||
|
||||
I2PThread t = new I2PThread(handler, "SAMStreamSessionSocketHandler");
|
||||
t.start();
|
||||
I2PThread t = new I2PThread(handler, "SAMStreamSessionSocketHandler");
|
||||
t.start();
|
||||
|
||||
return id;
|
||||
return id;
|
||||
}
|
||||
|
||||
/* Create an unique id, either positive or negative */
|
||||
private int createUniqueId() {
|
||||
synchronized (idLock) {
|
||||
return --lastNegativeId;
|
||||
}
|
||||
synchronized (idLock) {
|
||||
return --lastNegativeId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -233,9 +261,9 @@ public class SAMStreamSession {
|
||||
* @param id Handler id
|
||||
*/
|
||||
private SAMStreamSessionSocketHandler getSocketHandler(int id) {
|
||||
synchronized (handlersMapLock) {
|
||||
return (SAMStreamSessionSocketHandler)handlersMap.get(new Integer(id));
|
||||
}
|
||||
synchronized (handlersMapLock) {
|
||||
return (SAMStreamSessionSocketHandler)handlersMap.get(new Integer(id));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -244,9 +272,9 @@ public class SAMStreamSession {
|
||||
* @param id Handler id
|
||||
*/
|
||||
private boolean checkSocketHandlerId(int id) {
|
||||
synchronized (handlersMapLock) {
|
||||
return (!(handlersMap.get(new Integer(id)) == null));
|
||||
}
|
||||
synchronized (handlersMapLock) {
|
||||
return (!(handlersMap.get(new Integer(id)) == null));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -255,42 +283,41 @@ public class SAMStreamSession {
|
||||
* @param id Handler id to be removed
|
||||
*/
|
||||
private void removeSocketHandler(int id) {
|
||||
SAMStreamSessionSocketHandler removed;
|
||||
SAMStreamSessionSocketHandler removed;
|
||||
|
||||
synchronized (handlersMapLock) {
|
||||
removed = (SAMStreamSessionSocketHandler)handlersMap.remove(new Integer(id));
|
||||
}
|
||||
synchronized (handlersMapLock) {
|
||||
removed = (SAMStreamSessionSocketHandler)handlersMap.remove(new Integer(id));
|
||||
}
|
||||
|
||||
if (removed == null) {
|
||||
_log.error("BUG! Trying to remove inexistent SAM STREAM session socket handler " + id);
|
||||
recv.stopStreamReceiving();
|
||||
} else {
|
||||
removed.stopRunning();
|
||||
_log.debug("Removed SAM STREAM session socket handler " + id);
|
||||
}
|
||||
if (removed == null) {
|
||||
_log.error("BUG! Trying to remove inexistent SAM STREAM session socket handler " + id);
|
||||
recv.stopStreamReceiving();
|
||||
} else {
|
||||
removed.stopRunning();
|
||||
_log.debug("Removed SAM STREAM session socket handler " + id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove and close all the socket handlers managed by this SAM
|
||||
* STREAM session.
|
||||
*
|
||||
* @param id Handler id to be removed
|
||||
*/
|
||||
private void removeAllSocketHandlers() {
|
||||
Integer id;
|
||||
Set keySet;
|
||||
Iterator iter;
|
||||
Integer id;
|
||||
Set keySet;
|
||||
Iterator iter;
|
||||
|
||||
synchronized (handlersMapLock) {
|
||||
keySet = handlersMap.keySet();
|
||||
iter = keySet.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
id = (Integer)iter.next();
|
||||
((SAMStreamSessionSocketHandler)handlersMap.get(id)).stopRunning();
|
||||
}
|
||||
handlersMap.clear();
|
||||
}
|
||||
synchronized (handlersMapLock) {
|
||||
keySet = handlersMap.keySet();
|
||||
iter = keySet.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
id = (Integer)iter.next();
|
||||
((SAMStreamSessionSocketHandler)handlersMap.get(id)).stopRunning();
|
||||
}
|
||||
handlersMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -301,76 +328,76 @@ public class SAMStreamSession {
|
||||
*/
|
||||
public class SAMStreamSessionServer implements Runnable {
|
||||
|
||||
private Object runningLock = new Object();
|
||||
private boolean stillRunning = true;
|
||||
private Object runningLock = new Object();
|
||||
private boolean stillRunning = true;
|
||||
|
||||
private I2PServerSocket serverSocket = null;
|
||||
private I2PServerSocket serverSocket = null;
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session server
|
||||
*
|
||||
*/
|
||||
public SAMStreamSessionServer() {
|
||||
_log.debug("Instantiating new SAM STREAM session server");
|
||||
/**
|
||||
* Create a new SAM STREAM session server
|
||||
*
|
||||
*/
|
||||
public SAMStreamSessionServer() {
|
||||
_log.debug("Instantiating new SAM STREAM session server");
|
||||
|
||||
serverSocket = socketMgr.getServerSocket();
|
||||
}
|
||||
serverSocket = socketMgr.getServerSocket();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop a SAM STREAM session server
|
||||
*
|
||||
*/
|
||||
public void stopRunning() {
|
||||
_log.debug("SAMStreamSessionServer.stopRunning() invoked");
|
||||
synchronized (runningLock) {
|
||||
if (stillRunning) {
|
||||
stillRunning = false;
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (I2PException e) {
|
||||
_log.error("I2PException caught", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Stop a SAM STREAM session server
|
||||
*
|
||||
*/
|
||||
public void stopRunning() {
|
||||
_log.debug("SAMStreamSessionServer.stopRunning() invoked");
|
||||
synchronized (runningLock) {
|
||||
if (stillRunning) {
|
||||
stillRunning = false;
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (I2PException e) {
|
||||
_log.error("I2PException caught", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
_log.debug("SAM STREAM session server running");
|
||||
I2PSocket i2ps;
|
||||
public void run() {
|
||||
_log.debug("SAM STREAM session server running");
|
||||
I2PSocket i2ps;
|
||||
|
||||
while (stillRunning) {
|
||||
try {
|
||||
i2ps = serverSocket.accept();
|
||||
while (stillRunning) {
|
||||
try {
|
||||
i2ps = serverSocket.accept();
|
||||
|
||||
_log.debug("New incoming connection");
|
||||
_log.debug("New incoming connection");
|
||||
|
||||
int id = createSocketHandler(i2ps, 0);
|
||||
if (id == 0) {
|
||||
_log.error("SAM STREAM session handler not created!");
|
||||
i2ps.close();
|
||||
continue;
|
||||
}
|
||||
int id = createSocketHandler(i2ps, 0);
|
||||
if (id == 0) {
|
||||
_log.error("SAM STREAM session handler not created!");
|
||||
i2ps.close();
|
||||
continue;
|
||||
}
|
||||
|
||||
_log.debug("New connection id: " + id);
|
||||
recv.notifyStreamConnection(id, i2ps.getPeerDestination());
|
||||
} catch (I2PException e) {
|
||||
_log.debug("Caught I2PException", e);
|
||||
break;
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
_log.debug("New connection id: " + id);
|
||||
recv.notifyStreamConnection(id, i2ps.getPeerDestination());
|
||||
} catch (I2PException e) {
|
||||
_log.debug("Caught I2PException", e);
|
||||
break;
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
serverSocket.close(); // In case it wasn't closed, yet
|
||||
} catch (I2PException e) {
|
||||
_log.debug("Caught I2PException", e);
|
||||
}
|
||||
try {
|
||||
serverSocket.close(); // In case it wasn't closed, yet
|
||||
} catch (I2PException e) {
|
||||
_log.debug("Caught I2PException", e);
|
||||
}
|
||||
|
||||
_log.debug("Shutting down SAM STREAM session server");
|
||||
}
|
||||
|
||||
_log.debug("Shutting down SAM STREAM session server");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -380,109 +407,109 @@ public class SAMStreamSession {
|
||||
* @author human
|
||||
*/
|
||||
public class SAMStreamSessionSocketHandler implements Runnable {
|
||||
|
||||
private I2PSocket i2pSocket = null;
|
||||
private OutputStream i2pSocketOS = null;
|
||||
|
||||
private I2PSocket i2pSocket = null;
|
||||
private OutputStream i2pSocketOS = null;
|
||||
|
||||
private Object runningLock = new Object();
|
||||
private boolean stillRunning = true;
|
||||
private Object runningLock = new Object();
|
||||
private boolean stillRunning = true;
|
||||
|
||||
private int id;
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session socket handler
|
||||
*
|
||||
* @param s Socket to be handled
|
||||
* @param id Unique id assigned to the handler
|
||||
*/
|
||||
public SAMStreamSessionSocketHandler(I2PSocket s, int id) throws IOException {
|
||||
_log.debug("Instantiating new SAM STREAM session socket handler");
|
||||
private int id;
|
||||
|
||||
/**
|
||||
* Create a new SAM STREAM session socket handler
|
||||
*
|
||||
* @param s Socket to be handled
|
||||
* @param id Unique id assigned to the handler
|
||||
*/
|
||||
public SAMStreamSessionSocketHandler(I2PSocket s, int id) throws IOException {
|
||||
_log.debug("Instantiating new SAM STREAM session socket handler");
|
||||
|
||||
i2pSocket = s;
|
||||
i2pSocketOS = s.getOutputStream();
|
||||
this.id = id;
|
||||
}
|
||||
i2pSocket = s;
|
||||
i2pSocketOS = s.getOutputStream();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send bytes through the SAM STREAM session socket handler
|
||||
*
|
||||
* @param data Data to be sent
|
||||
*
|
||||
* @return True if data has been sent without errors, false otherwise
|
||||
*/
|
||||
public boolean sendBytes(byte[] data) {
|
||||
if (_log.shouldLog(Log.DEBUG)) {
|
||||
_log.debug("Handler " + id + ": sending " + data.length
|
||||
+ " bytes");
|
||||
}
|
||||
try {
|
||||
i2pSocketOS.write(data);
|
||||
} catch (IOException e) {
|
||||
_log.error("Error sending data through I2P socket", e);
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Send bytes through the SAM STREAM session socket handler
|
||||
*
|
||||
* @param data Data to be sent
|
||||
*
|
||||
* @return True if data has been sent without errors, false otherwise
|
||||
*/
|
||||
public boolean sendBytes(byte[] data) {
|
||||
if (_log.shouldLog(Log.DEBUG)) {
|
||||
_log.debug("Handler " + id + ": sending " + data.length
|
||||
+ " bytes");
|
||||
}
|
||||
try {
|
||||
i2pSocketOS.write(data);
|
||||
} catch (IOException e) {
|
||||
_log.error("Error sending data through I2P socket", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop a SAM STREAM session socket handler
|
||||
*
|
||||
*/
|
||||
public void stopRunning() {
|
||||
_log.debug("stopRunning() invoked on socket handler " + id);
|
||||
synchronized (runningLock) {
|
||||
if (stillRunning) {
|
||||
stillRunning = false;
|
||||
try {
|
||||
i2pSocket.close();
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Stop a SAM STREAM session socket handler
|
||||
*
|
||||
*/
|
||||
public void stopRunning() {
|
||||
_log.debug("stopRunning() invoked on socket handler " + id);
|
||||
synchronized (runningLock) {
|
||||
if (stillRunning) {
|
||||
stillRunning = false;
|
||||
try {
|
||||
i2pSocket.close();
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
_log.debug("SAM STREAM session socket handler running");
|
||||
public void run() {
|
||||
_log.debug("SAM STREAM session socket handler running");
|
||||
|
||||
int read = -1;
|
||||
byte[] data = new byte[SOCKET_HANDLER_BUF_SIZE];
|
||||
int read = -1;
|
||||
byte[] data = new byte[SOCKET_HANDLER_BUF_SIZE];
|
||||
|
||||
try {
|
||||
InputStream in = i2pSocket.getInputStream();
|
||||
try {
|
||||
InputStream in = i2pSocket.getInputStream();
|
||||
|
||||
while (stillRunning) {
|
||||
read = in.read(data);
|
||||
if (read == -1) {
|
||||
_log.debug("Handler " + id + ": connection closed");
|
||||
break;
|
||||
}
|
||||
|
||||
recv.receiveStreamBytes(id, data, read);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
}
|
||||
|
||||
try {
|
||||
i2pSocket.close();
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
}
|
||||
while (stillRunning) {
|
||||
read = in.read(data);
|
||||
if (read == -1) {
|
||||
_log.debug("Handler " + id + ": connection closed");
|
||||
break;
|
||||
}
|
||||
|
||||
recv.receiveStreamBytes(id, data, read);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
}
|
||||
|
||||
try {
|
||||
i2pSocket.close();
|
||||
} catch (IOException e) {
|
||||
_log.debug("Caught IOException", e);
|
||||
}
|
||||
|
||||
if (stillRunning) {
|
||||
removeSocketHandler(id);
|
||||
// FIXME: we need error reporting here!
|
||||
try {
|
||||
recv.notifyStreamDisconnection(id, "OK", null);
|
||||
} catch (IOException e) {
|
||||
_log.debug("Error sending disconnection notice for handler "
|
||||
+ id, e);
|
||||
}
|
||||
}
|
||||
if (stillRunning) {
|
||||
removeSocketHandler(id);
|
||||
// FIXME: we need error reporting here!
|
||||
try {
|
||||
recv.notifyStreamDisconnection(id, "OK", null);
|
||||
} catch (IOException e) {
|
||||
_log.debug("Error sending disconnection notice for handler "
|
||||
+ id, e);
|
||||
}
|
||||
}
|
||||
|
||||
_log.debug("Shutting down SAM STREAM session socket handler " +id);
|
||||
}
|
||||
_log.debug("Shutting down SAM STREAM session socket handler " +id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user