📄 grouphelper.java
字号:
// Instant P2P PeerGroupManager Code (Thanks !!!!)
if (!passwdMembershipPeerGroupModuleImplAdv.getModuleSpecID()
.equals(PeerGroup.allPurposePeerGroupSpecID)) {
passwdMembershipPeerGroupModuleImplAdv
.setModuleSpecID(IDFactory
.newModuleSpecID(passwdMembershipPeerGroupModuleImplAdv
.getModuleSpecID().getBaseClass()));
} else {
ID passwdGrpModSpecID = ID
.create(URI
.create("urn:jxta:uuid-DeadBeefDeafBabaFeedBabe00000001"
+ "04" + "06"));
passwdMembershipPeerGroupModuleImplAdv
.setModuleSpecID((ModuleSpecID) passwdGrpModSpecID);
} // End Else
membershipServiceFound = true;
} // end if (allPurposePeerGroupServiceID.
// equals(PeerGroup.membershipClassID))
}// end While
return passwdMembershipPeerGroupModuleImplAdv;
}
// ������Ա��¼������
private ModuleImplAdvertisement createPasswdMembershipServiceModuleImplAdv(
ModuleImplAdvertisement allPurposePeerGroupMemershipServiceModuleImplAdv) {
// Create a new ModuleImplAdvertisement for the
// Membership Service
ModuleImplAdvertisement passwdMembershipServiceModuleImplAdv = (ModuleImplAdvertisement) AdvertisementFactory
.newAdvertisement(ModuleImplAdvertisement
.getAdvertisementType());
passwdMembershipServiceModuleImplAdv
.setModuleSpecID(PasswdMembershipService.passwordMembershipSpecID);
passwdMembershipServiceModuleImplAdv
.setCode(PasswdMembershipService.class.getName());
passwdMembershipServiceModuleImplAdv
.setDescription(" Module Impl Advertisement for the PasswdMembership Service");
passwdMembershipServiceModuleImplAdv
.setCompat(allPurposePeerGroupMemershipServiceModuleImplAdv
.getCompat());
passwdMembershipServiceModuleImplAdv
.setUri(allPurposePeerGroupMemershipServiceModuleImplAdv
.getUri());
passwdMembershipServiceModuleImplAdv
.setProvider(allPurposePeerGroupMemershipServiceModuleImplAdv
.getProvider());
return passwdMembershipServiceModuleImplAdv;
}
public PeerGroup getPeerGroup(PeerGroup myNetPeerGroup,
PeerGroupID satellaPeerGroupID) {
// First discover the peer group
// In most cases we should use discovery listeners so that
// we can do the discovery asynchroneously.
// Here I won't, for increased simplicity and because
// The Peer Group Advertisement is in the local cache for sure
PeerGroup satellaPeerGroup;
DiscoveryService myNetPeerGroupDiscoveryService = null;
if (myNetPeerGroup != null) {
myNetPeerGroupDiscoveryService = myNetPeerGroup
.getDiscoveryService();
} else {
log.error("Can't join Peer Group since it's parent is null");
System.exit(1);
}
boolean isGroupFound = false;
Enumeration localPeerGroupAdvertisementEnumeration = null;
PeerGroupAdvertisement satellaPeerGroupAdvertisement = null;
try {
localPeerGroupAdvertisementEnumeration = myNetPeerGroupDiscoveryService
.getLocalAdvertisements(DiscoveryService.GROUP, "GID",
satellaPeerGroupID.toString());
} catch (java.io.IOException e) {
log.error("Can't Discover Local Adv");
}
if (localPeerGroupAdvertisementEnumeration != null) {
while (localPeerGroupAdvertisementEnumeration.hasMoreElements()) {
PeerGroupAdvertisement pgAdv = null;
pgAdv = (PeerGroupAdvertisement) localPeerGroupAdvertisementEnumeration
.nextElement();
if (pgAdv.getPeerGroupID().equals(satellaPeerGroupID)) {
satellaPeerGroupAdvertisement = pgAdv;
isGroupFound = true;
break;
}
}
}
try {
satellaPeerGroup = myNetPeerGroup
.newGroup(satellaPeerGroupAdvertisement);
} catch (net.jxta.exception.PeerGroupException e) {
log.error("Can't create Peer Group from Advertisement");
e.printStackTrace();
return null;
}
return satellaPeerGroup;
}
public ArrayList<PeerGroup> discoverLocalPeerGroup(
PeerGroup myNetPeerGroup, Map map,ArrayList<PeerGroup> gList) {
// First discover the peer group
// In most cases we should use discovery listeners so that
// we can do the discovery asynchroneously.
// Here I won't, for increased simplicity and because
// The Peer Group Advertisement is in the local cache for sure
//ArrayList<PeerGroup> gList = new ArrayList<PeerGroup>();
PeerGroup satellaPeerGroup = null;
DiscoveryService myNetPeerGroupDiscoveryService = null;
if (myNetPeerGroup != null) {
myNetPeerGroupDiscoveryService = myNetPeerGroup
.getDiscoveryService();
} else {
log.error("Can't join Peer Group since it's parent is null");
}
//boolean isGroupFound = false;
Enumeration localPeerGroupAdvertisementEnumeration = null;
PeerGroupAdvertisement satellaPeerGroupAdvertisement = null;
try {
localPeerGroupAdvertisementEnumeration = myNetPeerGroupDiscoveryService
.getLocalAdvertisements(DiscoveryService.GROUP, null,
null);
} catch (java.io.IOException e) {
log.error(e.getMessage());
}
if (localPeerGroupAdvertisementEnumeration != null) {
while (localPeerGroupAdvertisementEnumeration.hasMoreElements()) {
PeerGroupAdvertisement pgAdv = null;
pgAdv = (PeerGroupAdvertisement) localPeerGroupAdvertisementEnumeration
.nextElement();
log.info(pgAdv.getDocument(MimeMediaType.XMLUTF8).toString());
String name = pgAdv.getName();
String descr = pgAdv.getDescription();
String id = pgAdv.getPeerGroupID().toString();
if (CFUtil.checkConditions(map,name,descr,id)) {
satellaPeerGroupAdvertisement = pgAdv;
log.info("find a Advertisement = "
+ satellaPeerGroupAdvertisement.getDocument(
MimeMediaType.XMLUTF8).toString());
try {
//myNetPeerGroup.getLoader().loadClass(arg0);
//System.out.println("IIIIIDDDDDDDD="+PeerGroupID.create(URI.create(id)));
satellaPeerGroup = myNetPeerGroup
.newGroup(satellaPeerGroupAdvertisement);
//System.out.println("Parent group name="+myNetPeerGroup.getPeerGroupName());
// myNetPeerGroup.newGroup(satellaPeerGroupAdvertisement.getPeerGroupID(), satellaPeerGroupAdvertisement,
// satellaPeerGroupAdvertisement.getName(), satellaPeerGroupAdvertisement.getDescription());
// //isGroupFound = true;
} catch (net.jxta.exception.PeerGroupException e) {
e.printStackTrace();
/*
log.error("-------------Can't create Peer Group from Advertisement");
AdvertisementFactory
.newAdvertisement(PeerGroupAdvertisement.getAdvertisementType());
try{
satellaPeerGroup = myNetPeerGroup
.newGroup(satellaPeerGroupAdvertisement);
}catch(Exception eee){
eee.printStackTrace();
}
*/
/*
System.out.println(e.getMessage());
String eMsg = e.getMessage();
if(eMsg.indexOf("Could not find group implementation with")!=-1){
String msid = eMsg.replace("Could not find group implementation with", "").trim();
try{
RefJxtaLoader rl = (RefJxtaLoader)myNetPeerGroup.getLoader();
rl.loadClass(msid);
System.out.println("load OK...................");
satellaPeerGroup = myNetPeerGroup
.newGroup(satellaPeerGroupAdvertisement);
}catch(Exception eee){
eee.printStackTrace();
}
}*/
// e.printStackTrace();
//myNetPeerGroup.getLoader().loadClass(arg0);
//RefJxtaLoader loader = new RefJxtaLoader("");
}
//System.out.println("this load PeerGroup is : "+satellaPeerGroup);
if(satellaPeerGroup!=null){
gList.add(satellaPeerGroup);
discoverLocalPeerGroup(satellaPeerGroup,null,gList);
}
}
}
}
return gList;
}
public void joinPeerGroup(PeerGroup satellaPeerGroup, String login,
String passwd) {
// Get the Heavy Weight Paper for the resume
// Alias define the type of credential to be provided
StructuredDocument creds = null;
try {
// Create the resume to apply for the Job
// Alias generate the credentials for the Peer Group
AuthenticationCredential authCred = new AuthenticationCredential(
satellaPeerGroup, null, creds);
// Create the resume to apply for the Job
// Alias generate the credentials for the Peer Group
MembershipService membershipService = satellaPeerGroup.getMembershipService();
//�Լ�����֤��
// Send the resume and get the Job application form
// Alias get the Authenticator from the Authentication creds
Authenticator auth = membershipService.apply(authCred);
// Fill in the Job Application Form
// Alias complete the authentication
completeAuth(auth, login, passwd);
// Check if I got the Job
// Alias Check if the authentication that was submitted was
// accepted.
if (!auth.isReadyForJoin()) {
log.error("Failure in authentication.");
log.error("Group was not joined. Does not know how to complete authenticator");
}
// I got the Job, Join the company
// Alias I the authentication I completed was accepted,
// therefore join the Peer Group accepted.
membershipService.join(auth);
initConPipe(satellaPeerGroup);//初始化基本通讯
} catch (Exception e) {
log.error("Failure in authentication.");
log.error("Group was not joined. Login was incorrect.");
e.printStackTrace();
}
}
/**
* 初始化基本通讯
* 创建组或者加入组时必须
*
* */
private boolean initConPipe(PeerGroup satellaPeerGroup){
PeerGroupAdvertisement pgAdv = satellaPeerGroup.getPeerGroupAdvertisement();
StructuredDocument doc = pgAdv.getServiceParam(PeerGroup.pipeClassID);
TextElement te;
String id = null;
String name = "";
String type = null;
Enumeration enu = doc.getChildren(PIPE_ID_STR);
if(enu.hasMoreElements()){
te = (TextElement)enu.nextElement();
id = te.getTextValue();
}
enu = doc.getChildren(PIPE_NAME_STR);
if(enu.hasMoreElements()){
te = (TextElement)enu.nextElement();
name = te.getTextValue();
}
log.info(satellaPeerGroup.getPeerGroupName()+"组基本通讯信息:id ="+id+" name="+name+" type="+type);
enu = doc.getChildren(PIPE_TYPE_STR);
if(enu.hasMoreElements()){
te = (TextElement)enu.nextElement();
type = te.getTextValue();
}
if(id==null || type==null)
return false;
PipeAdvertisement pa = CFUtil.crearePipeAdvertisement(id, type, name);
PipeService pipsrv = satellaPeerGroup.getPipeService();
OutputPipe output;
try {
output = pipsrv.createOutputPipe(pa, 1000);//基础通讯管道
IPGResource pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(satellaPeerGroup);
pgres.getOutputPipes().put(PIPE_OUT_KEY, output);
} catch (IOException ex) {
log.error(ex.getMessage());
return false;
}
InputPipe input;
try {
//GroupBaseCommListener
GroupBaseCommListener lstr = new GroupBaseCommListener(satellaPeerGroup,null);
input = pipsrv.createInputPipe(pa, lstr);//基础通讯管道
IPGResource pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(satellaPeerGroup);
pgres.getInputPipes().put(PIPE_IN_KEY, input);
pgres.getInputListener().put(GroupBaseCommListener.BASE_COMM_KEY, lstr);
} catch (IOException ex) {
log.error(ex.getMessage());
return false;
}
return true;
}
// ������֤
private void completeAuth(Authenticator auth, String login, String passwd)
throws Exception {
Method[] methods = auth.getClass().getMethods();
Vector authMethods = new Vector();
// Find out with fields of the application needs to be filled
// Alias Go through the methods of the Authenticator class and
// copy them sorted by name into a vector.
for (int eachMethod = 0; eachMethod < methods.length; eachMethod++) {
if (methods[eachMethod].getName().startsWith("setAuth")) {
if (Modifier.isPublic(methods[eachMethod].getModifiers())) {
// sorted insertion.
for (int doInsert = 0; doInsert <= authMethods.size(); doInsert++) {
int insertHere = -1;
if (doInsert == authMethods.size())
insertHere = doInsert;
else {
if (methods[eachMethod].getName().compareTo(
((Method) authMethods.elementAt(doInsert))
.getName()) <= 0)
insertHere = doInsert;
} // end else
if (-1 != insertHere) {
authMethods.insertElementAt(methods[eachMethod],
insertHere);
break;
} // end if ( -1 != insertHere)
} // end for (int doInsert=0
} // end if (modifier.isPublic
} // end if (methods[eachMethod]
} // end for (int eachMethod)
Object[] AuthId = { login };
Object[] AuthPasswd = { passwd };
for (int eachAuthMethod = 0; eachAuthMethod < authMethods.size(); eachAuthMethod++) {
Method doingMethod = (Method) authMethods.elementAt(eachAuthMethod);
String authStepName = doingMethod.getName().substring(7);
if (doingMethod.getName().equals("setAuth1Identity")) {
// Found identity Method, providing identity
doingMethod.invoke(auth, AuthId);
} else if (doingMethod.getName().equals("setAuth2_Password")) {
// Found Passwd Method, providing passwd
doingMethod.invoke(auth, AuthPasswd);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -