* 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 Object idLock = new Object();
|
||||||
private int lastNegativeId = 0;
|
private int lastNegativeId = 0;
|
||||||
|
|
||||||
|
// Can we create outgoing connections?
|
||||||
|
private boolean canCreate = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new SAM STREAM session.
|
* Create a new SAM STREAM session.
|
||||||
*
|
*
|
||||||
* @param dest Base64-encoded destination (private key)
|
* @param dest Base64-encoded destination (private key)
|
||||||
|
* @param dir Session direction ("RECEIVE", "CREATE" or "BOTH")
|
||||||
* @param props Properties to setup the I2P session
|
* @param props Properties to setup the I2P session
|
||||||
* @param recv Object that will receive incoming data
|
* @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 {
|
SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
||||||
ByteArrayInputStream bais;
|
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.
|
* Create a new SAM STREAM session.
|
||||||
*
|
*
|
||||||
* @param destStream Input stream containing the destination keys
|
* @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 props Properties to setup the I2P session
|
||||||
* @param recv Object that will receive incoming data
|
* @param recv Object that will receive incoming data
|
||||||
*/
|
*/
|
||||||
public SAMStreamSession(InputStream destStream, Properties props,
|
public SAMStreamSession(InputStream destStream, String dir,
|
||||||
SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
Properties props, SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException {
|
||||||
initSAMStreamSession(destStream, props, recv);
|
initSAMStreamSession(destStream, dir, props, recv);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initSAMStreamSession(InputStream destStream, Properties props,
|
private void initSAMStreamSession(InputStream destStream, String dir,
|
||||||
SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException{
|
Properties props, SAMStreamReceiver recv) throws IOException, DataFormatException, SAMException{
|
||||||
this.recv = recv;
|
this.recv = recv;
|
||||||
|
|
||||||
_log.debug("SAM STREAM session instantiated");
|
_log.debug("SAM STREAM session instantiated");
|
||||||
@ -101,11 +106,27 @@ public class SAMStreamSession {
|
|||||||
throw new SAMException("Error creating I2PSocketManager");
|
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();
|
server = new SAMStreamSessionServer();
|
||||||
Thread t = new I2PThread(server, "SAMStreamSessionServer");
|
Thread t = new I2PThread(server, "SAMStreamSessionServer");
|
||||||
|
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the SAM STREAM session Destination.
|
* Get the SAM STREAM session Destination.
|
||||||
@ -123,7 +144,12 @@ public class SAMStreamSession {
|
|||||||
* @param dest Base64-encoded Destination to connect to
|
* @param dest Base64-encoded Destination to connect to
|
||||||
* @param props Options to be used for connection
|
* @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)) {
|
if (checkSocketHandlerId(id)) {
|
||||||
_log.debug("The specified id (" + id + ") is already in use");
|
_log.debug("The specified id (" + id + ") is already in use");
|
||||||
return false;
|
return false;
|
||||||
@ -168,7 +194,9 @@ public class SAMStreamSession {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
|
if (server != null) {
|
||||||
server.stopRunning();
|
server.stopRunning();
|
||||||
|
}
|
||||||
removeAllSocketHandlers();
|
removeAllSocketHandlers();
|
||||||
recv.stopStreamReceiving();
|
recv.stopStreamReceiving();
|
||||||
}
|
}
|
||||||
@ -274,7 +302,6 @@ public class SAMStreamSession {
|
|||||||
* Remove and close all the socket handlers managed by this SAM
|
* Remove and close all the socket handlers managed by this SAM
|
||||||
* STREAM session.
|
* STREAM session.
|
||||||
*
|
*
|
||||||
* @param id Handler id to be removed
|
|
||||||
*/
|
*/
|
||||||
private void removeAllSocketHandlers() {
|
private void removeAllSocketHandlers() {
|
||||||
Integer id;
|
Integer id;
|
||||||
|
@ -205,9 +205,21 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMStrea
|
|||||||
if (style.equals("RAW")) {
|
if (style.equals("RAW")) {
|
||||||
rawSession = new SAMRawSession(dest, props, this);
|
rawSession = new SAMRawSession(dest, props, this);
|
||||||
} else if (style.equals("STREAM")) {
|
} 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 {
|
} else {
|
||||||
_log.debug("Unrecognized SESSION STYLE: \"" + style + "\"");
|
_log.debug("Unrecognized SESSION STYLE: \"" + style +"\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return writeString("SESSION STATUS RESULT=OK DESTINATION="
|
return writeString("SESSION STATUS RESULT=OK DESTINATION="
|
||||||
@ -489,6 +501,10 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMStrea
|
|||||||
_log.debug("STREAM CONNECT failed: " + e.getMessage());
|
_log.debug("STREAM CONNECT failed: " + e.getMessage());
|
||||||
return writeString("STREAM STATUS RESULT=I2P_ERROR ID="
|
return writeString("STREAM STATUS RESULT=I2P_ERROR ID="
|
||||||
+ id + "\n");
|
+ 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")) {
|
} else if (opcode.equals("CLOSE")) {
|
||||||
if (props == null) {
|
if (props == null) {
|
||||||
|
Reference in New Issue
Block a user