forked from I2P_Developers/i2p.i2p
* Added support for SESSION CREATE STYLE=STREAM DIRECTION={CREATE|RECEIVE|BOTH}
* M-x untabify (human)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user