Posted on 2007-05-28 15:40
change 阅读(165)
评论(0) 编辑 收藏
最近在JXTA的官方网站上面下载了一份JxtaProgGuide看了看,练习了一下上面的示例程序~~~~大抵上感觉的编程的模式就是:
//Method to start the JXTA platform.
NetPeerGroupFactory factory = new NetPeerGroupFactory();//这是默认的创建的一个组。
netPeerGroup = factory.getInterface();
然后就是获取相应的服务如发现服务(用于发现和发布广告,那么什么是广告呢?
Advertisements 就是:
All JXTA network resources— such as peers, peer groups, pipes, and services —are represented by an
advertisement. Advertisements are language-neutral meta-data structures represented as XML documents. The
JXTAprotocols use advertisements to describe and publish the existence of a peer resources. Peers discover
resources by searching for their corresponding advertisements, and may cache any discovered advertisements
locally.),管道服务(用于创建IN/OUT管道来接发消息,这里创建OutPipe管道会触发outputPipeEvent(OutputPipeEvent event) 事件,而当 Inpipe 管道有消息到来的时候会触发pipeMsgEvent(PipeMsgEvent event)事件 ,而这里In/Out 管道间的联系则就是广告的用处了,它通过PipeID标示出所用的管道来建立他们之间的联系而不至于混乱。对等点间的通讯就要依赖于它了)
discovery = netPeerGroup.getDiscoveryService();
rdv = netPeerGroup.getRendezVousService();
然后是通过所获取的服务来注册监听器在通过发现事件来获取一个广告,或者是直接通过服务来获取一个广告,总之目的就是要获取一个所要找的广告 。如监听:
discovery.addDiscoveryListener(this);此时需要implements DiscoveryListener接口,
实现里面的 discoveryEvent(DiscoveryEvent ev) 方法,然后通过 DiscoveryEvent 获取广告
DiscoveryResponseMsg res = ev.getResponse();
// Get the responding peer's advertisement
PeerAdvertisement peerAdv = res.getPeerAdvertisement();
或者是直接通过服务来获取一个广告
discovery.getRemoteAdvertisements(null, DiscoveryService.GROUP, null, null, 5);
在要不就是i通过一个发现服务来发布一个广告,这里的发布广告分本地发布和远程发布
discoveryService.publish(Adv,PeerGroup.DEFAULT_LIFETIME,PeerGroup.DEFAULT_EXPIRATION);
discoveryService.remotePublish(Adv,PeerGroup.DEFAULT_EXPIRATION);
那么一个对等点如何才能够加入一个Group呢
StructuredDocument creds = null;
// Generate the credentials for the Peer Group
AuthenticationCredential authCred = new AuthenticationCredential( grp, null, creds );
// Get the MembershipService from the peer group
MembershipService membership = grp.getMembershipService();
// Get the Authenticator from the Authentication creds
Authenticator auth = membership.apply( authCred );
// Check if everything is okay to join the group
if (auth.isReadyForJoin()){
Credential myCred = membership.join(auth);
System.out.println("Successfully joined group " + grp.getPeerGroupName());
// display the credential as a plain text document.
System.out.println("\nCredential: ");
StructuredTextDocument doc = (StructuredTextDocument)myCred.getDocument(new MimeMediaType("text/plain"));
StringWriter out = new StringWriter();
doc.sendToWriter(out);
System.out.println(out.toString());
out.close();
}