1 package uk.org.distorted.tripe; package object peers {
3 import java.io.{BufferedReader, File, FileReader, Reader};
4 import java.net.{InetAddress, Inet4Address, Inet6Address,
7 import scala.collection.mutable.{HashMap, HashSet};
8 import scala.concurrent.Channel;
9 import scala.util.control.Breaks;
10 import scala.util.matching.Regex;
12 val RX_COMMENT = """(?x) ^ \s* (?: [;\#] .* )? $""".r;
13 val RX_GRPHDR = """(?x) ^ \s* \[ (.*) \] \s* $""".r;
14 val RX_ASSGN = """(?x) ^
15 ([^\s:=] (?: [^:=]* [^\s:=])?)
17 (| [^\s\#;]\S* (?: \s+ [^\s\#;]\S*)*)
18 (?: \s+ (?: [;\#].*)? )? $""".r;
19 val RX_CONT = """(?x) ^ \s+
20 (| [^\s\#;]\S* (?: \s+ [^\s\#;]\S*)*)
21 (?: \s+ (?: [;\#].*)? )? $""".r;
22 val RX_REF = """(?x) \$ \( ([^)]+) \)""".r;
23 val RX_RESOLVE = """(?x) \$ ([46*]*) \[ ([^\]]+) \]""".r;
24 val RX_PARENT = """(?x) [^\s,]+""".r
26 def with_cleaner[T](body: Cleaner => T): T = {
27 val cleaner = new Cleaner;
29 finally { cleaner.cleanup(); }
33 var cleanups: List[() => Unit] = Nil;
34 def apply(cleanup: => Unit) { cleanups +:= { () => cleanup; } }
35 def cleanup() { cleanups foreach { _() } }
38 def lines(r: Reader) = new Traversable[String] {
39 val in: BufferedReader = new BufferedReader(r);
40 override def foreach[T](f: String => T) {
41 while (true) in.readLine match {
48 def thread(name: String, run: Boolean = true, daemon: Boolean = true)
49 (body: => Unit): Thread = {
50 val t = new Thread(new Runnable { override def run() { body } }, name);
57 val BREAK = new Breaks;
60 class BulkResolver(val nthreads: Int = 8) {
61 import BulkResolver.BREAK._;
62 class Host(val name: String) {
63 var a4, a6: Seq[InetAddress] = Seq.empty;
65 def addaddr(a: InetAddress) { a match {
66 case _: Inet4Address => a4 +:= a;
67 case _: Inet6Address => a6 +:= a;
71 def get(flags: String): Seq[InetAddress] = {
72 var wanta4, wanta6, any, all = false;
73 var b = Seq.newBuilder[InetAddress];
74 for (ch <- flags) ch match {
75 case '*' => all = true;
76 case '4' => wanta4 = true; any = true;
77 case '6' => wanta6 = true; any = true;
80 if (!any) { wanta4 = true; wanta6 = true; }
83 (all, b.result) match {
85 case (false, aa@(Nil | Seq(_))) => aa
86 case (false, Seq(a, _*)) => Seq(a)
90 val ch = new Channel[Host];
91 val map = HashMap[String, Host]();
94 val workers = Array.tabulate(nthreads) { i =>
95 thread(s"resolver worker #$i") {
98 val host = ch.read; if (host == null) break;
99 println(s";; ${Thread.currentThread.getName} resolving `${host.name}'");
101 for (a <- InetAddress.getAllByName(host.name)) host.addaddr(a);
102 } catch { case e: UnknownHostException => () }
105 println(s";; ${Thread.currentThread.getName} done'");
110 def prepare(name: String) {
111 println(s";; prepare host `$name'");
113 if (!(map contains name)) {
114 val host = new Host(name);
124 for (t <- workers) t.join();
127 def resolve(name: String, flags: String): Seq[InetAddress] =
128 map(name).get(flags);
131 def fmtpath(path: Seq[String]) =
132 path.reverse map { i => s"`$i'" } mkString " -> ";
134 class ConfigSyntaxError(val file: File, val lno: Int, val msg: String)
136 override def getMessage(): String = s"$file:$lno: $msg";
138 class MissingConfigSection(val sect: String) extends Exception {
139 override def getMessage(): String =
140 s"missing configuration section `$sect'";
142 class MissingConfigItem(val sect: String, val key: String,
143 val path: Seq[(String)]) extends Exception {
144 override def getMessage(): String = {
145 val msg = s"missing configuration item `$key' in section `$sect'";
147 else msg + s" (wanted while expanding ${fmtpath(path)})"
150 class AmbiguousConfig(val key: String,
151 val v0: String, val p0: Seq[String],
152 val v1: String, val p1: Seq[String])
154 override def getMessage(): String =
155 s"ambiguous answer resolving key `$key': " +
156 s"path ${fmtpath(p0)} yields `$v0', but ${fmtpath(p1)} yields `$v1'";
159 class ConfigCycle(val key: String, path: Seq[String]) extends Exception {
160 override def getMessage(): String =
161 s"found a cycle ${fmtpath(path)} looking up key `$key'";
163 class NoHostAddresses(val sect: String, val key: String, val host: String)
165 override def getMessage(): String =
166 s"no addresses found for `$host' (key `$key' in section `$sect')";
170 sealed abstract class ConfigCacheEntry;
171 case object StillLooking extends ConfigCacheEntry;
172 case object NotFound extends ConfigCacheEntry;
173 case class Found(value: String, path: Seq[String])
174 extends ConfigCacheEntry;
177 class Config { conf =>
179 class Section(val name: String) {
180 val itemmap = HashMap[String, String]();
181 val cache = HashMap[String, ConfigCacheEntry]();
182 override def toString: String = s"${getClass.getName}($name)";
183 def parents: Seq[Section] =
184 (itemmap.get("@inherit")
185 map { pp => (RX_PARENT.findAllIn(pp) map { conf.section _ }).toList }
188 def get_internal(key: String, path: Seq[String] = Nil):
189 Option[(String, Seq[String])] = {
190 val incpath = name +: path;
192 for (r <- cache.get(key)) r match {
193 case StillLooking => throw new ConfigCycle(key, incpath)
194 case NotFound => return None
195 case Found(v, p) => return Some((v, p ++ path));
198 for (v <- itemmap.get(key)) {
199 cache(key) = Found(v, Seq(name));
200 return Some((v, incpath));
203 cache(key) = StillLooking;
205 ((None: Option[(String, Seq[String])]) /: parents) { (st, parent) =>
206 parent.get_internal(key, incpath) match {
208 case newst@Some((v, p)) => st match {
210 case Some((vv, _)) if v == vv => st
211 case Some((vv, pp)) =>
212 throw new AmbiguousConfig(key, v, p, vv, pp)
216 case None => cache(key) = NotFound; None
218 cache(key) = Found(v, p dropRight path.length);
223 def get(key: String, resolve: Boolean = true,
224 path: Seq[String] = Nil): String = {
226 case "name" => itemmap.getOrElse("name", name)
227 case _ => get_internal(key).
228 getOrElse(throw new MissingConfigItem(name, key, path))._1
230 expand(key, v0, resolve, path)
233 def expand(key: String, value: String, resolve: Boolean,
234 path: Seq[String]): String = {
235 val v1 = RX_REF.replaceAllIn(value, { m =>
236 Regex.quoteReplacement(get(m.group(1), resolve, path))
238 val v2 = if (!resolve) v1
239 else RX_RESOLVE.replaceAllIn(v1, { m =>
240 resolver.resolve(m.group(2), m.group(1)) match {
242 throw new NoHostAddresses(name, key, m.group(2));
244 Regex.quoteReplacement((addrs map { _.getHostAddress }).
251 def items: Seq[String] = {
252 val b = Seq.newBuilder[String];
253 val seen = HashSet[String]();
254 val visiting = HashSet[String](name);
255 var stack = List(this);
257 while (stack != Nil) {
258 val sect = stack.head; stack = stack.tail;
259 for (k <- sect.itemmap.keys)
260 if (!(seen contains k)) { b += k; seen += k; }
261 for (p <- sect.parents)
262 if (!(visiting contains p.name))
263 { stack ::= p; visiting += p.name; }
268 val sectmap = new HashMap[String, Section];
269 def sections: Iterator[Section] = sectmap.values.iterator;
270 def section(name: String): Section =
271 sectmap.getOrElse(name, throw new MissingConfigSection(name));
273 val resolver = new BulkResolver;
275 def parseFile(path: File): this.type = {
276 println(s";; parse ${path.getPath}");
277 with_cleaner { clean =>
278 val in = new FileReader(path); clean { in.close(); }
281 val b = new StringBuilder;
282 var key: String = null;
283 var sect: Section = null;
286 sect.itemmap(key) = b.result;
287 println(s";; in `${sect.name}', set `$key' to `${b.result}'");
292 for (line <- lines(in)) line match {
295 case RX_GRPHDR(grp) =>
297 sect = sectmap.getOrElseUpdate(grp, new Section(grp));
300 throw new ConfigSyntaxError(
301 path, lno, "no config value to continue");
304 case RX_ASSGN(k, v) =>
306 throw new ConfigSyntaxError(
307 path, lno, "no active section to update");
312 throw new ConfigSyntaxError(path, lno, "incomprehensible line");
318 def parse(path: File): this.type = {
319 if (!path.isDirectory) parseFile(path);
321 f <- path.listFiles sortBy { _.getName };
324 tail = name(name.length - 1);
325 if tail != '#' && tail != '~'
329 def parse(path: String): this.type = parse(new File(path));
332 println(";; resolving all...");
333 for ((_, sect) <- sectmap) {
334 println(s";; resolving in section `${sect.name}'...");
335 for (key <- sect.items) {
336 println(s";; resolving in key `$key'...");
337 val mm = RX_RESOLVE.findAllIn(sect.get(key, false));
338 for (host <- mm) { resolver.prepare(mm.group(2)); }
343 def dumpsect(sect: Section) {
344 for (k <- sect.items.filterNot(_.startsWith("@")).sorted)
345 println(s";; `$k' -> `${sect.get(k)}'")
347 for (sect <- sectmap.values.toSeq sortBy { _.name })
348 if (sect.name.startsWith("@")) ();
349 else if (sect.name.startsWith("$")) {
350 println(s";; special section `${sect.name}'");
353 println(s";; peer section `${sect.name}'");