jxta学习笔记3~jxta运行的基本步骤~

来源:互联网 发布:防火墙如何开启403端口 编辑:程序博客网 时间:2024/05/01 21:16

跑通了helloworld后,又运行了官网上的示例程序。现在总结下jxta运行的基本步骤:

1、创建一个组:

 

NetPeerGroupFatory factory = new NetPeerGroupFactory;

netPeerGroup = factory.getInterface( );//该方法返回一个PeerGroup对象

 

2、获取相应服务(发现服务,发现和发布广告):

 

首先在这里重新明确两个概念:

(1)广告:所有网络资源体现,以xml形式表现。

(2)管道服务:创建IN/OUT管道来接收发送消息。

          OutPipe管道创建时会触发outputPipeEvent事件

          InPipe有消息到来时会触发pipeMsgEvent事件

  IN/OUT之间的联系通过广告,通过PipeID标识出来的管道建立联系。

 

discovery = netPeerGroup.getDiscoveryService( );//获得一个发现服务

rdv = netPeerGroup.getRendezVousService( );//集合点服务

 

3、通过注册服务监听器,在通过发现一个事件来获取广告。

 

discovery.addDisgoveryListener(this);//此处需要实现DiscoveryListener接口并实现其中的discoveryListener方法

 

API中的discoveryListener方法:

1、

   public void discoveryEvent(DiscoveryEvent e) {
     DiscoveryResponseMsg msg = e.getResponse();//获得DiscoveryService message "Response"
     if (myQueryID == e.getQueryID()) {
       int advCount = msg.getResponseCount();
     }
   }

 

   由DiscoveryResponseMsg对象获取相应peer的广告:

 

   PeerAdvertisement peerAdv =res.getPeerAdvetisement( );

2、通过服务直接获取广告

   discovery.getRemoteAdvertisements(String peerid,int type,String attribute,String value,int threshold)  

  

参数:
peerid - peer的id 如果为空则传播到所有的与这个广告相匹配的peer,如果指定peerid则传到指定的peer。
type - 发现的类型:可选项有 PEER, GROUP 或者 ADV。
attribute - indexed element name 本人目前翻译水平有限,不做中文翻译了,看这段API:
public abstract String[] getIndexFields()

             Returns the element names on which this advertisement should be indexed.

             如果为空则说明可选项为任意指定广告。

             value - 广告的定义值,可以精确定义也可以模糊匹配。

             threshold - 极限,所有可以相应peer所拥有的最大广告数。

             A threshold of 0, and type of PEER has a special behaviour.

返回值:
查询所发现的ID。

 

3、发布广告

    本地发布:

      void publish(Advertisement adv,
              long lifetime,
              long expiration)
              throws IOException

    远程发布:

      void remotePublish(String peerid,
                   Advertisement adv)

 

原创粉丝点击