Está en la página 1de 15

Practical JXTA II

Cracking the P2P puzzle


Practical JXTA II
Copyright © 2010 by DawningStreams, Inc. The Netherlands

First edition: July, 2008.

Second edition: July, 2010.


All right reserved. No part of this book may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information stor-
age and retrieval system, without written permission from the publisher, except for the inclusion
of brief quotations in a review.
International Standard Book Number: 978-1-4461-3956-1
Limit of Liability/Disclaimer of Warranty: While the publisher and the author have used their
best effort in preparing this book, they make no representation or warranties with respect to the
accuracy or completeness of the contents of this book.
The software code examples, advice, strategies herein are provided “as is” and any expressed or
implied warranties, including, but not limited to, the implied warranties or merchantability and
fitness for a particular purpose are disclaimed. No warranty may be created or extended by sales
representatives or written sales materials. In no event, shall DawningStreams, Inc. be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not
limited to, procurement of substitute goods or services; loss of use, data, or profit; or business in-
terruption) however caused and on any theory of liability, whether in contract, strict liability, or
tort (including negligence or otherwise) arising in any way out of the use of the software code ex-
amples, advice, strategies, even if advised of the possibility of such damages. You should consult
with a professional where appropriate.
Trademarks: DawningStreams, Inc. is a registered trademarks of DawningStreams, Inc. in the
United States and other countries and may not be be used without permission. All other trade-
marks are the property of their respective owners. DawningStreams, Inc. is not associated with
any products or vendors mentioned in this book.
Publisher: Lulu Enterprises, Inc. (www.lulu.com)

-2
Table of Contents
Foreword 11
Why This Book?.............................................................................................................................................................................12
Why A Second Edition?................................................................................................................................................................13
Who Should Read This Book?.....................................................................................................................................................14
Prerequisites..................................................................................................................................................................................15
Introduction 16
Peer to Peer....................................................................................................................................................................................16
A Bad Reputation?.................................................................................................................................................................16
A Quick Historical Review....................................................................................................................................................18
Architectures & Design Principles..............................................................................................................................................20
Client/Server..........................................................................................................................................................................20
Three-tier & Multi-tier ..........................................................................................................................................................21
Clustering & Load Balancing ...............................................................................................................................................21
Service Oriented Architecture & Middleware....................................................................................................................22
Grid Computing.....................................................................................................................................................................23
Cloud Computing..................................................................................................................................................................23
About Needs That Have Shaped IT Solutions....................................................................................................................23
What is P2P?..................................................................................................................................................................................24
ICQ...........................................................................................................................................................................................24
Napster....................................................................................................................................................................................25
Gnutella...................................................................................................................................................................................26
Kazaa.......................................................................................................................................................................................27
BitTorrent................................................................................................................................................................................28
Freenet.....................................................................................................................................................................................28
Pure P2P..................................................................................................................................................................................28
Initial Objectives and Evolution...........................................................................................................................................29
Benefits....................................................................................................................................................................................30
Drawbacks..............................................................................................................................................................................30
So, What Is The Use of P2P?..................................................................................................................................................32
Towards Universality ...........................................................................................................................................................32
JXTA...............................................................................................................................................................................................34
Introduction............................................................................................................................................................................34
The Three Layer Cake............................................................................................................................................................35
The JXTA Project....................................................................................................................................................................36
JXSE Community....................................................................................................................................................................36
C/C++..............................................................................................................................................................................37
JXME.................................................................................................................................................................................37
Documentation, Forum & FAQ............................................................................................................................................37
Understanding JXTA 39
Introduction...................................................................................................................................................................................39
A Tribes-In-Islands Metaphor...............................................................................................................................................39
The Concepts.................................................................................................................................................................................40
Overview.................................................................................................................................................................................40
Peer..........................................................................................................................................................................................41
Peer Group..............................................................................................................................................................................43
Service......................................................................................................................................................................................44
Resources.................................................................................................................................................................................47
Advertisement........................................................................................................................................................................47
Peer Advertisement.........................................................................................................................................................48
Peer Group Advertisement............................................................................................................................................48
Publication.......................................................................................................................................................................48
ID..............................................................................................................................................................................................49

-3
Content....................................................................................................................................................................................50
Codats......................................................................................................................................................................................50
Pipes.........................................................................................................................................................................................50
Module....................................................................................................................................................................................50
The Protocols.................................................................................................................................................................................52
Message...................................................................................................................................................................................53
Endpoint Routing Protocol (ERP)........................................................................................................................................53
Endpoint Service.............................................................................................................................................................53
Endpoint Address...........................................................................................................................................................54
Endpoint Routing Protocol.............................................................................................................................................54
ERP Messages & Advertisements..................................................................................................................................56
Top-Down-Top................................................................................................................................................................57
Rendezvous Protocol (RVP)..................................................................................................................................................58
Message Propagation Protocol.......................................................................................................................................58
PeerView Protocol...........................................................................................................................................................59
Rendezvous Advertisement...........................................................................................................................................60
Rendezvous Lease Protocol............................................................................................................................................61
Peer Connection to Rendezvous ...................................................................................................................................61
Propagation Control........................................................................................................................................................61
Top-Down-Top................................................................................................................................................................63
Pipe Binding Protocol (PBP).................................................................................................................................................63
Pipe Advertisement.........................................................................................................................................................64
Pipe Resolver Message...................................................................................................................................................64
Propagate Pipe Message Header...................................................................................................................................65
Top-Down-Top................................................................................................................................................................65
Peer Resolver Protocol (PRP)................................................................................................................................................65
Resolver Query Message................................................................................................................................................66
Resolver Response Message...........................................................................................................................................66
Shared Resource Distributed Index (SRDI)..................................................................................................................66
Resolver SRDI Message..................................................................................................................................................67
Top-Down-Top................................................................................................................................................................68
Peer Information Protocol (PIP)............................................................................................................................................68
PIP Query Message.........................................................................................................................................................69
PIP Response Message....................................................................................................................................................69
Top-Down-Top................................................................................................................................................................69
Peer Discovery Protocol (PDP).............................................................................................................................................70
Discovery Query Message..............................................................................................................................................70
Discovery Query Response............................................................................................................................................70
Top-Down-Top................................................................................................................................................................71
Access Control...............................................................................................................................................................................71
Membership Service...............................................................................................................................................................72
Access Service.........................................................................................................................................................................72
The Ignition Process......................................................................................................................................................................73
Module....................................................................................................................................................................................73
Service......................................................................................................................................................................................74
Peer Group As A Service – Part I...................................................................................................................................74
Well-known Module Class, Specification and Implementation IDs..........................................................................74
Bootstrapping JXTA........................................................................................................................................................74
Peer Group As A Service – Part II.................................................................................................................................77
Loading Other Services...................................................................................................................................................77
Network Boundaries 79
Reminder........................................................................................................................................................................................79
IP..............................................................................................................................................................................................80
Unicast, Broadcast & Multicast......................................................................................................................................81
IPv4 Versus IPv6..............................................................................................................................................................81
TCP..........................................................................................................................................................................................81
Handshake.......................................................................................................................................................................81
UDP..........................................................................................................................................................................................82
Datagram..........................................................................................................................................................................82

-4
Port...........................................................................................................................................................................................82
Firewalls..................................................................................................................................................................................83
NAT.........................................................................................................................................................................................84
Some Limitations.............................................................................................................................................................86
PAT...................................................................................................................................................................................86
Proxy........................................................................................................................................................................................87
Router......................................................................................................................................................................................87
Multicasting Versus Subnets..........................................................................................................................................87
Natural Network Boundaries...............................................................................................................................................88
Artificial Network Boundaries.............................................................................................................................................89
JXTA Transportation Layer..........................................................................................................................................................89
JXSE Transportation Layer...........................................................................................................................................................90
Overcoming Firewalls............................................................................................................................................................90
Relay Service....................................................................................................................................................................91
Overcoming NATs.................................................................................................................................................................91
Overcoming Proxies...............................................................................................................................................................92
Seeds........................................................................................................................................................................................94
Peer Accessibility..........................................................................................................................................................................95
WAN........................................................................................................................................................................................95
LAN.........................................................................................................................................................................................96
Same Subnet.....................................................................................................................................................................96
Different Subnets.............................................................................................................................................................96
NAT Traversal...............................................................................................................................................................................97
IP Obstacles......................................................................................................................................................................97
Port Prediction.................................................................................................................................................................98
Punching Holes...............................................................................................................................................................98
Hairpin Issue....................................................................................................................................................................98
RFC 5128...........................................................................................................................................................................99
TURN.......................................................................................................................................................................................99
STUN.......................................................................................................................................................................................99
STUNT...................................................................................................................................................................................100
TTL-less Version of STUNT.........................................................................................................................................102
New NAT Classification...............................................................................................................................................102
NUTSS...................................................................................................................................................................................103
P2PNAT.................................................................................................................................................................................103
NATBlaster...........................................................................................................................................................................104
NatTrav.................................................................................................................................................................................104
PWNAT.................................................................................................................................................................................105
ALG........................................................................................................................................................................................106
UPnP......................................................................................................................................................................................107
ICE..........................................................................................................................................................................................107
ICE-TCP..........................................................................................................................................................................108
“Behave” Compliant NAT..................................................................................................................................................108
RFC 5382.........................................................................................................................................................................108
RFC 4787.........................................................................................................................................................................109
Where Does JXSE Stand?.....................................................................................................................................................110
Conclusion...................................................................................................................................................................................111
JXSE Cryptographic Layer 113
Cryptography Reminder............................................................................................................................................................113
Alice, Bob & Eve...................................................................................................................................................................113
Introduction To Secure Communication...........................................................................................................................114
...And Vicious Circles!..........................................................................................................................................................117
Public Key Infrastructure (PKI)..........................................................................................................................................119
Who Certifies The Root Certificate Authority?..........................................................................................................120
Duties Of A Certificate Authority...............................................................................................................................120
Web Of Trust.........................................................................................................................................................................121
X.509.......................................................................................................................................................................................122
X.500 & Principal..................................................................................................................................................................123
Hash Function.......................................................................................................................................................................123

-5
The Collision Issue........................................................................................................................................................124
Key Sizes ..............................................................................................................................................................................125
Assumptions ........................................................................................................................................................................125
The DRM Illusion..........................................................................................................................................................126
Calling Experts.....................................................................................................................................................................126
Personal Security Environment (PSE).......................................................................................................................................127
Java Cryptography Architecture........................................................................................................................................127
KeyStore................................................................................................................................................................................127
Creation..........................................................................................................................................................................128
Automatic X.509 Certificate & Private Key Creation................................................................................................128
Big Big Big Warning!.....................................................................................................................................................131
Registering Your Own X.509........................................................................................................................................132
PSE Configuration................................................................................................................................................................132
Other Concepts............................................................................................................................................................................134
Secure Socket Layer (SSL)...................................................................................................................................................134
Transport Security Layer (TLS)..........................................................................................................................................134
Virtual Private Network (VPN)..........................................................................................................................................134
JXTA Security Layer....................................................................................................................................................................135
About Cipher Suites.............................................................................................................................................................135
About Encryption of Private Keys......................................................................................................................................135
PSE Configuration Advertisement Encryption..........................................................................................................137
Conclusion............................................................................................................................................................................137
Where Does JXSE 2.6 Stand?........................................................................................................................................138
Architectural Considerations 139
Identity Issues..............................................................................................................................................................................139
IP Addresses As Identities..................................................................................................................................................139
Defining An Identity in JXTA.............................................................................................................................................140
Creation Of Peer IDs & Importation Of IDs From Other Systems..................................................................................141
Peer Group Creation & Identity.........................................................................................................................................141
Configuration Modes..................................................................................................................................................................143
JXTA Peer Types...................................................................................................................................................................143
Minimal Edge Peer........................................................................................................................................................143
Full-Featured Edge Peer...............................................................................................................................................143
Rendezvous Peer...........................................................................................................................................................143
Relay Peer.......................................................................................................................................................................143
JXSE Configuration Modes..................................................................................................................................................144
ADHOC..........................................................................................................................................................................144
EDGE..............................................................................................................................................................................144
RENDEZVOUS..............................................................................................................................................................145
RELAY............................................................................................................................................................................145
PROXY............................................................................................................................................................................146
Peer Type versus Configuration Types..............................................................................................................................146
About Implementation Of Services....................................................................................................................................146
Network Scope............................................................................................................................................................................147
To Be Or Not To Be?.............................................................................................................................................................147
A Relay...........................................................................................................................................................................147
A RendezVous...............................................................................................................................................................148
Number of Seeds...........................................................................................................................................................149
Network Administration............................................................................................................................................................149
Default IP Ports....................................................................................................................................................................149
Default HTTP Port...............................................................................................................................................................149
Default Multicasting Port & IP Address............................................................................................................................150
Implementing Seeds.............................................................................................................................................................150
About Subnets......................................................................................................................................................................150
Cryptography..............................................................................................................................................................................151
Implementing Your Own Cryptography Layer................................................................................................................151
Exportation Limitations................................................................................................................................................151
Access Control Implementation................................................................................................................................................152

-6
Using JXSE 153
Getting Started.............................................................................................................................................................................153
Creating A Project................................................................................................................................................................153
Maven....................................................................................................................................................................................154
Javadoc..................................................................................................................................................................................154
First Connection & Local Configuration...................................................................................................................................154
Example 100 - Starting And Stopping JXTA.....................................................................................................................154
Example 110 – Creating A Local Configuration...............................................................................................................156
Example 120 – Retrieving, Modifying & Saving A Local Configuration.......................................................................157
Miscellaneous.......................................................................................................................................................................159
Understanding ConfigParam.......................................................................................................................................159
Modifying Other Configuration Parameters..............................................................................................................159
Loading A Configuration From A URI.......................................................................................................................159
Automatic Change Of IP Addresses...........................................................................................................................159
Local Configuration Directory Structure....................................................................................................................160
New Configuration Objects & Connecting With OSGi...........................................................................................................160
Example 150 - Configuration Objects.................................................................................................................................160
Example 160 – Connecting with OSGi...............................................................................................................................161
Exploring Connectivity Issues...................................................................................................................................................163
Creating & Using Seeds.......................................................................................................................................................163
About The Default JXTA Seeds...........................................................................................................................................166
Running Multiple Peers On A Single Device....................................................................................................................166
Multiple Peer In The Same JVM..................................................................................................................................166
The Proper Angle To Strike A Match.................................................................................................................................167
Jack, The Rendezvous..........................................................................................................................................................167
Anna, The Edge....................................................................................................................................................................169
Testing Angles Between Jack And Anna...........................................................................................................................171
Angle I - Start Jack, Then Anna, Delete Configuration & Approve Seed................................................................171
Angle II - Start Anna, Then Jack, Delete Configuration & Approve Seed..............................................................172
Angle III - Start Jack, Then Anna, Keep Configuration & Disapprove Seed..........................................................173
Remarks & Warning......................................................................................................................................................173
Aminah, The Other RendezVous.......................................................................................................................................174
Chihiro, The Other Edge.....................................................................................................................................................175
Automatic Reconnection To Other RendezVous..............................................................................................................178
Connectivity Manager.........................................................................................................................................................179
Robert, The Relay.................................................................................................................................................................180
Relay Connectivity...............................................................................................................................................................182
Summary...............................................................................................................................................................................182
Creating Peers And Peer Groups..............................................................................................................................................183
Example 200 – Creating IDs................................................................................................................................................183
Example 210 – Creating A New Peer.................................................................................................................................184
Example 220 – Creating A Customized Peer Group........................................................................................................185
Discovery Operations.................................................................................................................................................................188
Finding Advertisements......................................................................................................................................................188
Example 300 - Retrieving And Flushing Local Advertisements.....................................................................................188
Example 310 – Remote Search For Advertisements.........................................................................................................190
Local And Remote Publication...........................................................................................................................................194
Messages & Advertisements......................................................................................................................................................194
Example 400 – Creating An Empty Message....................................................................................................................194
Example 410 – Add String, Long & Int Elements.............................................................................................................194
Example 420 – Retrieving Message Elements...................................................................................................................195
Example 430 – Add Byte Array Element And Retrieve InputStream.............................................................................196
Example 440 – Adding An Advertisement In A Message...............................................................................................197
Example 500 – Customized Advertisement......................................................................................................................198
Attributes........................................................................................................................................................................201
Methods..........................................................................................................................................................................201
Instantiator.....................................................................................................................................................................202
Example 510 – Registering A Customized Advertisement Instance..............................................................................202
Private Keys, Certificates & KeyStores.....................................................................................................................................203

-7
Example 600 - Exporting & Importing Private Keys And X.509 Certificates.................................................................203
Example 610 – Working With A KeyStore........................................................................................................................205
Example 620 – Custom PSE Peer Group & Joining .........................................................................................................207
Simple Pipe Communication.....................................................................................................................................................210
Chandra, The Rendezvous Listening to Messages...........................................................................................................211
Dimitri, The Edge Sending Messages................................................................................................................................213
Running The Example.........................................................................................................................................................215
Unicast Secure Pipes............................................................................................................................................................217
Bidirectional Pipe Communication...........................................................................................................................................217
Adelaide, The RendezVous At One End...........................................................................................................................217
Quinisela, The Edge At The Other End.............................................................................................................................220
Running The Example.........................................................................................................................................................222
JXTA Socket & Socket Server.....................................................................................................................................................223
Lidong, The Rendezvous And JXTA Socket Server..........................................................................................................223
Ayrton, The Edge And JXTA Socket..................................................................................................................................226
Running The Example.........................................................................................................................................................228
JXTA Multicast Socket................................................................................................................................................................229
Hans, The Rendezvous, A Multicast Participant..............................................................................................................229
Teyacapan, The Edge, Another Multicast Participant......................................................................................................231
Running The Example.........................................................................................................................................................233
Implementing A Customized Service.......................................................................................................................................233
Introduction..........................................................................................................................................................................233
Example 700 – Creating Module IDs..................................................................................................................................233
Example 710 – Astrology Service .......................................................................................................................................234
Static And Non-Static Attributes.................................................................................................................................238
Static Methods...............................................................................................................................................................239
Static Class......................................................................................................................................................................239
Implementation Of Interfaces......................................................................................................................................239
RendezVous Joe, The Astrologer .......................................................................................................................................239
Edge Jill, The Customer ......................................................................................................................................................241
Running The Example.........................................................................................................................................................244
Self-Service Weather Forecasting Method.........................................................................................................................244
Peer Information.........................................................................................................................................................................245
Activation Of The Peer Information Service.....................................................................................................................245
Ping...............................................................................................................................................................................................245
Example 800 – Ping..............................................................................................................................................................246
Example 810 – Pong.............................................................................................................................................................247
Running the Example..........................................................................................................................................................249
Miscellaneous..............................................................................................................................................................................249
Sending A Message With The Endpoint Service..............................................................................................................249
Customized Queries.............................................................................................................................................................249
About Registering Handlers In Services............................................................................................................................250
JXSE Shell.....................................................................................................................................................................................250
About The Future... 251
...Of P2P........................................................................................................................................................................................251
Perception vs. Facts..............................................................................................................................................................251
About Needs That Have Shaped IT Solutions – Part II.............................................................................................251
Distributed Content.............................................................................................................................................................251
CHURN.................................................................................................................................................................................252
Economical............................................................................................................................................................................252
Costs And Return On Investment...............................................................................................................................252
Potential For Profit........................................................................................................................................................254
...Of JXTA.....................................................................................................................................................................................255
Technological........................................................................................................................................................................255
Simplification Of Complexity - Universality..............................................................................................................255
New Applications..........................................................................................................................................................255
Accountability & Vendor's Credibility.......................................................................................................................255
Educational...........................................................................................................................................................................256

-8
Documentation, Books And Tutorials.........................................................................................................................256
Code Examples..............................................................................................................................................................257
The Community...................................................................................................................................................................257
The Protocols........................................................................................................................................................................258
JXTA vs. OSGi................................................................................................................................................................258
...Of JXSE......................................................................................................................................................................................258
Security..................................................................................................................................................................................258
Cache Pollution..............................................................................................................................................................259
Conclusion...................................................................................................................................................................................259
Appendix 261
Protocol Messages Types............................................................................................................................................................261
Advertisement Attributes...........................................................................................................................................................262
Peer Advertisement..............................................................................................................................................................262
Peer Group Advertisement.................................................................................................................................................262
Module Class Advertisement.............................................................................................................................................262
Module Specification Advertisement ................................................................................................................................262
Module Implementation Advertisement ..........................................................................................................................263
Pipe Advertisement.............................................................................................................................................................263
Rendezvous Advertisement................................................................................................................................................264
Route Advertisement...........................................................................................................................................................264
Access Point Advertisement...............................................................................................................................................264
Index.............................................................................................................................................................................................265

-9
Who Should Read This Book?
This book is intended for:

• JXTA early adopters – If you are one of those who adopted JXTA since its inception, this
book will help keep you up-to-date with the recent evolution of the JXTA implementation.
Contrary to many books published earlier, this book contains code examples using the
most recent features of JXSE, including those delivered as part of release 2.6.

• Functional analysts and software architects – This book covers all the JXTA concepts (in-
cluding their usage), that a functional analyst or a software architect needs to understand,
in order to design JXTA-based P2P applications. The official JXTA specification document
contains several abstract terms referencing each other, together with a lot of technical lan-
guage, making it hard to understand by everyone. This book is solving this entanglement
by using many metaphors from the real world.

• Software developers – This book will explain altogether the basic concepts of JXTA and
how to use JXSE, the Java implementation of JXTA, to develop your own P2P applica-
tions. It contains several basic code examples explaining how to use JXTA, step-by-step.
These were designed to be as didactic as possible, that is, without optimization, to facilit-
ate the learning. You will be able to take inspiration from these building blocks for your
own software implementations.

• System and network administrators – The JXTA technology is abstract in its concepts, but
it relies heavily on network transportation. Many transportation protocols can be imple-
mented with JXTA, although TCP/IP and HTTP are the most common. Nearly all imple-
mentations of JXTA require mechanisms to overcome firewalls, NATs and proxies bound-
aries. This book explains how administrators can improve the overall performance of JX-
TA-based applications using an adequate configuration of their systems, and without com-
promising security policies.

• Managers and decision makers – If you are a manager or a decision maker, you may
have heard about peer-to-peer technologies. You may even have used them for chatting,
phoning over the Internet, or on your mobile phone and hand-held devices, and would like
to know more about them. This book explains the origin of P2P and demystifies the un-
deserved negative publicity it received at the beginning of the decade. It describes the be-
nefits of P2P, how to take advantage of it. Security and control access issues to copyright

Foreword Who Should Read This Book? - 14


protected documents are also discussed here.

This book does not explain how to implement JXTA. However, it can be used as a base to under-
stand JXTA. Developers willing to implement the JXTA protocols will have to refer to the official
specification documentation available online at https://jxta-spec.dev.java.net/ for technical details.

Prerequisites
This book assumes that software developers know about the Java programming language and
concepts such as URI (Uniform Resource Identifiers), URL (Uniform Resource Locater) and URN
(Uniform Resource Name). They should master the event firing and even listening mechanism.
Functional analysts and software architects need to know about XML, TCP/IP and HTTP, in gen-
eral. System and network administrators should be aware of how TCP/IP, firewalls, NATs, proxies,
routers and other types of network transportation systems operate in general. Managers and de-
cision makers do not need to know more than general IT concepts.

The code examples provided in this book are relying on version 2.6 of JXSE, which requires JDK
1.5 (or a higher version).

Foreword Prerequisites - 15
13. Press the OK button of the 'waiting for peers to connect' of the rendezvous to which Chi-
hiro is connected.

14. The corresponding rendezvous displays the peer connected to it (i.e., Chihiro). Click OK.

15. Stop the JXTA network for that rendezvous peer.

16. Go back to back to Chihiro and click OK of the windows indicating that she is waiting for
the shutdown of the rendezvous peer.

17. A window indicating that Chihiro is going to wait for 60 seconds to let the cache list of con-
nected peers refresh is displayed. Click OK.

18. After 60 seconds, Chihiro displays another window indicating that she is ready to wait for
another rendezvous connection again. Click OK.

19. Relatively quickly, Chihiro indicates that she is now connected to the other rendezvous
still running. Click OK and shut down the JXTA network for her.

20. Back to the remaining rendezvous peer, click on OK on the screen indicating that the ren-
dezvous is waiting for peers to connect.

21. The remaining rendezvous indicates that Chihiro is now connected to it.

22. Click OK and shut down the JXTA network for the remaining peer.

We can notice that with a bit of patience Chihiro, who was connected to one of its seed rendez-
vous, has managed to reconnect to another rendezvous seed when her initial rendezvous went
down. This is performed automatically by JXSE. Edge peers try to maintain contact with at least
and at most one rendezvous peer over time. They check their rendezvous connections every 15
56
seconds .

Metaphorically: The Jack tribe, the Aminah tribe and the Chihiro tribe are all living on the same big
island, but they cannot establish contact with each other by land because of insurmountable
mountains. Chihiro has heard about the Jack and the Aminah tribes. She establishes contact with
one of them, but after some time, that tribe decides to go hide in its cave. Since Chihiro does not
want to be alone, she automatically establishes contact with the third tribe.

Connectivity Manager
This book revision contains a new example explaining how to implement a connectivity manager.

56 Unfortunately, unexpected loss of connections are not always detected and handled 'quickly' by JXSE. This a know issue. Solving it
requires much refactoring in core code. This could not be accomplished for release 2.6 and has been postponed to a further release.

Using JXSE Exploring Connectivity Issues - 179


It is made of 5 java classes, but we will not include all code here:

• ConnectivityMonitor – This is a frame displaying the connectivity status of peers.

• DelayedJxtaNetworkStopper – Allows an asynchronous stop of the JXTA network.

• Edge_Gina – A simple EDGE peer. A possibility to enable TCP and HTTP communica-
tion or not is provided.

• Relay_Robert – A simple RELAY peer.

• RendezVous_Mya – A simple RENDEZVOUS peer.

Robert, The Relay


The following is the code used for Robert, a relay peer:

...

public class Relay_Robert {

// Static

public static final String Name_RELAY = "RELAY";


public static final PeerID PID_RELAY = IDFactory.newPeerID(PeerGroupID.defaultNetPeerGroupID,
Name_RELAY.getBytes());
public static final int HttpPort_RELAY = 9900;
public static final int TcpPort_RELAY = 9715;
public static final File ConfigurationFile_RELAY = new File("."
+ System.getProperty("file.separator") + Name_RELAY);

public static void main(String[] args) {

try {

Using JXSE Exploring Connectivity Issues - 180


// Removing any existing configuration?
NetworkManager.RecursiveDelete(ConfigurationFile_RELAY);

// Creation of the network manager


final NetworkManager MyNetworkManager = new NetworkManager(
NetworkManager.ConfigMode.RELAY,
Name_RELAY, ConfigurationFile_RELAY.toURI());

// Retrieving the network configurator


NetworkConfigurator MyNetworkConfigurator = MyNetworkManager.getConfigurator();

// Setting Configuration
MyNetworkConfigurator.setUseMulticast(false);

MyNetworkConfigurator.setTcpPort(TcpPort_RELAY);
MyNetworkConfigurator.setTcpEnabled(true);
MyNetworkConfigurator.setTcpIncoming(true);
MyNetworkConfigurator.setTcpOutgoing(true);

MyNetworkConfigurator.setHttpPort(HttpPort_RELAY);
MyNetworkConfigurator.setHttpEnabled(true);
MyNetworkConfigurator.setHttpIncoming(true);
MyNetworkConfigurator.setHttpOutgoing(true);

// Setting the Peer ID


MyNetworkConfigurator.setPeerID(PID_RELAY);

// Starting the JXTA network


PeerGroup NetPeerGroup = MyNetworkManager.startNetwork();

// Starting the connectivity monitor


new ConnectivityMonitor(NetPeerGroup);

// Stopping the network asynchronously


ConnectivityMonitor.TheExecutor.schedule(
new DelayedJxtaNetworkStopper(
MyNetworkManager,
"Click to stop " + Name_RELAY,
"Stop"),
0,
TimeUnit.SECONDS);

} catch (IOException Ex) {

System.err.println(Ex.toString());

} catch (PeerGroupException Ex) {

System.err.println(Ex.toString());

Robert the Relay

1. Any existing configuration is deleted.

2. The network manager instance and corresponding network configurator are created.

3. Multicasting is disabled, while TCP and HTTP communication are enabled (we will explain
why later).

4. A peer ID is set.

Using JXSE Exploring Connectivity Issues - 181


5. The connectivity monitor frame is set and started. It will start the JXTA network.

6. An asynchronous task is created to asynchronously stop the JXTA network.

By using Robert, Mya and Gina, one can test various connectivity scenarios with the connectivity
manager.

Relay Connectivity
57
A connection to a relay peer can be established either via TCP or HTTP . This can be checked
with edge Gina by enabling/disabling TCP and HTTP communication and by using Robert and
Mya. Edge peers loop through their seeds of relay to try to maintain a permanent connection to
their a relay peer.

One may wonder when HTTP communication would be needed if TCP can be used by peers hav-
ing a private address, to connect to relay peers. Many corporations only allow outbound HTTP
traffic and block other ports and TCP traffic. In this case, HTTP becomes necessary.

Summary
The following table indicates which type of communication should be enabled according to the
connection mode:

Configuration ADHOC EDGE RENDEZVOUS RELAY


TCP outgoing Not required Preferably Yes If possible
TCP incoming Not required Not essential Yes Required
HTTP outgoing Not required Preferably No required Not required
HTTP incoming Not required Not essential Not required Required
Multicasting Required Preferably Preferably If possible

• It does not make sense to configure an edge with no outgoing communication enabled.
58
• If an edge is configured only with multicasting, make it an adhoc to operate better .

• A relay peer should have either TCP incoming or HTTP incoming allowed. Otherwise,
edge peers will not be able to connect to it.

• Peers on the same LAN do not need rendezvous to connect if they all have multicasting
enabled.

57 In version 2.5 of the Programmer's Guide, it is mentioned that “If you are located behind a firewall or NAT, you must use HTTP and
specify a relay node“. This is not correct.
58 An edge expects availability of TCP to connect to a rendezvous.

Using JXSE Exploring Connectivity Issues - 182

También podría gustarte