📄 simplerosterplugin.java
字号:
package net.jxta.myjxta.plugins.example;import java.util.ArrayList;import java.util.Iterator;import net.jxta.myjxta.plugin.IPluginNotificationHandler;import net.jxta.myjxta.plugin.Plugin;import net.jxta.myjxta.plugin.PluginBase;import net.jxta.myjxta.plugin.PluginContainer;import net.jxta.myjxta.presence.PeerStatus;import net.jxta.myjxta.presence.PresenceController;import net.jxta.myjxta.util.Group;import net.jxta.myjxta.util.Log4J;import org.apache.log4j.Logger;/** * Example Plugin that sends a ping command to every known peer if the user * stats or stops the plugin (via the plugin menu entry). * If the user starts the plugin an "online" state will be transmitted via the * ping command, if the user stops the plugin an "away" state will be transmitted * * Additionally an "offline" state will be send to each known peer * if the user resigns from a joined group * * This code is only an example of the plugin interface - it was not the * intention of the author to implement a very clever implementation of the * typical p2p presence problem.... * */public class SimpleRosterPlugin extends PluginBase implements Plugin, IPluginNotificationHandler{ private static final Logger LOG = Logger.getLogger(SimpleRosterPlugin.class); //the plugin maintains a list of joined groups (via groupJoined(...)) private ArrayList<Group> m_joinedGroups = new ArrayList<Group>(); public SimpleRosterPlugin() { super(); } public void init(PluginContainer c) { super.init(c); //start(); // you can activate a plugin inside the init method } public void start() { super.start(); for (Iterator<Group> iterator = m_joinedGroups.iterator(); iterator.hasNext();) { Group group = iterator.next(); setOnlineStateForGroup(group); resendOwnStatus(group); } } public void stop() { LOG.log(Log4J.STATUS_LEVEL, "SimpleRosterPlugin stopped"); for (Iterator<Group> iterator = m_joinedGroups.iterator(); iterator.hasNext();) { Group group = iterator.next(); PresenceController.setOwnPeerStatus(group, PeerStatus.getAwayState("signoff notifier disabled")); resendOwnStatus(group); } super.stop(); } public void destroy() { if (m_running) { stop(); } super.destroy(); } public String getName() { return "Signoff Notifier (SimpleRoster)"; } // END OF PLUGIN API // HELPER METHODS private void setOnlineStateForGroup(Group p_group) { LOG.info("setting online state for group" + p_group); PresenceController.setOwnPeerStatus(p_group, PeerStatus.getOnlineState("signoff notifier enabled")); } private void resendOwnStatus(Group p_group) { LOG.info("sending ping for:" + p_group); PresenceController.pingAllNodesInGroup(p_group); } public IPluginNotificationHandler getPluginNotificationHander() { return this; //for now we will implement this interface ourselve, //the plugin will grow so this will be moved into a separate class } //this method is called by myjxta every time a group is joined public void groupJoined(Group p_group) { if (!p_group.isVisible()) return; m_joinedGroups.add(p_group); if (isRunning()) { setOnlineStateForGroup(p_group); } } public void groupResigned(Group p_group) { if (!p_group.isVisible()) return; m_joinedGroups.remove(p_group); if (isRunning()) { //user has left the group... send the notification to inform others... fireResignListenerForGroup(p_group); try { Thread.sleep(500); //hack! WARNING, THIS WILL BLOCK THE SWING THREAD FOR 500ms, dont increase the value! } catch (InterruptedException e) { e.printStackTrace(); } } } private void fireResignListenerForGroup(Group p_group) { LOG.info("sending ping for:" + p_group); //first set our own state to offline for this group PresenceController.setOwnPeerStatus(p_group, PeerStatus.getOfflineState("signed off")); //now send a general status ping to all (known) peers in the group //thats inefficient.. we should use a propagation pipe broadcast for this, but its //only an example.... PresenceController.pingAllNodesInGroup(p_group); } public void groupStateChanged(Group p_group) { if (!p_group.isVisible()) return; if (!isRunning()) { // if we are not active we are not interested in connection events return; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -