📄 servermanager.java
字号:
package de.uni_bremen.informatik.p2p.plugins.filesharing.control;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import javax.swing.SwingUtilities;
import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.InputStreamMessageElement;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.PipeAdvertisement;
import org.apache.log4j.Logger;
import de.uni_bremen.informatik.p2p.peeranha42.core.network.Network;
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.data.SearchResult;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.Share;
/**
* The class provides functions processing server needs which will be called
* from serverside.
*
* @author Lars Kordes, Philipp Hoheisel, Daniel Gehrke
*/
public class ServerManager {
/** Logger for warnings, debugs and fatals */
private static Logger log = Logger.getLogger(ServerManager.class);
/** Chunksize */
//public final static int CHUNKSIZE = 16000; // Maximum size for JXTAMessage is 16384 before it will be chunked by JXTA. Leaving some space for the protocoll.
// Maxium search results to send
private static final int MAXSEARCHRESULTS = 100;
/**
* Searches in own shares for specified term considering wildcards.
* In case the term includes no wildcards, *-Wildcards are
* added as prefix and suffix to ensure loose searchresults.
*
* @param term Searchterm
* @param pid PeerID
*/
public static void searchFor(PeerGroup pg,String term, PipeAdvertisement pa, int searchType) {
log.debug("\n\n\n\nSearchfor: " + term);
DocumentClassifier dc = new DocumentClassifier();
ArrayList results = new ArrayList();
// Wildcarding
if (term.indexOf("*") == -1 && term.indexOf("?") == -1) { // if term has no wildcards, wildcards are added
term = "*" + term + "*"; // to ensure any results - Loose Search Method
}
term = term.replaceAll("\\*","(.*)"); // replace usual wildcards with regex
term = term.replaceAll("\\?","(.)"); // replace usual wildcards with regex
term = term.toLowerCase();
// Search
Share share;
for (int i = 0; i < FilesharingState.sharedfiles.size() && i < MAXSEARCHRESULTS; i++) { // not more than MAXSEARCHRESULTS results
share = (Share) FilesharingState.sharedfiles.get(i);
if (share.file.getName().toLowerCase().matches(term)) {
// Determine filetype
int fileType = dc.getFileType(share.file.getName());
// Only add to result, if filetype equals searchtype (-1 for downward compatibility)
if (fileType == searchType || fileType == -1) {
results.add(share);
}
}
}
if (results.size() > 0) sendResultList(pg, results, pa);
}
/**
* Sends a list with results to a search query to the specified user
*
* @param res List of files
* @param pid Userid of user to receive the results
*/
private static void sendResultList(PeerGroup pg, ArrayList res,PipeAdvertisement pa) {
Message response;
String sendResult;
File file;
Share share;
for (int i = 0; i < res.size(); i++) {
try {
share = (Share) res.get(i);
file = (File) share.file;
response = new Message();
StringMessageElement type =
new StringMessageElement("RES", "ResponseToRequest:", null);
response.addMessageElement(type);
StringMessageElement fname =
new StringMessageElement("fname",
file.getName(), null);
response.addMessageElement(fname);
StringMessageElement hash = new StringMessageElement("hash", share.hash, null);
response.addMessageElement(hash);
StringMessageElement uname =
new StringMessageElement("uname",
FilesharingState.username, null);
response.addMessageElement(uname);
StringMessageElement flen =
new StringMessageElement("flen",
Long.toString(file.length()), null);
response.addMessageElement(flen);
long nrOfChunks = FileUtilities.getNrOfChunks(file);
StringMessageElement nrofchunks =
new StringMessageElement("nrofchunks",
Long.toString(nrOfChunks), null);
response.addMessageElement(nrofchunks);
// put pipe advertisment for download into msg
InputStream is = FilesharingState.pa.getDocument(MimeMediaType.XMLUTF8).getStream();
InputStreamMessageElement element = new InputStreamMessageElement("PipeAdvertisment", MimeMediaType.XMLUTF8, is, null);
response.addMessageElement(element);
// Network.sendMsg(FilesharingState.peergroup, pid, response);
// Network.sendMsg(FilesharingState.pluginid,FilesharingState.peergroup,pa,response);
Network.sendMsg(FilesharingState.pluginid,pg,pa,response);
} catch (Exception e) {
log.fatal(e);
}
}
String text = "";
for (int i = 0; i < res.size(); i++) {
text += "\n " + ((Share) res.get(i)).file.getName();
}
log.debug("Sending resultlist: " + text);
}
/**
* The method adds a searchresult to the searchresultlist of State and
* refreshes the table that presents the view of the list.
*
* @param filename Filename of result
* @param username Username holding the file
* @param size Size of file
* @param peerid Peerid of the user
*/
public static void processResponse(String filename, String hash,
String username,
String size,
String nrOfChunksString,
PipeAdvertisement pa) {
long nrOfChunks = -1;
try{
nrOfChunks = Long.parseLong(nrOfChunksString);
} catch (Exception e){
log.fatal("Parsing of resultlist failed");
return;
}
SearchResult response;
if((response = isInList(FilesharingState.searchresults, filename, hash)) != null) {
if(!pipeAdvertisementIsIn(response.sources, pa)) response.sources.add(pa);
} else {
response = new SearchResult(filename, hash, Long.parseLong(size), nrOfChunks);
response.sources.add(pa);
FilesharingState.searchresults.add(response);
}
/*
SearchResult response =
new SearchResult(filename, username, Long.parseLong(size), nrOfChunks, pa);
//add
*/
Runnable update = new Runnable() {
public void run() {
FilesharingState.searchpanel.getTable().updateUI();
}
};
SwingUtilities.invokeLater(update);
}
/**
* The methode searchs for a pipeadvertisment in a given list.
*
* @param pipelist List with pipes.
* @param pa Pipeadvertisement
* @return true, if the list contains the pipeadvertisement, else false.
*/
private static boolean pipeAdvertisementIsIn(ArrayList pipelist, PipeAdvertisement pa) {
if(pa == null || pipelist == null) return false;
PipeAdvertisement padv;
for(int i=0; i<pipelist.size(); i++) {
padv = (PipeAdvertisement) pipelist.get(i);
if(padv.equals(pa)) return true;
}
return false;
}
/**
* Method looks for SearchResult in list with same hash value like the given one.
*
* @param list List that gets browsed.
* @param hash Searched hash value.
* @return SearchResult with given hash value or null.
*/
private static SearchResult isInList(ArrayList list, String name, String hash) {
SearchResult result = null;
for(int i=0; i<list.size(); i++) {
result = (SearchResult) list.get(i);
if(result.filename.indexOf(name) != -1 && result.hash.indexOf(hash) != -1) return result;
}
return null;
}
/**
* Processes the Download and writes down the received file.
*
* @param filename Filename
* @param data byte array including the file
* @param packetnr Number of package
*/
/*
public static void processDownload(String filename,
byte data[], long packetnr) {
log.debug("Paket empfangen (" + filename + ") Nummer:" + packetnr);
if (data != null) {
try {
RandomAccessFile raf = new RandomAccessFile(DownloadManager.DOWNLOADDIR + File.separator + filename + FileUtilities.TEMPSUFFIX, "rws");
raf.seek(packetnr * CHUNKSIZE);
raf.write(data);
raf.close();
DownloadManager.receivedChunk(filename,packetnr); // notify DownloadManager of received chunk
} catch (FileNotFoundException fnfe) {
log.fatal(fnfe.toString());
} catch (IOException io) {
log.fatal(io.toString());
}
}
Runnable update = new Runnable() {
public void run() {
FilesharingState.searchpanel.getTable().updateUI();
}
};
SwingUtilities.invokeLater(update);
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -