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

📄 softphonemanager.java.svn-base

📁 开源项目openfire的完整源程序
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2007 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package net.java.sipmack.softphone;

import org.jivesoftware.sparkplugin.calllog.LogManager;
import org.jivesoftware.sparkplugin.calllog.LogPacket;
import org.jivesoftware.sparkplugin.preferences.SipPreference;
import org.jivesoftware.sparkplugin.preferences.SipPreferences;
import org.jivesoftware.sparkplugin.sipaccount.SipAccount;
import org.jivesoftware.sparkplugin.sipaccount.SipAccountPacket;
import org.jivesoftware.sparkplugin.ui.call.MissedCalls;
import org.jivesoftware.spark.plugin.phone.resource.PhoneRes;
import net.java.sipmack.common.DialSoundManager;
import net.java.sipmack.common.Log;
import net.java.sipmack.events.UserActionListener;
import net.java.sipmack.media.AudioMediaSession;
import net.java.sipmack.media.AudioReceiverChannel;
import net.java.sipmack.media.JmfMediaManager;
import net.java.sipmack.media.MediaException;
import net.java.sipmack.sip.Call;
import net.java.sipmack.sip.CommunicationsException;
import net.java.sipmack.sip.Interlocutor;
import net.java.sipmack.sip.InterlocutorUI;
import net.java.sipmack.sip.NetworkAddressManager;
import net.java.sipmack.sip.SIPConfig;
import net.java.sipmack.sip.SipManager;
import net.java.sipmack.sip.SipRegisterStatus;
import net.java.sipmack.sip.event.CallEvent;
import net.java.sipmack.sip.event.CallListener;
import net.java.sipmack.sip.event.CallRejectedEvent;
import net.java.sipmack.sip.event.CallStateEvent;
import net.java.sipmack.sip.event.CommunicationsErrorEvent;
import net.java.sipmack.sip.event.CommunicationsListener;
import net.java.sipmack.sip.event.MessageEvent;
import net.java.sipmack.sip.event.RegistrationEvent;
import net.java.sipmack.sip.event.UnknownMessageEvent;
import net.java.sipmack.softphone.gui.DefaultGuiManager;
import net.java.sipmack.softphone.gui.GuiManager;
import net.java.sipmack.softphone.listeners.InterlocutorListener;
import net.java.sipmack.softphone.listeners.RegisterEvent;
import net.java.sipmack.softphone.listeners.SoftPhoneListener;
import org.jivesoftware.resource.Res;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.phone.PhoneManager;
import org.jivesoftware.spark.preference.PreferenceManager;
import org.jivesoftware.spark.util.ModelUtil;

import javax.sdp.MediaDescription;
import javax.sdp.SessionDescription;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Title: SIPark
 * Description:JAIN-SIP Audio/Video phone application
 *
 * @author Thiago Rocha Camargo (thiago@jivesoftware.com) The
 *         <code>SoftPhoneManager</code> class that Manage SIP Stack, Sessions
 *         and Calls
 *         Use getInstance() to get a SoftPhone instance to use
 * @version 1.0, 20/07/2006
 */

public class SoftPhoneManager implements CommunicationsListener, CallListener, UserActionListener, SoftPhone {

    private static SoftPhoneManager singleton;
    private static final Object LOCK = new Object();
    private SipAccount saccount;

    private final List<SoftPhoneListener> softPhoneListeners = new CopyOnWriteArrayList<SoftPhoneListener>();

    SoftPhoneMedia softPhoneMedia = null;

    SoftPhoneSecurity softPhoneSecurity = null;

    SipManager sipManager = null;

    JmfMediaManager mediaManager = null;

    private GuiManager guiManager = null;

    private String authUserName = "";

    private String username = "";

    private String password = null;// new char[10];

    public String server = "";

    protected String msgBuffer = "";

    protected Integer unregistrationLock = new Integer(0);

    private static SipRegisterStatus status = SipRegisterStatus.Unregistered;

    public static final String userAgent = "SIPark";

    public String callTo = "";

    private LogManager logManager;

    private SipPreferences preferences;

    private SipPreference preference;

    private int lines = 1;

    private DialSoundManager dtmfSounds;

    private MissedCalls missedCalls;

    // Define UIs
    private JCheckBoxMenuItem registerMenu;

    private Map<Component, CallRoomState> callRooms = new HashMap<Component, CallRoomState>();

    /**
     * Private constructor of the class.
     */
    private SoftPhoneManager() {
    }

    /**
     * Initializes the core phone objects.
     */
    private void initializePhone() {
        // Load Preferences
        loadPreferences();

        if (preferences == null) {
            return;
        }


        guiManager = new GuiManager();
        guiManager.addUserActionListener(this);
        logManager = new LogManager(this);

        this.getLogManager().setRemoteLogging(true);

        registerMenu = new JCheckBoxMenuItem(PhoneRes.getIString("phone.enabled"));
        registerMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                if (getStatus() == SipRegisterStatus.Unregistered ||
                        getStatus() == SipRegisterStatus.RegistrationFailed) {

                    register();
                } else {
                    handleUnregisterRequest();
                }
            }
        });


        SIPConfig.setPreferredNetworkAddress(preferences.getPreferredAddress());
        NetworkAddressManager.start();

        // Initialize Missed calls
        missedCalls = new MissedCalls();

        final JMenu actionsMenu = SparkManager.getMainWindow().getMenuByName(Res.getString("menuitem.actions"));
        actionsMenu.add(registerMenu);
    }


    /**
     * Type of states a jingle call can be in.
     */
    public static enum CallRoomState {
        /**
         * The component is in a call.
         */
        inCall,

        /**
         * The components call has ended.
         */
        callWasEnded,

        /**
         * The call is muted.
         */
        muted,

        /**
         * The call is on hold.
         */
        onHold
    }

    /**
     * Returns the singleton instance of <CODE>SoftPhoneManager</CODE>,
     * creating it if necessary.
     * <p/>
     *
     * @return the singleton instance of <Code>SoftPhoneManager</CODE>
     */
    public static SoftPhoneManager getInstance() {
        // Synchronize on LOCK to ensure that we don't end up creating
        // two singletons.
        synchronized (LOCK) {
            if (null == singleton) {
                SoftPhoneManager controller = new SoftPhoneManager();
                singleton = controller;
                controller.initializePhone();
                return controller;
            }
        }
        return singleton;
    }

    /**
     * Add SoftPhoneListener
     *
     * @param softPhoneListener the listener.
     */
    public void addSoftPhoneListener(SoftPhoneListener softPhoneListener) {
        softPhoneListeners.add(softPhoneListener);
    }

    /**
     * Add InterlocutorListener
     *
     * @param interlocutorListener the listener.
     */
    public void addInterlocutorListener(InterlocutorListener interlocutorListener) {
        guiManager.addInterlocutorListener(interlocutorListener);
    }

    /**
     * Remove InterlocutorListener
     *
     * @param interlocutorListener the listener
     */
    public void removeInterlocutorListener(InterlocutorListener interlocutorListener) {
        guiManager.removeInterlocutorListener(interlocutorListener);
    }

    /**
     * Return the DefaultGuiManager
     */
    public DefaultGuiManager getDefaultGuiManager() {
        return guiManager;
    }

    /**
     * Create the softphone handlers and stack
     */
    public void createSoftPhone(String server) throws MediaException {

        this.server = server;
        SIPConfig.setServer(server);

        if (sipManager != null) {
            destroySoftPhone();
        }

        sipManager = new SipManager();
        softPhoneMedia = new SoftPhoneMedia();
        softPhoneSecurity = new SoftPhoneSecurity();
        mediaManager = new JmfMediaManager();

        sipManager.addCommunicationsListener(this);
        sipManager.setSecurityAuthority(softPhoneSecurity);

        try {
            // put in a seperate thread
            sipManager.start();
            if (sipManager.isStarted()) {
                Log.debug("createSoftPhone", "SIP STARTED");
            }
        }
        catch (CommunicationsException exc) {
            Log.error("createSoftPhone", exc);
        }

    }

    /**
     * Destroys the softphone handlers and stack
     */
    public void destroySoftPhone() {

        try {
            sipManager.stop();
        }
        catch (Exception exc) {
            Log.error("destroySoftPhone", exc);
        }
    }

    /**
     * Return the current SIP connection status
     *
     * @return status
     */
    public SipRegisterStatus getStatus() {
        return status;
    }

    /**
     * Gets the current username
     *
     * @return The current connection username
     */
    public String getUsername() {
        return username;
    }

    /**
     * Gets the current server
     *
     * @return The current connection server
     */
    public String getServer() {
        return server;
    }

    /**
     * Handle an UnregisterRequest
     */
    public void handleUnregisterRequest() {
        if (sipManager != null) {
            try {
                sipManager.endAllCalls();
                sipManager.unregister();
            }
            catch (Exception e) {
                Log.error("handleUnregisterRequest", e);
            }
        }
    }

    /**
     * Handle a Re-Register Request.
     * This method will only have effect if the user has successfully registered beforeat least once before.
     */
    public void handleReRegisterRequest() {
        if (this.password != null && !this.username.equals("")) {
            try {
                sipManager.startRegisterProcess(username, authUserName, password);
            }
            catch (CommunicationsException exc) {
                Log.error("handleRegisterRequest", exc);
            }
        }
    }

    /**
     * Handle an RegisterRequest
     *
     * @param username username
     * @param password password
     */
    public void handleRegisterRequest(String username, String password) {
        handleRegisterRequest(username, null, password);

⌨️ 快捷键说明

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