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

📄 remotemonitorcontrol.java

📁 jxta官方例程
💻 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. * *//* * RemoteMonitorControl.java * * Created on April 9, 2005, 10:06 AM */package net.jxta.myjxta.plugins.vijxta;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.util.Iterator;import java.util.LinkedList;import javax.imageio.ImageIO;import javax.imageio.ImageReader;import javax.imageio.stream.MemoryCacheImageInputStream;import javax.swing.JComponent;import net.jxta.endpoint.ByteArrayMessageElement;import net.jxta.endpoint.MessageElement;import net.jxta.myjxta.dialog.DialogListener;import net.jxta.myjxta.dialog.DialogMessage;import org.apache.log4j.Level;import org.apache.log4j.Logger;/** *  This class represents a video monitor of a remote video data feed.  The remote *  data are images passed via DialogMessage's. This calss also acts as a DialogListener *  for all incoming messages.  Video messages are parsed here while Command *  messages are send to CallControl. * * @author jamoore */public class RemoteMonitorControl extends Thread implements DialogListener, Monitor{        private static final Logger LOG = Logger.getLogger (RemoteMonitorControl.class);        private ViJxtaCallControl viJxtaCallControl = null;        private LinkedList incomingBuffer = null;        private int monitorState = STOPPED;        private int receiveState = STOPPED;        private MyLock bufferStarvedLock = null;        private String originator = null;        private int imageCompression = 0;        private int imagesPerMessage = 0;        private long timeOfLastMessage = 0;        private long lastReceivedMessage  = 0;        private int messagesReceived  = 0;        private int bytesReceived = 0;        private int imageSize = 0;        private int averageImageSize = 0;        private long averageImageDecodeTime = 0;        private long imageDecodeTime = 0;        private Dimension formatSize = null;        private String formatType = null;        private RemoteMonitorCanvas monitorComponent = null;        private boolean receive = false;                private long imageDrawTime = 0;        private long averageImageDrawTime = 0;        private MyLock pausedLock = null;        /** Creates a new instance of RemoteMonitorControl */    public RemoteMonitorControl (ViJxtaCallControl viJxtaCallControl) {                LOG.setLevel(Level.INFO);                if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("RemoteMonitorControl Instantiated");                    }                this.viJxtaCallControl = viJxtaCallControl;                getCallControl ().getDialog ().addListener (this);                incomingBuffer = new LinkedList ();                bufferStarvedLock = new MyLock();                pausedLock = new MyLock();            }        public void run() {                if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("Receive Thread : RUN");                    }                while(true) {                         try {                if(getReceiveState () == this.STOPPED) {                                        if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("run : Stopped");                    }                    //we should send any remaining data in buffer first                    break;                }                                if(getReceiveState() == this.PAUSED) {                    if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("run : Paused");                    }                                        synchronized(pausedLock) {                                                pausedLock.wait();                    }                }                if(incomingBuffer.size () > 0) {                                        displayImage();                                    }else{                    synchronized(bufferStarvedLock) {                                                bufferStarvedLock.setLocked(true);                                                bufferStarvedLock.wait();                    }                }            } catch (Exception e) {                e.printStackTrace ();            }        }        if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("Receive Thread: Ended");                    }    }                    public void receive (DialogMessage msg) {                if (LOG.isEnabledFor (Level.INFO)) {            //LOG.info ("Begin receive (DialogMessage)");            //LOG.info("ReceiveDialogMessage");        }                lastReceivedMessage = System.currentTimeMillis ();                        /** retrieve the messages command */        String sessionCommand = getMessageSessionCommand (msg);                        if (sessionCommand.equals (ViJxtaCallControl.COMMAND_VIJXTA_DATA)) {                                    if(this.isReceive () && getCallControl ().getProtocolState () == ViJxtaCallControl.SESSION_VIJXTA_INCALL) {                                receiveViJxtaData (msg);            }                    }else{                        if(getOriginator() == null) {                        setOriginator (msg.getOriginator ());        }            /** otherwise it is a session command that we need to deal with */            getCallControl ().callControl (sessionCommand, msg);                    }    }    private String getMessageSessionCommand (DialogMessage msg) {                String command = null;                        MessageElement me = msg.getMessageElement (ViJxtaCallControl.TAG_SESSION_COMMAND);                if (me != null) {                        if (me instanceof ByteArrayMessageElement) {                                ByteArrayMessageElement commandDataElement =                        (ByteArrayMessageElement) me;                                command = commandDataElement.toString ();                            } else {                                if (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("getMessageSessionCommand is not instanceof ByteArrayMessageElement");                }            }                    } else {            if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("sessionMessageCommandElement is null");            }        }                return command;    }                    public void receiveViJxtaData (DialogMessage msg) {                this.timeOfLastMessage = System.currentTimeMillis ();                byte[] imageBytes = getImageBytes (msg);                        if(imageBytes != null) {                                    incomingBuffer.addLast( new ByteArrayInputStream(imageBytes));                                                this.messagesReceived+=1;                        this.bytesReceived+=imageBytes.length;                        boolean locked = bufferStarvedLock.isLocked();                        if(locked && incomingBuffer.size () > 0) {                                synchronized(bufferStarvedLock) {                                        bufferStarvedLock.setLocked (false);                                        bufferStarvedLock.notifyAll();                }            }                    }else{            if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("reveiceViJxtaData : image data is null");            }        }            }        private byte[] getImageBytes (DialogMessage msg) {                byte[] imageBytes = null;                MessageElement me = msg.getMessageElement (ViJxtaCallControl.TAG_IMAGE_DATA);                if (me != null) {                        if (me instanceof ByteArrayMessageElement) {                                ByteArrayMessageElement imageDataElelment =                        (ByteArrayMessageElement) me;                                imageBytes = imageDataElelment.getBytes ();                            } else {                                if (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("getMessageImageBytes Image DATA Element is not instanceof ByteMessageElement");                }            }                    } else {            if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("imageMessageElement is null");            }        }                this.imageSize = imageBytes.length;                if(averageImageSize != 0) {                        this.averageImageSize = (averageImageSize + imageBytes.length) / 2;        }else{                        this.averageImageSize = imageBytes.length;        }                            return imageBytes;    }        public void startMonitor() {                this.setMonitorState (STARTED);                if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("startMonitor");                    }                }            public void startReceive() {                this.setReceiveState(STARTED);                this.start();                if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("startReceive");                    }        }        public void stopReceive() {                        this.setReceiveState(STOPPED);                        if (LOG.isEnabledFor (Level.INFO)) {                        LOG.info ("stopMonitor");                    }        }        public void pauseReceive() {                this.setReceiveState(PAUSED);    }        public void resumeReceive() {                this.setReceiveState(STARTED);                synchronized(pausedLock) {                        pausedLock.notifyAll ();                    }    }        public void obtainHardware() {

⌨️ 快捷键说明

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