Changeset 442

Show
Ignore:
Timestamp:
02/13/03 13:21:03 (6 years ago)
Author:
sholloway
Message:

*** empty log message ***

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBTelepathy/RBTelepathy/Connection.py

    r437 r442  
    2525 
    2626import logging 
     27import weakref 
     28 
    2729import ErrorTypes 
    2830 
     
    3234 
    3335class Connection(object): 
     36    """ 
     37    A Connection is the intermediatary between packet handlers and the 
     38    protocol.  OnStreamPacket distributes inbound packets based on packet type 
     39    and namespace to the registered packet handler.  Packet handlers 
     40    registrations are managed with the LoadHandler and UnloadHandler methods. 
     41    In this way, new features and functionality can be added within the 
     42    framework by simply extending the packet handler table. 
     43 
     44    The Connection is also the central repository for sharing state information 
     45    between packet handlers.  So, common information like login id can be found 
     46    here, or a few steps from here. 
     47 
     48    Related Packages: 
     49        Packet 
     50        Stream 
     51    """ 
     52 
    3453    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3554    #~ Constants / Variables / Etc.  
     
    4867        self.RoutedPacketHandlers = self.RoutedPacketHandlers.copy() 
    4968 
     69    #~ Connection Operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     70 
    5071    def LoadDefaultHandlers(self): 
    5172        pass 
     73 
     74    def Shutdown(self, *args, **kw): 
     75        raise NotImplementedError 
    5276 
    5377    def SendPacket(self, *args, **kw): 
    5478        raise NotImplementedError 
    5579 
     80    #~ Handler Management ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     81     
     82    def LoadHandler(self, HandlerFactory, *args, **kw): 
     83        try: 
     84            connection = weakref.proxy(self) 
     85            handler = HandlerFactory(connection, *args, **kw) 
     86            self.protocol.packetbuilder.AddElementFactories(handler.ElementFactories) 
     87            for key in handler.StreamPacketHandlers: 
     88                self.StreamPacketHandlers[key] = handler 
     89            for key in handler.RoutedPacketHandlers: 
     90                self.RoutedPacketHandlers[key] = handler 
     91        except ErrorTypes.RBMessagingError, e:  
     92            raise 
     93        except: 
     94            self.log.critical('Unexpected error loading handler %r', HandlerFactory, exc_info=1) 
     95            raise 
     96 
     97    def UnloadHandler(self, handler): 
     98        try: 
     99            self.protocol.packetbuilder.RemoveElementFactories(handler.ElementFactories) 
     100            for key in handler.StreamPacketHandlers: 
     101                try: del self.StreamPacketHandlers[key] 
     102                except KeyError: pass 
     103            for key in handler.RoutedPacketHandlers: 
     104                try: del self.RoutedPacketHandlers[key] 
     105                except KeyError: pass 
     106        except ErrorTypes.RBMessagingError, e:  
     107            raise 
     108        except: 
     109            self.log.critical('Unexpected error unloading handler %r', handler, exc_info=1) 
     110            raise 
     111 
     112    #~ Event Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     113 
    56114    def OnRoutedPacket(self, packet, *args, **kw): 
    57115        self.log.debug('Received Routed packet (%s, %s)', packet.namespace, packet.node) 
    58         #print '>>>', packet.toxml() 
    59116 
    60117        # Lookup our packet handler 
     
    67124            raise ErrorTypes.RoutedHandlerError("No RoutedPacketHandler for (%s, %s)" % (packet.namespace, packet.node)) 
    68125 
    69         return packethandler.OnRoutedPacket(self, packet, *args, **kw) 
     126        return packethandler.OnRoutedPacket(packet, *args, **kw) 
    70127 
    71128    def OnStreamPacket(self, packet, *args, **kw): 
    72129        self.log.debug('Received Stream packet (%s, %s)', packet.namespace, packet.node) 
    73         #print '>>>', packet.toxml() 
    74130 
    75131        # Lookup our packet handler 
     
    82138            raise ErrorTypes.PacketHandlerError("No StreamPacketHandler for (%s, %s)" % (packet.namespace, packet.node)) 
    83139 
    84         return packethandler.OnStreamPacket(self, packet, *args, **kw) 
     140        return packethandler.OnStreamPacket(packet, *args, **kw) 
    85141 
    86142    def OnStreamShutdown(self, how): 
  • trunk/RBTelepathy/RBTelepathy/Packet/AuthenticationHandler.py

    r437 r442  
    5353    ElementFactories = {(RBMessagingNamespace, 'authentication'): EF.Static(AuthenticationElement)} 
    5454    StreamPacketHandlers = ElementFactories.keys() 
    55     RoutedPacketHandlers = () 
     55    RoutedPacketHandlers = {} 
    5656 
    5757    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    6060 
    6161    def __init__(self, connection): 
     62        self.connection = connection 
    6263        self._ValidTypes = {} 
    6364 
    64     def OnRoutedPacket(self, connection, packet, *args, **kw): 
     65    def OnRoutedPacket(self, packet, *args, **kw): 
    6566        raise NotImplementedError, 'Authetication does not accept RoutedPackets yet' 
    6667 
    67     def OnStreamPacket(self, connection, packet, *args, **kw): 
     68    def OnStreamPacket(self, packet, *args, **kw): 
    6869        self.log.debug('Received auth type=%r packet', packet.attrs.get('type', )) 
    6970        try: 
     
    7475        try: 
    7576            OnAuthType = self._ValidTypes[AuthType] 
    76             return OnAuthType(self, connection, packet, *args, **kw) 
     77            return OnAuthType(self, packet, *args, **kw) 
    7778        except KeyError: 
    7879            raise ErrorTypes.AuthenticationError('Invalid or out of sequence type "%s"' % (AuthType,)) 
     
    8687        self._ValidTypes[AuthType] = getattr(klass, '_type_'+AuthType) 
    8788 
    88     def _send_packet(self, connection, AuthType, XMLBody=None): 
     89    def _send_packet(self, AuthType, XMLBody=None): 
    8990        xml = "<authentication xmlns='%s' type='%s'" % (RBMessagingNamespace, AuthType) 
    9091        if XMLBody: 
    9192            xml += ">" + XMLBody + "</authentication>" 
    9293        else: xml += "/>" 
    93         connection.SendPacket(xml) 
     94        self.connection.SendPacket(xml) 
    9495 
    9596#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    115116    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    116117 
    117     def _type_query(self, connection, packet, *args, **kw): 
    118         self._reply_options(connection, packet, *args, **kw) 
     118    def _type_query(self, packet, *args, **kw): 
     119        self._reply_options(packet, *args, **kw) 
    119120 
    120     def _reply_options(self, connection, packet, *args, **kw): 
    121         self._send_packet(connection, 'options', '<simple/>') 
     121    def _reply_options(self, packet, *args, **kw): 
     122        self._send_packet('options', '<simple/>') 
    122123 
    123     def _type_select(self, connection, packet, *args, **kw): 
    124         self._reply_challenge(connection, packet, *args, **kw) 
     124    def _type_select(self, packet, *args, **kw): 
     125        self._reply_challenge(packet, *args, **kw) 
    125126 
    126     def _reply_challenge(self, connection, packet, *args, **kw): 
     127    def _reply_challenge(self, packet, *args, **kw): 
    127128        self._EnableType('response') 
    128         self._send_packet(connection, 'challenge', '<simple addr=""/>') 
     129        self._send_packet('challenge', '<simple addr=""/>') 
    129130 
    130     def _type_response(self, connection, packet, *args, **kw): 
     131    def _type_response(self, packet, *args, **kw): 
    131132        try: 
    132133            simple = packet['simple',][0] 
     
    135136 
    136137        if simple.addr.authority: 
    137             connection.OnAuthenticated(True, simple.addr) 
    138             self._reply_success(connection, packet, *args, **kw) 
     138            self.connection.OnAuthenticated(True, simple.addr) 
     139            self._reply_success(packet, *args, **kw) 
    139140        else: 
    140             connection.OnAuthenticated(False, simple.addr) 
    141             self._reply_failure(connection, packet, *args, **kw) 
     141            self.connection.OnAuthenticated(False, simple.addr) 
     142            self._reply_failure(packet, *args, **kw) 
    142143 
    143     def _reply_success(self, connection, packet, *args, **kw): 
    144         self._send_packet(connection, 'success') 
     144    def _reply_success(self, packet, *args, **kw): 
     145        self._send_packet('success') 
    145146 
    146     def _reply_failure(self, connection, packet, *args, **kw): 
    147         self._send_packet(connection, 'failure') 
     147    def _reply_failure(self, packet, *args, **kw): 
     148        self._send_packet('failure') 
    148149        raise ErrorTypes.AuthenticationError('Invalid response to challenge') 
    149150 
     
    161162    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    162163 
    163     def AuthorizeAs(self, connection, loginaddr): 
     164    def AuthorizeAs(self, loginaddr): 
    164165        self.loginaddr = URIAddress.URIAddress(loginaddr) 
    165         self._reply_query(connection, None) 
     166        self._reply_query(None) 
    166167 
    167168    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    169170    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    170171 
    171     def _reply_query(self, connection, packet, *args, **kw): 
     172    def _reply_query(self, packet, *args, **kw): 
    172173        self._EnableType('options') 
    173         self._send_packet(connection, 'query') 
     174        self._send_packet('query') 
    174175 
    175     def _type_options(self, connection, packet, *args, **kw): 
    176         self._reply_select(connection, packet, *args, **kw) 
     176    def _type_options(self, packet, *args, **kw): 
     177        self._reply_select(packet, *args, **kw) 
    177178 
    178     def _reply_select(self, connection, packet, *args, **kw): 
     179    def _reply_select(self, packet, *args, **kw): 
    179180        self._EnableType('challenge') 
    180         self._send_packet(connection, 'select', '<simple/>') 
     181        self._send_packet('select', '<simple/>') 
    181182 
    182     def _type_challenge(self, connection, packet, *args, **kw): 
    183         self._reply_response(connection, packet, *args, **kw) 
     183    def _type_challenge(self, packet, *args, **kw): 
     184        self._reply_response(packet, *args, **kw) 
    184185 
    185     def _reply_response(self, connection, packet, *args, **kw): 
     186    def _reply_response(self, packet, *args, **kw): 
    186187        self._EnableType('success') 
    187188        self._EnableType('failure') 
    188         self._send_packet(connection, 'response', '<simple addr=%s/>' % (xmlquoteattr(str(self.loginaddr)),)) 
     189        self._send_packet('response', '<simple addr=%s/>' % (xmlquoteattr(str(self.loginaddr)),)) 
    189190 
    190     def _type_success(self, connection, packet, *args, **kw): 
    191         connection.OnAuthenticated(True, self.loginaddr) 
     191    def _type_success(self, packet, *args, **kw): 
     192        self.connection.OnAuthenticated(True, self.loginaddr) 
    192193 
    193     def _type_failure(self, connection, packet, *args, **kw): 
    194         connection.OnAuthenticated(False, self.loginaddr) 
     194    def _type_failure(self, packet, *args, **kw): 
     195        self.connection.OnAuthenticated(False, self.loginaddr) 
    195196 
  • trunk/RBTelepathy/RBTelepathy/Packet/Elements.py

    r437 r442  
    8787class StreamRootElement(RootElementBase): 
    8888    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     89    #~ Constants / Variables / Etc.  
     90    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     91 
     92    __slots__ = ['datastreams', '_OnStreamCurrent'] 
     93 
     94    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    8995    #~ OnStreamData adaptor 
    9096    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    119125 
    120126class RouteableRootElement(RootElementBase): 
     127    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     128    #~ Constants / Variables / Etc.  
     129    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     130 
     131    __slots__ = [] 
     132 
     133    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     134    #~ Public Methods  
     135    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     136 
    121137    def GetAddresses(self, nodename='to'): 
    122138        existing = self.iternodes(nodename, RBMessagingNamespace) 
     
    155171 
    156172class URIAddressElement(RootElementBase): 
     173    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     174    #~ Constants / Variables / Etc.  
     175    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     176 
     177    __slots__ = ['_addr'] 
     178 
     179    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     180    #~ Public Methods  
     181    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     182 
    157183    def OnUpdateContent(self): 
    158184        RootElementBase.OnUpdateContent(self) 
    159         self.attrs['addr'] = str(self.addr) 
     185        try: self.attrs['addr'] = str(self._addr) 
     186        except AttributeError: pass 
    160187 
    161188    #~ addr property ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    162189 
    163190    def _get_addr(self): 
    164         if self._addr is None: 
     191        try: 
     192            return self._addr 
     193        except AttributeError: 
    165194            self._addr = URIAddress.URIAddress(self.attrs.get('addr', '')) 
    166         return self._addr 
     195            return self._addr 
    167196    def _set_addr(self, value): self._addr = value 
    168197    def _del_addr(self, value): del self._addr 
    169198    addr = property(_get_addr, _set_addr, _del_addr) 
    170     _addr = None 
    171199 
  • trunk/RBTelepathy/RBTelepathy/Packet/ErrorHandler.py

    r433 r442  
    5050    ElementFactories = {(RBMessagingNamespace, 'error'): EF.Static(ErrorElement)} 
    5151    StreamPacketHandlers = ElementFactories.keys() 
    52     RoutedPacketHandlers = () 
     52    RoutedPacketHandlers = {} 
    5353 
    5454    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    5757 
    5858    def __init__(self, connection): 
    59         pass 
     59        self.connection = connection 
    6060 
    6161#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    7272    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    7373 
    74     def OnStreamPacket(self, connection, packet, *args, **kw): 
     74    def OnStreamPacket(self, packet, *args, **kw): 
    7575        self.log.warn('%s: %s', packet.attrs.get('type', 'UnknownError'), str(packet)) 
    7676 
  • trunk/RBTelepathy/RBTelepathy/Packet/MessageHandler.py

    r437 r442  
    5858    ElementFactories = {(RBMessagingNamespace, 'message'): EF.Static(MessageElement)} 
    5959    StreamPacketHandlers = ElementFactories.keys() 
    60     RoutedPacketHandlers = ElementFactories.keys() 
     60    RoutedPacketHandlers = {} 
    6161 
    6262    log = logging.getLogger('HostMessageHandler') 
     
    6767 
    6868    def __init__(self, connection): 
     69        self.connection = connection 
    6970        self.router = connection.model().GetRouter((RBMessagingNamespace, 'message')) 
    7071        if self.router: 
     
    7374            self.router.AuthorityRoutes[key] = [connection] 
    7475 
    75     def OnRoutedPacket(self, connection, packet, addresses): 
     76    def OnRoutedPacket(self, packet, addresses): 
    7677        # Copy the root level packet, so we can adjust it's addresses 
    7778        #packet = copy.copy(packet) 
     
    7980        packet.SetAddresses(addresses) 
    8081        self.log.debug('Sending routed packet to protocol') 
    81         connection.SendPacket(packet) 
     82        self.connection.SendPacket(packet) 
    8283 
    83     def OnStreamPacket(self, connection, packet): 
     84    def OnStreamPacket(self, packet): 
    8485        # TODO: Flush out 
    8586        #fromlist = packet.GetAddresses('from') 
     
    106107    ElementFactories = {(RBMessagingNamespace, 'message'): EF.Static(MessageElement)} 
    107108    StreamPacketHandlers = ElementFactories.keys() 
    108     RoutedPacketHandlers = ElementFactories.keys() 
     109    RoutedPacketHandlers = {} 
    109110 
    110111    log = logging.getLogger('HostMessageHandler') 
     
    115116 
    116117    def __init__(self, connection): 
     118        self.connection = connection 
    117119        router = connection.model().Routers.get((RBMessagingNamespace, 'message'), None) 
    118120 
    119     def OnRoutedPacket(self, connection, packet, addresses): 
     121    def OnRoutedPacket(self, packet, addresses): 
    120122        print 'OnRoutedPacket:' 
    121123        print packet._toXML() 
     
    125127        print 
    126128 
    127     def OnStreamPacket(self, connection, packet): 
     129    def OnStreamPacket(self, packet): 
    128130        print 'OnStreamPacket:' 
    129131        print packet._toXML() 
  • trunk/RBTelepathy/RBTelepathy/Packet/StreamElements.py

    r433 r442  
    3232class StreamBase(PacketElementBase): 
    3333    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    34     #~ Constants / Variables / Etc.  
    35     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    36  
    37     StreamContent = '' 
    38  
    39     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    4034    #~ Methods  
    4135    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    4236 
    4337    def _xmlInitStarted(self):  
     38        self.StreamContent = '' 
    4439        self._owner.datastreams.append(self) 
    4540 
  • trunk/RBTelepathy/RBTelepathy/Packet/URIAddress.py

    r417 r442  
    3131 
    3232class URIAddress(URIAuthorityDefault): 
     33    """URIAddress provides methods for manipulating URIs. 
     34     
     35    Can handle proto:account@network/path?query#fragment 
     36    In addition to the functionality present in URIAuthroityDefault, 
     37    URIAddress splits out two more properties: account and network. 
     38    Also important to know is that if the following is encountered:: 
     39 
     40        proto:testing/path?query#fragment 
     41 
     42    "testing" is the *account* name, and the network is assumed to be the 
     43    same as the current connection.  To correct this, use a fully qualified 
     44    name:: 
     45 
     46        proto:account@testing/path?query#fragment 
     47 
     48    The reason for this is so I can simply use an account name when I know I'm 
     49    on the same network.  (Or should be. ;)  
     50    """ 
     51 
    3352    def _getAuthority(self): 
    3453        if self.network is not None: 
  • trunk/RBTelepathy/RBTelepathy/Routing/RouterBase.py

    r437 r442  
    3131 
    3232class RouterBase(object): 
    33     def RoutePacket(self, packet, addresslist): 
    34         """Template method that governs generalized packet routing. 
    35         Handles multiple addresses to multiple destinations with the least number of commands. 
    36         Returns a list of callable objects, that represent the commands to complete the routing action.""" 
     33    """ 
     34    Routers are responsible for directing traffic between packet handlers, and 
     35    thereby, between Connections.  There are many ways to route these packets, 
     36    and possibly just as many different interfaces needed to do so.  This 
     37    interface takes a fairly simple and extensible approach to allowing for 
     38    these different implementations, while still providing some structure. 
     39    """ 
    3740 
    38         destinations = {} 
    39         # Find routing destinations for all addresses 
    40         for address in addresslist: 
    41             # Record common destinations 
    42             destinationroutes = self.FindRouteDestinations(address) or () 
    43             for dst in destinationroutes: 
    44                 destinations.setdefault(dst, []).append(address) 
    45             if not destinationroutes: 
    46                 dst = self.DefaultDestination(address) 
    47                 destinations.setdefault(dst, []).append(address) 
     41    def RoutePacket(self, *args, **kw): 
     42        """Synonym for OnRoutedPacket""" 
     43        return self.OnRoutedPacket(*args, **kw) 
    4844 
    49         # Now send the packet to the destination, including all the addresses 
    50         commandresults = [self.CommandRouteToDestination(dst, packet, dstaddresslist) 
    51                     for dst, dstaddresslist in destinations.iteritems() if dst] 
    52         return commandresults 
     45    def OnRoutedPacket(self, packet, *args, **kw): 
     46        """Entry point for generalized packet routing. 
    5347 
    54     # Make OnRoutedPacket equivlant to RoutePacket so we can chain routers 
    55     OnRoutedPacket = RoutePacket 
    56     # Alternative, if other code is needed 
    57     ##def OnRoutedPacket(self, packet, addresslist): 
    58     ##    self.RoutePacket(packet, addresslist) 
    59              
    60     def DefaultDestination(self, address): 
    61         """Returns the default destination for an otherwise unrouteable address. 
    62         Can return None, raise an error, or return a valid destination.""" 
    63         raise ErrorTypes.RoutingError("No route found for '%s'" % (address,)) 
    64  
    65     def FindRouteDestinations(self, address): 
    66         """Finds destination route(s) for address. 
    67         If multiple are routes are provided, the packet will be sent to all returned.""" 
    68         return [] 
    69  
    70     def CommandRouteToDestination(self, destination, packet, addresslist): 
    71         """Returns a callable object that carries out the routing of packet to destination with the addresslist.""" 
    72         if destination is None: 
    73             return ErrorRoutingCommand(destination, packet, addresslist) 
    74         else: 
    75             return DefaultRoutingCommand(destination, packet, addresslist) 
     48        Returns a list of callable objects, that represent the commands to 
     49        complete the routing action.  This allows more control to the caller 
     50        and more informative exceptions to be raised.""" 
     51        raise NotImplementedError 
    7652 
    7753#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    7955#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    8056 
    81 class RoutingException(KeyError): 
    82     pass 
    83  
    84 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    85  
    8657class RoutingCommandBase(object): 
    87     def __init__(self, destination, packet, addresslist):  
     58    def __init__(self, destination, packet, keylist):  
    8859        pass 
    8960 
    9061    def __call__(self, *args, **kw):  
    91         """See Commit()""" 
     62        """Synonym for Commit()""" 
    9263        return self.Commit(*args, **kw) 
    9364 
    9465    def Errors(self):  
    9566        """Simple method to query for errors""" 
    96         return [] 
     67        return () 
    9768 
    9869    def Commit(self):  
     
    10172#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    10273 
    103 class ErrorRoutingCommand(RoutingCommandBase, RoutingException): 
    104     def __init__(self, destination, packet, addresslist): 
    105         self.addresslist = addresslist 
     74class ErrorRoutingCommand(RoutingCommandBase, ErrorTypes.RoutingError): 
     75    def __init__(self, destination, packet, keylist): 
     76        self.keylist = keylist 
    10677 
    10778    def Errors(self): 
    108         return ['No route found for "%s"' % (each,) for each in self.addresslist] 
     79        return ['No route found for "%s"' % (each,) for each in self.keylist] 
    10980 
    11081    def Commit(self):  
    111         raise self, "No routes found for %s" % (self.addresslist,) 
     82        raise self, 'No routes found for %s' % (self.keylist,) 
    11283 
    11384#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    11485 
    11586class DefaultRoutingCommand(RoutingCommandBase): 
    116     def __init__(self, destination, packet, addresslist): 
     87    def __init__(self, destination, packet, keylist=()): 
    11788        self.destination = destination 
    11889        self.packet = packet 
    119         self.addresslist = addresslist 
     90        self.keylist = keylist 
    12091 
    12192    def Commit(self): 
    122         self.destination.OnRoutedPacket(self.packet, self.addresslist) 
     93        return self.destination.OnRoutedPacket(self.packet, self.keylist) 
    12394 
  • trunk/RBTelepathy/RBTelepathy/Routing/SimpleRouter.py

    r437 r442  
    2424#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    2525 
    26 from RouterBase import RouterBase 
     26from KeylistRouterBase import KeylistRouterBase  
    2727from RBMessaging import ErrorTypes 
    2828 
     
    3131#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3232 
    33 class AuthorityDictRouter(RouterBase): 
     33class AuthorityDictRouter(KeylistRouterBase): 
     34    """WARNING: In an extremely simple prototype stage!""" 
     35 
    3436    def __init__(self): 
    3537        self.AuthorityRoutes = {} 
     
    3941        If multiple are routes are provided, the packet will be sent to all returned.""" 
    4042        result = self.AuthorityRoutes.get(address.authority) 
    41         return result 
     43        if isinstance(result, (list, tuple, dict)): 
     44            return result 
     45        else: return (result,) 
    4246 
  • trunk/RBTelepathy/RBTelepathy/SocketConnections.py

    r419 r442  
    5252#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    5353 
    54 class ProtocolBaseConnection(Connection.Connection): 
     54class ProtocolConnection(Connection.Connection): 
    5555    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    5656    #~ Constants / Variables / Etc.  
     
    8989        return self 
    9090    BuildFromSocket = classmethod(BuildFromSocket) 
    91      
    92     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    93     #~ Public Methods  
    94     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    95      
    96     def LoadHandler(self, HandlerFactory, *args, **kw): 
    97         try: 
    98             handler = HandlerFactory(self, *args, **kw) 
    99             self.protocol.packetbuilder.AddElementFactories(handler.ElementFactories) 
    100             for key in handler.StreamPacketHandlers: 
    101                 self.StreamPacketHandlers[key] = handler 
    102             for key in handler.RoutedPacketHandlers: 
    103                 self.RoutedPacketHandlers[key] = handler 
    104         except ErrorTypes.RBMessagingError, e:  
    105             raise 
    106         except: 
    107             self.log.critical('Unexpected error loading handler %s', HandlerFactory, exc_info=1) 
    108             raise 
    109  
    110 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    111  
    112 class ClientConnection(ProtocolBaseConnection): 
    113     """TODO: 
    114         Load Packet.ErrorHandler 
    115         Load Packet.AuthenticationHandler for Client Connection 
    116      
    117     This process should be controllable by an xml loader 
    118     """ 
    119  
    120     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    121     #~ Constants / Variables / Etc.  
    122     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    123  
    124     log = logging.getLogger('ClientConnection') 
    125  
    126     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    127     #~ Public Class Methods  
    128     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    12991 
    13092    def BuildFromInfo(klass, model, hostname=DefaultHost, port=DefaultPort): 
     
    13496        return klass.BuildFromSocket(model, clientsocket) 
    13597    BuildFromInfo = classmethod(BuildFromInfo) 
    136  
     98     
    13799    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    138100    #~ Public Methods  
     
    142104        self.protocol.SendPacket(*args, **kw) 
    143105 
     106#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     107 
     108class ClientConnection(ProtocolConnection): 
     109    """Created at the request of an in-process client, client connections represent outbound requests to external services. 
     110     
     111    These External services include other routers.""" 
     112 
     113    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     114    #~ Constants / Variables / Etc.  
     115    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     116 
     117    log = logging.getLogger('ClientConnection') 
     118 
     119    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     120    #~ Public Methods  
     121    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     122 
    144123    def LoadDefaultHandlers(self): 
    145         ProtocolBaseConnection.LoadDefaultHandlers(self) 
     124        ProtocolConnection.LoadDefaultHandlers(self) 
    146125        self.protocol.packetbuilder = StandardStreamPacketBuilder() 
    147126        self.LoadHandler(ErrorHandler.LogErrorHandler) 
     
    160139#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    161140 
    162 class HostConnection(ProtocolBaseConnection): 
    163     """TODO: 
    164         Load Packet.ErrorHandler 
    165         Load Packet.AuthenticationHandler for Host Connection 
    166      
    167     TODO On Authenticated: 
    168         Load Packet.ConfigureHandler for Host Connection 
    169         Load Packet.MessageHandler for Host Connection 
    170      
    171     This process should be controllable by an xml loader 
    172     """ 
     141class HostConnection(ProtocolConnection): 
     142    """Created in response to inbound requests, host connections handle inbound traffic *from* a client connection.""" 
    173143 
    174144    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    186156 
    187157    def LoadDefaultHandlers(self): 
    188         ProtocolBaseConnection.LoadDefaultHandlers(self) 
     158        ProtocolConnection.LoadDefaultHandlers(self) 
    189159        self.protocol.packetbuilder = StandardStreamPacketBuilder() 
    190160        self.LoadHandler(ErrorHandler.LogErrorHandler) 
     
    204174 
    205175class ServerConnection(Connection.Connection): 
     176    """Creates HostConnection setups from incomming requests of the server socket.""" 
     177 
    206178    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    207179    #~ Constants / Variables / Etc.  
  • trunk/RBTelepathy/RBTelepathy/Stream/Protocol.py

    r437 r442  
    9898        return self.data and True or False 
    9999 
     100    def Shutdown(self): 
     101        self.log.info('Shutting down stream protocol') 
     102        self.stream.shutdown('local') 
     103 
    100104    def StreamError(self, error, forceshutdown=False): 
    101105        if isinstance(error, ErrorTypes.RBMessagingError): 
     
    110114 
    111115            if error.shutdown or forceshutdown: 
    112                 self.stream.shutdown('local'
     116                self.Shutdown(
    113117                del self.data 
    114118        else: 
     
    119123            self.stream.write('''<error xmlns='%s' %s>%s</error>%s''' % (namespace, errortype, errortext, self.delimiter)) 
    120124            if forceshutdown: 
    121                 self.stream.shutdown('local'
     125                self.Shutdown(
    122126                del self.data 
    123127