专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »p2p技术 » p2p编程:JXTA P2P编程技术例程(4) »正文

p2p编程:JXTA P2P编程技术例程(4)

来源: 发布时间:星期三, 2008年12月17日 浏览:3次 评论:0
广告处理


在我们建立了发现监听器的后 它将不停加入些新发现module介绍说明通告到我们本地缓冲中 每次processPrimes思路方法被时候 客户peer将尝试连接module介绍说明通告代表peer 接入到他们输入通道中传递个消息去化这个peer质数发现服务)
这个思路方法个元素就是决定我们能够委托以工作peer集合 应该记得个通告有个期限限制 因此我们要消除那些不在有效通告
Public processPrimes( low, high) {
        Set Copy = null;
        synchronized(adverts) {
               Set Copy = (Set) adverts.clone;
        }
        ArrayList workingList = ArrayList;
        ArrayList expired = ArrayList;
        long currentTime = .getCurrentTimeMillis;
        Iterator it = workingSet.iterator;
        while (it.hasNext) {
               ModuleSpecAdvertisement ad = (ModuleSpecAdvertisement)it.next;
               (ad.getLocalExpirationTime > currentTime + (2 * 60 *1000)) {
                      workingList.addElement(ad);
               } {
                       expired.addElement(ad);
               }
        }
removeExpired(expired);
前述段执行了对个简单缓冲中被发现服务管理 让removeExpired思路方法去删除那些过期和即将过期通告(这里没有详细介绍说明removeExpired)
当我们有了个有效通告集合的后我们能够开始对它们进行操作以获得我们需要用来发送消息管道通告 .
注意这个工作分发是人为: 些peer可能比其它peer更有能力 些又可能 网络连接方面更好些 这些区别应该在分配工作时候都被考虑到 事实上 也不会故意把工作分配为过多片段 网络通信时间可能比花在实际计算质数时间更多 (这个例子是介绍说明如何从个ModuleSpecAdvertisement得到个管道通告 这样创建个新消息 然后怎样通过管道发送个消息)
Listing 16.14展示了这些自然数列是如何被分为子列每个子列又能和个将送往别peer消息相对应 消息被插入到个hash表映射中 映射键值显示了消息状态: 是否已经发送了? 我们是否收到了它结果?
Listing 16.14 Creating New Messages
Map messageMap = HashMap;
size = workingList.size
mod = high % size;
high -= mod;
perPiece = high / size;
for ( i=0; i < size; i) {
        //create a message
        Message msg = pipeSvc.createMessage;
        msg.String(ServiceConstants.LOW_INT, low);
        //last message will get to compute a bit more
        (i size-1) {
               high = low + perPiece ?1 + mod;
        } {
               high = low + perPiece -1;
        }
        msg.String(ServiceConstants.HIGH_INT, high);
        low perPiece;

        //we neither sent the message, nor did we get a response
        StatusMap statusMap = StatusMap(false, false);
        StatusMap statusMap = StatusMap(false, false);
        messageMap.put(statusMap, msg);
}
StatusMap 就是对布尔值这里并没有列出
我们最后步就是从每个ModuleSpecAdvertisement中提取管道通告 打开每个管道 发送个消息到那管道 然后将这个消息标记为“已经发送”
应记得个通告就是个结构化文档 类似于XML文档 它能轻易被转换为个文本文档然后打印出来 在开发和测试时候查阅通告内容是非常有好处
Listing 16.15 Pring an Advertisement
Collection ads = messageMap.values;
Iterator it = ads.iterator;
while (it.hasNext) {
        ModuleSpecAdvertisement ad = (ModuleSpecAdvertisement)it.next;
        //First, pr out ModuleSpec advertisement _disibledevent=PASSWORD primecruncher.PrimeClient

这是介绍P2P简单开发文档 包括了些大概意思希望对大家有点点帮助翻译水平也有限希望得到谅解

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: