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

📄 messengersession.java

📁 基于jxta的P2P框架的系统
💻 JAVA
字号:
/**
 * -- Copyright (C) 2006 Hisham Khalil. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 *
 * Author: Hisham Khalil <hishberlin@hotmail.com>
 */
package connex.plugins.Messenger;

import connex.core.net.ConnectionClient;
import net.jxta.endpoint.Message;
import connex.core.net.MemberConnection;
import connex.core.net.ConnectionFactory;
import net.jxta.endpoint.MessageElement;
import connex.core.Presence.PresenceService;
import net.jxta.endpoint.StringMessageElement;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;
import net.jxta.endpoint.ByteArrayMessageElement;
import net.jxta.document.MimeMediaType;
import javax.swing.ImageIcon;
import connex.core.WS.Workspace;

public class MessengerSession
    implements ConnectionClient, SendListener {
  private MemberConnection mConnection;

  /**
   * @link aggregationByValue
   * @directed
   * @supplierCardinality 1
   * @clientCardinality 1
   */
  private MsgrSessionGUI gui;
  private String sessionID;

  public MessengerSession(MemberConnection mConnection) {
    this.mConnection = mConnection;
    this.sessionID = mConnection.getRemotPeerID();
    mConnection.setClient(this);
    if (mConnection.connect()) {
      MessengerService.getInstance().addSession(mConnection.getRemotPeerID(), this);
      gui = new MsgrSessionGUI(mConnection.getRemotePeerName());
      gui.setSender(this);

    }

  }

  public MessengerSession(Workspace ws, String name, String desMembID,
                          String pipeId) {
    this.mConnection = (MemberConnection) ConnectionFactory.newMemberConnection(
        ws, desMembID,
        pipeId, this);
    this.sessionID = desMembID;

    if (mConnection.connect()) {
      MessengerService.getInstance().addSession(desMembID, this);
      gui = new MsgrSessionGUI(name);
      gui.setSender(this);
    }
  }

  public synchronized void stop() {
    try {
      if (mConnection.disConnect()) {
        MessengerService.getInstance().removeSession(this.sessionID);
        System.out.println("sessionid Removed");
        mConnection = null;

        gui.dispose();
        gui = null;

      }

    }
    catch (Exception ex) {
    }
  }

  public String getClientName() {
    return "MessengerServiceSession";
  }

  protected void showWindow() {
    if (null != gui) {
      gui.setVisible(true);
    }
  }

  public synchronized void sendChatMessage(String text) {
    Message msg = createChatMessage(text);
    System.out.println(msg.getByteLength());
    if (msg != null) {
      mConnection.send(createChatMessage(text));
    }
  }

  public void sendPicture(File file) {
    mConnection.send(createPictureMessage(file));
  }

  public void reciveMessage(Message message) {
    process(message);
  }

  private void process(Message msg) {
    String version = null;
    String membId = null;
    String membName = null;
    String type = null;
    String data = null;

    /* Message header*/

    MessageElement el = msg.getMessageElement(MessengerProtocol.nameSpace,
                                              MessengerProtocol.version);

    if (el != null) {
      version = el.toString();
    }

    el = msg.getMessageElement(MessengerProtocol.nameSpace,
                               MessengerProtocol.membIDTag);
    if (el != null) {
      membId = el.toString();

    }

    el = msg.getMessageElement(MessengerProtocol.nameSpace,
                               MessengerProtocol.membNameTag);
    if (el != null) {
      membName = el.toString();
    }

    el = msg.getMessageElement(MessengerProtocol.nameSpace,
                               MessengerProtocol.typeTag);
    if (el != null) {
      type = el.toString();

      /*  if (LOG.isEnabledFor(Level.INFO)) {
          LOG.info("Recived Message Type = " + type);
        }*/

      /*** Messagetype*****************/

      if (type.equals(MessengerProtocol.message)) {
        el = msg.getMessageElement(MessengerProtocol.nameSpace,
                                   MessengerProtocol.messageTag);
        if (el != null) {
          data = el.toString();
        }
        gui.showMsg(membName, data);

        return;
      }
      if (type.equals(MessengerProtocol.picture)) {
        byte[] imgbuff = null;

        try {
          imgbuff = msg.getMessageElement(MessengerProtocol.nameSpace, "image").
              getBytes(false);
        }
        catch (Exception ex) {
        }
        gui.setHisImage(new ImageIcon(imgbuff));
      }

    }
  }

  /**
   * Create the Message Header
   * @param type String
   * @return Message
   */
  private Message createMessage(String type) {
    Message msg = new Message();
    /* version */
    msg.addMessageElement(MessengerProtocol.nameSpace,
                          new
                          StringMessageElement(MessengerProtocol.
                                               versionTag,
                                               MessengerProtocol.version, null));
    /* MemberID*/
    msg.addMessageElement(MessengerProtocol.nameSpace,
                          new
                          StringMessageElement(MessengerProtocol.
                                               membIDTag,
                                               PresenceService.getInstance().
                                               getmOwnPeerAdv().getPeerID().
                                               toString(), null));
    /* MemberName*/
    msg.addMessageElement(MessengerProtocol.nameSpace,
                          new
                          StringMessageElement(MessengerProtocol.membNameTag
                                               ,
                                               PresenceService.getInstance().
                                               getmOwnPprofileAdv().getName(), null));

    /* type */
    msg.addMessageElement(MessengerProtocol.nameSpace,
                          new
                          StringMessageElement(MessengerProtocol.typeTag,
                                               type, null));
    return msg;
  }

  /**
   * create a new chatMessage
   * @param messageText String
   * @return Message
   */
  private Message createChatMessage(String messageText) {
    Message msg = this.createMessage(MessengerProtocol.message);
    /*Messagedata*/
    msg.addMessageElement(MessengerProtocol.nameSpace,
                          new
                          StringMessageElement(MessengerProtocol.messageTag,
                                               messageText, null));

    return msg;
  }

  /**
   * create a new pictureMessage
   * @param messageText String
   * @return Message
   */
  private Message createPictureMessage(File file) {
    Message msg = this.createMessage(MessengerProtocol.picture);
    /*Messagedata*/
    InputStream in = null;
    int buffSize = 64 * 1024;
    int length = 0;
    byte[] buff = new byte[buffSize];
    try {
      in = new FileInputStream(file);
      length = in.read(buff);
      in.close();
    }
    catch (Exception ex) {
    }

    ByteArrayMessageElement meli = new ByteArrayMessageElement("image",
        MimeMediaType.AOS, buff, 0, length, null);

    msg.addMessageElement(MessengerProtocol.nameSpace, meli);

    return msg;
  }

  public void closedFromRemote() {
    this.mConnection = null;
    MessengerService.getInstance().removeSession(this.sessionID);
    gui.freez();

  }

}

⌨️ 快捷键说明

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