Вы находитесь на странице: 1из 14

Distributed Programming in Java

Networking (4)

Peer-to-Peer
A peer-to-peer (or P2P) computer network is a
network that relies primarily on the computing power and bandwidth of the participants in the network rather than concentrating it in a relatively low number of servers. P2P networks are typically used for connecting nodes via largely ad hoc connections. Such networks are useful for many purposes. Sharing content les (see le sharing) containing audio, video, data or anything in digital format is very common, and real-time data, such as telephony trafc, is also passed using P2P technology

2/40

Properties of P2P
A pure peer-to-peer network does not have the notion of
clients or servers, but only equal peer nodes that simultaneously function as both "clients" and "servers" to the other nodes on the network. This model of network arrangement differs from the clientserver model where communication is usually to and from a central server. A typical example for a non peer-to-peer le transfer is an FTP server where the client and server programs are quite distinct, and the clients initiate the download/uploads and the servers react to and satisfy these requests.

3/40

Acceptor-Connector

Decouple the connection of peer


active passive

components from the processing the peers perform after they are connected

[BH]

passive

active
4/40

Case Study

JSquiggle is a peer-to-peer program written in

Java for handheld devices. Runs on desktops and PDAs can be used to write to any other JSquiggle within the same local network or far away by simply put their IP addresses. No centralized control each device is a peer in a P2P network
5/40

Open protocols uses HTTP and XML for Ordered messages uses logical timestamp
to ensure messages are in order messaging between peers

http://www.aspectsolution.com/sketch

6/40

Usage Scenario

Peer connect to any other peer, eg using a


wireless network (WLAN)

7/40

How to use it

8/40

9/40

10/40

11/40

12/40

Use Cases

Discussed Draw Sketch Add Peer Add Message Not discussed: Search Peer, Get Info, Save Sketch, Open Sketch, and Print Sketch
13/40

Draw Sketch
Precondition Peers are added in the peer lists Postcondition Sketch message is constructed and sent to peer 1. Capture sketch activites until pen is released 2. Increase the logical time for new event 3. Construct sketch message 4. Send the message to each peer on the list
14/40

Add Peer
Precondition Peer's IP address is known Post condition Peers are added to peer list 1. User inputs peer's IP address 2. User inputs peer's port number 3. Add the peer into list of peers
15/40

Add Message
Precondition Peer is started at correct port Post condition Sketch messages are processed/drawn on the screen 1. Remote peer sends out sketch message 2. System gets and parses the message 3. If logical time is bigger, advance the logical clock 4. Execute the message (ie update sketch)
16/40

Sending a Message

17/40

SketchCanvas

Press mouse: start, dragging: add new points,


release mouse: create and send path

18/40

Separate the construction of commands


from their representation

Command Builder

19/40

SketchSender

20/40

SketchSender

Send command to all known peers

21/40

SketchPeer

Follows Half-Sync/Half-Async pattern

22/40

SketchPeer.SenderThread

23/40

Scalable Vector Graphics

Scalable Vector Graphics (SVG) is an

XML markup language for describing twodimensional vector graphics, both static and animated, and either declarative or scripted. It is an open standard created by the World Wide Web Consortium.

24/40

Command Format

Commands are encoded in XML using the

Scalable Vector Graphics SVG standard for vector graphics M = move-to, L = link

25/40

Command Format

26/40

Receiving a Message
parse

27/40

Acceptor-Connector

Decouple the connection of peer


active passive

components from the processing the peers perform after they are connected

[BH]

passive

active
28/40

JSquiggle

Creates a SketchCanvas (a ServiceHandler) and


a SketchServer (an Acceptor) The drama unfolds when the SketchServer receives a connection request ...

29/40

SketchServer

A typical Acceptor, SketchServer accepts a

client request and hands it over to a SketchService to process (which will invoke our SketchCanvas ... eventually)

30/40

10

SketchService

Parse the command, then process it ...

31/40

Command Format

Commands are encoded in XML using the

Scalable Vector Graphics SVG standard for vector graphics M = move-to, L = link

32/40

SketchParser

Decipher the command using the XmlParser


from org.kxml (small footprint)

33/40

11

SketchReceiver

Complete the Command Message pattern History of commands for redrawing

34/40

Command Message

Send command (with state) in a message Both sender and receiver contain code for
processing the command

35/40

Command Message

Invokes a command on a remote server Message handling code separate from code
to process command messages

36/40

12

Message Ordering

Correct ordering of messages ensures by Each peer increments clock between any When sending message m, use current clock Upon receiving message m, check the
value as timestamp Tm timestamp Tm, and match the clock to Tm, if current clock is less than Tm
37/40

using Lamports timestamp algorithm two successive events

Lamports Algorithm
if a before b then Ta < Tb

38/40

Lamport Clock

39/40

13

Use in JSquiggle

State contains the peers local state,


including its timestamp

40/40

14