예제 #1
0
def _register_service(application, service_addr, service_resp_addr, service_passphrase, handle=True):
    """ Create and store a connection and it's listener and waiting_clients queue.
    """
    logging.debug("service_passphrase: %s" % service_passphrase)
    assure_resource(application)
    key = create_resource_key(service_addr, _SERVICE_RESOURCE_TYPE)        
    if not is_resource_registered(key):
        # create our service connection
        logging.debug("register_service creating service_conn: %s" % service_addr)
            
        service_conn = ServiceClientConnection(
                        service_addr,  service_resp_addr, service_passphrase
                    )

        # create and start our listener
        logging.debug("register_service starting listener: %s" % service_addr)
        coro_spawn(
            service_response_listener,
            application, 
            service_addr, 
            service_resp_addr, 
            service_conn, 
            service_passphrase,
            handle
        )
        # give above process a chance to start
        coro_sleep(0)
    
        # add us to the list
        resource = {'service_conn': service_conn, 'waiting_clients': {}}
        register_resource(resource, key)
        logging.debug("register_service success: %s" % key)
    else:
        logging.debug("register_service ignored: %s already registered" % service_addr)
    return True
예제 #2
0
def _get_service_info(application, service_addr):
    if _service_is_registered(application, service_addr):
        key = create_resource_key(service_addr, _SERVICE_RESOURCE_TYPE)
        service_info = get_resource(key)
        return service_info
    else:
        raise Exception("%s service not registered" % service_addr)
예제 #3
0
def _unregister_service(application, service_addr,service_passphrase):
    """ unregister a service.
    """
    if not self._service_is_registered(service_addr):
        logging.debug("unregister_resource ignored: %s not registered" % service_addr)
        return False
    else:
        service_info = _get_service_info(application, service_addr)
        service_conn = service_info['service_conn']
        waiting_clients = service_info['waiting_clients']
        service_conn.close()
        for sock in waiting_clients:
            logging.debug("killing internal reply socket %s" % sock)
            sock.close()
                
        key = create_resource_key(service_addr, _SERVICE_RESOURCE_TYPE)    
        unregister_resource(key)
        logging.debug("unregister_service success: %s" % service_addr)
        return True
예제 #4
0
def _service_is_registered(application, service_addr):
    """ Check if a service is registered"""
    key = create_resource_key(service_addr, _SERVICE_RESOURCE_TYPE)
    return is_resource_registered(key)