JXTA 体验

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值