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

📄 networkeddevicemonitorcontrol.java

📁 jxta官方例程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * 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 java.awt.Component;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.SocketTimeoutException;import java.net.URL;import java.util.Hashtable;import java.util.Iterator;import java.util.LinkedList;import java.util.Vector;import javax.imageio.ImageIO;import javax.imageio.ImageReader;import javax.imageio.stream.MemoryCacheImageInputStream;import javax.swing.JComponent;import javax.swing.JLabel;import org.apache.log4j.Level;import org.apache.log4j.Logger;import net.jxta.myjxta.dialog.DialogMessage;import net.jxta.endpoint.StringMessageElement;import net.jxta.endpoint.ByteArrayMessageElement;/** * * @author Jeff Moore */public class NetworkedDeviceMonitorControl extends Thread implements Monitor, DeviceMonitorControl {        static final Logger LOG = Logger.getLogger (NetworkedDeviceMonitorControl.class);        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 int MINIMUM_REFRESH_RATE = 15;        private static int MAXIMUM_REFRESH_RATE = 1;        private static 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 (LOG.isEnabledFor (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 (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("Transmit (Consumer) Thread: RUN");                }                while (true) {                                        if(getTransmitState () == Monitor.STOPPED) {                                                if (LOG.isEnabledFor (Level.INFO)) {                            LOG.info ("run : Stopped");                        }                        //we should send any remaining data in buffer first                        break;                    }                                        if(getTransmitState () == Monitor.PAUSED) {                                                synchronized(transmitPausedLock) {                            if (LOG.isEnabledFor (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 (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("Transmit (Consumer) Thread: ENDED");                }            }        };    }                /** producer... runs every n ms (the refreshRate), sets the capture flag*/    public void run () {        if (LOG.isEnabledFor (Level.INFO)) {            LOG.info ("Image Capture (Producer) Thread RUN");        }        while(true) {                        /** module state mechanics*/            if(getMonitorState () == this.PAUSED) {                                synchronized(monitorPausedLock) {                    if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("run : monitor Paused");                    }                    try {                                                monitorPausedLock.wait ();                                            }catch(InterruptedException ix) {                        ix.printStackTrace ();                    }                }            }                        if(getMonitorState () == this.STOPPED) {                if (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("run : Stopped");                }                                break;            }                        captureImage ();                                    try {                                Thread.sleep (getImageCaptureDelay ());                            }catch(InterruptedException ix) {                ix.printStackTrace ();            }                                }                if (LOG.isEnabledFor (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 (LOG.isEnabledFor (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 () {

⌨️ 快捷键说明

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