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

📄 filesharing.java

📁 peeranha42是jxta的 p2p程序核心
💻 JAVA
字号:
package de.uni_bremen.informatik.p2p.plugins.filesharing;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Vector;
import net.jxta.id.IDFactory;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeID;
import net.jxta.pipe.PipeService;
import org.apache.log4j.Logger;
import de.uni_bremen.informatik.p2p.peeranha42.core.network.Network;
import de.uni_bremen.informatik.p2p.peeranha42.core.network.NetworkException;
import de.uni_bremen.informatik.p2p.peeranha42.core.plugin.Client;
import de.uni_bremen.informatik.p2p.peeranha42.core.plugin.Info;
import de.uni_bremen.informatik.p2p.peeranha42.core.plugin.Plugin;
import de.uni_bremen.informatik.p2p.peeranha42.core.plugin.gui.P42_application_pane;
import de.uni_bremen.informatik.p2p.peeranha42.core.plugin.gui.P42_gui_default;
import de.uni_bremen.informatik.p2p.peeranha42.core.plugin.loader.UnloadException;
import de.uni_bremen.informatik.p2p.plugins.filesharing.control.InitUtilities;
import de.uni_bremen.informatik.p2p.plugins.filesharing.control.PeerGroupSearch.DocumentClassifier;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.FilesharingState;
import de.uni_bremen.informatik.p2p.plugins.filesharing.gui.P42FilesharingPane;
import de.uni_bremen.informatik.p2p.plugins.filesharing.gui.PreferencePanel;
import de.uni_bremen.informatik.p2p.plugins.filesharing.network.FilesharingRendezvousListener;
import de.uni_bremen.informatik.p2p.plugins.filesharing.network.NetworkHandler;
import de.uni_bremen.informatik.p2p.plugins.filesharing.network.download.server.DownloadManager2;


/**
 * The Filesharingclass represents a plugin for Peeranha42 Client. It is the
 * starting point. Therefor it implements the Plugin interface.
 *
 * @author Lars Kordes, Philipp Hoheisel, Daniel Gehrke
 */
public class Filesharing
    implements Plugin {
    public static boolean ready;

	/** current program thread */
    private volatile Thread mainThread;

    /** Logger for warnings, debugs and fatals */
    protected static Logger log = Logger.getLogger(Filesharing.class);

    /** NetworkManager handling the networkmessages */
    private NetworkHandler networkHandler;
    
    /**
     * This Method is called when the plugin is to be terminated.
     *
     * @see de.uni_bremen.informatik.p2p.plugin_client.plugin_core.Plugin#stop()
     */
    public void stop() {
    	FilesharingState.plugin = null;
    	FilesharingState.downloadpanel = null;
    	FilesharingState.sharedpanel = null;
    	FilesharingState.searchpanel = null;
    	
        // remove gui from core gui
        P42_gui_default.removeAppPanes(FilesharingState.pluginid);

        try {
            Client.getPlugInLoader().unloadPlugIn(FilesharingState.pluginid);
        } catch (UnloadException ue) {
            log.debug(ue.toString());
        }
        
        mainThread = null;
        
        networkHandler.disconnect();
        
        ready = false;
    }

    /**
     * The method is called by peeranha42 classloader. It initializes the
     * plugin.
     *
     * @see de.uni_bremen.informatik.p2p.plugin_client.plugin_core.Plugin#run()
     */
    public void run() {
    	ready = false;
    	
    	mainThread = Thread.currentThread();
    	
    	// init configuration file
    	InitUtilities.loadPreferences();
    	
        // setup lists for downloadjobs and searchresults
        FilesharingState.downloads = new ArrayList();
        FilesharingState.searchresults = new ArrayList();
        
        // setup preference gui
        P42_application_pane[] prefs = new P42_application_pane[1];
        prefs[0] = new PreferencePanel();
        P42_gui_default.addPrefPanels(FilesharingState.pluginid, prefs);
        
        // set user name to peer name
        FilesharingState.username = Client.getNetwork().netPeerGroup.getPeerAdvertisement().getName();

        // Networkmanager
        networkHandler = new NetworkHandler(this);
        networkHandler.init();
        FilesharingState.networkHandler = networkHandler;
        
        // creating pipe advertisement for downloads
		FilesharingState.pa = null;
		FilesharingState.pa = net.jxta.util.PipeUtilities.createNewPipeAdvertisement(FilesharingState.peergroup, PipeService.UnicastType);
		// create unique pipeID
		PipeID pipeid = IDFactory.newPipeID(FilesharingState.peergroup.getPeerGroupID());
		FilesharingState.pa.setPipeID(pipeid);
		// set name
		FilesharingState.pa.setName("Download_pipe_of_" + FilesharingState.username);
		// set peerid in description
		PeerID own_peerid = FilesharingState.peergroup.getPeerAdvertisement().getPeerID();
		String own_peerid_str = own_peerid.toURI().toString();
		FilesharingState.pa.setDescription(own_peerid_str);
		log.info("My pipe advertisement for downloads: " + FilesharingState.pa);
        
        try {
        	FilesharingState.downloadmanager = new DownloadManager2(FilesharingState.sharedfiles, FilesharingState.pa);
        	FilesharingState.downloadmanager.start();
        }
        catch(NetworkException e) {
        	log.error("Cannot init download manager. Filesharing plugin is getting stopped.");
        	stop();
        }
        
        // setup gui
        Vector appPanes = new Vector();
        P42FilesharingPane filesharingpane = new P42FilesharingPane();
        appPanes.add(filesharingpane);
        P42_gui_default.addAppPanes(appPanes);
        
        FilesharingRendezvousListener listener = new FilesharingRendezvousListener(this);
        Network.addP42RendezvousListener(FilesharingState.pluginid, FilesharingState.peergroup, listener);
		while(!ready) {
			try {
				Thread.sleep(1000);
			}
			catch(Exception e) {
				
			}
		}
        Network.removeP42RendezvousListener(FilesharingState.pluginid, FilesharingState.peergroup, listener);
        
        filesharingpane.showUserInterface();
        
        // Classify Documents
        DocumentClassifier dc = new DocumentClassifier();
        dc.start();
    }

    /**
     * Method returns the literal name of the plugin.
     *
     * @see de.uni_bremen.informatik.p2p.plugin_client.plugin_core.Plugin#name()
     */
    public String name() {
        return FilesharingState.pluginName;
    }

    /**
     * getInfo() returns an Info object for this plugin, this object contains
     * some information about the plugin, like e.g. author, name  and version.
     *
     * @return An Info object containing plugin meta information.
     */
    public Info getInfo() {
    	// create uri
    	URI uri = null;
        try {
            uri = new URI("http://peeranha42.informatik.uni-bremen.de");
        } catch (URISyntaxException e) {
            log.warn("the URI does not match the conventions from ietf (look at http://www.ietf.org/rfc/rfc2396.txt)");
        }
        
        // create info object
        Info info =
            new Info(FilesharingState.pluginid,
                     name(),
                     "1.0",
                     "D. Gehrke, P. Hoheisel & L. Kordes",
                     uri,
                     "(c)2004 University Bremen, Germany",
                     "<HTML>This is the P42 Filesharing Plugin.<BIG><BR><FONT color=red>Enjoy it!</FONT></BIG></HTML>",
                     "<HTML>Visit our project website: http://peeranha42.informatik.uni-bremen.de</HTML>");
        
        return info;
    }

    /* (non-Javadoc)
     * @see de.uni_bremen.informatik.p2p.peeranha42.core.plugin.Plugin#connectionState(net.jxta.peergroup.PeerGroup, boolean)
     */
    public void connectionState(PeerGroup arg0, boolean arg1) {
    }
}

⌨️ 快捷键说明

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