defaultidecontrollerdriver.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 190 行

JAVA
190
字号
/*
 * $Id: DefaultIDEControllerDriver.java,v 1.2 2004/02/24 08:03:24 epr Exp $
 */
package org.jnode.driver.ide;

import java.util.ArrayList;
import java.util.Iterator;

import javax.naming.NameNotFoundException;

import org.apache.log4j.Logger;
import org.jnode.driver.Device;
import org.jnode.driver.DeviceAlreadyRegisteredException;
import org.jnode.driver.DeviceManager;
import org.jnode.driver.Driver;
import org.jnode.driver.DriverException;
import org.jnode.naming.InitialNaming;
import org.jnode.system.ResourceNotFreeException;
import org.jnode.work.Work;
import org.jnode.work.WorkUtils;

/**
 * @author epr
 */
public class DefaultIDEControllerDriver extends Driver {

    /** My logger */
    private final Logger log = Logger.getLogger(getClass());

    /** The actual worker */
    private final IDEBus[] ideProcessors;

    /** The actual IDE devices found */
    private final ArrayList devices = new ArrayList();

    private DeviceManager devMan;

    /**
     * Create a new instance
     */
    public DefaultIDEControllerDriver() {
        final int max = IDEConstants.IDE_NR_TASKFILES;
        this.ideProcessors = new IDEBus[ max];
    }

    /**
     * Add the given command to the queue of commands to be executed.
     * 
     * @param command
     */
    protected void execute(IDECommand command) {
        ideProcessors[ command.getTaskFile()].execute(command);
    }

    /**
     * Add the given command to the queue of commands to be executed and wait
     * for the command to finish.
     * 
     * @param command
     */
    protected void executeAndWait(IDECommand command)
            throws InterruptedException {
        ideProcessors[ command.getTaskFile()].executeAndWait(command);
    }

    /**
     * Probe for the existence of a given IDE device.
     * 
     * @param taskfile
     *            The taskfile to probe. [0..1]
     * @param master
     */
    protected IDEDriveDescriptor probe(int taskfile, boolean master)
            throws InterruptedException {
        return ideProcessors[ taskfile].probe(master);
    }

    protected void registerDevices() throws IDEException, DriverException {
        log.debug("Probing IDE devices");
        try {
            devMan = (DeviceManager) InitialNaming.lookup(DeviceManager.NAME);
        } catch (NameNotFoundException ex) {
            throw new IDEException("Cannot find device manager", ex);
        }
        final IDEDriveDescriptor[] devs = new IDEDriveDescriptor[ 4];

        try {
            devs[ 0] = probe(0, true);
            devs[ 1] = probe(0, false);
            devs[ 2] = probe(1, true);
            devs[ 3] = probe(1, false);
        } catch (InterruptedException ex) {
            throw new IDEException("Probe interrupted");
        }

        log.debug("After probe");

        int count = 0;
        for (int i = 0; i < devs.length; i++) {
            final IDEDriveDescriptor descr = devs[ i];
            if (descr != null) {
                final String name = "hd" + ((char) ('a' + i));
                final IDEDevice dev = new IDEDevice(ideProcessors[ i / 2],
                        i / 2, ((i % 2) == 0), name, descr, this);
                if (descr.isDisk()) {
                    dev.setDriver(new IDEDiskDriver());
                } else if (descr.isCDROM()) {
                    dev.setDriver(new IDECDROMDriver());
                }
                try {
                    devMan.register(dev);
                } catch (DeviceAlreadyRegisteredException ex) {
                    throw new IDEException("Error in register device", ex);
                } catch (DriverException ex) {
                    throw new IDEException("Error in register device", ex);
                }
                devices.add(dev);
                log.info(name + "=" + descr.getModel());
                count++;
            }
        }

        log.info("Found " + count + " IDE devices");
    }

    protected void unregisterDevices() throws DriverException {
        for (Iterator i = devices.iterator(); i.hasNext();) {
            IDEDevice dev = (IDEDevice) i.next();
            devMan.unregister(dev);
        }
    }

    /**
     * Create and start the structures required by this driver.
     */
    private void startDriver() throws ResourceNotFreeException {
        final Device dev = getDevice();
        final int max = ideProcessors.length;
        for (int i = 0; i < max; i++) {
            this.ideProcessors[ i] = new IDEBus(dev, i);
            ideProcessors[ i].start(dev);
        }
    }

    /**
     * Stop and release the structures required by this driver.
     */
    private void stopDriver() {
        final int max = ideProcessors.length;
        for (int i = 0; i < max; i++) {
            ideProcessors[ i].stop();
            ideProcessors[ i] = null;
        }
    }

    /**
     * Start the IDE controller device.
     * 
     * @see org.jnode.driver.Driver#startDevice()
     */
    protected void startDevice() throws DriverException {
        try {
            startDriver();
            WorkUtils.add(new Work("IDE.registerDevices") {
                public void execute() {
                    try {
                        registerDevices();
                    } catch (IDEException ex) {
                        log.error("Error starting IDE devices", ex);
                    } catch (DriverException ex) {
                        log.error("Error starting IDE devices", ex);
                    }                    
                }
            });
        } catch (ResourceNotFreeException ex) {
            throw new DriverException(ex);
        }
    }

    /**
     * Stop the IDE controller device.
     * 
     * @see org.jnode.driver.Driver#stopDevice()
     */
    protected void stopDevice() throws DriverException {
        unregisterDevices();
        stopDriver();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?