Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo...

19
Errata Corrige

Transcript of Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo...

Page 1: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

Errata Corrige

Page 2: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

DHT Routing (Tapestry)

• Tabella di routing (base k=4, digit d=4)

Consideriamo il nodo x con id (x1, x2, x3, x4):(1+x1, *, *, *) (x1, 1+x2, *, *) (x1, x2,1+x3, *) (x1, x2, x3, 1+x4)

(2+x1, *, *, *) (x1, 2+x2, *, *) (x1, x2, 2+x3, *) (x1, x2, x3, 2+x4)

(3+x1, *, *, *) (x1, 3+x2, *, *) (x1, x2, 3+x3, *) (x1, x2, x3, 3+x4)

(in totale sono k-1 d nodi -- n=dk -> d=logk n)

• Es. tabella di routing (1323 base 4)(2, 1, 3, 0) (1, 0, 2, 3) (1, 3, 3, 2) (1, 3, 2, 0)

(3, 1, 2, 2) (1, 1, 1, 3) (1, 3, 0, 1) (1, 3, 2, 1)

(0, 3, 2, 1) (1, 2, 1, 2) (1, 3, 1, 1) (1, 3, 2, 2)

Page 3: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

DHT Routing (Tapestry)

• Tabella di routing (base k=4, digit d=4)

Consideriamo il nodo x con id (x1, x2, x3, x4):(1+x1, *, *, *) (x1, 1+x2, *, *) (x1, x2,1+x3, *) (x1, x2, x3, 1+x4)

(2+x1, *, *, *) (x1, 2+x2, *, *) (x1, x2, 2+x3, *) (x1, x2, x3, 2+x4)

(3+x1, *, *, *) (x1, 3+x2, *, *) (x1, x2, 3+x3, *) (x1, x2, x3, 3+x4)

(in totale sono k-1 d nodi -- n=kd -> d=logk n)

• Es. tabella di routing (1323 base 4)(2, 1, 3, 0) (1, 0, 2, 3) (1, 3, 3, 2) (1, 3, 2, 0)

(3, 1, 2, 2) (1, 1, 1, 3) (1, 3, 0, 1) (1, 3, 2, 1)

(0, 3, 2, 1) (1, 2, 1, 2) (1, 3, 1, 1) (1, 3, 2, 2)

Page 4: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

Koorde

2

,

),(

n

yxd

APL Nyx

m.lookup(k,ks,i)m.lookup(k,ks,i)

11 if k if k (m, m.successor] return successor(m, m.successor] return successor

22 else if i else if i (m, m.successor] (m, m.successor]

33 return d.lookup(k,ks<<1, ireturn d.lookup(k,ks<<1, itopbit(ks))topbit(ks))

44 else else

55 return m.successor.lookup(k,ks,i)return m.successor.lookup(k,ks,i)

Cerchiamo il predecessore del

nodo immaginario?

Passiamo al nuovo nodo del

grafo di de Bruijn

Il passo 3 viene eseguito al massimo b volte

Quante volte eseguiamo il passo 5, vale a dire quanto impieghiamo per trovare il predecessore di un nodo immaginario?

Page 5: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

Koorde

2

,

),(

n

yxd

APL Nyx

m.lookup(k,ks,i)m.lookup(k,ks,i)

11 if k if k (m, m.successor] return successor(m, m.successor] return successor

22 else if i else if i [m, m.successor) [m, m.successor)

33 return d.lookup(k,ks<<1, ireturn d.lookup(k,ks<<1, itopbit(ks))topbit(ks))

44 else else

55 return m.successor.lookup(k,ks,i)return m.successor.lookup(k,ks,i)

Cerchiamo il predecessore del

nodo immaginario?

Passiamo al nuovo nodo del

grafo di de Bruijn

Il passo 3 viene eseguito al massimo b volte

Quante volte eseguiamo il passo 5, vale a dire quanto impieghiamo per trovare il predecessore di un nodo immaginario?

Page 6: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

Programming with JXTAProgramming with JXTA

Hello WorldHello World

Peer discoveryPeer discovery

Page 7: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

JXTA: Hello WorldJXTA: Hello World

RequirementRequirement Java SDK (Java SDK (http://java.sun.com)[jdk1.6.0_03] JXTA (JXTA (http://download.jxta.org/index.html)

[JXTA 2.5] jxse-lib-2.5jxse-lib-2.5

jxse-tutorials-2.5jxse-tutorials-2.5

jxse-doc-2.5jxse-doc-2.5

Page 8: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

JXTA: Hello WorldJXTA: Hello World

Compilare usando JXTA javac -classpath .\lib\jxta.jar SimpleJxtaApp.java

Lanciare un applicazione java -classpath .\lib\jxta.jar .\lib\bcprov-jdk14.jar;. SimpleJxtaApp

Page 9: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

Programming with JXTAProgramming with JXTA

Peer discoveryPeer discovery Gli advertisement vengono mantenuti nella cache di Gli advertisement vengono mantenuti nella cache di

JXTA (./.jxta/cm)JXTA (./.jxta/cm) In particolare per ogni gruppo sarà presente una In particolare per ogni gruppo sarà presente una

sottodirectorysottodirectory ./.jxta/cm/jxta-NetGroup./.jxta/cm/jxta-NetGroup

./.jxta/cm/group-ID./.jxta/cm/group-ID Le directory contengono tre tipi di fileLe directory contengono tre tipi di file

*.idx file, indice*.idx file, indice

record-offset.tbl, entry listrecord-offset.tbl, entry list

advertisement.tbl, advertisementadvertisement.tbl, advertisement

Page 10: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

Programming with JXTAProgramming with JXTA

Peer discoveryPeer discovery Per leggere i messaggi dalla cache si usa il metodo Per leggere i messaggi dalla cache si usa il metodo

getLocalAdvertisement();getLocalAdvertisement(); Per scrivere nella cache si usa invece il metodo Per scrivere nella cache si usa invece il metodo

getRemoteAdvertisement(); (il quale invia un getRemoteAdvertisement(); (il quale invia un messaggio di discovery e attende la risposta).messaggio di discovery e attende la risposta).

Come si ottiene la rispostaCome si ottiene la rispostaSi può aspettare e successivamente controllare la propria Si può aspettare e successivamente controllare la propria cache (getLocalAdvertisement())cache (getLocalAdvertisement())

Si può utilizzare un Discovery Listner (utilizzato nella demo).Si può utilizzare un Discovery Listner (utilizzato nella demo).

Page 11: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

import net.jxta.discovery.DiscoveryEvent;import net.jxta.discovery.DiscoveryListener;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.peergroup.PeerGroup;import net.jxta.platform.NetworkManager;import net.jxta.protocol.DiscoveryResponseMsg;import java.io.File;import java.util.Enumeration;public class DiscoveryClient implements DiscoveryListener {

private transient NetworkManager manager; private transient DiscoveryService discovery; //Constructor for the DiscoveryClient

public DiscoveryClient() { try { manager = new NetworkManager(NetworkManager.ConfigMode.ADHOC, "DiscoveryClient", new File(new File(".cache"), "DiscoveryClient").toURI()); manager.startNetwork(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } // Get the NetPeerGroup PeerGroup netPeerGroup = manager.getNetPeerGroup();

// get the discovery service discovery = netPeerGroup.getDiscoveryService(); }

Programming with JXTAProgramming with JXTA

Per instanziare JXTA

Per discovery

Page 12: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

// main //@param args command line args public static void main(String args[]) { DiscoveryClient disocveryClient = new DiscoveryClient(); disocveryClient.start(); }

/** * loop forever attempting to discover advertisements every minute */ public void start() { long waittime = 60 * 1000L;

try { // Add ourselves as a DiscoveryListener for DiscoveryResponse events discovery.addDiscoveryListener(this); discovery.getRemoteAdvertisements( null, // no specific peer (propagate) DiscoveryService.ADV // Adv type(DiscoveryService.ADV null, // Attribute = any

null, // Value = any 1, // one advertisement response is all we are looking for null); // no query specific listener. we are using a global listener

Programming with JXTAProgramming with JXTA

Page 13: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

while (true) { // wait a bit before sending a discovery message try { System.out.println("Sleeping for :" + waittime); Thread.sleep(waittime); } catch (Exception e) { // ignored } } System.out.println("Sending a Discovery Message"); // look for any peer discovery.getRemoteAdvertisements( // no specific peer (propagate) null, // Adv type DiscoveryService.ADV, // Attribute = name "Name", // Value = the tutorial "Discovery tutorial", // one advertisement response is all we are looking for 1, // no query specific listener. we are using a global listener null); } } catch (Exception e) { e.printStackTrace(); } }

Programming with JXTAProgramming with JXTA

Page 14: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

/** * This method is called whenever a discovery response is received, which are * either in response to a query we sent, or a remote publish by another node * @param ev the discovery event */ public void discoveryEvent(DiscoveryEvent ev) { DiscoveryResponseMsg res = ev.getResponse(); // let's get the responding peer's advertisement System.out.println(" [ Got a Discovery Response [" + res.getResponseCount() + " elements] from peer : " + ev.getSource() + " ]");

Advertisement adv; Enumeration en = res.getAdvertisements(); if (en != null) { while (en.hasMoreElements()) { adv = (Advertisement) en.nextElement(); System.out.println(adv); } } }

/** * Stops the platform */ public void stop() { // Stop JXTA manager.stopNetwork(); }}

Programming with JXTAProgramming with JXTA

Page 15: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

import net.jxta.discovery.DiscoveryEvent;import net.jxta.discovery.DiscoveryListener;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.id.IDFactory;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;import net.jxta.pipe.PipeService;import net.jxta.platform.NetworkManager;import net.jxta.protocol.DiscoveryResponseMsg;import net.jxta.protocol.PipeAdvertisement;import java.io.File;import java.util.Enumeration;

public class DiscoveryServer implements DiscoveryListener {

private transient NetworkManager manager; private transient DiscoveryService discovery;

public DiscoveryServer() { try { manager = new NetworkManager(NetworkManager.ConfigMode.ADHOC, "DiscoveryServer", new File(new File(".cache"), "DiscoveryServer").toURI()); manager.startNetwork(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } PeerGroup netPeerGroup = manager.getNetPeerGroup(); // get the discovery service discovery = netPeerGroup.getDiscoveryService(); }

Programming with JXTAProgramming with JXTA

Page 16: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

/** * main * @param args command line args */ public static void main(String args[]) { DiscoveryServer disocveryServer = new DiscoveryServer(); disocveryServer.start(); }

public void start() { long lifetime = 60 * 2 * 1000L; long expiration = 60 * 2 * 1000L; long waittime = 60 * 3 * 1000L; try { while (true) { PipeAdvertisement pipeAdv = getPipeAdvertisement(); // publish the advertisement with a lifetime of 2 mintutes System.out.println( "Publishing the following advertisement with lifetime :" + lifetime + " expiration :" + expiration); System.out.println(pipeAdv.toString()); discovery.publish(pipeAdv, lifetime, expiration); discovery.remotePublish(pipeAdv, expiration); try { System.out.println("Sleeping for :" + waittime); Thread.sleep(waittime); } catch (Exception e) {// ignored } } } catch (Exception e) { e.printStackTrace(); } }

Programming with JXTAProgramming with JXTA

Page 17: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

/** * This method is called whenever a discovery response is received, which are * either in response to a query we sent, or a remote publish by another node * * @param ev the discovery event */ public void discoveryEvent(DiscoveryEvent ev) {

DiscoveryResponseMsg res = ev.getResponse();

// let's get the responding peer's advertisement System.out.println(" [ Got a Discovery Response [" + res.getResponseCount() + " elements] from peer : " + ev.getSource() + " ]");

Advertisement adv; Enumeration en = res.getAdvertisements();

if (en != null) { while (en.hasMoreElements()) { adv = (Advertisement) en.nextElement(); System.out.println(adv); } } }

Programming with JXTAProgramming with JXTA

Page 18: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

/** * Creates a pipe advertisement * * @return a Pipe Advertisement */ public static PipeAdvertisement getPipeAdvertisement() { PipeAdvertisement advertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());

advertisement.setPipeID(IDFactory.newPipeID(PeerGroupID.defaultNetPeerGroupID)); advertisement.setType(PipeService.UnicastType); advertisement.setName("Discovery tutorial"); return advertisement; }

/** * Stops the platform */ public void stop() { // Stop JXTA manager.stopNetwork(); }}

Programming with JXTAProgramming with JXTA

Page 19: Errata Corrige. DHT Routing (Tapestry) Tabella di routing (base k=4, digit d=4) Consideriamo il nodo x con id (x 1, x 2, x 3, x 4 ): (1+x 1, *, *, *)

DemoDemo

Peer DiscoveryPeer Discovery