Forráskód Böngészése

Rename 'CellHandler' to 'ChannelManager'

because it manages channels.  Each channel manages circuits, and each
circuit handles cells.  Similarly, the subclass CellRelay becomes
RelayChannelManager and the subclass CellClient becomes
ClientChannelManager.
Ian Goldberg 6 éve
szülő
commit
2895f1c8e0
2 módosított fájl, 89 hozzáadás és 89 törlés
  1. 43 43
      client.py
  2. 46 46
      relay.py

+ 43 - 43
client.py

@@ -13,8 +13,8 @@ class VanillaCreatedExtendedHandler:
     """A handler for VanillaCreatedCircuitCell and
     VanillaExtendedCircuitCell cells."""
 
-    def __init__(self, cellhandler, ntor, expecteddesc):
-        self.cellhandler = cellhandler
+    def __init__(self, channelmgr, ntor, expecteddesc):
+        self.channelmgr = channelmgr
         self.ntor = ntor
         self.expecteddesc = expecteddesc
         self.onionkey = expecteddesc.descdict['onionkey']
@@ -42,15 +42,15 @@ class VanillaCreatedExtendedHandler:
 
         nexthop = None
         while nexthop is None:
-            nexthop = self.cellhandler.consensus.select_weighted_relay( \
-                    self.cellhandler.consensus_cdf)
+            nexthop = self.channelmgr.consensus.select_weighted_relay( \
+                    self.channelmgr.consensus_cdf)
             if nexthop.descdict['addr'] in \
                     [ desc.descdict['addr'] \
                         for desc in circhandler.circuit_descs ]:
                 nexthop = None
 
         # Construct the VanillaExtendCircuitCell
-        ntor = relay.NTor(self.cellhandler.perfstats)
+        ntor = relay.NTor(self.channelmgr.perfstats)
         ntor_request = ntor.request()
         circextendmsg = relay.VanillaExtendCircuitCell( \
                 nexthop.descdict['addr'], ntor_request)
@@ -58,14 +58,14 @@ class VanillaCreatedExtendedHandler:
         # Set up the reply handler
         circhandler.replace_celltype_handler( \
                 relay.VanillaExtendedCircuitCell, \
-                VanillaCreatedExtendedHandler(self.cellhandler, ntor, nexthop))
+                VanillaCreatedExtendedHandler(self.channelmgr, ntor, nexthop))
 
         # Send the cell
         circhandler.send_cell(circextendmsg)
 
 
-class CellClient(relay.CellHandler):
-    """The subclass of CellHandler for clients."""
+class ClientChannelManager(relay.ChannelManager):
+    """The subclass of ChannelManager for clients."""
 
     def __init__(self, myaddr, dirauthaddrs, perfstats):
         super().__init__(myaddr, dirauthaddrs, perfstats)
@@ -163,7 +163,7 @@ class Client:
         self.perfstats = network.PerfStats(network.EntType.CLIENT)
         self.perfstats.name = "Client at %s" % self.netaddr
         self.perfstats.is_bootstrapping = True
-        self.cellhandler = CellClient(self.netaddr, dirauthaddrs, self.perfstats)
+        self.channelmgr = ClientChannelManager(self.netaddr, dirauthaddrs, self.perfstats)
 
         # Register for epoch tick notifications
         network.thenetwork.wantepochticks(self, True)
@@ -175,7 +175,7 @@ class Client:
         network.thenetwork.wantepochticks(self, False)
 
         # Close relay connections
-        self.cellhandler.terminate()
+        self.channelmgr.terminate()
 
     def get_consensus(self):
         """Fetch a new consensus."""
@@ -186,8 +186,8 @@ class Client:
         # we'll need a consensus (uh, oh; in that case, fetch the
         # consensus from a fallback relay).
 
-        self.cellhandler.get_consensus_from_fallbackrelay()
-        print('client consensus=', self.cellhandler.consensus)
+        self.channelmgr.get_consensus_from_fallbackrelay()
+        print('client consensus=', self.channelmgr.consensus)
 
     def newepoch(self, epoch):
         """Callback that fires at the start of each epoch"""
@@ -196,7 +196,7 @@ class Client:
         self.get_consensus()
 
         # If we don't have a guard, pick one and make a channel to it
-        self.cellhandler.ensure_guard()
+        self.channelmgr.ensure_guard()
 
 
 
@@ -247,18 +247,18 @@ if __name__ == '__main__':
 
     print('ticked; epoch=', network.thenetwork.getepoch())
 
-    relays[3].cellhandler.send_msg(relay.RelayRandomHopMsg(30), relays[5].netaddr)
+    relays[3].channelmgr.send_msg(relay.RelayRandomHopMsg(30), relays[5].netaddr)
 
     # See what channels exist and do a consistency check
     for r in relays:
-        print("%s: %s" % (r.netaddr, [ str(k) for k in r.cellhandler.channels.keys()]))
+        print("%s: %s" % (r.netaddr, [ str(k) for k in r.channelmgr.channels.keys()]))
         raddr = r.netaddr
-        for ad, ch in r.cellhandler.channels.items():
-            if ch.peer.cellhandler.myaddr != ad:
-                print('address mismatch:', raddr, ad, ch.peer.cellhandler.myaddr)
+        for ad, ch in r.channelmgr.channels.items():
+            if ch.peer.channelmgr.myaddr != ad:
+                print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
 
-            if ch.peer.cellhandler.channels[raddr].peer is not ch:
-                print('asymmetry:', raddr, ad, ch, ch.peer.cellhandler.channels[raddr].peer)
+            if ch.peer.channelmgr.channels[raddr].peer is not ch:
+                print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
 
     # Start some clients
     numclients = 1
@@ -271,19 +271,19 @@ if __name__ == '__main__':
 
     # See what channels exist and do a consistency check
     for c in clients:
-        print("%s: %s" % (c.netaddr, [ str(k) for k in c.cellhandler.channels.keys()]))
+        print("%s: %s" % (c.netaddr, [ str(k) for k in c.channelmgr.channels.keys()]))
         caddr = c.netaddr
-        for ad, ch in c.cellhandler.channels.items():
-            if ch.peer.cellhandler.myaddr != ad:
-                print('address mismatch:', caddr, ad, ch.peer.cellhandler.myaddr)
+        for ad, ch in c.channelmgr.channels.items():
+            if ch.peer.channelmgr.myaddr != ad:
+                print('address mismatch:', caddr, ad, ch.peer.channelmgr.myaddr)
 
-            if ch.peer.cellhandler.channels[caddr].peer is not ch:
-                print('asymmetry:', caddr, ad, ch, ch.peer.cellhandler.channels[caddr].peer)
+            if ch.peer.channelmgr.channels[caddr].peer is not ch:
+                print('asymmetry:', caddr, ad, ch, ch.peer.channelmgr.channels[caddr].peer)
 
     # Pick a bunch of bw-weighted random relays and look at the
     # distribution
     for i in range(100):
-        r = clients[0].cellhandler.consensus.select_weighted_relay(clients[0].cellhandler.consensus_cdf)
+        r = clients[0].channelmgr.consensus.select_weighted_relay(clients[0].channelmgr.consensus_cdf)
         print("relay",r.descdict["addr"])
 
     relays[3].terminate()
@@ -294,35 +294,35 @@ if __name__ == '__main__':
     # Tick the epoch
     network.thenetwork.nextepoch()
 
-    clients[0].cellhandler.new_circuit()
-    circhandler = clients[0].cellhandler.channels[clients[0].cellhandler.guardaddr].circuithandlers[2]
+    clients[0].channelmgr.new_circuit()
+    circhandler = clients[0].channelmgr.channels[clients[0].channelmgr.guardaddr].circuithandlers[2]
     circhandler.send_cell(relay.StringCell("hello world"))
 
     # See what channels exist and do a consistency check
     for r in relays:
-        print("%s: %s" % (r.netaddr, [ str(k) + str([ck for ck in r.cellhandler.channels[k].circuithandlers.keys()]) for k in r.cellhandler.channels.keys()]))
+        print("%s: %s" % (r.netaddr, [ str(k) + str([ck for ck in r.channelmgr.channels[k].circuithandlers.keys()]) for k in r.channelmgr.channels.keys()]))
         raddr = r.netaddr
-        for ad, ch in r.cellhandler.channels.items():
-            if ch.peer.cellhandler.myaddr != ad:
-                print('address mismatch:', raddr, ad, ch.peer.cellhandler.myaddr)
+        for ad, ch in r.channelmgr.channels.items():
+            if ch.peer.channelmgr.myaddr != ad:
+                print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
 
-            if ch.peer.cellhandler.channels[raddr].peer is not ch:
-                print('asymmetry:', raddr, ad, ch, ch.peer.cellhandler.channels[raddr].peer)
+            if ch.peer.channelmgr.channels[raddr].peer is not ch:
+                print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
 
     # See what channels exist and do a consistency check
     for c in clients:
-        print("%s: %s" % (c.netaddr, [ str(k) + str([ck for ck in c.cellhandler.channels[k].circuithandlers.keys()]) for k in c.cellhandler.channels.keys()]))
+        print("%s: %s" % (c.netaddr, [ str(k) + str([ck for ck in c.channelmgr.channels[k].circuithandlers.keys()]) for k in c.channelmgr.channels.keys()]))
         caddr = c.netaddr
-        for ad, ch in c.cellhandler.channels.items():
-            if ch.peer.cellhandler.myaddr != ad:
-                print('address mismatch:', caddr, ad, ch.peer.cellhandler.myaddr)
+        for ad, ch in c.channelmgr.channels.items():
+            if ch.peer.channelmgr.myaddr != ad:
+                print('address mismatch:', caddr, ad, ch.peer.channelmgr.myaddr)
 
-            if ch.peer.cellhandler.channels[caddr].peer is not ch:
-                print('asymmetry:', caddr, ad, ch, ch.peer.cellhandler.channels[caddr].peer)
+            if ch.peer.channelmgr.channels[caddr].peer is not ch:
+                print('asymmetry:', caddr, ad, ch, ch.peer.channelmgr.channels[caddr].peer)
 
             if ch.circuithandlers.keys() != \
-                    ch.peer.cellhandler.channels[caddr].circuithandlers.keys():
-                print('circuit asymmetry:', caddr, ad, ch.peer.cellhandler.myaddr)
+                    ch.peer.channelmgr.channels[caddr].circuithandlers.keys():
+                print('circuit asymmetry:', caddr, ad, ch.peer.channelmgr.myaddr)
 
     for d in dirauths:
         print(d.perfstats)

+ 46 - 46
relay.py

@@ -190,8 +190,8 @@ class VanillaExtendCircuitHandler:
         circhandler.replace_celltype_handler(VanillaExtendCircuitCell, None)
 
         # Allocate a new circuit id to the requested next hop
-        cellhandler = circhandler.channel.cellhandler
-        nexthopchannel = cellhandler.get_channel_to(cell.hopaddr)
+        channelmgr = circhandler.channel.channelmgr
+        nexthopchannel = channelmgr.get_channel_to(cell.hopaddr)
         newcircid, newcirchandler = nexthopchannel.new_circuit()
 
         # Connect the existing and new circuits together
@@ -279,7 +279,7 @@ class CircuitHandler:
         if isinstance(cell, EncryptedCell):
             cell = self.crypt_layer.decrypt_msg(cell)
 
-        print("CircuitHandler: %s received cell %s on circuit %d from %s" % (self.channel.cellhandler.myaddr, cell, self.circid, self.channel.peer.cellhandler.myaddr))
+        print("CircuitHandler: %s received cell %s on circuit %d from %s" % (self.channel.channelmgr.myaddr, cell, self.circid, self.channel.peer.channelmgr.myaddr))
 
         # If it's still encrypted, it's for sure meant for forwarding to
         # our adjacent hop, which had better exist.
@@ -294,13 +294,13 @@ class CircuitHandler:
             elif isinstance(cell, StringCell):
                 # Default handler; just print the message in the cell
                 print("CircuitHandler: %s received '%s' on circuit %d from %s" \
-                        % (self.channel.cellhandler.myaddr, cell,
-                            self.circid, self.channel.peer.cellhandler.myaddr))
+                        % (self.channel.channelmgr.myaddr, cell,
+                            self.circid, self.channel.peer.channelmgr.myaddr))
             else:
                 # I don't know how to handle this cell?
                 raise ValueError("CircuitHandler: %s received unknown cell type %s on circuit %d from %s" \
-                        % (self.channel.cellhandler.myaddr, cell,
-                            self.circid, self.channel.peer.cellhandler.myaddr))
+                        % (self.channel.channelmgr.myaddr, cell,
+                            self.circid, self.channel.peer.channelmgr.myaddr))
 
     def replace_celltype_handler(self, celltype, handler):
         """Add an object with a received_cell(circhandler, cell) method
@@ -325,8 +325,8 @@ class Channel(network.Connection):
     client or a relay, transporting cells from various circuits."""
     def __init__(self):
         super().__init__()
-        # The CellRelay managing this Channel
-        self.cellhandler = None
+        # The RelayChannelManager managing this Channel
+        self.channelmgr = None
         # The Channel at the other end
         self.peer = None
         # The function to call when the connection closes
@@ -376,21 +376,21 @@ class Channel(network.Connection):
 
     def send_msg(self, msg):
         """Send the given NetMsg on the channel."""
-        self.cellhandler.perfstats.bytes_sent += msg.size()
-        self.peer.received(self.cellhandler.myaddr, msg)
+        self.channelmgr.perfstats.bytes_sent += msg.size()
+        self.peer.received(self.channelmgr.myaddr, msg)
 
     def received(self, peeraddr, msg):
         """Callback when a message is received from the network."""
-        print('Channel: %s received %s from %s' % (self.cellhandler.myaddr, msg, peeraddr))
-        self.cellhandler.perfstats.bytes_received += msg.size()
+        print('Channel: %s received %s from %s' % (self.channelmgr.myaddr, msg, peeraddr))
+        self.channelmgr.perfstats.bytes_received += msg.size()
         if isinstance(msg, CircuitCellMsg):
             circid, cell = msg.circid, msg.cell
             self.circuithandlers[circid].received_cell(cell)
         else:
-            self.cellhandler.received_msg(msg, peeraddr, self)
+            self.channelmgr.received_msg(msg, peeraddr, self)
 
 
-class CellHandler:
+class ChannelManager:
     """The class that manages the channels to other relays and clients.
     Relays and clients both use subclasses of this class to both create
     on-demand channels to relays, to gracefully handle the closing of
@@ -419,7 +419,7 @@ class CellHandler:
         if peeraddr in self.channels:
             self.channels[peeraddr].close()
 
-        channel.cellhandler = self
+        channel.channelmgr = self
         self.channels[peeraddr] = channel
         channel.closer = lambda: self.channels.pop(peeraddr)
 
@@ -434,18 +434,18 @@ class CellHandler:
                 self.perfstats)
         self.channels[addr] = newchannel
         newchannel.closer = lambda: self.channels.pop(addr)
-        newchannel.cellhandler = self
+        newchannel.channelmgr = self
 
         return newchannel
 
     def received_msg(self, msg, peeraddr, channel):
         """Callback when a NetMsg not specific to a circuit is
         received."""
-        print("CellHandler: Node %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
+        print("ChannelManager: Node %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
 
     def received_cell(self, circid, cell, peeraddr, channel):
         """Callback with a circuit-specific cell is received."""
-        print("CellHandler: Node %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
+        print("ChannelManager: Node %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
 
     def send_msg(self, msg, peeraddr):
         """Send a message to the peer with the given address."""
@@ -459,8 +459,8 @@ class CellHandler:
         channel.send_cell(circid, cell)
 
 
-class CellRelay(CellHandler):
-    """The subclass of CellHandler for relays."""
+class RelayChannelManager(ChannelManager):
+    """The subclass of ChannelManager for relays."""
 
     def __init__(self, myaddr, dirauthaddrs, onionprivkey, idpubkey, perfstats):
         super().__init__(myaddr, dirauthaddrs, perfstats)
@@ -479,7 +479,7 @@ class CellRelay(CellHandler):
     def received_msg(self, msg, peeraddr, channel):
         """Callback when a NetMsg not specific to a circuit is
         received."""
-        print("CellRelay: Node %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
+        print("RelayChannelManager: Node %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
         if isinstance(msg, RelayRandomHopMsg):
             if msg.ttl > 0:
                 # Pick a random next hop from the consensus
@@ -510,7 +510,7 @@ class CellRelay(CellHandler):
 
     def received_cell(self, circid, cell, peeraddr, channel):
         """Callback with a circuit-specific cell is received."""
-        print("CellRelay: Node %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
+        print("RelayChannelManager: Node %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
         return super().received_cell(circid, cell, peeraddr, channel)
 
 
@@ -540,8 +540,8 @@ class Relay(network.Server):
         network.thenetwork.wantepochticks(self, True, end=True)
         network.thenetwork.wantepochticks(self, True)
 
-        # Create the CellRelay connection manager
-        self.cellhandler = CellRelay(self.netaddr, dirauthaddrs,
+        # Create the RelayChannelManager connection manager
+        self.channelmgr = RelayChannelManager(self.netaddr, dirauthaddrs,
                 self.onionkey, self.idkey.verify_key, self.perfstats)
 
         # Initially, we're not a fallback relay
@@ -564,7 +564,7 @@ class Relay(network.Server):
         self.uploaddesc(False)
 
         # Close connections to other relays
-        self.cellhandler.terminate()
+        self.channelmgr.terminate()
 
         # Stop listening to our own bound port
         self.close()
@@ -578,7 +578,7 @@ class Relay(network.Server):
         # Download the new consensus, which will have been created
         # already since the dirauths' epoch_ending callbacks happened
         # before the relays'.
-        self.cellhandler.get_consensus()
+        self.channelmgr.get_consensus()
 
     def newepoch(self, epoch):
         self.uploaddesc()
@@ -607,7 +607,7 @@ class Relay(network.Server):
             descmsg = dirauth.DirAuthDelDescMsg(desc)
 
         # Upload them
-        for a in self.cellhandler.dirauthaddrs:
+        for a in self.channelmgr.dirauthaddrs:
             c = network.thenetwork.connect(self, a, self.perfstats)
             c.sendmsg(descmsg)
             c.close()
@@ -632,8 +632,8 @@ class Relay(network.Server):
         ourchannel.peer = peerchannel
         ourchannel.next_circid = 1
 
-        # Add our channel to the CellRelay
-        self.cellhandler.add_channel(ourchannel, peer)
+        # Add our channel to the RelayChannelManager
+        self.channelmgr.add_channel(ourchannel, peer)
 
         return peerchannel
 
@@ -675,18 +675,18 @@ if __name__ == '__main__':
 
     print('ticked; epoch=', network.thenetwork.getepoch())
 
-    relays[3].cellhandler.send_msg(RelayRandomHopMsg(30), relays[5].netaddr)
+    relays[3].channelmgr.send_msg(RelayRandomHopMsg(30), relays[5].netaddr)
 
     # See what channels exist and do a consistency check
     for r in relays:
-        print("%s: %s" % (r.netaddr, [ str(k) for k in r.cellhandler.channels.keys()]))
+        print("%s: %s" % (r.netaddr, [ str(k) for k in r.channelmgr.channels.keys()]))
         raddr = r.netaddr
-        for ad, ch in r.cellhandler.channels.items():
-            if ch.peer.cellhandler.myaddr != ad:
-                print('address mismatch:', raddr, ad, ch.peer.cellhandler.myaddr)
+        for ad, ch in r.channelmgr.channels.items():
+            if ch.peer.channelmgr.myaddr != ad:
+                print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
 
-            if ch.peer.cellhandler.channels[raddr].peer is not ch:
-                print('asymmetry:', raddr, ad, ch, ch.peer.cellhandler.channels[raddr].peer)
+            if ch.peer.channelmgr.channels[raddr].peer is not ch:
+                print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
 
     # Stop some relays
     relays[3].terminate()
@@ -703,20 +703,20 @@ if __name__ == '__main__':
 
     # See what channels exist and do a consistency check
     for r in relays:
-        print("%s: %s" % (r.netaddr, [ str(k) for k in r.cellhandler.channels.keys()]))
+        print("%s: %s" % (r.netaddr, [ str(k) for k in r.channelmgr.channels.keys()]))
         raddr = r.netaddr
-        for ad, ch in r.cellhandler.channels.items():
-            if ch.peer.cellhandler.myaddr != ad:
-                print('address mismatch:', raddr, ad, ch.peer.cellhandler.myaddr)
+        for ad, ch in r.channelmgr.channels.items():
+            if ch.peer.channelmgr.myaddr != ad:
+                print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
 
-            if ch.peer.cellhandler.channels[raddr].peer is not ch:
-                print('asymmetry:', raddr, ad, ch, ch.peer.cellhandler.channels[raddr].peer)
+            if ch.peer.channelmgr.channels[raddr].peer is not ch:
+                print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
 
-    channel = relays[3].cellhandler.get_channel_to(relays[5].netaddr)
+    channel = relays[3].channelmgr.get_channel_to(relays[5].netaddr)
     circid, circhandler = channel.new_circuit()
-    peerchannel = relays[5].cellhandler.get_channel_to(relays[3].netaddr)
+    peerchannel = relays[5].channelmgr.get_channel_to(relays[3].netaddr)
     peerchannel.new_circuit_with_circid(circid)
-    relays[3].cellhandler.send_cell(circid, StringCell("test"), relays[5].netaddr)
+    relays[3].channelmgr.send_cell(circid, StringCell("test"), relays[5].netaddr)
 
     idpubkey = dirauth.DirAuth.consensus.consdict["relays"][1].descdict["idkey"]
     onionpubkey = dirauth.DirAuth.consensus.consdict["relays"][1].descdict["onionkey"]