summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/noreply/fancydress/type3/PathSpec.java115
1 files changed, 51 insertions, 64 deletions
diff --git a/src/org/noreply/fancydress/type3/PathSpec.java b/src/org/noreply/fancydress/type3/PathSpec.java
index 07c24da..9c83679 100644
--- a/src/org/noreply/fancydress/type3/PathSpec.java
+++ b/src/org/noreply/fancydress/type3/PathSpec.java
@@ -209,61 +209,54 @@ public class PathSpec {
}
/**
- * A class holding a list of Servers and a Crossover point location.
+ * make a Path from a serverlist an a crossover point.
+ *
+ * @param servers a list of servers, for null elements a random recommended server is chosen
+ * @param crossoverPoint place the crossover point before this server.
+ *
+ * @throws Mix3PathProblemException if no useable random servers can be found
*/
- private class ServerlistWithCrossover {
- public Server[] servers;
- public int crossoverPoint;
-
- public ServerlistWithCrossover(Server[] servers, int crossoverPoint) {
- this.servers = servers;
- this.crossoverPoint = crossoverPoint;
- }
-
- private void fillInRandoms() throws Mix3PathProblemException {
- Server[] recommended = dir.getRecommendedServers();
- for (int i=servers.length-1; i>=0 ; i--) {
- if (servers[i] != null)
- continue;
- ArrayList s = new ArrayList();
- for (int r=0; r<recommended.length; r++)
- if (
- /* last hop or */
- ((i == servers.length-1) || (
- /* next hop is different */
- (recommended[r] != servers[i+1]) &&
- /* and can talk to next hop */
- dir.areFriends(recommended[r], servers[i+1]) )) &&
- /* first hop, or previos hop is random, or */
- ((i == 0) || (servers[i-1] == null) || (
- /* previous hop is different */
- (recommended[r] != servers[i-1]) &&
- /* and prev hop can talk to this */
- dir.areFriends(servers[i-1], recommended[r]) ))
- )
- s.add(recommended[r]);
- if (s.size() == 0)
- throw new Mix3PathProblemException("Cannot find useable servers for random hop.");
- servers[i] = (Server) s.get(CryptoPrimitives.randInt(s.size()));
- }
+ private Path makePathFromServerlist(Server[] servers, int crossoverPoint) throws Mix3PathProblemException {
+ /* Fill in randoms */
+ Server[] recommended = dir.getRecommendedServers();
+ for (int i=servers.length-1; i>=0 ; i--) {
+ if (servers[i] != null)
+ continue;
+ ArrayList s = new ArrayList();
+ for (int r=0; r<recommended.length; r++)
+ if (
+ /* last hop or */
+ ((i == servers.length-1) || (
+ /* next hop is different */
+ (recommended[r] != servers[i+1]) &&
+ /* and can talk to next hop */
+ dir.areFriends(recommended[r], servers[i+1]) )) &&
+ /* first hop, or previos hop is random, or */
+ ((i == 0) || (servers[i-1] == null) || (
+ /* previous hop is different */
+ (recommended[r] != servers[i-1]) &&
+ /* and prev hop can talk to this */
+ dir.areFriends(servers[i-1], recommended[r]) ))
+ )
+ s.add(recommended[r]);
+ if (s.size() == 0)
+ throw new Mix3PathProblemException("Cannot find useable servers for random hop.");
+ servers[i] = (Server) s.get(CryptoPrimitives.randInt(s.size()));
}
- public Path getPath() throws Mix3PathProblemException {
- fillInRandoms();
- Hop[] hops = new Hop[servers.length];
- for (int i=0; i<hops.length ; i++)
- hops[i] = new Hop(servers[i]);
-
- if (crossoverPoint == -1)
- crossoverPoint = (hops.length+1)/2;
- int len1 = crossoverPoint;
- int len2 = hops.length - len1;
- Hop[] l1 = new Hop[len1];
- Hop[] l2 = new Hop[len2];
- System.arraycopy(hops, 0, l1, 0, len1);
- System.arraycopy(hops, len1, l2, 0, len2);
- return new Path(l1, l2);
- }
+ Hop[] hops = new Hop[servers.length];
+ for (int i=0; i<hops.length ; i++)
+ hops[i] = new Hop(servers[i]);
+
+ if (crossoverPoint == -1)
+ crossoverPoint = (hops.length+1)/2;
+ int len1 = crossoverPoint;
+ int len2 = hops.length - len1;
+ Hop[] l1 = new Hop[len1];
+ Hop[] l2 = new Hop[len2];
+ System.arraycopy(hops, 0, l1, 0, len1);
+ System.arraycopy(hops, len1, l2, 0, len2);
+ return new Path(l1, l2);
}
/**
@@ -273,8 +266,9 @@ public class PathSpec {
* for client site fragmentation assembly). If none
* was chosen, pass null.
* @return a list of servers and a crossover point as an ServerlistWithCrossover object.
+ * @throws Mix3PathProblemException if no useable random servers can be found
*/
- private ServerlistWithCrossover concatComponents(Server exitHop) throws Mix3PathProblemException {
+ private Path concatComponents(Server exitHop) throws Mix3PathProblemException {
Server[][] components = new Server[pathComponents.length][];
int length = 0;
int crossoverBefore = -1;
@@ -300,7 +294,7 @@ public class PathSpec {
if (exitHop != null)
servers[servers.length-1] = exitHop;
- ServerlistWithCrossover result = new ServerlistWithCrossover(servers, crossoverBefore);
+ Path result = makePathFromServerlist(servers, crossoverBefore);
return result;
}
@@ -344,16 +338,9 @@ public class PathSpec {
exitHop = recommendedExitHops[ CryptoPrimitives.randInt(recommendedExitHops.length) ];
- ServerlistWithCrossover[] serverlists = new ServerlistWithCrossover[payload.numPackets()];
- for (int i=0; i<serverlists.length; i++) {
- serverlists[i] = concatComponents( exitHop );
- if (serverlists[i].servers.length < 2)
- throw new Mix3PathProblemException("Path too short.");
- }
-
- Path[] result = new Path[serverlists.length];
- for (int i=0; i<serverlists.length; i++)
- result[i] = serverlists[i].getPath();
+ Path[] result = new Path[payload.numPackets()];
+ for (int i=0; i<result.length; i++)
+ result[i] = concatComponents( exitHop );
return result;
}