Esempio n. 1
0
 def __init__(self, cfg, blacklist):
     # 
     # connections = {ID: protocol}
     # 
     self.connections = {} # (ID -> (protocol object, name) )
     self.password = cfg.password
     self.manager = ManagerServer(cfg.name, cfg.uuid)
     self.blacklist = blacklist
     manager_protocol = self.manager.protocol(self, 1)
     manager_protocol.factory = self.manager
     try:
         self.manager._connectionMade(manager_protocol)
     except Exception as e:
         print e
         raise
     self.connections[1] = manager_protocol
Esempio n. 2
0
class LabradManagerFactory(protocol.ServerFactory):
    '''
    The LabradManagerFactory actually handles most of the business of the labrad manager
    since it knows about all the client connections.  This takes care of receiving messages
    and dispatching them to the correct client protocol object.  Requests targeted at the
    manager are intercepted and sent to a special in-memory server.
    '''
    protocol = LabradManager
    def __init__(self, cfg, blacklist):
        # 
        # connections = {ID: protocol}
        # 
        self.connections = {} # (ID -> (protocol object, name) )
        self.password = cfg.password
        self.manager = ManagerServer(cfg.name, cfg.uuid)
        self.blacklist = blacklist
        manager_protocol = self.manager.protocol(self, 1)
        manager_protocol.factory = self.manager
        try:
            self.manager._connectionMade(manager_protocol)
        except Exception as e:
            print e
            raise
        self.connections[1] = manager_protocol
        
    def handlePacket(self, source, dest, context, request, records):
        # (0,0) seems to be a special context when talking to the manager.  In particular,
        # context expiration notices have to come from (0,0).  Since the manager doesn't keep context
        # information, 
        if context[0] == 0 and context[1] != 0 and not (source==1 or dest==1):
            context = (source, context[1])
        self.manager.update_packet_counts(source, dest)
        self.manager.register_context(source if request >= 0 else dest, context)

#        if dest in self.connections:
        target = self.connections[dest]
#        print "tgt: %s, src: %s, ctx: %s, req: %s, rec: %s" % (target.ID, source, context, request, records)
        if isinstance(target, InMemoryProtocol):
            # the only InMemoryProtocol is the Manager itself (the
            # registry is in the same process but connects via TCP to
            # the loopback address.  The key difference for an
            # InMemoryProtocol is that it doesn't have an underlying
            # transport and the messages are not flattened and
            # unflattened.
            target.requestReceived(source, context, request, records)
        else:
            # This compares the record data types to those registered
            # for each server setting ID, and attempts to coerce them.
            # This is slow, and needs to be optimized, possibly with a
            # C implementation.  Better yet, we would get the records
            # unflattened.
            try:
                records = self.manager.update_types(source, dest, request, records)
            except Exception as e:
                print "failed to update types"
                traceback.print_exc()
                raise
            target.sendPacket(source, context, request, records)

    def stopFactory(self):
        print "factory stopping"
        protocol.ServerFactory.stopFactory(self)
        for k,p in self.connections.items():
            if k==1:
                continue
            p.transport.loseConnection()
            print "Dropping connection ID %s" % k
        self.connections[1].connectionLost(protocol.connectionDone)
        #del self.manager

    def startFactory(self):
        print "starting factory"
        protocol.ServerFactory.startFactory(self)
    def register_server(self, protocol, server_name, desc, remarks):
        '''
        Called when a new server connection happens.  Here we just need to know
        the ID->Protocol object mapping, but we need to also notify the in-memory
        manager server so that it can manage the list of settings.
        '''
        ID = self.manager.register_server(server_name, desc, remarks)
        if ID:
            self.connections[ID] = protocol
        return ID

    def register_client(self, protocol, client_name):
        '''
        Called when a new client connection happens.  Here we just need to know
        the ID->Protocol object mapping, but we need to also notify the in-memory
        manager server.
        '''
        ID = self.manager.register_client(client_name)
        if ID:
            self.connections[ID] = protocol
        return ID

    def unregister_connection(self, ID):
        print "unregistering connection %s" % ID
        self.manager.unregister_connection(ID)
        del self.connections[ID]

    def connectionMade(self, prot):
        '''
        Checks the blacklist.
        '''
        addr = prot.transport.getPeer()
        if not self.blacklist.check_ip(addr.host):
            raise RuntimeError("IP blacklisted")