📄 vojxtainviteaction.java
字号:
/* * Created on Feb 25, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */package net.jxta.myjxta.plugins.vojxta;import net.jxta.document.AdvertisementFactory;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredTextDocument;import net.jxta.endpoint.ByteArrayMessageElement;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.TextDocumentMessageElement;import net.jxta.myjxta.View;import net.jxta.myjxta.dialog.Dialog;import net.jxta.myjxta.dialog.DialogManager;import net.jxta.myjxta.dialog.DialogMessage;import net.jxta.myjxta.dialog.util.RemoteCommandInvoker;import net.jxta.myjxta.plugins.vojxta.command.VOJXTACommand;import net.jxta.myjxta.util.Group;import net.jxta.myjxta.util.GroupNode;import net.jxta.myjxta.util.Peer;import net.jxta.myjxta.util.PeerNode;import net.jxta.myjxta.util.Resources;import net.jxta.pipe.PipeService;import net.jxta.protocol.PipeAdvertisement;import org.apache.log4j.Level;import org.apache.log4j.Logger;import javax.swing.*;import java.awt.event.ActionEvent;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.ResourceBundle;/** * @author Ravi * @author jamoore * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */public class VoJxtaInviteAction extends AbstractAction { private static final int INTERVAL = 250; private static final int MAX = INTERVAL * 4 * 75; private static final Logger LOG = Logger.getLogger(VoJxtaInviteAction.class.getName()); private View view = null; private static final ResourceBundle STRINGS = Resources.getStrings(); public VoJxtaInviteAction(String name, View view) { super(name); this.view = view; if (LOG.isEnabledFor(Level.INFO)) { LOG.info("VOIPAction instantiated"); } } public void actionPerformed(ActionEvent ae) { PeerNode pn = (PeerNode) this.view.getJxtaNode(PeerNode.class); final Peer p = pn != null ? pn.getPeer() : null; final Group g = pn != null ? ((GroupNode) (pn.getParent())).getGroup() : null; String status = null; if (p != null && g != null) { status = this.STRINGS.getString("status.command.initiate") + ": " + p.getName(); if (LOG.isEnabledFor(Level.INFO)) { LOG.info(status); } new Thread(new Runnable() { public void run() { process(g, p); } }, getClass().getName() + ":getConnection").start(); } else { status = this.STRINGS.getString("error.peer.invalid") + " " + p.getName(); if (LOG.isEnabledFor(Level.ERROR)) { LOG.error(status); } } if (status != null) { this.view.setStatus(status); } } private void process(Group g, Peer p) { LOG.setLevel(Level.INFO); String status = "initiating vojxta action"; this.view.setStatus(status); if (LOG.isEnabledFor(Level.INFO)) { LOG.info(status); } //get pipe adv from DialogManager - this is the VOIPPipe StructuredDocument d = getPipeAdv(g); //attach it as a message element TextDocumentMessageElement te = d != null ? new TextDocumentMessageElement(VOJXTACommand.PIPE, (StructuredTextDocument) d, null) : null; if (te != null) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info(status); LOG.info("instantiating VOIPCommand"); } //command fetecher will send out a command on the command pipe RemoteCommandInvoker cf = new RemoteCommandInvoker(g, p.getPipeAdvertisement(), new VOJXTACommand(), this.view.getControl()); Map m = new HashMap(); // add the our pipe element m.put(VOJXTACommand.PIPE, te); if (LOG.isEnabledFor(Level.INFO)) { LOG.info("dispatch VOIPcommand"); } cf.invoke(MAX, m); //get respsonse DialogMessage r = cf.getResponse(); if (LOG.isEnabledFor(Level.INFO)) { LOG.info("VOIPInvitationCommand response: " + r); } //this is why its important to have a consistent PIPE element in TestCommand MessageElement pae = r != null ? r.getMessageElement(VOJXTACommand.PIPE) : null; if (pae != null) { status = "vojxta pipe import" + ": " + p.getName(); this.view.setStatus(status); if (LOG.isEnabledFor(Level.INFO)) { LOG.info(status); LOG.info("adding dialog"); } VoJxtaDialogPanel.setInitiatedLocally(true); this.view.getControl().addDialog( DialogManager.getDialog(VoJxtaDialog.class, g, getPipeAdv(pae), this.view.getControl())); } else { status = "request vojxta pipe" + ": " + p.getName(); this.view.setStatus(status); if (LOG.isEnabledFor(Level.ERROR)) { LOG.error(status); } } } else { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("invalid response"); } } LOG.setLevel(Level.ERROR); } private PipeAdvertisement getPipeAdv(MessageElement me) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("get pipeAdv"); } StructuredDocument sd = null; if (true || me instanceof ByteArrayMessageElement) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("me instanceof ByteMessageElement"); } try { sd = StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, me.getStream()); } catch (IOException ioe) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("can't document", ioe); } } } else { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("me not instanceof ByteMessageElement"); } } PipeAdvertisement pa = null; if (sd != null) { try { pa = (PipeAdvertisement) AdvertisementFactory. newAdvertisement(MimeMediaType.XMLUTF8, sd.getStream()); } catch (IOException ioe) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("can't get pipe advertisement", ioe); } } } return pa; } private StructuredDocument getPipeAdv(Group g) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("getPipeAdvl"); } String pn = Dialog.getDialogNamer(VoJxtaDialog.class). getDialogName(g.getPeerGroup().getPeerName()); PipeAdvertisement pa = DialogManager.getInstance(g, pn, PipeService.UnicastType).getPipeAdv(g.getPeerGroup()); return (StructuredDocument) pa.getDocument(MimeMediaType.XMLUTF8); } public boolean isEnabled() { PeerNode jxtaNode = (PeerNode) view.getJxtaNode(PeerNode.class); return jxtaNode != null; // we need a check that this isnt our own peer }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -