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

📄 filesharingservice.java

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

import java.io.*;
import java.lang.reflect.*;

import javax.swing.*;

import net.jxta.share.*;

import connex.plugins.filesharing.CMService;

import connex.plugins.filesharing.ui.ResultsTable;
import connex.plugins.filesharing.ui.FilesharingPanel;
import connex.session.plugin.Plugin;
import connex.core.WS.Workspace;
import java.util.Hashtable;
import java.util.Enumeration;
import connex.core.WS.Boot;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;

public class FilesharingService
    implements Plugin {
  /**
   * @directed
   * @link aggregationByValue
   */
  private CMService myCMS;

  //  private SearchManager searchM;
  /**
   * @directed
   * @supplierCardinality 1
   * @clientCardinality 1
   */
  private ShareManager shareM = null;

  protected static FilesharingService fsc = null;
  private FilesharingPanel fspanel = null;
  private File dir;
  private boolean started = false;
  private Workspace ws;

  /**
   * @directed
   * @supplierCardinality 0..*
   * @clientCardinality 1
   * @clientRole create
   * @link aggregation
   */
  private ContentSearch search;
  private Hashtable<String, ContentDownload> downloads = new Hashtable<String,
      ContentDownload> ();

  /**
   * @directed
   * @supplierCardinality 0..*
   * @clientCardinality 1
   * @clientRole create
   * @link aggregation
   */
  private ContentDownload dmg;
  private File folder;
  //private ResultsTable rt;
  static final Logger LOG = Logger.getLogger(FilesharingService.class);

  public FilesharingService() {
    fsc = this;

  }

  public void initPlugin(Object param) {
   LOG.setLevel(Level.INFO);
    this.ws = (Workspace) param;
    myCMS = CMService.getInstance();
    try {

      myCMS.init(ws.getPeerGroup(), null, null);

      this.fspanel = new FilesharingPanel();
      if (LOG.isEnabledFor(Level.INFO)) {
        LOG.info("CMS init success...\n");
      }

    }
    catch (Exception e) {
      if (LOG.isEnabledFor(Level.INFO)) {
      LOG.info("CMS init failure...");
    }

      e.printStackTrace();
      // System.exit( -1);
    }

  }

  public void startPlugin() {
    if (started) {
      return;
    }

    myCMS.startApp(ws.getWorkspaceFolder());
    if (LOG.isEnabledFor(Level.INFO)) {
      LOG.info("CMS started...\n");
    }


    started = true;

  }

  public JPanel getMainUI() {

    return this.fspanel;

  }

  public void stopPlugin() {
    myCMS.stopApp();

    started = false;
    Enumeration<ContentDownload> num = downloads.elements();
    ContentDownload dMangr;
    while (num.hasMoreElements()) {
      dMangr = downloads.remove(num.nextElement().getContentAdvertisement().
                                getContentId().toString());
      dMangr.cancel();

    }

    fspanel.cleanUP();

    fspanel = null;
    if (LOG.isEnabledFor(Level.INFO)) {
      LOG.info("CMS stoped...\n");
    }


  }

  public Workspace getWorkspace() {
    return this.ws;
  }

  public void downloadContent(ContentAdvertisement[] res) {

    if (downloads.containsKey(res[0].getContentId().toString())) {
      JOptionPane.showMessageDialog(null, "File already downloaded",
                                    "Download Info",
                                    JOptionPane.INFORMATION_MESSAGE);
      return;
    }

    try {
      dmg = new ContentDownload(ws.getPeerGroup(), res);

      downloads.put(res[0].getContentId().toString(), dmg);

    }
    catch (InvocationTargetException ex) {
    }

  }

  public void cancelDownload(String id) {
    if (downloads.containsKey(id)) {

      dmg = downloads.remove(id);

      dmg.cancel();
    }

  }

  public void searchContent(String subStr) {
    ResultsTable rt = new ResultsTable();

    fspanel.addResultsTable(rt);
    rt.startSearch(subStr);

  }

  public synchronized void shareContent(ShareFile file) {
    if (shareM == null) {
      shareM = new ShareManager(myCMS, getShareList());
    }

    if (!shareM.isRunning()) {
      shareM.share(file);
    }
    else {
      JOptionPane.showMessageDialog(null, "Please waite ...",
                                    "Download Info",
                                    JOptionPane.INFORMATION_MESSAGE);

    }
  }

  public void unshareContent(ShareFile file) {
    if (shareM == null) {
      shareM = new ShareManager(myCMS, getShareList());
    }
    shareM.unShare(file);
  }

  public void unshareAll() {
    if (shareM == null) {
      shareM = new ShareManager(myCMS, getShareList());
    }
    shareM.unshareAll();

  }

  public void loadContents() {
    if (shareM == null) {
      shareM = new ShareManager(myCMS, getShareList());
    }
    shareM.loadContents();

  }

  public DefaultListModel getShareList() {
    return fspanel.getShareList();
  }

  public void setDownloadDir(String downloadDir) {
    dir = new File(downloadDir);
    if (false == dir.exists()) {
      // create the directory
      dir.mkdir();
    }

  }

  public static FilesharingService getInstance() {
    if (fsc == null) {
      return new FilesharingService();
    }
    else {
      return fsc;
    }
  }

  public void createSession(String id) {
  }

}

⌨️ 快捷键说明

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