📄 network.java
字号:
* 第二个元素是加入组的ID;<br>
* 第三个元素是PeerGroup对象,是当前Peer在加入的组;<br>
* 第四个元素是PipeService对象,当前组管道服务。为了以后建立输入管道。<br>
* 第五个元素是PipeAdvertisement对象,是用户在该组建立的管道通告。为了以后建立输入管道。<br>
*
*/
public String joinGroup(int index,String name){
String result = null;
Hashtable currentGroup = new Hashtable(5);
PeerGroup foundGroup = null;
PeerGroupAdvertisement foundGroupAdv = null;
PipeService foundGroupPipes = null;
PipeAdvertisement foundGroupMyAdv = null;
InputPipe foundGroupPipeIn = null;
DiscoveryService foundGroupDisco = null;
String foundGroupID = null;
String foundGroupName = null;
try {
foundGroupAdv = (PeerGroupAdvertisement) tableList.get(index);
foundGroup = netpg.newGroup(foundGroupAdv);
foundGroupID = MyUtil.getGroupID_34(foundGroup.getPeerGroupID().toString());
foundGroupName = foundGroup.getPeerGroupName();
//判断是否已经加入了该组。
if ( joinedPeerGroups.containsKey(foundGroupID) ) {
statusLabel.setText("状态:已经加入了该组,无法创建它!");
return null;
}
statusLabel.setText(
"发现了 Peergroup 通告 And 进入了 " + foundGroupName);
} catch (Exception e) {
statusLabel.setText("状态:加入组时失败!");
return null;
}
//得到加入组的发现服务和管道服务。
try{
foundGroupDisco = foundGroup.getDiscoveryService();
foundGroupPipes = foundGroup.getPipeService();
} catch (Exception e) {
statusLabel.setText("状态:获得组中服务时失败!");
return null;
}
//创建输入管道通告,
//并发布之。
statusLabel.setText(
"发布加入该组后在该组的管道通告。");
try{
foundGroupMyAdv = (PipeAdvertisement)
AdvertisementFactory.newAdvertisement(
PipeAdvertisement.getAdvertisementType());
foundGroupMyAdv.setPipeID(
IDFactory.newPipeID(foundGroup.getPeerGroupID() ));
foundGroupMyAdv.setName(foundGroupName + ":Pipe:" + name);
foundGroupMyAdv.setType(PipeService.UnicastType);
foundGroupDisco.publish(foundGroupMyAdv,
PeerGroup.DEFAULT_LIFETIME,
PeerGroup.DEFAULT_EXPIRATION);
foundGroupDisco.remotePublish(foundGroupMyAdv,
PeerGroup.DEFAULT_EXPIRATION);
} catch (Exception e) {
statusLabel.setText("状态:初始化管道时失败!");
return null;
}
result = MyUtil.getGroupID_34(foundGroup.getPeerGroupID().toString());
currentGroup.put("type","join");
currentGroup.put("PeerGroupID",result);
currentGroup.put("PeerGroup",foundGroup);
currentGroup.put("PipeService",foundGroupPipes);
currentGroup.put("PipeAdvertisement",foundGroupMyAdv);
joinedPeerGroups.putGroup(result,currentGroup);
return result;
//开始传输"信息文件"
}
/**发布一个新的组,<br>
*用于使一个新组在riverCrescentNet里出现,<br>
*在这个新组里可以提供新的文件下载服务。<br>
*
*@param groupName 建立新组的名字,是一个字符串。<br>
*@param newGroupDescription 建立新组的描述,是一个字符串。<br>
*@param name 在当前组中的名字,或标示。<br>
*
*@return 一个String对象,组ID<br>
* 如果创建新组时失败,返回null。<br>
*
*此方法内嵌一个Hashtable:
* 第一个元素是动作类型;<br>
* 第二个元素是加入组的ID;<br>
* 第三个元素是PeerGroup对象,是当前Peer在加入的组;<br>
* 第四个元素是PipeService对象,当前组管道服务。为了以后建立输入管道。<br>
* 第五个元素是PipeAdvertisement对象,是用户在该组建立的管道通告。为了以后建立输入管道。<br>
*
*/
public String publishNewPeerGroup(String newGroupName,String newGroupDescription,String name){
String result = null;
Hashtable currentGroup = new Hashtable(5);
//如果已经加入了这个组,返回null。
//if ( joinedPeerGroups.haveKey(newGroupName) ) {
// statusLabel.setText("状态:已经加入了该组,无法创建它!");
// return null;
//}
PeerGroup newGroup = null;
PipeService newGroupPipes = null;
PipeAdvertisement newGroupMyAdv = null;
InputPipe newGroupPipeIn = null;
PeerGroupAdvertisement newGroupAdv = null;
DiscoveryService newGroupDisco = null;
int count = 3;
statusLabel.setText("状态:正在创建 " + newGroupName + " 组");
try {
// Create a new, all-purpose peergroup.
ModuleImplAdvertisement implAdv =
riverCrescentNet.getAllPurposePeerGroupImplAdvertisement();
newGroup = riverCrescentNet.newGroup(
IDFactory.newPeerGroupID(),
implAdv,
newGroupName,
newGroupDescription);
newGroupAdv = newGroup.getPeerGroupAdvertisement();
} catch (Exception e) {
statusLabel.setText("状态:创建组时失败!");
return null;
}
try {
newGroupDisco = newGroup.getDiscoveryService();
newGroupPipes = newGroup.getPipeService();
} catch (Exception e) {
statusLabel.setText("状态:获得组中服务时失败!");
return null;
}
//创建输入管道通告,
//发布新组的管道通告。
statusLabel.setText(
"发布新组的管道通告。");
try{
newGroupMyAdv = (PipeAdvertisement)
AdvertisementFactory.newAdvertisement(
PipeAdvertisement.getAdvertisementType());
newGroupMyAdv.setPipeID(
IDFactory.newPipeID(newGroup.getPeerGroupID() ));
newGroupMyAdv.setName(newGroupName + ":Pipe:" + name);
newGroupMyAdv.setType(PipeService.UnicastType);
newGroupDisco.publish(newGroupMyAdv,
PeerGroup.DEFAULT_LIFETIME,
PeerGroup.DEFAULT_EXPIRATION);
newGroupDisco.remotePublish(newGroupMyAdv,
PeerGroup.DEFAULT_EXPIRATION);
} catch (Exception e) {
statusLabel.setText("状态:初始化管道时失败!");
return null;
}
result = MyUtil.getGroupID_34(newGroup.getPeerGroupID().toString());
currentGroup.put("type","publish");
currentGroup.put("PeerGroupID",result);
currentGroup.put("PeerGroup",newGroup);
currentGroup.put("PipeService",newGroupPipes);
currentGroup.put("PipeAdvertisement",newGroupMyAdv);
joinedPeerGroups.putGroup(result,currentGroup);
return result;
}
//删除曾经加入过的组,即,退出该组
public void delGroup(Object key){
joinedPeerGroups.delGroup(key);
}
//返回到现在为止已经加入的组.
public JoinedPeerGroups getJoinedPeerGroups(){
return this.joinedPeerGroups;
}
public DiscoveryService getRiverCrescentNetDiscoveryService(){
return this.disco;
}
public PipeService getRiverCrescentNetPipeService(){
return this.pipes;
}
public PeerGroup getRiverCrescentNet(){
return this.riverCrescentNet;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -