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

📄 networkeddevicemonitorcontrol.java

📁 Myjxta的源代码 基于JXTA的P2P即时通信系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) [2005] [Jeffrey Moore] * * Redistributions in source code form must reproduce the above copyright and  * this condition. * * The contents of this file are subject to the Sun Project JXTA License  * Version 1.1 (the "License"); you may not use this file except in compliance  * with the License. A copy of the License is available at  * http://www.jxta.org/jxta_license.html. * *//* * NetworkedDeviceMonitor.java * * Created on June 4, 2005, 6:37 AM */package net.jxta.myjxta.plugins.vijxta;import net.jxta.endpoint.ByteArrayMessageElement;import net.jxta.endpoint.StringMessageElement;import net.jxta.logging.Logging;import net.jxta.myjxta.dialog.DialogMessage;import javax.imageio.ImageIO;import javax.imageio.ImageReader;import javax.imageio.stream.MemoryCacheImageInputStream;import javax.swing.*;import java.awt.*;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.net.*;import java.util.Hashtable;import java.util.Iterator;import java.util.LinkedList;import java.util.Vector;import java.util.logging.Level;import java.util.logging.Logger;/** * @author Jeff Moore */public final class NetworkedDeviceMonitorControl extends Thread implements Monitor, DeviceMonitorControl {    static final Logger LOG = Logger.getLogger(NetworkedDeviceMonitorControl.class.getName());    private static final String REFRESH_RATE_UNIT_LABEL = "seconds";    private LinkedList outgoingBuffer = null;    private ViJxtaDialog viJxtaDialog = null;    private static final int SERVER_CHUNK_READ_SIZE = 1460;    private int refreshRate = 1;    private long imageCaptureDelay = 0;    private static final int DEFAULT_IMAGES_PER_MESSAGE = 1;    private int imagesPerMessage = DEFAULT_IMAGES_PER_MESSAGE;    private MonitorComponent monitorComponent = null;    private static final int MINIMUM_REFRESH_RATE = 15;    private static final int MAXIMUM_REFRESH_RATE = 1;    private static final int DEFAULT_REFRESH_RATE = MINIMUM_REFRESH_RATE;    private int imageCompression = Monitor.DEFAULT_IMAGE_COMPRESSION;    private MyLock captureLock = null;    private MyLock monitorPausedLock = null;    private MyLock transmitPausedLock = null;    private String deviceError = null;    private MyLock bufferStarvedLock = null;    private long imageEncodeTime = 0;    private long averageImageEncodeTime = 0;    private DialogMessage templateMessage = null;    private int bytesSent = 0;    private int messagesSent = 0;    private int monitorState = STOPPED;    private int transmitState = STOPPED;    private boolean transmit = false;    private Hashtable refreshRateTable = null;    private Hashtable refreshRateLabelTable = null;    private Thread dispatchThread = null;    private String urlString = null;    private String userName = null;    private String password = null;    private boolean authenticated = false;    private String mimeType = null;    private String responseString = null;    private Vector deviceErrorListeners = null;    public NetworkedDeviceMonitorControl(ViJxtaCallControl callControl) {        LOG.setLevel(Level.INFO);        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            LOG.info("NetworkedDeviceMonitorControl Instantiated");        }        deviceErrorListeners = new Vector();        refreshRateTable = new Hashtable();        refreshRateTable.put(new Integer(1), new Long((long) this.SECOND * 1));        refreshRateTable.put(new Integer(3), new Long((long) this.SECOND * 2));        refreshRateTable.put(new Integer(5), new Long((long) this.SECOND * 5));        refreshRateTable.put(new Integer(7), new Long((long) this.SECOND * 7));        refreshRateTable.put(new Integer(10), new Long((long) this.SECOND * 10));        refreshRateTable.put(new Integer(15), new Long((long) this.SECOND * 15));        refreshRateLabelTable = new Hashtable();        refreshRateLabelTable.put(new Integer(1), new JLabel("1"));        refreshRateLabelTable.put(new Integer(3), new JLabel("3"));        refreshRateLabelTable.put(new Integer(5), new JLabel("5"));        refreshRateLabelTable.put(new Integer(7), new JLabel("7"));        refreshRateLabelTable.put(new Integer(10), new JLabel("10"));        refreshRateLabelTable.put(new Integer(15), new JLabel("15"));        this.imageCaptureDelay = ((Long) refreshRateTable.get(new Integer(this.DEFAULT_FRAMES_PER_SECOND))).longValue();        this.viJxtaDialog = (ViJxtaDialog) callControl.getDialog();        outgoingBuffer = new LinkedList();        captureLock = new MyLock();        captureLock.setLocked(true);        monitorPausedLock = new MyLock();        transmitPausedLock = new MyLock();        bufferStarvedLock = new MyLock();        templateMessage = new DialogMessage(callControl.getDialog().getGroup()                .getPeerGroup().getPeerName(), null, callControl.getDialog().getGroup()                .getPeerGroup().getPeerGroupID().toString(), callControl.getDialog()                .getGroup().getPeerGroup().getPeerGroupName());        /** consumer thread... takes from the queue and sends message*/        dispatchThread = new Thread() {            public void run() {                if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {                    LOG.info("Transmit (Consumer) Thread: RUN");                }                while (true) {                    if (getTransmitState() == Monitor.STOPPED) {                        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {                            LOG.info("run : Stopped");                        }                        // we should send any remaining data in buffer first                        break;                    }                    if (getTransmitState() == Monitor.PAUSED) {                        synchronized (transmitPausedLock) {                            if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {                                LOG.info("run : tramsmit Paused");                            }                            try {                                transmitPausedLock.wait();                            }                            catch (InterruptedException ix) {                                ix.printStackTrace();                            }                        }                    }                    if (outgoingBuffer.size() >= getImagesPerMessage()) {                        dispatchImage(getImagesPerMessage());                    } else {                        try {                            synchronized (bufferStarvedLock) {                                bufferStarvedLock.setLocked(true);                                bufferStarvedLock.wait();                            }                        }                        catch (InterruptedException ix) {                            ix.printStackTrace();                        }                    }                }                if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {                    LOG.info("Transmit (Consumer) Thread: ENDED");                }            }        };    }    /**     * producer... runs every n ms (the refreshRate), sets the capture flag     */    public void run() {        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            LOG.info("Image Capture (Producer) Thread RUN");        }        while (true) {            /** module state mechanics*/            if (getMonitorState() == this.PAUSED) {                synchronized (monitorPausedLock) {                    if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {                        LOG.info("run : monitor Paused");                    }                    try {                        monitorPausedLock.wait();                    } catch (InterruptedException ix) {                        ix.printStackTrace();                    }                }            }            if (getMonitorState() == this.STOPPED) {                if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {                    LOG.info("run : Stopped");                }                break;            }            captureImage();            try {                Thread.sleep(getImageCaptureDelay());            } catch (InterruptedException ix) {                ix.printStackTrace();            }        }        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            LOG.info("Image Capture (Producer) Thread Ended");        }    }    protected void dispatchImage(int imagesInThisMessage) {        if (outgoingBuffer.size() >= imagesInThisMessage) {            byte[] imageBytes = null;            DialogMessage msg = getNewTemplateMessage();            StringMessageElement commandElement = new StringMessageElement(                    ViJxtaCallControl.TAG_SESSION_COMMAND, ViJxtaCallControl.COMMAND_VIJXTA_DATA, null);            msg.addMessageElement(ViJxtaCallControl.TAG_SESSION_COMMAND, commandElement);            imageBytes = ((ByteArrayOutputStream) outgoingBuffer.getFirst()).toByteArray();            outgoingBuffer.removeFirst();            ByteArrayMessageElement imageElement = new ByteArrayMessageElement(                    ViJxtaCallControl.TAG_IMAGE_DATA, null, imageBytes, 0, null);            msg.addMessageElement(ViJxtaCallControl.TAG_IMAGE_DATA, imageElement);            this.messagesSent = this.messagesSent + 1;            this.bytesSent = this.bytesSent + imageBytes.length;            this.viJxtaDialog.dispatch(msg);        }    }    public Hashtable getRefreshRateLabelTable() {        return this.refreshRateLabelTable;    }    public Dimension getFormatSize() {        return this.getMonitorComponent().getPreferredSize();    }    public void resetMonitor() {        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            LOG.info("resetMonitor");        }        if (this.getTransmitState() == this.STOPPED) {            this.stopMonitorCapture();            this.stopMonitor();            this.releaseHardware();        }    }    public int getTransmitState() {        return this.transmitState;    }    public String getRefreshRateUnitLabel() {        return this.REFRESH_RATE_UNIT_LABEL;    }    public void setTransmitState(int transmitState) {        this.transmitState = transmitState;    }    public void pauseMonitor() {        this.setMonitorState(this.PAUSED);    }    public void resumeMonitor() {        this.setMonitorState(this.STARTED);        synchronized (monitorPausedLock) {            monitorPausedLock.notifyAll();        }    }    public void pauseTransmit() {        this.setTransmitState(this.PAUSED);    }    public void resumeTransmit() {        this.setTransmitState(this.STARTED);        synchronized (transmitPausedLock) {            transmitPausedLock.notifyAll();        }    }    public long getImageCaptureDelay() {        return this.imageCaptureDelay;    }    public void setRefreshRate(int refreshRate) {        if (refreshRate >= this.MINIMUM_REFRESH_RATE &&                refreshRate <= this.MAXIMUM_REFRESH_RATE) {            this.refreshRate = refreshRate;            this.imageCaptureDelay = ((Long) refreshRateTable.get(new Integer(refreshRate))).longValue();        } else {            this.refreshRate = this.DEFAULT_REFRESH_RATE;            this.imageCaptureDelay = ((Long) refreshRateTable.get(new Integer(this.DEFAULT_REFRESH_RATE))).longValue();        }    }    public int getRefreshRate() {        return this.refreshRate;    }    public void setURL(String urlString) {        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            LOG.info("setUrl " + urlString);        }        this.urlString = urlString;    }    private URL getURL(String urlString) {        URL url = null;        if (urlString != null) {            try {                url = new URL(urlString);            } catch (MalformedURLException mux) {                mux.printStackTrace();                if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {                    LOG.info("Bad URL!! Make sure URL starts wtih http://");                }                setDeviceError("Bad URL!! Make sure URL starts wtih http://");                notifyDeviceErrorListeners();                return null;            }        } else {            setDeviceError("Bad URL!! Make sure URL starts wtih http://");            notifyDeviceErrorListeners();            return null;        }        return url;    }    public void setAuthenticated(boolean authenticated) {        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            //LOG.info ("authenticated "+authenticated);        }        this.authenticated = authenticated;    }    public boolean isAuthenticated() {        return this.authenticated;    }    public void setUserName(String userName) {        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            //LOG.info ("userName "+userName);        }        this.userName = userName;    }    public void setPassword(String password) {        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            //LOG.info ("password " + password);        }        this.password = password;    }    public String getUserName() {        return userName;    }    public String getPassword() {        return password;    }    public String getURLString() {        return urlString;    }    private String getCredentials() {        String rtn = null;        if (getUserName() != null && getPassword() != null) {            rtn = userName + ":" + password;        }        return rtn;    }    private String getEncodedCredentials() {        String encodedCredentials = null;        String credentials = getCredentials();        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            //LOG.info ("username:password "+getUserName ()+":"+getPassword ());            //LOG.info ("credentials "+credentials);        }        if (credentials != null) {            encodedCredentials = Base64.encode(credentials);        }        return encodedCredentials;    }    public void addErrorListener(DeviceMonitorControl.DeviceErrorListener listener) {        synchronized (deviceErrorListeners) {            deviceErrorListeners.add(listener);        }    }    private void notifyDeviceErrorListeners() {        // this isn't thread safe        Iterator itr = deviceErrorListeners.iterator();        while (itr.hasNext()) {            ((DeviceMonitorControl.DeviceErrorListener) itr.next()).                    deviceErrorEncountered(getDeviceError());        }    }    private String getDeviceError() {        return this.deviceError;    }    private void setDeviceError(String deviceError) {        this.deviceError = deviceError;    }    private HttpURLConnection getHttpConnection(URL url, boolean authenticate) {        HttpURLConnection camServer = null;        try {            camServer = (HttpURLConnection) url.openConnection();        } catch (IOException iox) {            iox.printStackTrace();            setDeviceError("CamServer I/O Error.");            notifyDeviceErrorListeners();            return null;        } catch (NullPointerException npx) {

⌨️ 快捷键说明

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