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

📄 bsdiscobean.java

📁 一款即时通讯软件
💻 JAVA
字号:
package edu.ou.kmi.buddyspace.plugins.disco.core;

/*
 * BSDiscoBean.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2003
 *
 *
 * Created on 18 August 2003, 17:18
 */

import java.util.*;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
import org.jabber.jabberbeans.Extension.*;

import edu.ou.kmi.buddyspace.plugins.disco.xml.*;

/**
 * <code>BSDiscoBean</code> provides jabber disco.
 * It relies on <code>IQBean</code> (this must be reset after reconnection).
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSDiscoBean implements PacketListener {
    protected ConnectionBean connBean = null;
    protected Vector discoListeners = null;
    protected final String name = "Disco";
    
    /**
     * Constructor
     */
    public BSDiscoBean() {
        discoListeners = new Vector();
    }
    
    /**
     * Sets existing and connected <code>ConnectionBean</code>.
     * Then this is registered as listener for packets.
     */
    public void setConnection(ConnectionBean connBean) {
        if (this.connBean != null)
            this.connBean.delPacketListener(this);
        this.connBean = connBean;
        if (connBean != null)
            connBean.addPacketListener(this);
    }
    
    /**
     * Returns currently used <code>ConnectionBean</code>.
     */
    public ConnectionBean getConnection() {
        return connBean;
    }
    
    
    /** Called when disconnected */
    public void disconnected() {
        if (connBean != null)
            connBean.delPacketListener(this);
        connBean = null;
    }
    
    /** Discovers items of the given jid and node */
    public boolean discoverItems(JID jid, String node, PacketID id) {
        if (connBean == null) return false;
        
        try {
            IQDiscoItemsBuilder dib = new IQDiscoItemsBuilder();
            dib.setNode(node);

            InfoQueryBuilder iqb = new InfoQueryBuilder();
            iqb.setType("get");
            iqb.setToAddress(jid);
            iqb.setIdentifier(id.getID());
            iqb.addExtension(new IQDiscoItems(dib));
            
            InfoQuery iq = (InfoQuery) iqb.build();
            if (id != null) id.setID(iq.getIdentifier());
            return sendIQ(iq);
            
        } catch (InstantiationException e) {
            return false;
        }
    }
    
    /** Discovers items of the given jid */
    public boolean discoverItems(JID jid, PacketID id) {
        return discoverItems(jid, null, id);
    }
    
    /** Discovers items of the given jid and node */
    public boolean discoverItems(String jidStr, String node, PacketID id) {
        JID jid = JID.fromString(jidStr);
        if (jid == null) return false;
        return discoverItems(jid, node, id);
    }
    
    /** Discovers info of the given jid and node */
    public boolean discoverInfo(JID jid, String node, PacketID id) {
        if (connBean == null) return false;
        
        try {
            IQDiscoInfoBuilder dib = new IQDiscoInfoBuilder();
            dib.setNode(node);

            InfoQueryBuilder iqb = new InfoQueryBuilder();
            iqb.setType("get");
            iqb.setToAddress(jid);
            iqb.setIdentifier(id.getID());
            iqb.addExtension(new IQDiscoInfo(dib));
            
            InfoQuery iq = (InfoQuery) iqb.build();
            if (id != null) id.setID(iq.getIdentifier());
            return sendIQ(iq);
            
        } catch (InstantiationException e) {
            return false;
        }
    }
    
    /** Discovers info of the given jid */
    public boolean discoverInfo(JID jid, PacketID id) {
        return discoverInfo(jid, null, id);
    }
    
    /** Discovers info of the given jid and node */
    public boolean discoverInfo(String jidStr, String node, PacketID id) {
        return discoverInfo(JID.fromString(jidStr), node, id);
    }
    
    /** Sends given iq packet */
    public boolean sendIQ(InfoQuery iq) {
        if (iq == null || connBean == null) return false;
        connBean.send(iq);
        return true;
    }
    
    /**
     * Frees all object bindings to allow object destroy
     */
    protected void prepareToDestroy() {
        //removeAllBrowseListeners();
        if (connBean != null) {
            connBean.delPacketListener(this);
            connBean = null;
        }
    }
    
    // *** LISTENER METHODS ***
    
    /** called when a packet is received and processed.
     */
    public void receivedPacket(PacketEvent pe) {
        if (pe == null) return;
        Packet p = pe.getPacket();
        if (p == null || !(p instanceof InfoQuery)) return;
        //if (p == null || !(p instanceof ContentPacket)) return;
        String id = ((ContentPacket)p).getIdentifier();
        String type = ((ContentPacket)p).getType();
        Enumeration extEnum = ((ContentPacket)p).Extensions();
        // empty result - possibly of "set"
        if (!extEnum.hasMoreElements() && type != null && type.equalsIgnoreCase("result")) {
            fireEmptyResultReceived((InfoQuery) p, id);
        }
        else while (extEnum.hasMoreElements()) {
            Object e = extEnum.nextElement();
            if (e instanceof IQDiscoItems) {
                if (type != null && type.equalsIgnoreCase("result"))
                    fireItemsReceived((IQDiscoItems) e, id);
                else if (type != null && type.equalsIgnoreCase("get"))
                    fireItemsRequestReceived((InfoQuery)p, id);
                else if (type != null && type.equalsIgnoreCase("error"))
                    fireError((InfoQuery)p, id);
            }
            else if (e instanceof IQDiscoInfo) {
                if (type != null && type.equalsIgnoreCase("result"))
                    fireInfoReceived((IQDiscoInfo) e, id);
                else if (type != null && type.equalsIgnoreCase("get"))
                    fireInfoRequestReceived((InfoQuery)p, id);
                else if (type != null && type.equalsIgnoreCase("error"))
                    fireError((InfoQuery)p, id);
            }
        }
    }
    
    /** called if a packet is not successfully sent (for instance, if the connection
     * dies while the packet is queued, or a packet is sent while disconnected).
     */
    public void sendFailed(PacketEvent pe) {
        if (pe == null) return;
        Packet p = pe.getPacket();
        //if (p == null || ! p instanceof InfoQuery) return;
        if (p == null || !(p instanceof ContentPacket)) return;
        String id = ((ContentPacket)p).getIdentifier();
        String type = ((ContentPacket)p).getType();
        Enumeration extEnum = ((ContentPacket)p).Extensions();
        while (extEnum.hasMoreElements()) {
            Object e = extEnum.nextElement();
            if (e instanceof IQDiscoItems /*|| e instanceof IQDiscoInfo*/) {
                fireSendFailed((InfoQuery)p, id);
            }
        }
    }
    
    /** called whenever a local client sends a packet, after the sending
     * is successful
     */
    public void sentPacket(PacketEvent pe) { }
    
    // *** browse listeners ***
    
    /**
     * Adds <code>BSDiscoListener</code> to listeners notified when
     * disco events.
     *
     * @see #removePresenceListener
     * @see #removeAllPresenceListeners
     * @see #firePresenceChanged
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    public void addDiscoListener(BSDiscoListener listener) {
        if (discoListeners.contains(listener)) return;
        discoListeners.add(listener);
    }

    /**
     * Removes <code>BSDiscoListener</code> to listeners notified about
     * disco events.
     *
     * @see #addPresenceListener
     * @see #removeAllPresenceListeners
     * @see #firePresenceChanged
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    public void removeDiscoListener(BSDiscoListener listener) {
        discoListeners.remove(listener);
    }

    
    /**
     * Removes all listeners notified when presence state of some of buddies changes.
     * This can be used before to free dependencies and allow dispose of
     * all objects.
     *
     * @see #addPresenceListener
     * @see #removePresenceListener
     * @see #firePresenceChanged
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    public void removeAllDiscoListeners() {
        discoListeners.clear();
    }

    /**
     * Notifies disco listeners about received disco#items.
     *
     * @see #addPresenceListener
     * @see #removePresenceListener
     * @see #removeAllPresenceListeners
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    private void fireItemsReceived(IQDiscoItems items, String id) {
        for (Enumeration e = discoListeners.elements(); e.hasMoreElements(); ) {
            BSDiscoListener listener = (BSDiscoListener) e.nextElement();
            listener.receivedItems(items, id);
        }
    }
    
    /**
     * Notifies disco listeners about received disco#info.
     *
     * @see #addPresenceListener
     * @see #removePresenceListener
     * @see #removeAllPresenceListeners
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    private void fireInfoReceived(IQDiscoInfo info, String id) {
        for (Enumeration e = discoListeners.elements(); e.hasMoreElements(); ) {
            BSDiscoListener listener = (BSDiscoListener) e.nextElement();
            listener.receivedInfo(info, id);
        }
    }
    
    /**
     * Notifies disco listeners about received request for disco#items.
     *
     * @see #addPresenceListener
     * @see #removePresenceListener
     * @see #removeAllPresenceListeners
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    private void fireItemsRequestReceived(InfoQuery iq, String id) {
        for (Enumeration e = discoListeners.elements(); e.hasMoreElements(); ) {
            BSDiscoListener listener = (BSDiscoListener) e.nextElement();
            listener.receivedItemsRequest(iq, id);
        }
    }
    
    /**
     * Notifies disco listeners about received request for disco#info.
     *
     * @see #addPresenceListener
     * @see #removePresenceListener
     * @see #removeAllPresenceListeners
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    private void fireInfoRequestReceived(InfoQuery iq, String id) {
        for (Enumeration e = discoListeners.elements(); e.hasMoreElements(); ) {
            BSDiscoListener listener = (BSDiscoListener) e.nextElement();
            listener.receivedInfoRequest(iq, id);
        }
    }
    
    /**
     * Notifies disco listeners about received empty result possibly of previous "set".
     *
     * @see #addPresenceListener
     * @see #removePresenceListener
     * @see #removeAllPresenceListeners
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    private void fireEmptyResultReceived(InfoQuery iq, String id) {
        for (Enumeration e = discoListeners.elements(); e.hasMoreElements(); ) {
            BSDiscoListener listener = (BSDiscoListener) e.nextElement();
            listener.receivedEmptyResult(iq, id);
        }
    }
    
    /**
     * Notifies disco listeners about received error.
     *
     * @see #addPresenceListener
     * @see #removePresenceListener
     * @see #removeAllPresenceListeners
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    private void fireError(InfoQuery iq, String id) {
        for (Enumeration e = discoListeners.elements(); e.hasMoreElements(); ) {
            BSDiscoListener listener = (BSDiscoListener) e.nextElement();
            listener.error(iq, id);
        }
    }
    
    /**
     * Notifies disco listeners about send failure.
     *
     * @see #addPresenceListener
     * @see #removePresenceListener
     * @see #removeAllPresenceListeners
     * @see #fireSubscriptionRequested
     * @see #fireSubscriptionApproved
     */
    private void fireSendFailed(InfoQuery iq, String id) {
        for (Enumeration e = discoListeners.elements(); e.hasMoreElements(); ) {
            BSDiscoListener listener = (BSDiscoListener) e.nextElement();
            listener.sendFailed(iq, id);
        }
    }

}

⌨️ 快捷键说明

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