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

📄 bsmapbean.java

📁 一款即时通讯软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package edu.ou.kmi.buddyspace.plugins.maps.core;

/*
 * BSMapBean.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 16 August 2002, 10:20
 */

import java.util.*;
import java.net.*;
import java.io.*;

import javax.swing.*;

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

import edu.ou.kmi.buddyspace.core.*;
import edu.ou.kmi.buddyspace.xml.*;
import edu.ou.kmi.buddyspace.plugins.maps.xml.*;
import edu.ou.kmi.buddyspace.utils.*;

/**
 * <code>BSMapBean</code> provides map plugin back-end.
 * It relies on <code>BSMessengerBean</code> and <code>BSInfoQueryBean</code>,
 * which must be set after each reconnection.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSMapBean implements PacketListener {
    protected MessengerBean msgBean = null;
    protected IQBean iqBean = null;
    protected String name = "Map";
    protected Hashtable maps = null;
    protected Vector mapListeners = null;
    private String mapPath;

    private String proxy = null;
    private String proxyPort = null;
    private String fileServer = null;
    private String fileServerPath = null;
    private String fileServerPort = null;

    private boolean recursiveMapsSend = true;

    /**
     * Constructor
     */
    public BSMapBean(String mapPath) {
        maps = new Hashtable();
        mapListeners = new Vector();
        this.mapPath = mapPath;
    }

    /**
     * Constructor, which sets existing and connected <code>IQBean</code>.
     * Then this is registered as listener for IQ packets.
     */
    public BSMapBean(String mapPath, IQBean iqBean) {
        maps = new Hashtable();
        mapListeners = new Vector();
        this.mapPath = mapPath;
        setIQBean(iqBean);
    }

    /**
     * Constructor, which sets existing and connected <code>MessengerBean</code>.
     * Then this is registered as listener for message packets.
     */
    public BSMapBean(String mapPath, MessengerBean msgBean) {
        maps = new Hashtable();
        mapListeners = new Vector();
        this.mapPath = mapPath;
        setMessengerBean(msgBean);
    }

    /**
     * Sets existing and connected <code>IQBean</code>.
     * Then this is registered as listener for IQ packets.
     */
    public void setIQBean(IQBean iqBean) {
        if (this.iqBean != null)
            this.iqBean.delPacketListener(this);
        this.iqBean = iqBean;
        if (iqBean != null)
            iqBean.addPacketListener(this);
        //servedID = null;
    }

    /**
     * Sets existing and connected <code>MessengerBean</code>.
     * Then this is registered as listener for message packets.
     */
    public void setMessengerBean(MessengerBean msgBean) {
        if (this.msgBean != null)
            this.msgBean.delPacketListener(this);
        this.msgBean = msgBean;
        if (msgBean != null)
            msgBean.addPacketListener(this);
    }

    /** Sets proxy for sending map files */
    public void setProxy(String proxy, String proxyPort) {
        this.proxy = proxy;
        this.proxyPort = proxyPort;
    }

    /** Sets file server for sending map files */
    public void setFileServer(String fileServer, String fileServerPath, String fileServerPort) {
        this.fileServer = fileServer;
        this.fileServerPath = fileServerPath;
        this.fileServerPort = fileServerPort;
    }

    /**
     * Returns currently used <code>IQBean</code>.
     */
    public IQBean getIQBean() {
        return iqBean;
    }

    /**
     * Returns currently used <code>MessengerBean</code>.
     */
    public MessengerBean getMessengerBean() {
        return msgBean;
    }

    /**
     * Frees all object bindings to allow object destroy
     */
    public void prepareToDestroy() {
        removeAllMapListeners();
        msgBean = null;
        iqBean = null;
    }

    /**
     * Invoked when a message packet is received.
     */
    public void receivedPacket(PacketEvent pe) {

        if (pe.getPacket() instanceof Message) {
            Message msg = (Message) pe.getPacket();
            handleMessage(msg);
            return;
        }

        else if (pe.getPacket() instanceof InfoQuery) {
            InfoQuery iq = (InfoQuery) pe.getPacket();
            //handleInfoQuery(iq);
            return;
        }

        else {
            //BSCore.logEvent(name, "warning: non-map packet received");
            return;
        }
    }

    /**
     * Handles <code>Message</code> packet.
     */
    protected void handleMessage(Message msg) {

        if ((new String("error")).equals(msg.getType()))
            return;

        JID jid = msg.getFromAddress();
        String originID = new String(jid.getUsername() + "@" + jid.getServer());

        Enumeration extensions;

        Vector curMaps = new Vector();
        extensions = msg.Extensions();
        // for all extensions
        while (extensions.hasMoreElements()) {
            Extension ext = (Extension) extensions.nextElement();
            // if map extension
            if (ext instanceof MapTag) {
                MapTag map = (MapTag) ext;
                logMap(map);
                // adds map into temporary hashtable
                curMaps.add(map);
                String completeID = new String(jid.getUsername() + "@" +
                                               jid.getServer() + "/" + map.getID());
                maps.put(completeID, map);
            }
        }

        // if that was not map message ends
        if (curMaps.isEmpty())
            return;

        // if it was map message, looks for included map images
        extensions = msg.Extensions();
        while (extensions.hasMoreElements()) {
            Extension ext = (Extension) extensions.nextElement();
            // for all included files
            if (ext instanceof OOB) {
                OOB oob = (OOB) ext;
                try {
                    handleMapOOB(oob, jid);
                } catch (IOException e) {}
            }
        }

        // for all just received maps
        Enumeration mapEnum = curMaps.elements();
        while (mapEnum.hasMoreElements()) {
            MapTag map = (MapTag) mapEnum.nextElement();
            // add references to referenced maps
            processMap(map, originID);
            // saves map into local cache
            saveMap(map, originID);
            fireMapReceived(map, originID);
        }
        curMaps.clear();
    }

    /** Handles received OOB extension.
     * Downloads it and stores in maps directory for given JID.
     */
    protected void handleMapOOB(OOB oob, JID jid)
                                    throws IOException {
        String originID = new String(jid.getUsername() + "@" + jid.getServer());
        handleMapOOB(oob, originID);
    }

    /** Handles received OOB extension.
     * Downloads it and stores in maps directory for given originID.
     */
    public void handleMapOOB(OOB oob, String originID)
                                    throws IOException {
        URL url;
        File file;
        String filename = oob.getDescription();
        String urlStr = oob.getURL();
        try {
            url = new URL(urlStr);
            //file = new File(url.getFile());
            //filename = f.getName();
        } catch (MalformedURLException e) {
            throw new IOException("Malformed URL");
        } catch (NullPointerException e) {
            throw new IOException("Unknown file name");
        }
        //file = new File(jidDir);
        file = new File(mapPath + "/" + originID);
        file.mkdirs();
        //file = new File(jidDir + "/" + filename);
        file = new File(mapPath + "/" + originID + "/" + filename);
        try {
            URLFileTransfer.downloadURL(url, file, proxy, proxyPort);
        }
        catch (IOException e) {
            throw e;
        }
    }

    /** Debug dump of map tag */
    protected void logMap(MapTag map) {
        BSCore.logEvent(name, "map id=" + map.getID() + " recieved");
        Enumeration layers = map.layers();
        while (layers.hasMoreElements()) {

            LayerTag layer = (LayerTag) layers.nextElement();
            BSCore.logEvent(name, "layer id=" + layer.getID() +
                                       " offset_x=" + layer.getOffsetX() +
                                       " offset_y=" + layer.getOffsetY() +
                                       " scale=" + layer.getScale() +
                                       " src=" + layer.getSrc() +
                                       " priority=" + layer.getPriority());

            ImgTag img = layer.getImg();
            if (img != null)
                BSCore.logEvent(name, "  img src=" + img.getSrc() +
                                           " width=" + img.getWidth() +
                                           " height=" + img.getHeight());

            Enumeration items = layer.getItems();
            while (items.hasMoreElements()) {
                Object o = items.nextElement();
                if (o instanceof ItemTag) {
                    ItemTag i = (ItemTag) o;
                    BSCore.logEvent(name, "  item jid=" + i.getJID().toString() +
                                                " lat=" + i.getLat() +
                                                " lon=" + i.getLon());
                }
                else if (o instanceof ClusterTag) {
                    ClusterTag c = (ClusterTag) o;
                    BSCore.logEvent(name, "  cluster size=" + c.getSize() +
                                                " lat=" + c.getLat() +
                                                " lon=" + c.getLon());
                }
            }
        }
    }

    /** Saves map into map directory for originID */
    public void saveMap(MapTag map, String originID) {
        File file = new File(mapPath + "/" + originID);
        file.mkdirs();
        String filename = new String(mapPath + "/" + originID + "/" +
                                     map.getID());
        try {

⌨️ 快捷键说明

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