?? pipeutil.java
字號:
package jxtamessenger.util;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Enumeration;
import java.util.logging.Logger;
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.id.IDFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeID;
import net.jxta.protocol.PipeAdvertisement;
import org.apache.commons.lang.StringUtils;
public class PipeUtil {
private static final Logger LOG = Logger.getLogger(PipeUtil.class.getName());
private static final int WaitTime = 500; // 遠程發現的等待時間,需根據網絡情況調整
private static final int MAXRETRIES = 20; // 遠程發現時的重試次數,需根據網絡情況調整
/**
* 獲取管道廣告,首先從本地緩存中發現管道廣告,如果沒有發現,再從遠程發現管道廣告,如果沒有發現,那么創建管道廣告。
* @param pg 用于發現或創建管道廣告的節點組
* @param name 用于發現或創建管道廣告的名稱,如果本地或遠程都沒有發現管道廣告,那么將使用該名稱創建管道廣告,因此該名稱不要使用通配符
* @param type 創建管道廣告的類型,目前支持3種基本類型
* JxtaUnicast 單向、不安全和不可靠
* JxtaUnicastSecure 單向、安全(使用TLS)
* JxtaPropagate 傳播廣告
* @param pipeId 指定生成管道廣告的PipeId,如果該值為空或null,那么使用系統生成的Id
* @param remote 如果創建管道廣告,那么是否遠程發布
* @return 在節點組內發現或創建的廣告對象
*/
public static PipeAdvertisement getPipeAdv(PeerGroup pg, String name, String type, String pipeId, boolean remote) {
PipeAdvertisement myAdv = null;
try {
myAdv = findPipeAdv(pg, name);
if(myAdv == null) {
// We did not find the pipe advertisement, so create one
LOG.info("Could not find the Pipe Advertisement");
// Create a pipe advertisement
myAdv = createAdv(pg, name, type, pipeId);
// We have the advertisement; publish it into our local cache or remote peer
publish(pg, myAdv, remote);
LOG.info("Created the Pipe Advertisement");
}
} catch(Exception e) {
LOG.severe("Could not get pipe Advertisement");
return null;
}
return myAdv;
}
/**
* 同步方式發現管道廣告
* @param pg 節點組,在該節點組內發現管道廣告
* @param name 管道廣告名稱,可使用通配符
* @return 管道廣告對象,如果沒有找到或發現過程發生異常,那么返回null
*/
public static PipeAdvertisement findPipeAdv(PeerGroup pg, String name) {
DiscoveryService discovery = pg.getDiscoveryService();
int count = MAXRETRIES; // Discovery retry count
PipeAdvertisement myAdv = null;
try {
LOG.info("Attempting to Discover the pipe advertisement");
// Check if we have already published ourselves
while(count-- > 0) {
// First, check locally if the advertisement is cached
myAdv = searchLocal(pg, name);
// If we found our pipe advertisement, we are done
if(myAdv != null)
break;
// We did not find the advertisement locally;
// send a remote request
discovery.getRemoteAdvertisements(null, DiscoveryService.ADV, PipeAdvertisement.NameTag, name, 1, null);
// Sleep to allow time for peers to respond to the discovery request
try {
Thread.sleep(WaitTime);
} catch(InterruptedException e) {
// ignored
}
}
} catch(Exception e) {
LOG.severe("Could not get pipe Advertisement");
return null;
}
if(myAdv != null) {
LOG.info(myAdv.toString());
} else {
LOG.info("myAdv is null.");
}
return myAdv;
}
/**
* 獲取管道廣告,首先從本地緩存中發現管道廣告,如果沒有發現,再從遠程發現管道廣告,如果沒有發現,那么使用系統生成的Id創建管道廣告。
* @param pg 用于發現或創建管道廣告的節點組
* @param name 用于發現或創建管道廣告的名稱,如果本地或遠程都沒有發現管道廣告,那么將使用該名稱創建管道廣告,因此該名稱不要使用通配符
* @param type 創建管道廣告的類型,目前支持3種基本類型
* JxtaUnicast 單向、不安全和不可靠
* JxtaUnicastSecure 單向、安全(使用TLS)
* JxtaPropagate 傳播廣告
* @param remote 如果創建管道廣告,那么是否遠程發布
* @return 在節點組內發現或創建的廣告對象
*/
public static PipeAdvertisement getPipeAdv(PeerGroup pg, String name, String type, boolean remote) {
return getPipeAdv(pg, name, type, null, remote);
}
/**
* 獲取管道廣告,首先從本地緩存中發現管道廣告,如果沒有發現,那么創建管道廣告。
* @param pg 用于發現或創建管道廣告的節點組
* @param name 用于發現或創建管道廣告的名稱,如果本地或遠程都沒有發現管道廣告,那么將使用該名稱創建管道廣告,因此該名稱不要使用通配符
* @param type 創建管道廣告的類型,目前支持3種基本類型
* JxtaUnicast 單向、不安全和不可靠
* JxtaUnicastSecure 單向、安全(使用TLS)
* JxtaPropagate 傳播廣告
* @param pipeId 指定生成管道廣告的PipeId,如果該值為空或null,那么使用系統生成的Id
* @param remote 如果創建管道廣告,那么是否遠程發布
* @return 在節點組內發現或創建的廣告對象
*/
public static PipeAdvertisement getPipeAdvWithoutRemoteDiscovery(PeerGroup pg, String name, String type, String pipeId, boolean remote) {
PipeAdvertisement pa = searchLocal(pg, name);
if (pa == null) {
pa = createAdv(pg, name, type, pipeId);
publish(pg, pa, remote);
}
return pa;
}
/**
* 獲取管道廣告,首先從本地緩存中發現管道廣告,如果沒有發現,再從遠程發現管道廣告,如果沒有發現,那么使用系統生成的Id創建管道廣告。
* @param pg 用于發現或創建管道廣告的節點組
* @param name 用于發現或創建管道廣告的名稱,如果本地或遠程都沒有發現管道廣告,那么將使用該名稱創建管道廣告,因此該名稱不要使用通配符
* @param type 創建管道廣告的類型,目前支持3種基本類型
* JxtaUnicast 單向、不安全和不可靠
* JxtaUnicastSecure 單向、安全(使用TLS)
* JxtaPropagate 傳播廣告
* @param remote 如果創建管道廣告,那么是否遠程發布
* @return 在節點組內發現或創建的廣告對象
*/
public static PipeAdvertisement getPipeAdvWithoutRemoteDiscovery(PeerGroup pg, String name, String type, boolean remote) {
return getPipeAdvWithoutRemoteDiscovery(pg, name, type, null, remote);
}
/**
* 創建管道廣告
* @param pg 用于創建管道廣告的節點組
* @param name 使用該名稱創建管道廣告
* @param type 創建管道廣告的類型,目前支持3種基本類型
* JxtaUnicast 單向、不安全和不可靠
* JxtaUnicastSecure 單向、安全(使用TLS)
* JxtaPropagate 傳播廣告
* @param pipeId 指定生成管道廣告的PipeId,如果該值為空或null,那么使用系統生成的Id
* @return 在節點組內創建的廣告對象
*/
public static PipeAdvertisement createAdv(PeerGroup pg, String name, String type, String pipeId) {
PipeAdvertisement pa = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
try {
pa.setPipeID(StringUtils.isEmpty(pipeId)?IDFactory.newPipeID(pg.getPeerGroupID()):(PipeID) IDFactory.fromURI(new URI(pipeId)));
pa.setName(name);
pa.setType(type);
} catch (URISyntaxException e) {
LOG.warning("a string could not be parsed as a URI reference");
e.printStackTrace();
}
return pa;
}
/**
* 本地搜索廣告對象
* @param pg 用于搜索管道廣告的節點組
* @param name 用于搜索管道廣告的名稱,可使用通配符
* @return 在節點組內創建的廣告對象(僅返回第一個發現的管道廣告)
*/
public static PipeAdvertisement searchLocal(PeerGroup pg, String name) {
DiscoveryService discoveryService = pg.getDiscoveryService();
Enumeration<Advertisement> pas = null;
try {
pas = discoveryService.getLocalAdvertisements(DiscoveryService.ADV, PipeAdvertisement.NameTag, name);
} catch (IOException e) {
return null;
}
PipeAdvertisement pa = null;
while (pas.hasMoreElements()) {
pa = (PipeAdvertisement) pas.nextElement();
if (pa.getName().equals(name)) {
return pa;
}
}
return null;
}
public static void publish(PeerGroup pg, PipeAdvertisement pa) {
publish(pg, pa, false);
}
public static void publish(PeerGroup pg, PipeAdvertisement pa, boolean remote) {
DiscoveryService ds = pg.getDiscoveryService();
try {
ds.publish(pa);
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (remote) {
ds.remotePublish(pa, DiscoveryService.ADV);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -