* Added support for SESSION CREATE STYLE=STREAM DIRECTION={CREATE|RECEIVE|BOTH}
* M-x untabify (human)
This commit is contained in:
@ -54,36 +54,41 @@ 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,
|
||||
public SAMStreamSession(String dest, String dir, Properties props,
|
||||
SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
||||
ByteArrayInputStream bais;
|
||||
|
||||
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{
|
||||
private void initSAMStreamSession(InputStream destStream, String dir,
|
||||
Properties props, SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException{
|
||||
this.recv = recv;
|
||||
|
||||
_log.debug("SAM STREAM session instantiated");
|
||||
@ -101,11 +106,27 @@ public class SAMStreamSession {
|
||||
throw new SAMException("Error creating I2PSocketManager");
|
||||
}
|
||||
|
||||
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!");
|
||||
}
|
||||
|
||||
if (canReceive) {
|
||||
server = new SAMStreamSessionServer();
|
||||
Thread t = new I2PThread(server, "SAMStreamSessionServer");
|
||||
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SAM STREAM session Destination.
|
||||
@ -123,7 +144,12 @@ 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 {
|
||||
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");
|
||||
}
|
||||
|
||||
if (checkSocketHandlerId(id)) {
|
||||
_log.debug("The specified id (" + id + ") is already in use");
|
||||
return false;
|
||||
@ -168,7 +194,9 @@ public class SAMStreamSession {
|
||||
*
|
||||
*/
|
||||
public void close() {
|
||||
if (server != null) {
|
||||
server.stopRunning();
|
||||
}
|
||||
removeAllSocketHandlers();
|
||||
recv.stopStreamReceiving();
|
||||
}
|
||||
@ -274,7 +302,6 @@ public class SAMStreamSession {
|
||||
* 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;
|
||||
|
@ -205,7 +205,19 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMStrea
|
||||
if (style.equals("RAW")) {
|
||||
rawSession = new SAMRawSession(dest, props, this);
|
||||
} else if (style.equals("STREAM")) {
|
||||
streamSession = new SAMStreamSession(dest, props, this);
|
||||
String dir = props.getProperty("DIRECTION");
|
||||
if (dir == null) {
|
||||
_log.debug("No DIRECTION parameter in STREAM session");
|
||||
return false;
|
||||
}
|
||||
if (!dir.equals("CREATE") && !dir.equals("RECEIVE")
|
||||
&& !dir.equals("BOTH")) {
|
||||
_log.debug("Unknow DIRECTION parameter value: " + dir);
|
||||
return false;
|
||||
}
|
||||
props.remove("DIRECTION");
|
||||
|
||||
streamSession = new SAMStreamSession(dest, dir,props,this);
|
||||
} else {
|
||||
_log.debug("Unrecognized SESSION STYLE: \"" + style +"\"");
|
||||
return false;
|
||||
@ -489,6 +501,10 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMStrea
|
||||
_log.debug("STREAM CONNECT failed: " + e.getMessage());
|
||||
return writeString("STREAM STATUS RESULT=I2P_ERROR ID="
|
||||
+ id + "\n");
|
||||
} catch (SAMInvalidDirectionException e) {
|
||||
_log.debug("STREAM CONNECT failed: " + e.getMessage());
|
||||
return writeString("STREAM STATUS RESULT=INVALID_DIRECTION ID="
|
||||
+ id + "\n");
|
||||
}
|
||||
} else if (opcode.equals("CLOSE")) {
|
||||
if (props == null) {
|
||||
|
Reference in New Issue
Block a user