Chapter 28 PeertoPeer Applications and JXTA Outline 28

  • Slides: 65
Download presentation
Chapter 28: Peer-to-Peer Applications and JXTA Outline 28. 1 28. 2 28. 3 28.

Chapter 28: Peer-to-Peer Applications and JXTA Outline 28. 1 28. 2 28. 3 28. 4 28. 5 28. 6 28. 7 28. 8 28. 9 28. 10 28. 11 28. 12 28. 13 Introduction Client/Server and Peer-to-Peer Applications Centralized vs. Decentralized Network Applications Peer Discovery and Searching Case Study: Deitel Instant Messenger Defining the Service Interface Defining the Service implementation Registering the Service Find Other Peers Compiling and Running the Example Improving Deitel Instant Messenger with Multicast Sockets 28. 12. 1 Registering the Peer 28. 12. 2 Finding Other Peers Introduction to JXTA 2002 Prentice Hall. All rights reserved.

28. 1 Introduction • Peer-to-peer (P 2 P) application – Each node performs both

28. 1 Introduction • Peer-to-peer (P 2 P) application – Each node performs both client and server functions – Distribution of processing responsibilities 2002 Prentice Hall. All rights reserved.

28. 2 Client/Server and Peer-to-Peer Applications • Client/Server application – e. g. , Yahoo!

28. 2 Client/Server and Peer-to-Peer Applications • Client/Server application – e. g. , Yahoo! search engine – Server offers common stores of programs and data – Client access the data provided by servers • Peer-to-peer (P 2 P) application – All computers act as both client and server 2002 Prentice Hall. All rights reserved.

28. 2 Client/Server and Peer-to-Peer Applications (cont. ) 2002 Prentice Hall. All rights reserved.

28. 2 Client/Server and Peer-to-Peer Applications (cont. ) 2002 Prentice Hall. All rights reserved.

28. 3 Centralized vs. Decentralized Network Applications • Centralized server – Weakness: dependency on

28. 3 Centralized vs. Decentralized Network Applications • Centralized server – Weakness: dependency on central server • If central server fails, so does the entire application • Decentralized server – True P 2 P application – No dependency on single node • If one node fails, the application can exist 2002 Prentice Hall. All rights reserved.

28. 4 Peer Discovery and Searching • Peer discovery – Finding peers in P

28. 4 Peer Discovery and Searching • Peer discovery – Finding peers in P 2 P application – Decentralizing an application often slows peer discovery • Distributed searching – Peer sends search criteria to several nearby peers – Searching performed by several systems 2002 Prentice Hall. All rights reserved.

28. 5 Case Study: Deitel Instant Messenger • Deitel Instant Messenger – P 2

28. 5 Case Study: Deitel Instant Messenger • Deitel Instant Messenger – P 2 P application – Uses Jini to bootstrap (register) users onto the P 2 P network 2002 Prentice Hall. All rights reserved.

28. 5 Case Study: Deitel Instant Messenger (cont. ) Fig. 28. 2 Sample windows

28. 5 Case Study: Deitel Instant Messenger (cont. ) Fig. 28. 2 Sample windows of Deitel Instant Messenger. 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // IMService. java // IMService interface defines the methods // through which the service proxy // communicates with the service. package com. deitel. advjhtp 1. jini. IM. service; Outline // Java core packages import java. rmi. *; // Deitel packages import com. deitel. advjhtp 1. jini. IMPeer; public interface IMService extends Remote { // return RMI reference to a remote IMPeer public IMPeer connect( IMPeer sender ) throws Remote. Exception; } Fig. 28. 3 Interface Enables users to send a remote IMService specifies reference to an IMPeer how service proxy interacts with the service. Lines 16 -17 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Outline // IMPeer. java // Interface that all Peer to Peer apps must implement package com. deitel. advjhtp 1. jini. IM; //java core packages import java. rmi. *; import java. util. *; public interface IMPeer extends Remote { // posts Message to peer public void send. Message( Message message ) throws Remote. Exception; // information methods public String get. Name() throws Remote. Exception; } Specifies the interface for communicating between peers Clients send messages to peers by calling send. Message Fig. 28. 4 Interface IMPeer specifies interaction between peers. Line 9 Lines 12 -13 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // Message. java // Message represents an object that can be sent to an IMPeer; // contains the sender and content of the message. package com. deitel. advjhtp 1. jini. IM; // Java core package import java. io. Serializable; public class Message implements Serializable { private static final long Serial. Version. UID = 20010808 L; private String from; private String content; // Message constructor public Message( String message. Sender. Name, String message. Content ) { from = message. Sender. Name; content = message. Content; } // get String representation public String to. String() { return from + ": " + content + "n"; } Outline Messages must be serialized for delivery over RMI Fig. 28. 5 Class Message defines an object for sending and receiving messages between Message constructor takes (part 1). as argumentspeers the sender’s name and message content Line 9 Lines 16 -21 // get Message sender's name public String get. Sender. Name() { return from; } 2002 Prentice Hall. All rights reserved.

34 35 36 37 38 39 40 // get Message content public String get.

34 35 36 37 38 39 40 // get Message content public String get. Content() { return content; } Outline } Fig. 28. 5 Class Message defines an object for sending and receiving messages between peers (part 2). 2002 Prentice Hall. All rights reserved.

28. 7 Defining the Service Implementation • Define service implementation – IMService. Impl •

28. 7 Defining the Service Implementation • Define service implementation – IMService. Impl • Implements interface IMService • Extends Unicast. Remote. Object 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 // IMService. Impl. java // IMService. Impl implements IMService interface // is service side of IM application package com. deitel. advjhtp 1. jini. IM. service; Outline // Java core packages import java. io. *; import java. rmi. server. Unicast. Remote. Object; import java. rmi. Remote. Exception; import java. util. String. Tokenizer; // Deitel packages import com. deitel. advjhtp 1. jini. IMPeer; import com. deitel. advjhtp 1. jini. IMPeer. Impl; import com. deitel. advjhtp 1. jini. IM. Message; import com. deitel. advjhtp 1. jini. IM. client. IMPeer. Listener; public class IMService. Impl extends Unicast. Remote. Object implements IMService, Serializable { Fig. 28. 6 IMService. Impl service implementation for Define the service our case study implementation (part 1). private static final long Serial. Version. UID = 20010808 L; private String user. Name = "Anonymous"; Line 18 // IMService no-argument constructor public IMService. Impl() throws Remote. Exception{} Lines 28 -31 // IMService constructor takes user. Name public IMService. Impl( String name ) throws Remote. Exception { user. Name = name; } Second constructor takes as a String argument the user’s name, which appears in the Peer. List window 2002 Prentice Hall. All rights reserved.

33 34 35 36 37 38 39 40 41 42 43 44 45 46

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 Outline // sets service s user. Name public void set. User. Name( String name ) { user. Name = name; } // return RMI reference to an IMPeer on the receiver side public IMPeer connect( IMPeer sender ) throws Remote. Exception { // Make a GUI and IMPeer. Impl to be sent to remote peer IMPeer. Listener listener = new IMPeer. Listener( user. Name ); IMPeer. Impl me = new IMPeer. Impl( user. Name ); me. add. Listener( listener ); // add remote peer to my GUI listener. add. Peer( sender ); //send my IMPeer. Impl to him return me; } } // end method connect First peer sends reference of itself to second peer by invoking IMService method connect Fig. 28. 6 IMService. Impl Second Peer stores first service peer’simplementation reference for usefor when our conversation starts case study (part 2). Line 40 Second peer returns reference of itself to first peer Line 51 Line 54 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // IMPeer. Listener. java // IMPeer. Listener extends JFrame and provides GUI for // conversations with other peers package com. deitel. advjhtp 1. jini. IM. client; Outline // Java core packages import java. awt. *; import java. awt. event. *; import java. rmi. Remote. Exception; // Java extension packages import javax. swing. *; import javax. swing. text. *; import javax. swing. border. *; // Deitel Packages import com. deitel. advjhtp 1. jini. IMPeer; import com. deitel. advjhtp 1. jini. IM. Message; public class IMPeer. Listener extends JFrame { // JText. Areas for displaying and inputting messages private JText. Area message. Area; private JText. Area input. Area; Fig. 28. 7 IMPeer. Listener is the GUI that starts communication GUIpeer for starting peer (part 1). communication Line 20 // Actions for sending messages, etc. private Action send. Action; // user. Name to add to outgoing messages private String user. Name = ""; // IMPeer to send messages to peer private IMPeer remote. Peer; 2002 Prentice Hall. All rights reserved.

35 36 37 38 39 40 41 42 43 44 45 46 47 48

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 // constructor public IMPeer. Listener( String name ) { super( "Conversation Window" ); Outline // set user name user. Name = name; // init send. Action = new Send. Action(); // create JText. Area for displaying messages message. Area = new JText. Area( 15, 15 ); // disable editing and wrap words at end of line message. Area. set. Editable( false ); message. Area. set. Line. Wrap( true ); message. Area. set. Wrap. Style. Word( true ); JPanel panel = new JPanel(); panel. set. Layout( new Border. Layout( 5, 5 ) ); panel. add( new JScroll. Pane( message. Area ), Border. Layout. CENTER ); Fig. 28. 7 IMPeer. Listener is the GUI that starts peer communication (part 2). // create JText. Area for entering new messages input. Area = new JText. Area( 4, 12 ); input. Area. set. Line. Wrap( true ); input. Area. set. Wrap. Style. Word( true ); // map Enter key in input. Area area to send. Action Keymap key. Map = input. Area. get. Keymap(); Key. Stroke enter. Key = Key. Stroke. get. Key. Stroke( Key. Event. VK_ENTER, 0 ); key. Map. add. Action. For. Key. Stroke( enter. Key, send. Action ); 2002 Prentice Hall. All rights reserved.

70 71 72 73 74 75 76 77 78 79 80 81 82 83

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 // lay out input. Area and send. Action JButton in Box box = Box. create. Vertical. Box(); box. add( new JScroll. Pane( input. Area ) ); box. add( new JButton( send. Action ) ); Outline panel. add( box, Border. Layout. SOUTH ); // lay out components Container container = get. Content. Pane(); container. add( panel, Border. Layout. CENTER ); set. Size( 200, 400 ); set. Visible( true ); } // Action for sending messages private class Send. Action extends Abstract. Action { // configure Send. Action public Send. Action() { put. Value( Action. NAME, "Send" ); put. Value( Action. SHORT_DESCRIPTION, "Send Message" ); put. Value( Action. LONG_DESCRIPTION, "Send an Instant Message" ); } // send message and clear input. Area public void action. Performed( Action. Event event ) { // send message to server try { Message message = new Message( user. Name, input. Area. get. Text() ); Fig. 28. 7 IMPeer. Listener is the GUI that starts peer communication (part 3). 2002 Prentice Hall. All rights reserved.

105 106 107 108 109 110 111 112 113 114 115 116 117 118

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 Outline // use RMI reference to send a Message remote. Peer. send. Message( message ); // clear input. Area. set. Text( "" ); display. Message ( message ); When user presses JButton, call method send. Message of remote peer } // catch error sending message catch( Remote. Exception remote. Exception ) { JOption. Pane. show. Message. Dialog( null, "Unable to send message. " ); remote. Exception. print. Stack. Trace(); } } // end method action. Performed } // end send. Action inner class public void display. Message( Message message ) { // display. Message uses Swing. Untilities. invoke. Later // to ensure thread-safe access to message. Area Swing. Utilities. invoke. Later( new Message. Displayer( message. get. Sender. Name(), message. get. Content() ) ); } // Message. Displayer displays a new message by appending // the message to the message. Area JText. Area. This Runnable // object should be executed only on the event-dispatch // thread, as it modifies a live Swing component. private class Message. Displayer implements Runnable { Fig. 28. 7 IMPeer. Listener is the GUI that starts peer communication (part 4). Line 107 2002 Prentice Hall. All rights reserved.

140 141 142 143 144 145 146 147 148 149 150 151 152 153

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 private String from. User; private String message. Body; Outline // Message. Displayer constructor public Message. Displayer( String from, String body ) { from. User = from; message. Body = body; } // display new message in message. Area public void run() { // append new message. Area. append( "n" + from. User + "> " + message. Body ); // move caret to end of message. Area to ensure new // message is visible on screen message. Area. set. Caret. Position( message. Area. get. Text(). length() ); } } // change title of window to name of peer set. Title( remote. Peer. get. Name() ); } Lines 167 -173 // end Message. Displayer inner class // add. Peer takes IMPeer as arg // associates IMPeer with send. Action to send messages public void add. Peer( IMPeer peer ) throws Remote. Exception { remote. Peer = peer; } Fig. 28. 7 IMPeer. Listener is the GUI that starts peer communication (part 5). Stores reference to remote IMPeer and set title of conversation window to the name of remote IMPeer 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // IMPeer. Impl. java // Implements the IMPeer interface package com. deitel. advjhtp 1. jini. IM; Outline // Java core packages import java. io. *; import java. net. *; import java. rmi. server. *; import java. util. *; // Deitel Packages import com. deitel. advjhtp 1. jini. IM. Message; import com. deitel. advjhtp 1. jini. IM. client. IMPeer. Listener; public class IMPeer. Impl extends Unicast. Remote. Object implements IMPeer { private String name; private IMPeer. Listener output; // No argument constructor public IMPeer. Impl() throws Remote. Exception { super(); name = "anonymous"; } // constructor takes user. Name public IMPeer. Impl( String my. Name ) throws Remote. Exception { name = my. Name; } Fig. 28. 8 Class IMPeer. Impl Define the peer is the IMPeer implementation (part 1). Line 17 2002 Prentice Hall. All rights reserved.

35 36 37 38 39 40 41 42 43 44 45 46 47 48

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public void add. Listener( IMPeer. Listener listener ) { output = listener; } // send message to this peer public void send. Message( Message message ) throws Remote. Exception { output. display. Message( message ); } // accessor for name public String get. Name() throws Remote. Exception { return name; } } Add an object of type. Outline IMPeer. Listener that will display the IMPeer. Impl’s actions Override method send. Message to send message to peer Fig. 28. 8 Class IMPeer. Impl is the IMPeer implementation (part 2). Lines 35 -38 Lines 41 -45 2002 Prentice Hall. All rights reserved.

28. 8 Registering the Service • Register (bootstrap) service with peer group 2002 Prentice

28. 8 Registering the Service • Register (bootstrap) service with peer group 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // IMService. Manager. java // IMService. Manager uses Join. Manager to find Lookup services, // registers the IMService with the Lookup services, // manages lease renewal package com. deitel. advjhtp 1. jini. IM; Outline // Java core packages import java. rmi. RMISecurity. Manager; import java. rmi. Remote. Exception; import java. io. IOException; // Jini core packages import net. jini. core. lookup. Service. ID; import net. jini. core. entry. Entry; // Jini extension packages import net. jini. lookup. entry. Name; import net. jini. lease. Lease. Renewal. Manager; import net. jini. lookup. Join. Manager; import net. jini. discovery. Lookup. Discovery. Manager; import net. jini. lookup. Service. IDListener; // Deitel packages import com. deitel. advjhtp 1. jini. IM. service. *; Fig. 28. 9 Class IMService. Manager registers IMService. Impl with lookup services (part 1). Line 33 public class IMService. Manager implements Service. IDListener { Join. Manager manager; Lookup. Discovery. Manager lookup. Manager ; String service. Name; // constructor takes name of the service public IMService. Manager( String screen. Name ) { System. set. Security. Manager( new RMISecurity. Manager() ); Constructor takes a String that specifies the Name Entry for the service 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 Outline // sets the service. Name of this service. Name = screen. Name; // use Join. Manager to register Seminar. Info service // and manage lease try { // create Lookup. Discovery. Manager for discovering // lookup services lookup. Manager = new Lookup. Discovery. Manager( new String[] { "" }, null ); // create and set Entry name for service // name used from constructor Entry[] entries = new Entry[ 1 ]; entries[ 0 ] = new Name( service. Name ); // create Join. Manager manager = new Join. Manager( create. Proxy(), entries, this, lookup. Manager, new Lease. Renewal. Manager() ); } Fig. 28. 9 Class IMService. Manager registers IMService. Impl with lookup services Use Jini’s Join. Manager 2). the service class to (part register with all known lookup services Line 56 // handle exception creating Join. Manager catch ( IOException exception ) { exception. print. Stack. Trace(); } } // end Seminar. Info. Join. Service constructor // return the Lookup. Discovery. Manager created by Join. Manager public Lookup. Discovery. Manager get. Discovery. Manager () { return lookup. Manager; 2002 Prentice Hall. All rights reserved.

71 72 73 74 75 76 77 78 79 80 81 82 83 84

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 Outline } // create service proxy private IMService create. Proxy() { // get Seminar. Proxy for Seminar. Info service try { return( new IMService. Impl( service. Name ) ); } // handle exception creating Seminar. Proxy catch ( Remote. Exception exception ) { exception. print. Stack. Trace(); } return null; } // end method create. Proxy // receive notification of Service. ID assignment public void service. IDNotify( Service. ID service. ID ) { System. err. println( "Service ID: " + service. ID ); } Fig. 28. 9 Class IMService. Manager registers IMService. Impl with lookup services (part 3). // informs all lookup services that service is ending public void logout() { manager. terminate(); } } 2002 Prentice Hall. All rights reserved.

28. 9 Find Other Peers • Create GUI enabling user to find other peers

28. 9 Find Other Peers • Create GUI enabling user to find other peers 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Peer. List. java // Initializes Service. Manager, starts service discovery // and lists all IM services in a window package com. deitel. advjhtp 1. jini. IM; Outline // Java core packages import java. awt. *; import java. awt. event. *; import java. net. Malformed. URLException; import java. util. *; import java. util. List; import java. io. IOException; import java. rmi. *; // Java extension packages import javax. swing. *; import javax. swing. event. *; // Jini core packages import net. jini. core. lookup. Service. Item; import net. jini. core. lookup. Service. Template; import net. jini. lookup. *; import net. jini. discovery. Lookup. Discovery. Manager; import net. jini. lease. Lease. Renewal. Manager; import net. jini. lookup. entry. Name; import net. jini. core. entry. Entry; import net. jini. core. discovery. Lookup. Locator; Fig. 28. 10 Class Peer. List is the GUI for finding peers (part 1). Lines 33 -34 // Deitel Packages import com. deitel. advjhtp 1. jini. IM. service. IMService; import com. deitel. advjhtp 1. jini. IM. client. IMPeer. Listener; public class Peer. List extends JFrame implements Service. Discovery. Listener { Peer. List is the main window of the Deitel Instant Messenger 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 private private Default. List. Model peers; JList peer. List; List service. Items; Service. Discovery. Manager service. Discovery. Manager ; Lookup. Cache cache; IMService. Manager my. Manager ; Lookup. Discovery. Manager lookup. Discovery. Manager ; Outline // initialize user. Name to anonymous private String user. Name = "anonymous"; // method called when Service. Discovery. Manager finds // IM service adds service proxy to service. Items // adds Service name to List. Model for JList public void service. Added( Service. Discovery. Event event ) { // get added service. Item Service. Item item = event. get. Post. Event. Service. Item(); Entry attributes[] = item. attribute. Sets; // iterates through attributes to find name for( int i = 0; i < attributes. length; i++ ) if ( attributes[ i ] instanceof Name ) { System. out. println( "Added: " + item ); service. Items. add( item. service ); peers. add. Element( ( ( Name )attributes[ i ] ). name ); break; } } // end method service. Added // empty method ignores sevice. Changed event public void service. Changed( Service. Discovery. Event event ) {} Fig. 28. 10 Class Obtain a Peer. List Service. Item that is the GUI represents addedpeers service forthe finding (part 2). Line 53 Lines 57 -65 Add service proxy to List and add Name to Default. List. Model if Name entry 2002 Prentice Hall. All rights reserved.

71 72 73 74 75 76 77 78 79 80 81 82 83 84

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 // removes services from Peer. List GUI and data structure // when service. Removed event occurs public void service. Removed( Service. Discovery. Event event ) { // get. Pre. Event because item has been removed // get. Post. Event would return null Service. Item item = event. get. Pre. Event. Service. Item(); Entry attributes[ ] = item. attribute. Sets; Outline // debug System. out. println( "Remove Event!" ); // remove from arraylist and Default. List. Model int index = service. Items. index. Of( item. service ); // print name of person removed if ( index >= 0 ) { System. out. println( "Removing from List: " + service. Items. remove( index )); Fig. 28. 10 Class Peer. List is the GUI for finding peers (part 3). System. out. println( "Removing from Def. List" + peers. element. At( index ) ); peers. remove. Element. At( index ); } } // end method Service. Removed // constructor public Peer. List() { super( "Peer List" ); System. set. Security. Manager( new RMISecurity. Manager() ); 2002 Prentice Hall. All rights reserved.

106 107 108 109 110 111 112 113 114 115 116 117 118 119

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 // get desired user. Name = JOption. Pane. show. Input. Dialog( Peer. List. this, "Please enter your name: " ); Outline // change title of window set. Title( user. Name + "'s Peer List Window" ); // Init Lists service. Items = new Array. List(); Container container = get. Content. Pane(); peers = new Default. List. Model(); // init components peer. List = new JList( peers ); peer. List. set. Visible. Row. Count( 5 ); JButton connect. Button = new JButton( "Connect" ); // do not allow multiple selections peer. List. set. Selection. Mode( List. Selection. Model. SINGLE_SELECTION ); Fig. 28. 10 Class Peer. List is the GUI for finding peers (part 4). Lines 135 -138 // set up event handler for connect. Button. add. Action. Listener( new Action. Listener() { public void action. Performed( Action. Event event ) { int item. Index = peer. List. get. Selected. Index(); Object selected. Service = service. Items. get( item. Index ); IMService peer. Proxy = ( IMService )selected. Service; Use index of selected JList item to retrieve IMService proxy 2002 Prentice Hall. All rights reserved.

141 142 143 144 145 146 147 148 149 150 151 152 153 154

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 // send info to remote peer // get RMI reference try { // set up gui and my peer. Impl IMPeer. Listener gui = new IMPeer. Listener( user. Name ); IMPeer. Impl me = new IMPeer. Impl( user. Name ); me. add. Listener( gui ); // Connect my. Gui to remote IMPeer object IMPeer my. Peer = peer. Proxy. connect( me ); gui. add. Peer( my. Peer ); } // connecting may cause Remote. Exception catch( Remote. Exception re ) { JOption. Pane. show. Message. Dialog ( null, "Couldn't Connect" ); re. print. Stack. Trace(); } } Outline Create IMPeer. Listener for IMPeer. Impl Fig. 28. 10 Class IMPeer. Impl will post all is the GUI messages. Peer. List sent by remote peer for finding peers to IMPeer. Listener (part 5). Lines 147 -151 Lines 154 -155 } ); // end connect. Button action. Listener // set up File menu JMenu file. Menu = new JMenu( "File" ); file. Menu. set. Mnemonic( 'F' ); 2002 Prentice Hall. All rights reserved.

172 173 174 175 176 177 178 179 180 181 182 183 184 185

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 // about Item JMenu. Item about. Item = new JMenu. Item( "About. . . " ); about. Item. set. Mnemonic( 'A' ); about. Item. add. Action. Listener( new Action. Listener() { public void action. Performed( Action. Event event ) { JOption. Pane. show. Message. Dialog( Peer. List. this, "Deitel Instant Messenger" , "About", JOption. Pane. PLAIN_MESSAGE ); } } ); file. Menu. add( about. Item ); // Add. Locator item JMenu. Item federate. Item = new JMenu. Item( "Add Locators" ); federate. Item. set. Mnemonic( 'L' ); federate. Item. add. Action. Listener( Fig. 28. 10 Class Peer. List is the GUI for finding peers Create dialog box to prompt user (part 6). URL for Jini-lookup-service new Action. Listener() { public void action. Performed( Action. Event event ) { // get Lookup. Service url to be added String locator = JOption. Pane. show. Input. Dialog( Peer. List. this, "Please enter locator in this" + "form: jini: //host: port/" ); try { Lookup. Locator new. Locator = new Lookup. Locator( locator ); Outline Lines 189 -223 2002 Prentice Hall. All rights reserved.

207 208 209 210 211 212 213 214 215 216 217 218 219 220

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 Outline // make one element Lookup. Locator array Lookup. Locator [] locators = { new. Locator }; // because add. Locators takes array lookup. Discovery. Manager. add. Locators( locators ); } catch( Malformed. URLException url. Exception ) { JOption. Pane. show. Message. Dialog( Peer. List. this, "invalid url" ); } } } ); file. Menu. add( federate. Item ); // set up JMenu. Bar and attach File menu JMenu. Bar menu. Bar = new JMenu. Bar(); menu. Bar. add ( file. Menu ); set. JMenu. Bar( menu. Bar ); Client registers with newly added lookup service; Fig. 28. 10 Class Peer. List window lists Peer. List is the GUI all other peers registered for finding peers with new lookup service (part 7). Line 212 // handow window closing event add. Window. Listener( new Window. Adapter(){ public void window. Closing( Window. Event w ) { System. out. println( "CLOSING WINDOW" ); // disconnects from lookup services my. Manager. logout(); System. exit( 0 ); } } 2002 Prentice Hall. All rights reserved.

242 243 244 245 246 247 248 249 250 251 252 253 254 255

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 Outline ); // lay out GUI components peer. List. set. Fixed. Cell. Width( 100 ); JPanel input. Panel = new JPanel(); input. Panel. add( connect. Button ); container. add( new JScroll. Pane( peer. List ) , Border. Layout. NORTH ); container. add( input. Panel, Border. Layout. SOUTH ); set. Size( 100, 170 ); set. Visible( true ); // peer list displays only other IMServices Class[] types = new Class[] { IMService. class }; Service. Template IMTemplate = new Service. Template( null, types, null ); Fig. 28. 10 Class Create the IMService. Manager, Peer. List is the GUI using user. Name to namepeers the peer for finding // Initialize IMService. Manager, Service. Discovery. Manager try { my. Manager = new IMService. Manager( user. Name ); (part 8). Line 263 // store Lookup. Discovery. Manager // generated by IMService. Manager lookup. Discovery. Manager = my. Manager. get. Discovery. Manager(); // Service. Discovery. Manager uses lookup. Discovery. Manager service. Discovery. Manager = new Service. Discovery. Manager ( lookup. Discovery. Manager, null ); // create a Lookup. Cache cache = service. Discovery. Manager. create. Lookup. Cache( IMTemplate, null, this ); 2002 Prentice Hall. All rights reserved.

277 278 279 280 281 282 283 284 285 286 287 288 289 290

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 } Outline // catch all exceptions and inform user of problem catch ( Exception manager. Exception) { JOption. Pane. show. Message. Dialog( null, "Error initializing IMService. Manger" + "or Service. Disovery. Manager" ); manager. Exception. print. Stack. Trace(); } } public static void main( String args[] ) { new Peer. List(); } } Fig. 28. 10 Class Peer. List is the GUI for finding peers (part 9). 2002 Prentice Hall. All rights reserved.

28. 10 Compiling and Running the Example • • • 1. Compile classes 2.

28. 10 Compiling and Running the Example • • • 1. Compile classes 2. Compile remote classes via rmic 3. Start rmid 4. Start HTTP server 5. Start lookup service 2002 Prentice Hall. All rights reserved.

28. 11 Improving Deitel Instant Messenger • Limitations – Not secure – Not scalable

28. 11 Improving Deitel Instant Messenger • Limitations – Not secure – Not scalable • Solutions – Filters • limit the number of service proxies that Service. Discovery. Manager downloads – Distributed searches • Facilitate locating peers 2002 Prentice Hall. All rights reserved.

28. 12 Deitel Instant Messenger with Multicast Sockets • True P 2 P application

28. 12 Deitel Instant Messenger with Multicast Sockets • True P 2 P application – Each node must run lookup service • Requires significant amounts of memory and processor time – Solution: • Use Multicast sockets – Advertise and find peers in network 2002 Prentice Hall. All rights reserved.

28. 12. 1 Registering the Peer • Jini implementation – provided mechanism to register

28. 12. 1 Registering the Peer • Jini implementation – provided mechanism to register and find peers – We must implement this mechanism via multicast sockets 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Multicast. Sending. Thread. java // Sends a multicast periodically containing a remote reference // to the IMService. Impl object package com. deitel. advjhtp 1. p 2 p; Outline // Java core packages import java. net. Multicast. Socket; import java. net. *; import java. rmi. registry. *; import java. io. *; // Deitel core packages import com. deitel. advjhtp 1. jini. IM. service. IMService. Impl; import com. deitel. advjhtp 1. jini. IM. service. IMService; public class Multicast. Sending. Thread extends Thread implements IMConstants { // Inet. Address of group for messages private Inet. Address multicast. Net. Address ; // Multicast. Socket for multicasting messages private Multicast. Socket multicast. Socket ; Fig. 28. 11 Multicast. Sending. Thread multicasts. Thread a peer’s broadcasts presence Datagram. Packets (part 1). Line 17 // Datagram packet to be reused private Datagram. Packet multicast. Packet ; // stub of local peer private IMService peer. Stub; // flag for terminating Multicast. Sending. Thread private boolean keep. Sending = true; private String user. Name; 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 Outline // Multicast. Sending. Thread constructor public Multicast. Sending. Thread( String my. Name ) { // invoke superclass constructor to name Thread super( "Multicast. Sending. Thread" ); user. Name = my. Name; // create a registry on default port 1099 try { Registry registry = Locate. Registry. create. Registry( 1099 ); peer. Stub = new IMService. Impl( user. Name ); registry. rebind( BINDING_NAME, peer. Stub ); } catch ( Remote. Exception remote. Exception ) { remote. Exception. print. Stack. Trace(); } try { Call create. Registry Fig. 28. 11 to instantiate an RMI Multicast. Sending registry on port broadcasts 1099 Thread Datagram. Packets (part 2). Rebind IMService. Impl to RMI registry Lines 47 -48 Line 49 // create Multicast. Socket for sending messages multicast. Socket = new Multicast. Socket ( MULTICAST_SENDING_PORT ); // set TTL for Multicast Socket multicast. Socket. set. Time. To. Live( MULTICAST_TTL ); Lines 59 -60 Instantiate Multicast. Socket for sending messages to peers // use Inet. Address reserved for multicast group multicast. Net. Address = Inet. Address. get. By. Name( MULTICAST_ADDRESS ); // create greeting packet String greeting = new String( HELLO_HEADER + user. Name ); 2002 Prentice Hall. All rights reserved.

71 72 73 74 75 76 77 78 79 80 81 82 83 84

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 multicast. Packet = new Datagram. Packet( greeting. get. Bytes(), greeting. get. Bytes(). length, multicast. Net. Address , MULTICAST_LISTENING_PORT ); Outline Create message packet containing peer’s name } // MULTICAST_ADDRESS IS UNKNOWN HOST catch ( java. net. Unknown. Host. Exception unknown. Host. Exception ) { System. err. println( "MULTICAST_ADDRESS is unknown" ); unknown. Host. Exception. print. Stack. Trace(); } // any other exception catch ( Exception exception ) { exception. print. Stack. Trace(); } } // deliver greeting message to peers public void run() { while ( keep. Sending ) { Fig. 28. 11 Multicast. Sending Thread broadcasts Datagram. Packets (part 3). Lines 72 -74 Lines 100 // deliver greeting try { // send greeting packet multicast. Socket. send( multicast. Packet ); Send message via Multicast. Socket Thread. sleep( MULTICAST_INTERVAL ); } 2002 Prentice Hall. All rights reserved.

105 106 107 108 109 110 111 112 113 114 115 116 117 118

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 // handle exception delivering message catch ( IOException io. Exception ) { io. Exception. print. Stack. Trace(); continue; } catch ( Interrupted. Exception interrupted. Exception ) { interrupted. Exception. print. Stack. Trace(); } Outline } // end while multicast. Socket. close(); } // end method run // send goodbye message public void logout() { String goodbye = new String( GOODBYE_HEADER + user. Name System. out. println( goodbye ); multicast. Packet = new Datagram. Packet( goodbye. get. Bytes(), goodbye. get. Bytes(). length, multicast. Net. Address , MULTICAST_LISTENING_PORT ); Fig. 28. 11 Multicast. Sending Thread broadcasts Datagram. Packets ); (part 4). Send message indicating that 123 -130 the peer. Lines is leaving network try { multicast. Socket. send( multicast. Packet ); Naming. unbind( BINDING_NAME ); } // error multicasting catch ( IOException io. Exception ) { System. err. println( "Could not Say Goodbye“ ); io. Exception. print. Stack. Trace(); } 2002 Prentice Hall. All rights reserved.

140 141 142 143 144 145 146 147 148 149 // unbinding may cause

140 141 142 143 144 145 146 147 148 149 // unbinding may cause many possible exceptions catch ( Exception unbinding. Exception ) { unbinding. Exception. print. Stack. Trace(); } Outline keep. Sending = false; } } Fig. 28. 11 Multicast. Sending Thread broadcasts Datagram. Packets (part 5). 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // IMConstants. java // contains constants used by IM application package com. deitel. advjhtp 1. p 2 p; Outline public interface IMConstants { public static final String MULTICAST_ADDRESS = "228. 5. 6. 10"; public static final int MULTICAST_TTL = 30; // port on local machine for broadcasting public static final int MULTICAST_SENDING_PORT = 6800; // port on local machine for receiving broadcasts public static final int MULTICAST_RECEIVING_PORT = 6789; // port on multicast ip address to send packets public static final int MULTICAST_LISTENING_PORT = 6789; public static final String HELLO_HEADER = "HELLOIM: "; public static final String GOODBYE_HEADER = "GOODBYE: "; Fig. 28. 12 of. Interface Definition several IMConstants constants used in the defines Deitel-Instantmulticast-socket Messenger version ofconstants. Deitel Instant Messenger Lines 5 -34 // time in milliseconds to wait between each multicast public static final int MULTICAST_INTERVAL = 10000; // how many MUTLICAST_INTERVALS before LEASE EXPIRATION public static final int PEER_TTL = 5; public static final int MESSAGE_SIZE = 256; public static String BINDING_NAME = "IMSERVICE"; } 2002 Prentice Hall. All rights reserved.

28. 12. 2 Finding Other Peers • Jini implementation – Listed new peers –

28. 12. 2 Finding Other Peers • Jini implementation – Listed new peers – Removed peers that left network 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Multicast. Receiving. Thread. java // Receive and process multicasts from multicast group package com. deitel. advjhtp 1. p 2 p; // Java core packages import java. net. Multicast. Socket; import java. net. *; import java. io. *; import java. util. *; // Deitel packages import com. deitel. advjhtp 1. p 2 p. Peer. Discovery. Listener; public class Multicast. Receiving. Thread extends Thread implements IMConstants { // Hash. Map containing peer names and time to live // used to implement leasing private Hash. Map peer. TTLMap; // Leasing. Thread reference private Leasing. Thread leasing. Thread ; // object that will respond to peer added or removed events private Peer. Discovery. Listener peer. Discovery. Listener ; Outline Multicast. Receiving. Thread listens for Datagram. Packets that contain notifications peers Fig. 28. 13 of. Class Multicastjoining and leaving network Receiving. Thread uses threads to add and remove peers (part 1). Line 14 // Multicast. Socket for receiving broadcast messages private Multicast. Socket multicast. Socket ; // Inet. Address of group for messages private Inet. Address multicast. Net. Address ; // flag for terminating Multicast. Receiving. Thread private boolean keep. Listening = true; 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 // Multicast. Receiving. Thread constructor public Multicast. Receiving. Thread ( String user. Name, Peer. Discovery. Listener peer. Event. Handler ) { // invoke superclass constructor to name Thread super( "Multicast. Receiving. Thread " ); Outline // set peer. Discovery. Listener = peer. Event. Handler; // connect Multicast. Socket to multicast address and port try { multicast. Socket = new Multicast. Socket( MULTICAST_RECEIVING_PORT ); multicast. Net. Address = Inet. Address. get. By. Name( MULTICAST_ADDRESS ); // join multicast group to receive messages multicast. Socket. join. Group( multicast. Net. Address ); // set 5 second time-out when waiting for new packets multicast. Socket. set. So. Timeout( 5000 ); } Fig. Instantiate 28. 13 Class Multicast. Socket Multicastto join multicast group Receiving. Thread uses threads to add and remove peers (part 2). Lines 48 -49 Lines 69 -70 // handle exception connecting to multicast address catch( IOException io. Exception ) { io. Exception. print. Stack. Trace(); } peer. TTLMap = new Hash. Map(); // create Leasing thread which decrements TTL of peers leasing. Thread = new Leasing. Thread(); leasing. Thread. set. Daemon( true ); Start Leasing. Thread 2002 Prentice as deamon thread. Hall. All rights reserved.

71 72 73 74 75 76 77 78 79 80 81 82 83 84

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 Outline leasing. Thread. start(); } // end Multicast. Receiving. Thread constructor // listen for messages from multicast group public void run() { while( keep. Listening ) { // create buffer for incoming message byte[] buffer = new byte[ MESSAGE_SIZE ]; // create Datagram. Packet for incoming message Datagram. Packet packet = new Datagram. Packet( buffer, MESSAGE_SIZE ); // receive new Datagram. Packet (blocking call) try { multicast. Socket. receive( packet ); } Fig. 28. 13 Class Multicast. Receive Datagram. Packet Receiving. Thread from Multicast. Socket uses threads to add and remove peers (part 3). // handle exception when receive times out catch ( Interrupted. IOException interrupted. IOException ) { Lines 84 -89 // continue to next iteration to keep listening continue; } // handle exception reading packet from multicast group catch ( IOException io. Exception ) { io. Exception. print. Stack. Trace(); break; } 2002 Prentice Hall. All rights reserved.

105 106 107 108 109 110 111 112 113 114 115 116 117 118

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 // put message data into String message = new String( packet. get. Data(), packet. get. Offset(), packet. get. Length() ); Outline Retrieve message stored in received Datagram. Packet // ensure non-null message if ( message != null ) { // trim extra whitespace from end of message = message. trim(); System. out. println( message ); // decide if goodbye or hello if ( message. starts. With( HELLO_HEADER ) ) { process. Hello ( message. substring( HELLO_HEADER. length() ), packet. get. Address(). get. Host. Address() ); } else if ( message. starts. With( GOODBYE_HEADER ) ) process. Goodbye ( message. substring( GOODBYE_HEADER. length() ) ); } // end if } // end while // leave multicast group and close Multicast. Socket try { multicast. Socket. leave. Group( multicast. Net. Address ); multicast. Socket. close(); } Fig. 28. 13 Class Multicast. Receiving. Thread useswhether threadsmessage to add Determine and remove peers is “hello” or “goodbye” (part 4). Lines 105 -106 Lines 118 -127 Lines 135 -136 Unsubscribe from Multicast. Socket 2002 Prentice Hall. All rights reserved.

139 140 141 142 143 144 145 146 147 148 149 150 151 152

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 // handle exception leaving group catch ( IOException io. Exception ) { io. Exception. print. Stack. Trace(); } Outline } // end run // process hello message from peer public void process. Hello( String peer. Name, String registry. Address ) { registry. Address += ( "/" + BINDING_NAME ); synchronized( peer. TTLMap ) { // if it is a new peer, call peer. Added event if ( !peer. TTLMap. contains. Key( peer. Name ) ) { peer. Discovery. Listener. peer. Added( peer. Name, registry. Address ); } // add to map or if present, refresh TTL peer. TTLMap. put( peer. Name, new Integer( PEER_TTL ) ); } Fig. 28. 13 Class Multicast. Receiving. Thread uses threads to add Addand peerremove to RMI peers registry (part 5). Lines 155 -157 Lines 172 -174 } // process goodbye message from peer public void process. Goodbye( String peer. Name ) { synchronized( peer. TTLMap ) { System. out. println( "Removing peer" + peer. Name ); if ( peer. TTLMap. contains. Key( peer. Name ) ) { peer. Discovery. Listener. peer. Removed( peer. Name ); Remove peer RMI registry 2002 to. Prentice Hall. All rights reserved.

174 175 176 177 178 179 180 181 182 183 184 185 186 187

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 Outline peer. TTLMap. remove( peer. Name ); } } } // periodically decrements the TTL of peers listed private class Leasing. Thread extends Thread { public void run() { while ( keep. Listening ) { // sleep try { Thread. sleep( MULTICAST_INTERVAL ); } Periodically decrement TTL of each peer in peer. TTLMap // Interrupted. Exception may interrupt Thread Sleep catch ( Interrupted. Exception interrupted. Exception ) { interrupted. Exception. print. Stack. Trace(); } // lock hashmap while decrementing TTL values synchronized( peer. TTLMap ) { Fig. 28. 13 Class Multicast. Receiving. Thread uses threads to add and remove peers (part 6). Line 180 // decrement peers Iterator peer. Iterator = peer. TTLMap. entry. Set(). iterator(); while ( peer. Iterator. has. Next() ) { // make new TTL of peer Map. Entry temp. Map. Entry = ( Map. Entry ) peer. Iterator. next(); 2002 Prentice Hall. All rights reserved.

208 209 210 211 212 213 214 215 216 217 218 219 220 221

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 Integer temp. Integer. TTL = ( Integer ) temp. Map. Entry. get. Value(); int temp. Int. TTL = temp. Integer. TTL. int. Value(); Outline // decrement TTL temp. Int. TTL --; // if lease expired, remove peer if ( temp. Int. TTL < 0 ) { peer. Discovery. Listener. peer. Removed( ( String ) temp. Map. Entry. get. Key() ); peer. Iterator. remove(); } // otherwise set TTL of peer to new value else temp. Map. Entry. set. Value( new Integer( temp. Int. TTL ) ); } // end while iterating through peers If peer’s lease has expired, remove that peer from peer. TTLMap Fig. 28. 13 Class Multicast. Receiving. Thread uses threads to add and remove peers (part 7). } // end synchronized } // end while in run method Lines 216 -220 } // end run method } // end class Leasing. Thread 2002 Prentice Hall. All rights reserved.

237 238 239 240 241 242 243 // stop listening for multicasts public void

237 238 239 240 241 242 243 // stop listening for multicasts public void logout() { // terminate thread keep. Listening = false; } Outline } Fig. 28. 13 Class Multicast. Receiving. Thread uses threads to add and remove peers (part 8). 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 //

1 2 3 4 5 6 7 8 9 10 11 12 13 // Peer. Discovery. Listener. java // Interface for listening to peer. Added or peer. Removed events package com. deitel. advjhtp 1. p 2 p; Outline public interface Peer. Discovery. Listener { // add peer with given name and ip address public void peer. Added( String name, String peer. Stub. Address ); // remove peer with given name public void peer. Removed( String name ); } Inform Peer. Discovery. Fig. Interface Listener that 28. 14 a peer has Peer. Discoveryentered, or has been removed listens for from, the Listener multicast group when peers are added and removed from peer groups. Lines 8 -11 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Peer. List. java // Starts broadcasting and receiving threads // and lists all IM peers in a window package com. deitel. advjhtp 1. p 2 p; Outline // Java core packages import java. awt. *; import java. awt. event. *; import java. net. Malformed. URLException; import java. util. *; import java. util. List; import java. io. IOException; import java. rmi. *; Fig. 28. 15 Modified Peer. List enables the user of classes // Java extension packages Multicastimport javax. swing. *; import javax. swing. event. *; Receiving. Thread and Peer. Discovery // Deitel Packages -Listener in the import com. deitel. advjhtp 1. jini. IM. service. IMService; import com. deitel. advjhtp 1. jini. IM. client. IMPeer. Listener; Deitel Instant import com. deitel. advjhtp 1. jini. IMPeer. Impl; Messenger (part 1). import com. deitel. advjhtp 1. jini. IMPeer; Modified Peer. List uses public class Peer. List extends JFrame Multicast. Receiving. Thread Line 25 implements Peer. Discovery. Listener, IMConstants { and Peer. Discovery. Listener // initialize user. Name to anonymous private String user. Name = "anonymous"; private Multicast. Sending. Thread multicast. Sender ; private Multicast. Receiving. Thread multicast. Receiver ; // list variables private Default. List. Model peer. Names ; private List peer. Stub. Addresses; // contains peer names // contains peer stubs 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 Outline private JList peer. JList; // add peer name and peer stub to lists public void peer. Added( String name, String peer. Stub. Address ) { // add name to peer. Names. add. Element( name ); // add stub to peer. Stub. Addresses. add( peer. Stub. Address ); } // end method peer. Added // removes services from Peer. List GUI and data structure public void peer. Removed( String name ) { // remove name from peer. Names int index = peer. Names. index. Of( name ); peer. Names. remove. Element. At( index ); // remove stub from peer. Stub. Addresses. remove( index ); } // end method peer. Removed // constructor public Peer. List() { super( "Peer List" ); // get desired user. Name = JOption. Pane. show. Input. Dialog( Peer. List. this, "Please enter your name: " ); Implement method peer. Added to add to lists Fig. peers 28. 15 Modified Peer. List enables the user of classes Multicast. Receiving. Thread and Peer. Discovery -Listener Implement method in the peer. Removed to Deitel Instant (part 2). remove Messenger peers from lists Lines 39 -47 Lines 51 -60 2002 Prentice Hall. All rights reserved.

71 72 73 74 75 76 77 78 79 80 81 82 83 84

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 Outline // change title of window set. Title( user. Name + "'s Peer List Window" ); // Init List data structures peer. Names = new Default. List. Model(); peer. Stub. Addresses = new Array. List(); // init components Container container = get. Content. Pane(); peer. JList = new JList( peer. Names ); peer. JList. set. Visible. Row. Count( 5 ); JButton connect. Button = new JButton( "Connect" ); // do not allow multiple selections peer. JList. set. Selection. Mode( List. Selection. Model. SINGLE_SELECTION ); // set up event handler for connect. Button. add. Action. Listener( new Action. Listener() { Fig. 28. 15 Modified Peer. List enables the user of classes Multicast. Receiving. Thread and Peer. Discovery -Listener in the Event handler for connecting peers Deitel Instant Messenger (part 3). public void action. Performed( Action. Event event ) { int item. Index = peer. JList. get. Selected. Index(); Line 89 String stub. Address = ( String ) peer. Stub. Addresses. get( item. Index ); // get RMI reference to IMService and IMPeer try { IMService peer. Stub = ( IMService ) Naming. lookup( "rmi: //" + stub. Address ); Lines 102 -104 Obtain reference to remote peer 2002 Prentice Hall. All rights reserved.

106 107 108 109 110 111 112 113 114 115 116 117 118 119

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 // set up gui and my peer. Impl IMPeer. Listener gui = new IMPeer. Listener( user. Name ); IMPeer. Impl me = new IMPeer. Impl( user. Name ); me. add. Listener( gui ); Outline // Connect my. Gui to remote IMPeer object IMPeer my. Peer = peer. Stub. connect( me ); gui. add. Peer( my. Peer ); } // malformed. URL passed to lookup catch( Malformed. URLException exception ) { JOption. Pane. show. Message. Dialog ( null, "Stub address incorrectly formatted" ); exception. print. Stack. Trace(); } // Remote object not bound to remote registry catch ( Not. Bound. Exception not. Bound. Exception ) { JOption. Pane. show. Message. Dialog( null, "Remote object not present in Registry" ); not. Bound. Exception. print. Stack. Trace(); } Fig. 28. 15 Modified Peer. List enables the user of classes Multicast. Receiving. Thread and Peer. Discovery -Listener in the Deitel Instant Messenger (part 4). // connecting may cause Remote. Exception catch ( Remote. Exception remote. Exception ) { JOption. Pane. show. Message. Dialog ( null, "Couldn't Connect" ); remote. Exception. print. Stack. Trace(); } 2002 Prentice Hall. All rights reserved.

140 141 142 143 144 145 146 147 148 149 150 151 152 153

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 } // end method Action. Performed Outline } // end Action. Listener anonymous inner class ); // end connect. Button action. Listener // set up File menu JMenu file. Menu = new JMenu( "File" ); file. Menu. set. Mnemonic( 'F' ); // about Item JMenu. Item about. Item = new JMenu. Item( "About. . . " ); about. Item. set. Mnemonic( 'A' ); about. Item. add. Action. Listener( new Action. Listener() { public void action. Performed( Action. Event event ) { JOption. Pane. show. Message. Dialog( Peer. List. this, "Deitel Instant Messenger" , "About", JOption. Pane. PLAIN_MESSAGE ); } } ); file. Menu. add( about. Item ); Fig. 28. 15 Modified Peer. List enables the user of classes Multicast. Receiving. Thread and Peer. Discovery -Listener in the Deitel Instant Messenger (part 5). // set up JMenu. Bar and attach File menu JMenu. Bar menu. Bar = new JMenu. Bar(); menu. Bar. add ( file. Menu ); set. JMenu. Bar( menu. Bar ); 2002 Prentice Hall. All rights reserved.

171 172 173 174 175 176 177 178 179 180 181 182 183 184

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 // handow window closing event add. Window. Listener( Outline new Window. Adapter(){ public void window. Closing( Window. Event w ) { System. out. println( "CLOSING WINDOW" ); // disconnects from lookup services multicast. Sender. logout(); multicast. Receiver. logout(); Fig. 28. 15 Modified Terminate each thread before Peer. List enables // join threads closingthe application user of classes try { Multicastmulticast. Sender. join(); multicast. Receiver. join(); Receiving. Thread } and Peer. Discovery catch( Interrupted. Exception interrupted. Exception ) { -Listener in the interrupted. Exception. print. Stack. Trace(); } Deitel Instant Messenger (part 6). System. exit( 0 ); } } ); Lines 181 -182 // lay out GUI components peer. JList. set. Fixed. Cell. Width( 100 ); JPanel input. Panel = new JPanel(); input. Panel. add( connect. Button ); container. add( new JScroll. Pane( peer. JList ) , Border. Layout. NORTH ); container. add( input. Panel, Border. Layout. SOUTH ); 2002 Prentice Hall. All rights reserved.

206 207 208 209 210 211 212 213 214 215 216 217 218 219

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 // Initialize threads try { Outline multicast. Receiver = new Multicast. Receiving. Thread ( user. Name, this ); multicast. Receiver. start(); multicast. Sender = new Multicast. Sending. Thread( user. Name ); multicast. Sender. start(); } // catch all exceptions and inform user of problem catch ( Exception manager. Exception ) { JOption. Pane. show. Message. Dialog( null, "Error initializing Multicast. Sending. Thread" + "or Multicast. Receiving. Thread " ); manager. Exception. print. Stack. Trace(); } } public static void main( String args[] ) { Peer. List peerlist = new Peer. List(); peerlist. set. Size( 100, 170 ); peerlist. set. Visible( true ); } Fig. 28. 15 Modified Peer. List enables the user of classes Multicast. Receiving. Thread and Peer. Discovery -Listener in the Deitel Instant Messenger (part 7). } 2002 Prentice Hall. All rights reserved.

28. 13 Introduction to JXTA • JXTA – Protocol that promotes interoperability among P

28. 13 Introduction to JXTA • JXTA – Protocol that promotes interoperability among P 2 P programs – Low-level, platform-independent and language-independent – Attempts to solve problems of P 2 P applications: • • Security/Authentication Peer Discovery Network Incompatibility Platform Incompatibility 2002 Prentice Hall. All rights reserved.

28. 13 Introduction to JXTA (cont. ) 2002 Prentice Hall. All rights reserved.

28. 13 Introduction to JXTA (cont. ) 2002 Prentice Hall. All rights reserved.