Commits

Peter Arrenbrecht committed d19808b

add configuration options

  • Participants
  • Parent commits c336780

Comments (0)

Files changed (1)

File src/discovery_tonfa.py

 import random
 from testing import DiscoveryTests, assertnodes
 
-MAX_SAMPLE = 200
-TRACE = False
+
+MAX_CLIENT_SAMPLE = 200
+MAX_CLIENT_ALL = MAX_CLIENT_SAMPLE
+CLIENT_ROOTS_ALWAYS = True
+
+MAX_SERVER_SAMPLE = 200
+MAX_SERVER_ALL = MAX_SERVER_SAMPLE
+
+HIDE_ALL = False
+HIDE_TRAFFIC = False
+HIDE_SAMPLER = True
+HIDE_SERVER = True
+HIDE_UPDATES = True
 
 
 def log2(n):
         self.writer = writer
         self.cfg = cfg
 
-    def _sample(self, nodes, stop, fromheads=True, fromroots=True):
+    def _sample(self, nodes, stop):
 
-        if len(nodes) <= MAX_SAMPLE:
+        if len(nodes) <= self.maxall:
             return set(nodes)
 
         sample = set()
         always = set()
 
-        self.writer.indent(quiesce=False)
+        self.writer.indent(quiesce=HIDE_SAMPLER)
 
         dags = []
-        if fromheads:
+        if self.sampleheads:
             dags.append(self.dag)
-        if fromroots:
+        if self.sampleroots:
             dags.append(self.dag.inverse())
 
         for dag in dags:
                 if d == factor:
                     sample.add(n)
 
-            always.update(heads)
+            if self.roots_always or not always:
+                always.update(heads)
 
         self.writer.step("finalize")
         assert sample
         sample.difference_update(always)
-        desiredlen = MAX_SAMPLE - len(always)
-        if len(sample) > desiredlen:
+        desiredlen = self.maxsample - len(always)
+        if desiredlen <= 0:
+            sample = set()
+        elif len(sample) > desiredlen:
             sample = set(random.sample(sample, desiredlen))
         elif len(sample) < desiredlen:
             more = desiredlen - len(sample)
     def __init__(self, dag, writer, cfg):
         Participant.__init__(self, dag, writer, cfg)
         self.roundtrips = 0
+        self.maxall = MAX_CLIENT_ALL
+        self.maxsample = MAX_CLIENT_SAMPLE
+        self.roots_always = CLIENT_ROOTS_ALWAYS
+        self.sampleheads = True
+        self.sampleroots = True
 
     def commonheads(self, server):
 
                 self.writer.show("still undecided: %i, sample size: %s"
                                  % (len(undecided), len(sample)), emptyline=True)
 
+                self.writer.step("updating")
+                self.writer.indent(quiesce=HIDE_UPDATES)
+
                 self.writer.step("updating common")
                 commoninsample = set(n for i, n in enumerate(sample) if yesno[i])
                 common.update(dag.ancestors(commoninsample, common))
                 undecided.difference_update(missing)
                 undecided.difference_update(common)
 
+                self.writer.unindent()
                 if not undecided:
                     break
 
 
     def __init__(self, dag, writer, cfg):
         Participant.__init__(self, dag, writer, cfg)
+        self.maxall = MAX_SERVER_ALL
+        self.maxsample = MAX_SERVER_SAMPLE
+        self.roots_always = True
+        self.sampleheads = False
+        self.sampleroots = True
 
     def heads(self):
         return self.dag.heads()
     def discover(self, sample):
         dag = self.dag
 
-        self.writer.indent()
+        self.writer.indent(quiesce=HIDE_SERVER)
 
         self.writer.step("yesno")
         nodes = dag.nodeset()
         self.writer.show("server remaining: %i" % len(allremaining))
         self.writer.unindent()
 
-        if len(allremaining) > MAX_SAMPLE:
-            mysample = self._sample(allremaining, common, fromheads=False)
+        if len(allremaining) > self.maxall:
+            mysample = self._sample(allremaining, common)
             return yesno, mysample, False
 
         return yesno, allremaining, True
 class Tests(DiscoveryTests):
 
     def __init__(self):
-        DiscoveryTests.__init__(self, quiet=False)
+        DiscoveryTests.__init__(self, quiet=HIDE_ALL)
         self.cfg = Config()
 
     def setupdags(self, a, b, ans, bns):
         s = Server(sdag, self.writer, self.cfg)
         c = Client(cdag, self.writer, self.cfg)
 
-        notraffic = False
-
-        self.writer.section("traffic", quiesce=notraffic)
+        self.writer.section("traffic", quiesce=HIDE_TRAFFIC)
         actual = c.commonheads(s)
         self.writer.unindent()
         assertnodes(list(self.expected), list(actual))