JXTA_ Java P2P Programming

3 downloads 253 Views 5MB Size Report
After you have selected OK, configuration files and directories will be written to disk and the platform will boot. Befo
JXTA: Java™ P2P Programming By Daniel Brookshier, Darren Govoni, Navaneeth Krishnan, Juan Carlos Soto Publisher: Sams Publishing Pub Date: March 22, 2002 ISBN: 0-672-32366-4 Table of Contents

Pages: 432

JXTA: Java P2P Programming provides an invaluable introduction to this new technology, filled with useful information and practical examples. It was created by members of the JXTA community, sharing their real-world experience to introduce developers to JXTA. It starts with the fundamentals of P2P and demonstrates how JXTA fulfills the P2P promise, then covers the essentials of JXTA including the protocols, the JXTA Shell, and groups. Later chapters include case studies demonstrating JXTA to synchronize ?> urn:jxta:uuid-AAA122616461AAAAAAA124615032503302 urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE000000010306 JPDA_ROOT_GROUP

46

Root application group

Peer Advertisement A peer advertisement is almost identical to a peer group advertisement. The key difference, of course, is its type. • • • • •

Name— Name of the peer. Description (Desc)— Description of the peer. PeerGroup ID (PID)— An ID that is associated with this peer instance. Debug Flag (Dbg)— Optional tag used for debugging. Service (Svc) — Optional list of elements that associate a group service, denoted by its Class ID (the value of an MCID element), and parameters via Parm elements. In most implementations, this will hold ?> urn:jxta:uuid-59616261646162614A7874615032503356 CFE39F036E4038ABE1801D40772DC803 urn:jxta:jxta-NetGroup Super Chicken urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE0000000805 tcp://198.1.0.70:9701/ jxtatls://uuid59616261646162614A7874615032503356CFE39F036E4038A BE1801D40772DC803/TlsTransport/jxtaWorldGroup jxta://uuid-59616261646162614A787461503250 3356CFE39F036E4038ABE1801D40772DC803/ http://JxtaHttpClientuuid-5961626164616261 4A7874615032503356CFE39F036E4038ABE1801D40772DC803/

47

urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE0000000105 MIICODCCAaGgAwIBAgIBATANBgkqhkiG9w0BAQUFADBkMRUwEwYDVQQKEwx3d3cu anh0YS5vcmcxCzAJBgNVBAcTAlNGMQswCQYDVQQGEwJVUzESMBAGA1UEAxMJU2th bmRhLUNBMR0wGwYDVQQLExRDMkEwNDlGMDBBRjc4NkEyOUI0RTAeFw0wMTEyMjEw NDIzNTlaFw0xMTEyMjEwNDIzNTlaMGQxFTATBgNVBAoTDHd3dy5qeHRhLm9yZzEL MAkGA1UEBxMCU0YxCzAJBgNaBAYTAlVTMRIwEAYDVQQDEwlTa2FuZGEtQ0ExHTAb BgNVBAsTFEMyQTA0OUYwMEFGNzg2QTI5QjRFMIGbMAsGCSqGSIb3DQEBAQOBiwAw gYcCgYEArRbPEHoij/J0PYaI/b7xPmj1MsVX5BmrJqNUjsaEroJewTJ3ffDAyNOl WWC/lTGD8maUhlK7vycVozboaaelOJSDjkZ/gDBYglwDrciFsMsKTcwdYdp6x3/s PmUwAHDu8AvPvpyq/2UNNCMxPL9G+OfVnBAxG5TdNvnpSVZXX8MCAREwDQYJKoZI hvcNAQEFBQADgYEAGRmbIRoq0EQQEfg3jIdc/WIChIpYCIZq06VLkESntqBCCnfN z/YWFMeJRwXGZmZqG321wMVpQytRMUr2ewnVXJjvsVZiH1erd1bgUzKoIpcJy6Bd X+/cuiFUWxkQu+GTNcjt1ytjGbpNy/kUxg7bPTOXU55c1XDzN/+ASMLqxo8=

Modules Modules are definitions of services and applications available on a peer or in a peer group. Modules are used to define the code to be executed. To ensure that different peers written with different languages, versions, and operating systems, modules are defined with three different XML advertisements: • • •

Module Class— Defines the specific version of a module. Module Specification— Defines the module with a cross platform definition that includes behavior. Module Implementation— Specific instance of a module on a platform.

Each advertisement is used to narrow the final implementation used by the peer. However, only the respective IDs may be available and no true advertisement available on the general network. The reason is that some modules are not public or they do not have different versions or implementations. Instead, the advertisements are internal to the peer and only referenced by their identifiers as needed. The following three sections define the detail of the different module advertisement types. Module Class Advertisement A Module Class advertisement defines behavior. Peer groups, peer, and other advertisements reference a module class ID that is defined by the module class advertisement. The advertisement has the following parameters: • • •

Module class ID (MCID)— Unique identifier used to reference the module class. Name— Name of the module. Used for searching and identification. Not guaranteed to be unique. Description (Desc)— Description used for searching and identification.

The following is a sample advertisement. This is the module class advertisement for the EX1 advertisement:

48

urn:jxta:uuid-2584FEB44D3B40E9A16A8419C9ABE09F05 JXTAMOD:JXTA-EX1 Tutorial example to use JXTA module advertisement Framework

Module Specification Advertisement (Module) A Module Specification Advertisement is the specification of a module. The advertisement contains information about the implementation referred to by a module specification ID. Because the code for a module is usually part of the peer application, there is no need to publish the advertisement for each ID, but it is a good practice to document the module via the advertisement. Each module has the following tags: • • • • • • • • • •

Module spec ID (MSID)— An ID that specifically defines this module. Compatibility statement— An XML specification used to define the compatibility of the code to a language and version. Name— Name of the specification. Description(Desc)— Description of the specification used for searching and identific ation. Creator (Crtr)— Creator of the specification. Specificaton URI document (SURI)— URI of a specification document. Version (Vers)— Version of this specification. Parameters (Parm)— List of parameters to be used by the implementation. Proxy— ModuleSpecID of a proxy module if one exists. Authenticator (Auth)— ModuleSpecID of an authenticator module if required.

The following is an example of the XML for the module specification for the EX1 example. Note that in addition to its standard tags, there is a pipe advertisement used to communicate to the module. This is done as a convenience, so that the user of the code does not need to look for an extra pipe advertisement: urn:jxta:uuid-88A7B34E2B354A75A181B34E6058D3DA0F 230D7557A24F159F80ABA479BC0C3B06 JXTASPEC:JXTA-EX1 sun.com http://www.jxta.org/Ex1 Version 1.0 urn:jxta:uuid-9CCCDF5AD8154D3D87A391210404E59 BE4B888209A2241A4A162A10916074A9504

49

JxtaUnicast JXTA-EX1

Module Implementation Advertisement The Module Implementation advertisement is the final link in the chain to define a module. The advertisement defines specific references to a specific language representation on a peer. This advertisement is used to launch the code. • • • •

Name— Optional name associated with the module. Description (Desc)— Optional string used to describe and allow for searching of key words to locate a module. ModuleSpecID (MSID)— ID that uniquely identifies the specification being implemented. Compatibility (Comp)— An element that describes the execution environment. For Java this would be the JVM version.

• • • •

Package URI (PURI)— Optional URI used to download the code of this implementation (not implemented in version 1.0). Code— Contains a reference used to load and execute the code of this implementation. For a Java module, this is a fully-qualified classname. Parameter (Parm)— Arbitrary parameters to be interpreted by the implementation's code. Provider (Prov)— Provider of the implementation.

The following is the module implementation for the standard peer group. Note that this implementation contains further implementation advertisements in the param tag. Also note that the last entry is the shell application. The shell is defined this way as the default application of the peer group. If the peer group is started after it is initialized, the code in the application tag is executed: urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE000000010306 JDK1.4 V1.0 Ref Impl net.jxta.impl.peergroup.StdPeerGroup http://www.jxta.org/download/jxta.jar sun.com General Purpose Peer Group Implementation urn:jxta:uuidDEADBEEFDEAFBABAFEEDBABE000000060106 JDK1.4 V1.0 Ref Impl net.jxta.impl.rendezvous.RendezVousServiceImpl http://www.jxta.org/download/jxta.jar sun.com Reference Implementation of the Rendezvous service

50

urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE000000030106 JDK1.4 V1.0 Ref Impl net.jxta.impl.discovery.DiscoveryServiceImpl http://www.jxta.org/download/jxta.jar sun.com Reference Implementation of the DiscoveryService service urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE000000050106 JDK1.4 V1.0 Ref Impl net.jxta.impl.membership.NullMembershipService http://www.jxta.org/download/jxta.jar sun.com Reference Implementation of the MembershipService service urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE000000070106 JDK1.4 V1.0 Ref Impl net.jxta.impl.peer.PeerInfoServiceImpl http://www.jxta.org/download/jxta.jar sun.com Reference Implementation of the Peerinfo service urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE000000020106 JDK1.4 V1.0 Ref Impl net.jxta.impl.resolver.ResolverServiceImpl http://www.jxta.org/download/jxta.jar sun.com Reference Implementation of the ResolverService service

51

urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE000000040106 JDK1.4 V1.0 Ref Impl

net.jxta.impl.pipe.PipeServiceImpl http://www.jxta.org/download/jxta.jar sun.com Reference Implementation of the PipeService service

urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE0000000C0206 JDK1.4 V1.0 Ref Impl net.jxta.impl.shell.bin.Shell.Shell http://www.jxta.org/download/jxta.jar sun.com JXTA Shell reference implementation

Pipe Advertisements Pipe advertisements describe the type of pipe. Pipe advertisements are rather simplistic. They only have a name, ID, and type. As we have discussed, there are several different types of pipe. The specific pipe type is listed in the Type tag. Pipes contain the following tags: • • •

Name— Name of the pipe. Id— The ID of the pipe. Type— The type of the pipe. Type is related to a protocol and therefore to endpoints on a peer. Types are UnicastType, UnicastSecureType, and PropagateType.

The following is an example of an XML pipe advertisement. This pipe is a unicast pipe: urn:jxta:uuid-59616261646162614E50472050325033A10C F46E7B7041B48C3EBF32A5DA2A4404 JxtaUnicast frodo.replyTo

Endpoint Router Messages

52

The router protocol uses query and response messages to discover routes. The query message supplies the peer ID of the destination. The ID of the origin is assumed that of the source. This message is sent from a peer to a peer, which implements the peer endpoint protocol. The XML endpoint router query message schema is as follows:

The router answer message contains the information about the route that was located by the router or a router it collaborated with to create the answer. The actual route is a list of peers that are all gateways, except the final destination that is not required to be a gateway:

Messages Message advertisements are used for the various messaging protocols, as well as for user defined messages. There are two different types of messages— XML and binary. XML Messages XML messages are used for transport mechanisms that only support text or as a general way to send a message. Because messages are seen as the most used type of > tcp://123.456.205.212 AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyM zQ1Njc4

53

OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprb G1ub3Bx cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkp aanqKmq q6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsc=

Binary Messages Binary messages are compact packets used to send information with as compact a type="jxta:PipeResolver"/>

Resolver The resolver is used to send queries throughout the P2P network. The resolver is used by base services, such as the discovery mechanism to search for advertisements. You can also use the resolver when you have a query response model of communication where answers come from a group of peers rather than a specific peer. The following is the XML schema for the ResolverQuery message:

The RendezVousPropagateMessage holds the information used by the rendezvous to determine specific information about the message and its routing.

57

JXTA Identifiers JXTA has many identifiers. We have shown most of them earlier in the chapter as parts of the advertisements. They are used for identification, indexing, and searching. There are usually two specific identifiers in most advertisements. The first is the advertisement ID. This ID is generated when the advertisement is created. One way to look at the advertisement identifier is that it is like an object reference. The identifier has no real value other than to give each advertisement a unique number. The second identifier is one associated with the advertisement type. The format of identifiers vary according to their intent and use. Identifiers vary from simple names to unique identifiers that can consist of rather long sequences of numbers and letters (please refer to the earlier section titled "Modules" for examples of the different identifiers).

Codat ID Codat is an invented word that is short for "code and port"+ ((BeepAdv)value).getPort() ; } else if (value instanceof String){ text = (String)value; } else { text += " NOT EVALUATED"; } if (text.length() == 0){ text = "none"; } setText(text); return this; } // end of getTreeCellRendererComponent() } // End of class AdvertisementTreeCellRenderer

Running Explorer

279

Running the Explorer is very simple. Just start the application and start selecting and expanding nodes on the advertisement tree. The application looks like that shown in Figure 11.3. The advertisement tree is on the left and the advertisement display is on the right. Figure 11.3. Screen shot of Explorer. The peer group Zoo is selected and its XML advertisement is shown on the right panel.

Be aware that the discovery process is very slow. If you select and then open a group leaf, you will see the sub-folders, but the sub-folders may not be populated with advertisements. Sometimes, you will be looking at a folder's contents, like groups, and the system will suddenly add new groups. The reason is that the discovery process is receiving a different set of groups from another rendezvous. This is normal. By now, you should understand that P2P computing rarely gives you exactly what you want all at once.

Learning about JXTA There are a few things to learn about the JXTA network. First is the response of the discovery mechanism. The remote discovery is very inconsistent as to when it begins to return> />

Self-Describing One very nice feature of XML is that the tags describe data. XML is self-describing because of its tags. This feature makes it quite easy to understand the contents and match them to concepts. Electronic Data Interchange (EDI) is another standard for data exchange that is not self-describing, because the packets of data only contain data, not the names of the data. With EDI, you cannot tell if data is in a message; with XML you can not.

Strong Typing and Syntax Validation XML is capable of being strongly typed and a custom syntax can be enforced and validated. However, not all XML implementations, including JXTA, take advantage of these capabilities. At the time of this writing, the XML of JXTA advertisements are not validated. The Java code does fail at times and, thus, gives the appearance of strong syntax validation. Validation may be provided in the future, or you could add validation yourself at various points. You will need a Data Type Definition file (DTD). The DTD describes the typing and the syntax of an XML specification. DTD files are available at JXTA.org. Alternatively, you can use an XML schema definition file to verify that the XML messages are correct. Validation of syntax has marginal value depending on how well the code is written. When an application is able to generate valid advertisements, there is little need for a DTD and the overhead of processing. Also, because advertisements are converted to objects or other constructs, it is pretty obvious when the XML has something unexpected.

290

Problems with XML XML is pretty verbose. One of the worst problems is with the way that tags are used. By duplicating the tag name in the end tag, XML begins to waste as much space as there are tags in an XML document. In addition, XML data is formatted as text. Text data can be much larger than its binary equivalent. This is especially true of numbers, which waste more space as they increase in the size of significant digits. Another problem with XML is compression. Sticking with the raw Unicode compared to the same thing as a compressed equivalent, you could see a difference of size. Unfortunately, compression requires CPU capacity as well as the extra memory required for encoding and decoding the compression. JXTA does not use compression because the extra resources may prevent JXTA from being used on smaller systems. Despite the resources required, JXTA may support some form of compressed XML in the future.

291