📄 talk.java
字号:
/* * Copyright (c) 2001 Sun Microsystems, Inc. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. * * $Id: talk.java,v 1.65 2005/06/10 01:46:41 hamada Exp $ */package net.jxta.impl.shell.bin.talk;import java.awt.Frame;import java.awt.Graphics;import java.awt.Image;import java.awt.event.ComponentEvent;import java.awt.event.WindowEvent;import java.io.FileInputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import net.jxta.discovery.DiscoveryEvent;import net.jxta.discovery.DiscoveryListener;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.MimeMediaType;import net.jxta.endpoint.InputStreamMessageElement;import net.jxta.endpoint.Message;import net.jxta.endpoint.Message.ElementIterator;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.StringMessageElement;import net.jxta.id.IDFactory;import net.jxta.impl.shell.*;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;import net.jxta.pipe.InputPipe;import net.jxta.pipe.OutputPipe;import net.jxta.pipe.PipeID;import net.jxta.pipe.PipeService;import net.jxta.protocol.DiscoveryResponseMsg;import net.jxta.protocol.PipeAdvertisement;/** * talk Shell command: send and receive message from other users. * */public class talk extends ShellApp implements Runnable, DiscoveryListener { static class ImageWindow extends Frame { static ImageWindow currentWin = null; Image currentImage = null; boolean sizeDone = false; public ImageWindow() { super(); enableEvents(WindowEvent.WINDOW_CLOSING | WindowEvent.WINDOW_CLOSED | ComponentEvent.COMPONENT_RESIZED); } private void initSize(int width, int height) { if (sizeDone) { return; } if (width > 640) { width = 640; } else if (width < 100) { width = 100; } if (height > 480) { height = 480; } else if (height < 100) { height = 100; } setSize(width, height); sizeDone = true; } public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & (WIDTH|HEIGHT)) == (WIDTH|HEIGHT)) { initSize(width, height); } // Make sure events keep comming until we have the size done. return super.imageUpdate(img, infoflags, x, y, width, height) || !sizeDone; } private void setImageData(byte[] data) { sizeDone = false; currentImage = getToolkit().createImage(data); // Get the image size. int width = currentImage.getWidth(this); int height = currentImage.getHeight(this); if ((width != -1) && (height != -1)) { initSize(width, height); } } // For some reason resizing a frame does not repaint it // unless the new size is bigger in both dimensions ! protected void processComponentEvent(ComponentEvent e) { if (e.getID() == ComponentEvent.COMPONENT_RESIZED) { repaint(); } super.processComponentEvent(e); } protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) { dispose(); } else if (e.getID() == WindowEvent.WINDOW_CLOSED) { currentWin = null; } super.processWindowEvent(e); } public void paint(Graphics g) { if (currentImage != null) { if (sizeDone) { g.drawImage(currentImage, 0, 0, getSize().width, getSize().height, this); } } } static void showImageFromData(String title, byte[] data) { if (currentWin == null) { currentWin = new ImageWindow(); currentWin.pack(); currentWin.show(); } currentWin.setTitle(title); currentWin.setImageData(data); } } private boolean doImages = true; private String handleImageFromMsg(String sender, Message msg) { String textVersion = ""; try { MessageElement imageElem = msg.getMessageElement("talkx", "image"); if (imageElem != null) { textVersion = "<image>"; String title = "from " + sender; MessageElement captionElem = msg.getMessageElement("talkx", "image_caption"); if (captionElem != null) { String caption = captionElem.toString(); title = title + " : " + caption; textVersion = "<image caption=" + caption + ">"; } if (doImages) { byte[] data = imageElem.getBytes(false); ImageWindow.showImageFromData(title, data); textVersion = ""; } } } catch (Exception e) { printStackTrace( "failure getting image from message", e ); doImages = false; } return textVersion; } private static final int WaitingTime = 500; // 1/2 second private static final int MAXRETRIES = 20; // 20 times WaitingTime = 10 seconds public static final String TalkNameTag = "JxtaTalkUserName"; public static final String TalkIDTag = "JxtaTalkPipeID"; private static final String EnvName = "talkd"; private static final String SenderName = "JxtaTalkSenderName"; private static final String SenderMessage = "JxtaTalkSenderMessage"; private static final String SENDERGROUPNAME = "GrpName"; private DiscoveryService discovery=null; private ShellEnv env = null; private PipeAdvertisement userAdv = null; private String userName = null; private Thread thread = null; private InputPipe pipeIn = null; private List results; /** * {@inheritDoc} **/ public int startApp(String[] args) { if ((args == null) || (args.length == 0)) { return syntaxError(); } env = getEnv(); discovery= getGroup().getDiscoveryService(); if (args[0].equals("-register") || args[0].equals("-r")) { return registerNewUser(args); } if (args[0].equals("-login") || args[0].equals("-l")) { return login(args); } if (args[0].equals("-logout") || args[0].equals("-lo")) { return logout(args); } if (args[0].equals("-search") || args[0].equals("-f")) { return findUsers(); } return sendMessage(args); } private boolean deamonRunning(String name) { ShellObject obj = env.get(EnvName + "." + name +"@"+(getGroup().getPeerGroupAdvertisement()).getName()); if (obj == null) { return false; } else { return true; } } private int login(String[] args) { if (args.length != 2) { return syntaxError(); } String name = args[1]; if (deamonRunning(name)) { consoleMessage( "user " + name + " is already listening"); return ShellApp.appMiscError; } PipeAdvertisement adv = findUserAdv(name); if (adv == null) { consoleMessage( name + " is not a registered user"); return ShellApp.appMiscError; } runDeamon(name, adv); return ShellApp.appNoError; } private int findUsers() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -