最近在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();
}