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

📄 messageprocessor.java

📁 基于jxta的局域网P2P文件共享,可以实现局域网中的文件p2p共享,实现文件快速传输及交流
💻 JAVA
字号:
package connex.plugins.whiteboard;

import net.jxta.endpoint.*;
import connex.core.Presence.PresenceService;
import net.jxta.document.MimeMediaType;
import connex.core.net.ObjectUtil;
import java.io.*;
import connex.core.net.WorkspaceConnection;
import org.apache.log4j.Level;

import org.apache.log4j.Logger;
import connex.core.net.WorkspaceConnection;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class MessageProcessor
    implements RemoteListener {
  private final static Logger LOG = org.apache.log4j.Logger.getLogger(
      MessageProcessor.class.getName());
  private WorkspaceConnection ses;

  /**
   * @clientCardinality 1
   * @supplierCardinality 1
   * @directed*/
  private ShapesContol shapes;

  protected MessageProcessor(WorkspaceConnection ses) {
    LOG.setLevel(Level.INFO);
    this.ses = ses;

    shapes = ShapesContol.getInstance();
    shapes.setRemoteListener(this);
  }

  public synchronized void sendShape(String key, ShapeObj shape) {
    ses.send(createShapeMessage(key, shape));
  }

  public synchronized void sendClear() {

    ses.send(WhiteboardProtocol.createMessage(WhiteboardProtocol.clearMessage));
  }

  public void sendRemove(String key) {
    Message msg = WhiteboardProtocol.createMessage(WhiteboardProtocol.removeMessage);
    msg.addMessageElement(WhiteboardProtocol.nameSpace,
                          new
                          StringMessageElement(WhiteboardProtocol.shapeIDTag,
                                               key, null));
    ses.send(msg);

  }
  public void sendLock(String key) {
   Message msg = WhiteboardProtocol.createMessage(WhiteboardProtocol.lockMessage);
   msg.addMessageElement(WhiteboardProtocol.nameSpace,
                         new
                         StringMessageElement(WhiteboardProtocol.shapeIDTag,
                                              key, null));
   ses.send(msg);

 }




  /**
   * process
   *
   * @param message Message
   */
  /**
   * create a new Message
   * @param messageText String
   * @return Message
   */
  private Message createShapeMessage(String key, ShapeObj shape) {
    Message msg = WhiteboardProtocol.createMessage(WhiteboardProtocol.shapeMessage);
    /*Messagedata*/
    byte[] buff = null;
    try {
      buff = objectToBytes(shape);
      msg.addMessageElement(WhiteboardProtocol.nameSpace,
                            new
                            StringMessageElement(WhiteboardProtocol.shapeIDTag,
                                                 key, null));
      msg.addMessageElement(WhiteboardProtocol.nameSpace,

                            new ByteArrayMessageElement(WhiteboardProtocol.shapeTag,
          MimeMediaType.AOS, buff, 0, buff.length, null));

      return msg;
    }
    catch (IOException ex) {
    }
    return msg;

  }

  protected void process(Message msg) {
    String version = null;
    String membId = null;
    String membName = null;
    String type = null;
    byte[] data = null;
    String key = null;

    /* Message header*/
    System.out.print("start Processing");
    MessageElement el = msg.getMessageElement(WhiteboardProtocol.nameSpace,
                                              WhiteboardProtocol.version);

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

    }

    el = msg.getMessageElement(WhiteboardProtocol.nameSpace,
                               WhiteboardProtocol.membIDTag);
    if (el != null) {
      membId = el.toString();
      if (membId.equals(PresenceService.getInstance().
                        getmOwnPeerAdv().getPeerID().
                        toString())) {
        return;
      }

    }

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

    }
    /*** Messagetype*****************/
    el = msg.getMessageElement(WhiteboardProtocol.nameSpace,
                               WhiteboardProtocol.typeTag);
    if (el != null) {
      type = el.toString();

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

      /*** MessageData*****************/

      if (type.equals(WhiteboardProtocol.shapeMessage)) {

        el = msg.getMessageElement(WhiteboardProtocol.nameSpace,
                                   WhiteboardProtocol.shapeIDTag);
        if (el != null) {

          key = el.toString();

        }

        el = msg.getMessageElement(WhiteboardProtocol.nameSpace,
                                   WhiteboardProtocol.shapeTag);

        if (el != null) {
          data = el.getBytes(false);

          setShape(key, data);

        }
        return;
      }
      if (type.equals(WhiteboardProtocol.clearMessage)) {
        synchronized (shapes) {
          shapes.remoteClear();
        }
        return;
      }
      if (type.equals(WhiteboardProtocol.removeMessage)) {
        el = msg.getMessageElement(WhiteboardProtocol.nameSpace,
                                   WhiteboardProtocol.shapeIDTag);
        if (el != null) {
          key = el.toString();
          synchronized (shapes) {
            shapes.remoteRemove(key);
          }
          return;
        }

      }
      if (type.equals(WhiteboardProtocol.lockMessage)) {
             el = msg.getMessageElement(WhiteboardProtocol.nameSpace,
                                        WhiteboardProtocol.shapeIDTag);
             if (el != null) {
               key = el.toString();
               synchronized (shapes) {
                 shapes.lock(key, membName);
               }
               return;
             }

           }

    }

  }

  private void setShape(String key, byte[] data) {
    System.out.print("  data length" + data.length);
    //gui.showMsg(membName, data);
    ShapeObj shape = null;
    try {
      shape = (ShapeObj) bytesToObject(data);
      System.out.println("  recieved shape id " + key);
    }
    catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    synchronized (shapes) {
      shapes.remotePut(key, shape);
    }
  }



 private byte[] objectToBytes(Object object) throws
      IOException {
    java.io.ObjectOutputStream out;
    java.io.ByteArrayOutputStream bs;

    bs = new java.io.ByteArrayOutputStream();
    out = new java.io.ObjectOutputStream(bs);
    out.writeObject(object);
    out.close();

    return bs.toByteArray();

  }

  private Object bytesToObject(byte[] bytes) throws
      IOException,
      ClassNotFoundException {
    Object res;
    java.io.ObjectInputStream in;
    java.io.ByteArrayInputStream bs;

    bs = new java.io.ByteArrayInputStream(bytes);
    in = new java.io.ObjectInputStream(bs);
    res = in.readObject();
    in.close();
    bs.close();
    return res;
  }
}

⌨️ 快捷键说明

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