summaryrefslogtreecommitdiff
path: root/src/org/noreply/fancydress/type3/PathSpec.java
blob: cbc608ff4d65a73e2c11f138d35cf76dfdc6d3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* $Id$ */
package org.noreply.fancydress.type3;

import org.noreply.fancydress.crypto.*;
import org.noreply.fancydress.status.*;
import org.noreply.fancydress.directory.*;
import org.noreply.fancydress.misc.Util;
import org.noreply.fancydress.type3.routing.*;
import java.net.InetAddress;
import java.util.*;


public class PathSpec {
	String pathSpec;
	Path singlePath;

	private String[] tokenize(String path) throws Mix3Exception {
		ArrayList nicks = new ArrayList();
		int indexFrom = 0;
		int indexOf;

		while ((indexOf = path.indexOf(',', indexFrom)) != -1) {
			String v = path.substring(indexFrom, indexOf).trim();
			if (v.equals(""))
				throw new Mix3Exception("Invalid path.");
			nicks.add( v );
			indexFrom = indexOf + 1;
		}
		String v = path.substring(indexFrom).trim();
		if (v.equals(""))
			throw new Mix3Exception("Invalid path.");
		nicks.add( v );

		String[] result = new String[nicks.size()];
		for (int i=0; i<result.length; i++)
			result[i] = (String) nicks.get(i);

		return result;
	}

	private Hop[] parseHalfPath(Directory dir, String path) throws Mix3Exception {
		String[] nicks = tokenize(path);
		Hop[] hops = new Hop[nicks.length];

		for (int i=0; i<hops.length; i++) {
			Server server = dir.getServer(nicks[i]);
			if (server == null)
				throw new Mix3Exception("Invalid path: "+nicks[i]+" not found");
			ServerDescriptor desc = server.getDescriptor();
			IncomingMMTPSection incoming = desc.getIncomingMMTPSection();
			Routing routing;

			if (incoming.getHostname() != null) { /* FIXME */
				routing = new RoutingHOST(incoming.getHostname(), incoming.getPort(), server.getKeyID());
			} else {
				routing = new RoutingIP4(incoming.getIP(), incoming.getPort(), server.getKeyID());
			}
			hops[i] = new Hop(routing, desc.getPacketKey());
		}
		return hops;
	}


	/**
	 * Create a path specification from a string.
	 *
	 * A path spec could look like "<code>Foo,Bar,?:Baz,*2,~1</code>".
	 *
	 * FIXME: right now we don't do any random foo.
	 *
	 * @param path given path
	 */
	public PathSpec(Directory dir, String pathSpec, boolean singleLeg) throws Mix3Exception {
		this.pathSpec = pathSpec;

		int crossover = pathSpec.indexOf(':');
		if (crossover < 0)
			throw new Mix3Exception("Path is not a valid path: no crossover point specified.");

		Hop[] leg1 = parseHalfPath(dir, pathSpec.substring(0, crossover));
		Hop[] leg2 = parseHalfPath(dir, pathSpec.substring(crossover+1));
		singlePath = new Path(leg1, leg2);
	}

	/**
	 * Return a path constructed from this PathSpec
	 *
	 * @param dir a directory
	 * @return path
	 */
	public Path getPath() throws Mix3Exception {
		return singlePath;
	}
}