⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commportidentifier.java

📁 JAVA手机短信开发包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)SimpleRead.java	1.12 98/06/25 SMI
 *
 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license 
 * to use, modify and redistribute this software in source and binary
 * code form, provided that i) this copyright notice and license appear
 * on all copies of the software; and ii) Licensee does not utilize the
 * software in a manner which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
 * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
 * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
 * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
 * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control
 * of aircraft, air traffic, aircraft navigation or aircraft
 * communications; or in the design, construction, operation or
 * maintenance of any nuclear facility. Licensee represents and
 * warrants that it will not use or redistribute the Software for such
 * purposes.
 */

package javax.comm;

import	java.io.*;
import	java.util.*;

/**
 * Communications port management. <CODE>CommPortIdentifier</CODE>
 * is the central class for controlling access to communications ports.
 * It includes methods for:
 * <UL>
 * <LI>
 * Determining the communications ports made available by the driver.
 * <LI>
 * Opening communications ports for I/O operations.
 * <LI>
 * Determining port ownership.
 * <LI>
 * Resolving port ownership contention.
 * <LI>
 * Managing events that indicate changes in port ownership status.
 * </UL>
 * <P>
 * An application first uses methods in <CODE>CommPortIdentifier</CODE>
 * to negotiate with the driver to discover which communication ports
 * are available and then select a port for opening. It then uses
 * methods in other classes like <CODE>CommPort</CODE>, <CODE>ParallelPort</CODE>
 * and <CODE>SerialPort</CODE> to communicate through the port.
 *
 * @author      Jagane Sundar
 * @version     1.11, 23 Jan 1998
 * @see         javax.comm.CommPort
 * @see         javax.comm.CommPortOwnershipListener
 * @see         javax.comm.ParallelPort
 * @see         javax.comm.SerialPort
 */
public class CommPortIdentifier {

    /**
     * Obtains an enumeration object that contains a <CODE>CommPortIdentifier</CODE>
     * object for each port in the system.
     *
     * @return	an <CODE> Enumeration </CODE> object that can be used to
     *		enumerate all the ports known to the system
     *
     * @see	java.util.Enumeration
     */
    static public Enumeration getPortIdentifiers() {

	SecurityManager sec = System.getSecurityManager();
	if (sec != null) {
	    sec.checkRead(propfilename);
	}

	return new CommPortEnumerator();
    }

    /**
     * Obtains a <CODE>CommPortIdentifier</CODE> object by using a port name.
     * The port name may have been stored in persistent storage by the application.
     *
     * @param		<CODE>portName</CODE>	name of the port to open
     * @return		a <CODE>CommPortIdentifier</CODE> object
     * @exception	NoSuchPortException if the port does not exist
     */
    static public CommPortIdentifier getPortIdentifier(String portName) 
						throws NoSuchPortException {

	SecurityManager sec = System.getSecurityManager();
	if (sec != null) {
	    sec.checkRead(propfilename);
	}

	CommPortIdentifier	portId = null;

	synchronized (lock) {
	    portId = masterIdList;
	    while (portId != null) {
		if (portId.name.equals(portName))
		    break;
		portId = portId.next;
	    }
	}
	if (portId != null) {
	    return portId;
	} else {
	    throw new NoSuchPortException();
	}
    }

    /**
     * Obtains the <CODE>CommPortIdentifier</CODE> object corresponding
     * to a port that has already been opened by the application.
     *
     * @param		port	a CommPort object obtained from a previous open
     * @return		a CommPortIdentifier object
     * @exception	NoSuchPortException if the port object is invalid
     */
    static public CommPortIdentifier getPortIdentifier(CommPort port)
						throws NoSuchPortException {

	SecurityManager sec = System.getSecurityManager();
	if (sec != null) {
	    sec.checkRead(propfilename);
	}

        CommPortIdentifier   portId = null;

        synchronized (lock) {
            portId = masterIdList;
            while (portId != null) {
                if (portId.port == port)
                    break;
                portId = portId.next;
            }
        }
	if (portId != null) {
	    return portId;
	} else {
	    throw new NoSuchPortException();
	}
    }

    /**
     * Adds <CODE>CommPort</CODE> object to the list of ports.
     */
    private static void addPort(CommPort port, int portType) {

	SecurityManager sec = System.getSecurityManager();
	if (sec != null) {
	    sec.checkRead(propfilename);
	}

	CommPortIdentifier newentry = 
		    new CommPortIdentifier(port.getName(), port, portType, null);
	CommPortIdentifier cur = masterIdList, prev = null;

	synchronized (lock) {
	    while (cur != null) {
	        prev = cur;
	        cur = cur.next;
	    }
	    if (prev != null) {
	        prev.next = newentry;
	    } else {
	        masterIdList = newentry;
	    }
	}
    }

    /**
     * Adds <CODE>portName</CODE> to the list of ports.
     * @param	portName The name of the port being added
     * @param   portType The type of the port being added
     * @param   driver The driver representing the port being added
     * @see	javax.comm.CommDriver
     * @since CommAPI 1.1
     */
    public static void addPortName(String portName, int portType,
						CommDriver driver) {

	SecurityManager sec = System.getSecurityManager();
	if (sec != null) {
	    sec.checkRead(propfilename);
	}

	CommPortIdentifier newentry = 
		    new CommPortIdentifier(portName, null, portType, driver);
	CommPortIdentifier cur = masterIdList, prev = null;

	synchronized (lock) {
	    while (cur != null) {
	        prev = cur;
	        cur = cur.next;
	    }
	    if (prev != null) {
	        prev.next = newentry;
	    } else {
	        masterIdList = newentry;
	    }
	}
    }

    /**
     * Returns the name of the port. The port name should be identifiable
     * by the user. Ideally, it should be the label on the hardware.
     * For example, "COM1" and "COM2" on PCs; "Serial A" and "Serial B"
     * on Sun Ultra workstations. The port name may be stored by an application
     * and subsequently used to create a <CODE>CommPortIdentifier</CODE>
     * object using <CODE>getPortIdentifier(String portName)</CODE> method.
     *
     * @return	the name of the port
     */
    public String getName() {
	return name;
    }
    String name;

    private int portType;
    /**
     * RS-232 serial port
     */
    public static final int PORT_SERIAL = 1;
    /**
     * IEEE 1284 parallel port
     */
    public static final int PORT_PARALLEL = 2;

    /**
     * Returns the port type.
     *
     * @return	portType - PORT_SERIAL or PORT_PARALLEL
     */
    public int getPortType() {
	return portType;
    }

    private boolean maskOwnershipEvents;

    /**
     * Opens the communications port. <CODE>open</CODE> obtains
     * exclusive ownership of the port. If the port is owned by some
     * other application, a <CODE>PORT_OWNERSHIP_REQUESTED</CODE> event
     * is propagated using the <CODE>CommPortOwnershipListener</CODE>
     * event mechanism. If the application that owns the port calls
     * <CODE>close</CODE> during the event processing, then this
     * <CODE>open</CODE> will succeed.
     *
     * There is one <CODE>InputStream</CODE> and one <CODE>OutputStream</CODE>
     * associated with each port. After a port is opened with
     * <CODE>open</CODE>, then all calls to <CODE>getInputStream</CODE>
     * will return the same stream object until <CODE>close</CODE>
     * is called.
     *
     * @param	    appname	Name of application making this call.
     *              This name will become the owner of the port.
     *              Useful when resolving ownership contention.
     * @param	    timeout	time in milliseconds to block waiting
     *              for port open.
     * @return	    a <CODE>CommPort</CODE> object
     * @exception	PortInUseException if the port is in use by some
     *			    other application that is not willing to relinquish
     *              ownership
     */
    public synchronized CommPort open(String appname, int timeout)
						throws PortInUseException {

	if (owned) {
	    maskOwnershipEvents = true;
	    fireOwnershipEvent(
			CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED);
	    maskOwnershipEvents = false;
	    if (owned) {
		throw new PortInUseException(owner);
	    }
	}

	port = driver.getCommPort(name, portType);

	if (port == null) {

	    /*
	     * Unable to create port. Most likely, port is in use by
	     * another application
	     */
	    /*******************************************************************
	     * FILL IN HERE.
	     * Use underlying OS's IPC mechanisms to communicate with
	     * other Java VMs that may be willing to give up ownership of
	     * port.
	     ******************************************************************/
	    if (port == null) {
		throw new PortInUseException("Unknown Windows Application");
	    }

	}

	owned = true;
	owner = appname;

	return port;

    }

    /**
     * Returns the owner of the port.
     *
     * @return	current owner of the port. 
     */
    public String getCurrentOwner() {
	if (owned) {
	    return owner;
	} else {
	    /*******************************************************************
	     * FILL IN HERE.
	     * Use underlying OS's IPC mechanisms to obtain information
	     * about the current owner of the port. The current owner
	     * may be another Java COMM application or an OS native app.
	     ******************************************************************/
	}
    }

    /**
     * Checks whether the port is owned.
     *
     * @return	boolean	<CODE>true</CODE> if the port is owned by some application,
     * 		<CODE>false</CODE> if the port is not owned.
     */
    public boolean isCurrentlyOwned() {
	if (owned) {
	    return true;
	} else {
	    /*******************************************************************
	     * FILL IN HERE.
	     * Use underlying OS's IPC mechanisms to obtain information
	     * about the current owner of the port. The current owner
	     * may be another Java COMM application or an OS native app.
	     ******************************************************************/
	}
    }

    OwnershipEventThread oeThread;

    CpoList cpoList = new CpoList();

    /**
     * Registers an interested application so that it can receive notification
     * of changes in port ownership. This includes notification of the following events:
     * <UL>
     * <LI>
     * <CODE>PORT_OWNED</CODE>: Port became owned
     * <LI>
     * <CODE>PORT_UNOWNED</CODE>: Port became unowned
     * <LI>
     * <CODE>PORT_OWNERSHIP_REQUESTED</CODE>: If the application owns this port
     * and is willing to give up ownership, then it should call
     * <CODE>close</CODE> now.
     * </UL>
     * The <CODE>ownershipChange</CODE> method of the listener registered
     * using <CODE>addPortOwnershipListener</CODE> will be called with one
     * of the above events.
     *
     * @param	listener a <CODE>CommPortOwnershipListener</CODE> callback object
     */
    public void addPortOwnershipListener(CommPortOwnershipListener listener) {

	cpoList.add(listener);

	if (oeThread == null) {
	    oeThread = new OwnershipEventThread(this);
	    oeThread.start();
	}

    }

⌨️ 快捷键说明

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