posts - 29, comments - 0, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          JXTA 服務發布

          Posted on 2007-05-28 15:41 change 閱讀(134) 評論(0)  編輯  收藏

          在jxta里面,所有的資源都是通過廣告來發布的,這里的服務業不例外,在這里的服務  發布里面有兩個很總要的概念,

          • ModuleClassAdvertisement— defines the service class; its main purpose is to formally document the
          existence of a module class. It is uniquely identified by a ModuleClassID.
          • ModuleSpecAdvertisement — defines a service specification; uniquely identified by a ModuleSpecID.
          Its main purpose is to provide references to the documentation needed in order to create conforming
          implementations of that specification. A secondary use is to make running instances usable remotely,
          by publishing any or all of the following:
          • PipeAdvertisement
          • ModuleSpecID of a proxy module
          • ModuleSpecID of an authenticator module
          • ModuleImplAdvertisement — defines an implementation of a given service specification. 

          這里的 ModuleClassAdvertisement  僅僅用來告知服務的存在,對等點若是需要訪問該服務的話,還需要發現與之關聯的 ModuleSpecAdvertisement  廣告信息。

          而這里的 ModuleSpecAdvertisement  則包含了 對等點節點 要訪問該服務所需要的所有相關信息,比如:管道廣告信息,通過它才能夠連接上所需要的服務。

          服務端的代碼示例大抵如下:

          創建發布 ModuleClassAdvertisement  :

          ModuleClassAdvertisement mcadv = (ModuleClassAdvertisement)AdvertisementFactory.newAdvertisement(ModuleClassAdvertisement.getAdvertisementType());
             mcadv.setName("JXTAMOD:JXTA-EX1");
             mcadv.setDescription("Tutorial example to use JXTA module advertisement Framework");
             ModuleClassID mcID = IDFactory.newModuleClassID();
             mcadv.setModuleClassID(mcID);//通過mcID來建立ModuleClassAdvertisement 與ModuleSpecAdvertisement 的聯系

          discovery.publish(mcadv);
           discovery.remotePublish(mcadv);

          創建發布 ModuleSpecAdvertisement :

          ModuleSpecAdvertisement mdadv = (ModuleSpecAdvertisement)AdvertisementFactory.newAdvertisement(ModuleSpecAdvertisement.getAdvertisementType());

          mdadv.setName("JXTASPEC:JXTA-EX1");
             mdadv.setVersion("Version 1.0");
             mdadv.setCreator("sun.com");
             mdadv.setModuleSpecID(IDFactory.newModuleSpecID(mcID));
             mdadv.setSpecURI("http://www.jxta.org/Ex1");

          PipeAdvertisement pipeadv = null;
             try {
              FileInputStream is = new FileInputStream("pipeserver.adv");
              pipeadv = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
              is.close();
             } catch (Exception e) {
              System.out.println("failed to read/parse pipe advertisement");
             }

          mdadv.setPipeAdvertisement(pipeadv);

          discovery.publish(mdadv);
           discovery.remotePublish(mdadv);
            myPipe = pipes.createInputPipe(pipeadv);

          在客戶端,通過不斷的查找廣告(分本地查找和遠端查找)來 獲取 所需要服務的廣告信息,通過它就可以獲取 管道信息 來創建管道以達到通訊的目的。客戶端代碼示例大抵如下:

          Enumeration en = null;
            while (true) {
             try {
              /* let's look first in our local cache to see if we have it! We try to discover an adverisement which as the (Name, JXTA-EX1) tag value
              en = discovery.getLocalAdvertisements(DiscoveryService.ADV,"Name","JXTASPEC:JXTA-EX1");
              //  Ok we got something in our local cache does not
              //  need to go further!
              if ((en != null) && en.hasMoreElements()) {
               break;
              }
              //  nothing in the local cache?, let's remotely query
              //  for the service advertisement.
              discovery.getRemoteAdvertisements(null,DiscoveryService.ADV,"Name","JXTASPEC:JXTA-EX1",1, null);
              //  The discovery is asynchronous as we do not know
              //  how long is going to take
              try { // sleep as much as we want. Yes we
               //  should implement asynchronous listener pipe...
               Thread.sleep(2000);
              } catch (Exception e) {}
             } catch (IOException e) {
              //  found nothing! move on
             }
             System.out.print(".");
            }
            System.out.println("we found the service advertisement");
            //  Ok get the service advertisement as a Spec Advertisement
            ModuleSpecAdvertisement mdsadv = (ModuleSpecAdvertisement)en.nextElement();
            try {
             //  let's print the advertisement as a plain text document
             StructuredTextDocument doc = (StructuredTextDocument)mdsadv.getDocument(MimeMediaType.TEXT_DEFAULTENCODING);
             StringWriter out = new StringWriter();
             doc.sendToWriter(out);
             System.out.println(out.toString());
             out.close();
             //  we can find the pipe to connect to the service
             //  in the advertisement.
             PipeAdvertisement pipeadv = mdsadv.getPipeAdvertisement();
             //  Ok we have our pipe advertiseemnt to talk to the service
             //  create the output pipe endpoint to connect  to the server
              myPipe = pipes.createOutputPipe(pipeadv, 10000);
             }

             //  send the message to the service pipe
             myPipe.send (msg);

          主站蜘蛛池模板: 金昌市| 灵武市| 石城县| 垣曲县| 枣庄市| 崇信县| 莱阳市| 镇巴县| 苍南县| 五大连池市| 潞西市| 泸西县| 蒲城县| 临颍县| 汝州市| 盐城市| 志丹县| 伊川县| 财经| 湟源县| 清远市| 永泰县| 缙云县| 轮台县| 阳新县| 乌鲁木齐县| 丹江口市| 黔南| 沅江市| 禄丰县| 台中市| 双流县| 襄垣县| 吉水县| 确山县| 富平县| 甘洛县| 威信县| 同德县| 宜兰市| 延安市|