/** {@inheritDoc} */ @Override public Document getDocument(MimeMediaType asMimeType) { StructuredDocument adv = StructuredDocumentFactory.newStructuredDocument(asMimeType, getAdvertisementType()); if (adv instanceof XMLDocument) { XMLDocument xmlDoc = (XMLDocument) adv; xmlDoc.addAttribute("xmlns:jxta", "http://jxta.org"); xmlDoc.addAttribute("xml:space", "preserve"); } Element e; e = adv.createElement(typeTag, Integer.toString(getDiscoveryType())); adv.appendChild(e); int threshold = getThreshold(); if (threshold < 0) { throw new IllegalStateException("threshold must be >= 0"); } e = adv.createElement(thresholdTag, Integer.toString(threshold)); adv.appendChild(e); PeerAdvertisement peerAdv = getPeerAdvertisement(); if ((peerAdv != null)) { e = adv.createElement(peerAdvTag, peerAdv.toString()); adv.appendChild(e); } String attr = getAttr(); if ((attr != null) && (attr.length() > 0)) { e = adv.createElement(queryAttrTag, attr.trim()); adv.appendChild(e); String value = getValue(); if ((value != null) && (value.length() > 0)) { e = adv.createElement(queryValueTag, value.trim()); adv.appendChild(e); } else { if (threshold < 0) { throw new IllegalStateException("Attribute specified, but no value was specified."); } } } return adv; }
/** @see net.jxta.discovery.DiscoveryListener#discoveryEvent(DiscoveryEvent) */ public void discoveryEvent(DiscoveryEvent event) { // copied from JXTA Examples - PeerDiscovery System.out.println("Process discovery event..."); DiscoveryResponseMsg response = event.getResponse(); // Get the responding peer's advertisement as a string String responderAdvString = response.getPeerAdv(); InputStream is = null; try { // create a peer advertisement is = new ByteArrayInputStream(responderAdvString.getBytes()); PeerAdvertisement responderAdv = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(XML_MIME_TYPE, is); System.out.println( " [ Got a Discovery Response [" + response.getResponseCount() + " elements] from peer: " + responderAdv.getName() + " ]"); } catch (java.io.IOException e) { // bogus peer, skip this message alltogether. System.out.println("Warning: cannot parse remote peer's advertisement.\n" + e.getMessage()); return; } finally { if (is != null) { try { is.close(); } catch (Throwable throwable) { System.out.println( "Ignoring failure to close stream " + "on remote peer's advertisement"); throwable.printStackTrace(); } } } // now print out each discovered peer PeerAdvertisement newAdv = null; Enumeration responses = response.getResponses(); while (responses.hasMoreElements()) { try { String responseString = (String) responses.nextElement(); // create an advertisement object from each element newAdv = (PeerAdvertisement) AdvertisementFactory.newAdvertisement( XML_MIME_TYPE, new ByteArrayInputStream(responseString.getBytes())); System.out.println(" Peer name = " + newAdv.getName()); } catch (java.io.IOException e) { // got a bad response. continue to the next response System.out.println("Warning: cannot parse response element.\n" + e.getMessage()); continue; } } // end while }