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

📄 msnmessenger.java

📁 J2me实现的MSN Messeger客户端程序。聊天、添加好友、删除好友、阻止好友
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)MSNMessenger.java
 *
 * Copyright (c) 2001-2002, JangHo Hwang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 	1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 	2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 	3. Neither the name of the JangHo Hwang nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *    $Id: MSNMessenger.java,v 2.0 2004/12/21 16:15:08 chenzs Exp $
 */
package vitular.msnp;

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;
import javax.microedition.rms.*;

import vitular.msnp.entity.MsnFriend;
import vitular.msnp.event.MsnAdapter;
import vitular.msnp.event.MsnListener;
import vitular.msnp.msg.MimeMessage;

//import rath.msnm.msg.FileTransferMessage;
//import rath.msnm.ftp.ToSendFile;
//import rath.msnm.ftp.VolatileTransferServer;
//import rath.msnm.ftp.VolatileDownloader;

/**
 * MSN ???n ???? b????? ??????; ??? '??
 * Entry point?? ??? ????????.
 * login; ?????? loginComplete ?????? ???? ?????
 * ?????? ???带 ????? ?? ???.
 * ????? ??8?? NS proc ???8?? NullPointerException; ?????????.
 * <p>
 * <pre><code>
 *  MSNMessenger msn = new MSNMessenger( "xiguel@hotmail.com", "12341234" );
 *  msn.setInitialStatus( UserStatus.ONLINE );
 *  msn.addMsnListener( new MsnAdapter() {
 *      public void progressTyping( SwitchboardSession ss,
 *          MsnFriend friend, String typingUser )
 *      {
 *          System.out.println( "Typing on " + friend.getLoginName() );
 *      }
 *      public void instantMessageReceived( SwitchboardSession ss,
 *          MsnFriend friend, MimeMessage mime )
 *      {
 *          System.out.println( "*** MimeMessage from " + friend.getLoginName() );
 *          System.out.println( mime.getMessage() );
 *          System.out.println( "*****************************" );
 *      }
 *  });
 *  msn.login();
 * </code></pre>
 *
 * @author Jang-Ho Hwang, rath@linuxkorea.co.kr
 * @version $Id: MSNMessenger.java,v 2.0 2004/12/21 16:15:08 chenzs Exp $
 */
public class MSNMessenger {
  /**
   * 无错误发生
   */
  static final short ERR_NONE = 0;

  /**
   * 网络连接出错
   */
  static final short ERR_CONNECTION_FAIL = 10;

  /**
   * 服务器关闭连接
   */
  static final short ERR_SESSION_CLOSE = 20;
  /**
   * 服务器内部错误
   */
  static final short ERR_SERVER_FAIL = 30;
  /**
   * SSL验证失败
   */
  static final short ERR_HTTPS_FAIL = 40;

  private NotificationProcessor ns = null; //通告服务器
  private BuddyGroup buddyGroup = null;    //好友及好友分组集合
  private BuddyList forwardList = null;//所有好友列表
//  private BuddyList onlineList = null;    //在线好友列表

  boolean isLogged = false;
  private String loginName = null;
  private String password = null;
  private String initStatus = UserStatus.ONLINE; //初始状态,即刚登陆时的状态表现
  private MsnListener base = null;
  protected Vector listeners = new Vector();

  private MsnFriend owner = null;
  private Hashtable sessionMap = new Hashtable();

  /**
   * 构造函数
   * @param loginName String 用户id
   * @param password String  用户密码
   */
  public MSNMessenger(String loginName, String password) {
    this.loginName = loginName;
    this.password = password;

    base = new Listener();
    buddyGroup = BuddyGroup.getInstance();
    forwardList = buddyGroup.getForwardList();
//    onlineList = new BuddyList("OL");

    // delete 2004/5/30 ceze
    // this.localCopy = new LocalCopy();
  }

  private void initLogon() {
    buddyGroup.clear();
  }

  //delete 2004/5/30 ceze
  /**
   * ??ο? ????? ????? ?????? ??, BuddyList?? ???????? ?????? ??????.
   *
   * @param serial
   * @param fireEvent
   */
  void storeLocalCopy(int serial) {
//    localCopy.setProperty("SerialNumber", String.valueOf(serial));
//    localCopy.storeInformation();
//    localCopy.storeBuddies(buddyGroup);

    fireBuddyListModifiedEvent();
  }

  /**
   * 获取好友集合
   * @return BuddyGroup
   */
  public BuddyGroup getBuddyGroup() {
    return buddyGroup;
  }

  /**
   * 在线好友列表
   * @return BuddyList
   */
//  public BuddyList getOnlineList(){
//    return onlineList;
//  }

  /**
   * 全部好友列表
   * @return BuddyList
   */
  public BuddyList getForwardList(){
    return forwardList;
  }

//  public LocalCopy getLocalCopy() {
//    return this.localCopy;
//  }

  /**
   * 设置初始状态:登陆时后的表现状态, 如OFFLINE即用隐身登陆
   * @param code String  -(UserStatus.ONLINE, UserStatus.OFFLINE.....)
   */
  public void setInitialStatus(String code) {
    this.initStatus = code;
  }

  /**
   * 获取初始状态
   * @return String
   */
  public String getInitialStatus() {
    return this.initStatus;
  }

  /**
   * 是否登陆到服务器
   * @return boolean
   */
  public boolean isLoggedIn() {
    return this.isLogged;
  }

  /**
   * 增加MsnListener监听接口
   * @param l MsnListener
   */
  public synchronized void addMsnListener(MsnListener l) {
    if (!listeners.contains(l))
      listeners.addElement(l);
  }

  /**
   * MsnListener接口数量
   * @return int
   */
  public int getListenerCount() {
    return listeners.size();
  }

  /**
   * 删除监听接口
   * @param l MsnListener
   */
  public synchronized void removeMsnListener(MsnListener l) {
    listeners.removeElement(l);
  }

  /**
   * 帐户名称
   */
  public String getLoginName() {
    return this.loginName;
  }

  /**
   * 密码
   */
  public String getPassword() {
    return this.password;
  }

  /**
   * 自身信息
   */
  public MsnFriend getOwner() {
    return this.owner;
  }

  /**
   * 登录服务器
   */
  public void login() {
    if (loginName == null || password == null)
      throw new IllegalArgumentException(
          "Login name and password must not be null");

    initLogon();
    ns = new NotificationProcessor(this, 1);
    ns.start();
  }

  /**
   * 注销
   */
  public void logout() {
    Enumeration e = sessionMap.elements();
    while (e.hasMoreElements()) {
      SwitchboardSession ss = (SwitchboardSession) e.nextElement();
      ss.cancel();
      ss.cleanUp();
    }
    sessionMap.clear();

    if (ns != null) {
      ns.cancel();
      try {
        ns.logout();
      }
      catch (IOException ex) {}
      ns = null;
    }
  }

  /**
   * 设置"我的状态"
   * @param status String
   * @throws IOException
   */
  public void setMyStatus(String status) throws IOException {
    this.ns.setMyStatus(status);
  }

  /**
   * 我的状态
   * @return String
   */
  public String getMyStatus() {
    return this.ns.getMyStatus();
  }

  public void setMyFriendlyName(String newName) throws IOException {
    this.ns.setMyFriendlyName(newName);
  }

  /**
   * 发起呼叫
   * @param loginName String 好友帐号
   * @throws IOException
   */
  public void doCall(String loginName) throws IOException {
    SwitchboardSession ss = findSwitchboardSession(loginName);
    if (ss == null)
      ns.doCallFriend(loginName);
  }

  /**
   * 添加好友
   * @param loginName String 好友帐户
   * @throws IOException
   */
  public void addFriend(String loginName) throws IOException {
    ns.requestAdd(loginName);
  }

  /**
   * 阻止好友
   * @param loginName String
   * @throws IOException
   */
  public void blockFriend(String loginName) throws IOException {
    ns.requestBlock(loginName, false);
  }

  /**
   * 取消阻止
   * @param loginName String
   * @throws IOException
   */
  public void unBlockFriend(String loginName) throws IOException {
    ns.requestBlock(loginName, true);
  }

  /**
   * 删除好友
   * @param loginName String
   * @throws IOException
   */
  public void removeFriend(String loginName) throws IOException {
    ns.requestRemove(loginName);
  }

  /**
   * 增加分组
   * @param groupName String 分组名称
   * @throws IOException
   */
  public void addGroup(String groupName) throws IOException {
    ns.requestCreateGroup(groupName);
  }

  /**
   * 删除分组
   * @param groupIndex int
   * @throws IOException
   */
  public void removeGroup(int groupIndex) throws IOException {
    ns.requestRemoveGroup(groupIndex);
  }

  /**
   * 分组改名
   * @param groupIndex int
   * @param newName String
   * @throws IOException
   */
  public void renameGroup(int groupIndex, String newName) throws IOException {
    ns.requestRenameGroup(groupIndex, newName);
  }

  /**
   * 好友在分组中重规类

⌨️ 快捷键说明

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