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

📄 bscore.java

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

/*
 * BSCore.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 * Created on 24 June 2002, 18:30
 */

import java.util.*;

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

/**
 * <code>BSCore</code> is the main class of BuddySpace backend. It provides
 * the jabber client functionality using other classes such as
 * <code>BSConnectionBean</code>, <code>BSMessengerBean</code>,
 * <code>BSPresenceBean</code>, <code>BSInfoQueryBean</code> and other
 * specialized classes.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSCore implements ConnectionListener,
                               BSAuthListener, BSRegListener {
                               //BSAgentsListener,

    private static Vector logListeners = null;
    private static Vector loginListeners = null;

    private BSConnectionBean connection = null;
    private BSLogger logger = null;
    private BSInfoQueryBean infoQuery = null;
    private BSAuthorizationBean auth = null;
    private BSRegisterBean register = null;
    private BSRosterBean roster = null;
    private BSPresenceBean presence = null;
    private BSMessengerBean messenger = null;
    private BSVideoConferenceBean videoConference = null;

    //BSAgentsBean agents;

    static int idCounter;

    private String username = null;
    private String password = null;
    private String resource = null;
    private String server = null;
    private int priority = 0;
    private boolean doRegister = false;
    private boolean doAuthenticate = false;

    public final static int KEEP_ALIVE_TIMEOUT = 120000;
    private KeepAlive keepAlive = null;

    private BSServedIDs servedIDs = new BSServedIDs();

    /** Creates new BSCore */
    public BSCore() {
        System.out.println("BSCore constructor");

        idCounter = 0;

        connection = new BSConnectionBean();
        connection.addConnectionListener(this);

        logListeners = new Vector();
        loginListeners = new Vector();

        logger = new BSLogger();

        infoQuery = new BSInfoQueryBean();
        //infoQuery.addIQListener(this);

        auth = new BSAuthorizationBean();
        auth.addAuthListener(this);

        register = new BSRegisterBean();
        //register.addRegListener(this);

        roster = new BSRosterBean();
        //roster.addRosterListener(this);

        presence = new BSPresenceBean();
        //presence.addPresenceListener(this);

        messenger = new BSMessengerBean();
        //messenger.addMessageListener(this);

        videoConference = new BSVideoConferenceBean();

        /*agents = new BSAgentsBean();
        agents.addAgentsListener(this);
        map = new BSMapBean();
        //map.addMapListener(this);*/
    }

    /**
     * Returns currently used BSConnectionBean
     */
    public BSConnectionBean getConnectionBean() {
        return connection;
    }

    /**
     * Returns currently used BSInfoQueryBean
     */
    public BSInfoQueryBean getInfoQueryBean() {
        return infoQuery;
    }

    /**
     * Returns currently used BSRosterBean
     */
    public BSRosterBean getRosterBean() {
        return roster;
    }

    /**
     * Returns currently used BSPresenceBean
     */
    public BSPresenceBean getPresenceBean() {
        return presence;
    }

    /**
     * Returns currently used BSMessengerBean
     */
    public BSMessengerBean getMessengerBean() {
        return messenger;
    }

    /**
     * Returns currently used BSAutorizationBean
     */
    public BSAuthorizationBean getAuthBean() {
        return auth;
    }

    /**
     * Returns currently used BSRegisterBean
     */
    public BSRegisterBean getRegisterBean() {
        return register;
    }

    public BSVideoConferenceBean getVideoConferenceBean() {
        return videoConference;
    }

    /**
     * Returns next unique ID typically used for messages,...
     */
    public static int getNextID() {
        return idCounter++;
    }

    /** Returns current connection state */
    public ConnectionEvent.EState getConnectionState() {
        return (connection == null)? ConnectionEvent.STATE_DISCONNECTED :
                                     connection.getConnectionState();
    }


    // *** logging functions ***

    /** Logs event */
    public static void logEvent(String sender, String log) {
        fireLogStatus(sender, log);
    }

    /** Logs message */
    public static void logMessage(String from, String subject, String body) {
        fireLogMessage(from, subject, body);
    }


    // *** connection ***

    /** Starts keep alive */
    protected synchronized void startKeepAlive() {
        System.out.println("BSCore startKeepAlive()");
        if (keepAlive != null) {
                keepAlive.shutdown();
        }
        ConnectionBean cb;
        if (connection == null || (cb = connection.getConnection()) == null) {
            keepAlive = null;
            return;
        }
        keepAlive = new KeepAlive(cb, KEEP_ALIVE_TIMEOUT);
        Thread t = new Thread() {
            public void run() {
                keepAlive.run();
        }};
        t.start();
    }


    /** Stops keep alive */
    protected synchronized void stopKeepAlive() {
        System.out.println("BSCore stopKeepAlive()");

        if (keepAlive != null) {
                keepAlive.shutdown();
                keepAlive = null;
        }
    }


    /** Sets connection state */
    protected synchronized void setConnectionState(ConnectionEvent.EState state) {

        if (state == ConnectionEvent.STATE_CONNECTED) {
            startKeepAlive();

            ConnectionBean cb;
            if (connection != null && (cb = connection.getConnection()) != null) {
                logger.setConnection(cb);
                infoQuery.setConnection(cb);
                auth.setIQBean(infoQuery.getIQBean());
                register.setIQBean(infoQuery.getIQBean());
                roster.setIQBean(infoQuery.getIQBean());
                presence.setConnection(cb);
                messenger.setConnection(cb);
                videoConference.setConnection(cb);
            }

            // if should directly authenticate
            if (!doRegister && doAuthenticate) {
                //auth.sendPassword(username, password, resource);
                auth.authorize(username, password, resource);
            }
            // if should register
            else if (doRegister) {
                //register.register();
                register.addRegListener(this);
                PacketID id = new PacketID();
                servedIDs.add(id);
                register.sendInfos(username, password, id);
            }
        }

        else if (state == ConnectionEvent.STATE_DISCONNECTED) {
            stopKeepAlive();

            // cmd4: kill the listeners for presence, otherwise setConnection
            // hangs (deadlock?) on reconnect after external disconnect.
            presence.setConnection(null);

            username = password = resource = server = null;
            doRegister = doAuthenticate = false;

            if (presence != null) {
                presence.clear();
                //presence.setMyPresence(false, null, null, 0);
                presence.setMyJID(null);
            }
            if (roster != null)
                roster.clear();
        }
    }


    /** Connects to given server */
    public boolean connect(String server, int port) {
        System.out.println("BSCore connect(" + server + ", " + port + ")");

        username = password = resource = server = null;
        doRegister = doAuthenticate = false;

        return connectImpl(server, port);
    }


    /** Implementation of connect */
    protected boolean connectImpl(String server, int port) {
        return connectImpl(server, port, false);
    }

    /** Implementation of connect */
    protected boolean connectImpl(String server, int port, boolean useSSL) {
        stopKeepAlive();
        if (connection == null) return false;
        return connection.connect(server, port, useSSL);
    }

    /** Logs in using given information */
    public synchronized boolean connect(String username, String password, String resource,
                        String server, int port, int priority, boolean newAccount) {
        return connect(username, password, resource, server, port, priority, newAccount, false);
    }

    /** Logs in using given information */
    public synchronized boolean connect(String username, String password, String resource,
                        String server, int port, int priority, boolean newAccount, boolean useSSL) {

        System.out.println("BSCore connect(" + username + ", " + server + ",...)");

        // when connection exists
        if (ConnectionEvent.STATE_DISCONNECTED != getConnectionState()) {

⌨️ 快捷键说明

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