def run_device_plugin(identity, msg):
    """Execute the plugin claim protocol. This happens whenever a client requests
    to claim a resource advertised by a plugin.

    """
    # Figure out the plugin that owns the device we want
    p = None
    dev_id = msg[2]
    for (plugin_name, device_list) in _devices.items():
        if dev_id in device_list:
            p = _plugins[plugin_name]

    if p is None:
        logging.warning("Cannot find device %s, failing claim", dev_id)
        queue.add(identity, ["s", "BPClaimDevice", dev_id, False])
        return

    # See whether we already have a claim on the device
    if dev_id in _dtc.keys():
        logging.warning("Device %s already claimed, failing claim", dev_id)
        queue.add(identity, ["s", "BPClaimDevice", dev_id, False])
        return

    # Client to system: bring up device process.
    #
    # Just name the new plugin process socket identity after the device id,
    # because why not.
    device_process = _start_process([p.executable_path, "--server_port=%s" %
                                     config.get_value("server_address")],
                                    dev_id)
    if not device_process:
        logging.warning("%s device process unable to start. removing.",
                        p.name)
        return

    # Device process to system: Register with known identity
    e = event.add(dev_id, "BPPluginRegisterClaim")
    try:
        # TODO: Make device open timeout a config option
        (i, m) = e.get(timeout=5)
    except greenlet.BPGreenletExit:
        # If we shut down now, just drop
        return
    except gevent.Timeout:
        # If we timeout, fail the claim
        logging.info("Device %s failed to start...", dev_id)
        queue.add(dev_id, ["s", "BPClose"])
        queue.add(identity, ["s", "BPClaimDevice", dev_id, False])
        return

    greenlet.add_identity_greenlet(dev_id, gevent.getcurrent())

    # Add a heartbeat now that the process is up
    hb = heartbeat.spawn_heartbeat(dev_id, gevent.getcurrent())

    # System to device process: Open device
    queue.add(dev_id, ["s", "BPPluginOpenDevice", dev_id])
    e = event.add(dev_id, "BPPluginOpenDevice")
    try:
        (i, m) = e.get()
    except greenlet.BPGreenletExit:
        queue.add(dev_id, ["s", "BPClose"])
        return

    # Device process to system: Open or fail
    if m[3] is False:
        logging.info("Device %s failed to open...", dev_id)
        queue.add(dev_id, ["s", "BPClose"])
        queue.add(identity, ["s", "BPClaimDevice", dev_id, False])
        return

    # System to client: confirm device claim
    queue.add(identity, ["s", "BPClaimDevice", dev_id, True])

    if identity not in _ctd.keys():
        _ctd[identity] = []
    _ctd[identity].append(dev_id)
    if dev_id not in _dtc.keys():
        _dtc[dev_id] = []
    _dtc[dev_id].append(identity)

    while True:
        try:
            gevent.sleep(1)
        except greenlet.BPGreenletExit:
            break

    if not hb.ready():
        hb.kill(exception=greenlet.BPGreenletExit, block=True, timeout=1)

    _ctd[identity].remove(dev_id)
    del _dtc[dev_id]

    # Remove ourselves, but don't kill since we're already shutting down
    greenlet.remove_identity_greenlet(dev_id, kill_greenlet=False)
    queue.add(dev_id, ["s", "BPClose"])
    logging.debug("Device keeper %s exiting...", dev_id)
dataset/ETHPy150Open metafetish/buttplug/buttplug/core/plugin.py/run_device_plugin/original