Пример #1
0
    def connect(cls,
                host,
                port,
                socket_class=PolledSocket,
                state_callback=None):
        """ Create an FCPClient which owns a new FCPConnection.

            NOTE: If you need multiple FCPClient instances it is
                  better to explictly create an FCPConnection and
                  use the FCPClient.__init__() method so that all
                  instances are multiplexed over the same connection.
            """
        sock = None
        conn = None
        raised = True
        try:
            sock = socket_class(host, port)
            conn = FCPConnection(sock, True, state_callback)
            raised = False
        finally:
            if raised:
                if conn:
                    conn.close()
                if sock:
                    sock.close()

        return FCPClient(conn)
Пример #2
0
def insert_files(host, port, names):
    socket = PolledSocket(host, port)
    connection = FCPConnection(socket, True)

    inserts = []
    for name in names:
        client = FCPClient(connection)
        client.in_params.default_fcp_params['MaxRetries'] = 3
        client.in_params.default_fcp_params['PriorityClass'] = 1

        inserts.append(client)
        client.in_params.async = True
        parts = name.split('/')

        client.put_file('CHK@' + '/' + parts[-1], name)
         # Hmmmm... Ugly. Add FCPConnection.wait_until_upload_finishes() ?
        while connection.is_uploading():
            socket.poll()
            time.sleep(.25)

    while min([insert.is_finished() for insert in inserts]) == False:
        if not socket.poll():
            break
        time.sleep(.25)

    uris = []
    for insert in inserts:
        if insert.response == None or len(insert.response) < 2 or insert.response[0] <> 'PutSuccessful':
            uris.append(None)
            continue

        uris.append(insert.response[1]['URI'])

    return uris
Пример #3
0
def setup(ui_, repo, params, stored_cfg):
    """ INTERNAL: Setup to run an Infocalypse extension command. """
    # REDFLAG: choose another name. Confusion w/ fcp param
    # REDFLAG: add an hg param and get rid of this line.
    #params['VERBOSITY'] = 1

    check_uri(ui_, params.get('INSERT_URI'))
    check_uri(ui_, params.get('REQUEST_URI'))

    if not is_writable(os.path.expanduser(stored_cfg.defaults['TMP_DIR'])):
        raise util.Abort("Can't write to temp dir: %s\n" %
                         stored_cfg.defaults['TMP_DIR'])

    verbosity = params.get('VERBOSITY', 1)
    set_debug_vars(verbosity, params)

    callbacks = UICallbacks(ui_)
    callbacks.verbosity = verbosity

    if not repo is None:
        # BUG:? shouldn't this be reading TMP_DIR from stored_cfg
        cache = BundleCache(repo, ui_, params['TMP_DIR'])

    try:
        async_socket = PolledSocket(params['FCP_HOST'], params['FCP_PORT'])
        connection = FCPConnection(async_socket, True,
                                   callbacks.connection_state)
    except socket.error, err:  # Not an IOError until 2.6.
        ui_.warn("Connection to FCP server [%s:%i] failed.\n" %
                 (params['FCP_HOST'], params['FCP_PORT']))
        raise err
Пример #4
0
    def connect(cls, host, port, socket_class = PolledSocket,
                state_callback = None):
        """ Create an FCPClient which owns a new FCPConnection.

            NOTE: If you need multiple FCPClient instances it is
                  better to explictly create an FCPConnection and
                  use the FCPClient.__init__() method so that all
                  instances are multiplexed over the same connection.
            """
        sock = None
        conn = None
        raised = True
        try:
            sock = socket_class(host, port)
            conn = FCPConnection(sock, True, state_callback)
            raised = False
        finally:
            if raised:
                if conn:
                    conn.close()
                if sock:
                    sock.close()

        return FCPClient(conn)
Пример #5
0
    def make_state_machine(self):
        if not self.connection is None:
            self.connection.close()

        callbacks = UICallbacks(FakeUI())
        callbacks.verbosity = 5
        # Knows about reading and writing bytes.
        async_socket = PolledSocket(FCP_HOST, FCP_PORT)
        # Knows about running the FCP protocol over async_socket.
        self.connection = FCPConnection(async_socket, True,
                                        callbacks.connection_state)
        # Knows about running requests from a request queue.
        runner = RequestRunner(self.connection, N_CONCURRENT)
        # Knows how to run series of requests to perform operations
        # on an archive in Freenet.
        sm = ArchiveStateMachine(runner, ArchiveUpdateContext())
        sm.transition_callback = callbacks.transition_callback
        sm.monitor_callback = callbacks.monitor_callback
        sm.params['CANCEL_TIME_SECS'] = CANCEL_TIME_SECS

        return sm
Пример #6
0
def execute_setup(ui_, host, port, tmp, cfg_file=None):
    """ Run the setup command. """
    def connection_failure(msg):
        """ INTERNAL: Display a warning string. """
        ui_.warn(msg)
        ui_.warn("It looks like your FCP host or port might be wrong.\n")
        ui_.warn("Set them with --fcphost and/or --fcpport and try again.\n")
        raise util.Abort("Connection to FCP server failed.")

    # Fix defaults.
    if host == '':
        host = '127.0.0.1'
    if port == 0:
        port = 9481

    if cfg_file is None:
        cfg_file = os.path.expanduser(DEFAULT_CFG_PATH)

    existing_name = ui_.config('infocalypse', 'cfg_file', None)
    if not existing_name is None:
        existing_name = os.path.expanduser(existing_name)
        ui_.status(MSG_HGRC_SET % existing_name)
        cfg_file = existing_name

    if os.path.exists(cfg_file):
        ui_.status(MSG_CFG_EXISTS % cfg_file)
        raise util.Abort("Refusing to modify existing configuration.")

    tmp = setup_tmp_dir(ui_, tmp)

    if not is_writable(tmp):
        raise util.Abort("Can't write to temp dir: %s\n" % tmp)

    # Test FCP connection.
    timeout_secs = 20
    connection = None
    default_private_key = None
    try:
        ui_.status("Testing FCP connection [%s:%i]...\n" % (host, port))

        connection = FCPConnection(PolledSocket(host, port))

        started = time.time()
        while (not connection.is_connected()
               and time.time() - started < timeout_secs):
            connection.socket.poll()
            time.sleep(.25)

        if not connection.is_connected():
            connection_failure(("\nGave up after waiting %i secs for an " +
                                "FCP NodeHello.\n") % timeout_secs)

        ui_.status("Looks good.\nGenerating a default private key...\n")

        # Hmmm... this waits on a socket. Will an ioerror cause an abort?
        # Lazy, but I've never seen this call fail except for IO reasons.
        client = FCPClient(connection)
        client.message_callback = lambda x, y: None  # Disable chatty default.
        default_private_key = client.generate_ssk()[1]['InsertURI']

    except FCPError:
        # Protocol error.
        connection_failure("\nMaybe that's not an FCP server?\n")

    except socket.error:  # Not an IOError until 2.6.
        # Horked.
        connection_failure("\nSocket level error.\n")

    except IOError:
        # Horked.
        connection_failure("\nSocket level error.\n")

    cfg = Config()
    cfg.defaults['HOST'] = host
    cfg.defaults['PORT'] = port
    cfg.defaults['TMP_DIR'] = tmp
    cfg.defaults['DEFAULT_PRIVATE_KEY'] = default_private_key
    Config.to_file(cfg, cfg_file)

    ui_.status("""\nFinished setting configuration.
FCP host: %s
FCP port: %i
Temp dir: %s
cfg file: %s

Default private key:
%s

The config file was successfully written!

""" % (host, port, tmp, cfg_file, default_private_key))
Пример #7
0
def execute_setup(ui_, host, port, tmp, cfg_file = None):
    """ Run the setup command. """
    def connection_failure(msg):
        """ INTERNAL: Display a warning string. """
        ui_.warn(msg)
        ui_.warn("It looks like your FCP host or port might be wrong.\n")
        ui_.warn("Set them with --fcphost and/or --fcpport and try again.\n")
        raise util.Abort("Connection to FCP server failed.")

    # Fix defaults.
    if host == '':
        host = '127.0.0.1'
    if port == 0:
        port = 9481

    if cfg_file is None:
        cfg_file = os.path.expanduser(DEFAULT_CFG_PATH)

    existing_name = ui_.config('infocalypse', 'cfg_file', None)
    if not existing_name is None:
        existing_name = os.path.expanduser(existing_name)
        ui_.status(MSG_HGRC_SET % existing_name)
        cfg_file = existing_name

    if os.path.exists(cfg_file):
        ui_.status(MSG_CFG_EXISTS % cfg_file)
        raise util.Abort("Refusing to modify existing configuration.")

    tmp = setup_tmp_dir(ui_, tmp)

    if not is_writable(tmp):
        raise util.Abort("Can't write to temp dir: %s\n" % tmp)

    # Test FCP connection.
    timeout_secs = 20
    connection = None
    default_private_key = None
    try:
        ui_.status("Testing FCP connection [%s:%i]...\n" % (host, port))

        connection = FCPConnection(PolledSocket(host, port))

        started = time.time()
        while (not connection.is_connected() and
               time.time() - started < timeout_secs):
            connection.socket.poll()
            time.sleep(.25)

        if not connection.is_connected():
            connection_failure(("\nGave up after waiting %i secs for an "
                               + "FCP NodeHello.\n") % timeout_secs)

        ui_.status("Looks good.\nGenerating a default private key...\n")

        # Hmmm... this waits on a socket. Will an ioerror cause an abort?
        # Lazy, but I've never seen this call fail except for IO reasons.
        client = FCPClient(connection)
        client.message_callback = lambda x, y:None # Disable chatty default.
        default_private_key = client.generate_ssk()[1]['InsertURI']

    except FCPError:
        # Protocol error.
        connection_failure("\nMaybe that's not an FCP server?\n")

    except socket.error: # Not an IOError until 2.6.
        # Horked.
        connection_failure("\nSocket level error.\n")

    except IOError:
        # Horked.
        connection_failure("\nSocket level error.\n")

    cfg = Config()
    cfg.defaults['HOST'] = host
    cfg.defaults['PORT'] = port
    cfg.defaults['TMP_DIR'] = tmp
    cfg.defaults['DEFAULT_PRIVATE_KEY'] = default_private_key
    Config.to_file(cfg, cfg_file)

    ui_.status("""\nFinished setting configuration.
FCP host: %s
FCP port: %i
Temp dir: %s
cfg file: %s

Default private key:
%s

The config file was successfully written!

""" % (host, port, tmp, cfg_file, default_private_key))