Errata Corrige
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)
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)
Koorde
m.lookup(k,ks,i)
1 if k (m, m.successor] return successor
2 else if i (m, m.successor]
3
return d.lookup(k,ks<<1, itopbit(ks))
4
else
5
return m.successor.lookup(k,ks,i)
Passiamo al
nuovo nodo del
grafo di de Bruijn
APL 
 d ( x, y )
x , yN
n2
Cerchiamo il
predecessore del
nodo immaginario?
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?
Koorde
m.lookup(k,ks,i)
1 if k (m, m.successor] return successor
2 else if i [m, m.successor)
3
return d.lookup(k,ks<<1, itopbit(ks))
4
else
5
return m.successor.lookup(k,ks,i)
Passiamo al
nuovo nodo del
grafo di de Bruijn
APL 
 d ( x, y )
x , yN
n2
Cerchiamo il
predecessore del
nodo immaginario?
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?
Programming with JXTA
Hello World
Peer discovery
JXTA: Hello World
Requirement


Java SDK (http://java.sun.com)[jdk1.6.0_03]
JXTA (http://download.jxta.org/index.html)
[JXTA 2.5]
jxse-lib-2.5
jxse-tutorials-2.5
jxse-doc-2.5
JXTA: 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
Programming with JXTA
Peer discovery


Gli advertisement vengono mantenuti nella cache di
JXTA (./.jxta/cm)
In particolare per ogni gruppo sarà presente una
sottodirectory
./.jxta/cm/jxta-NetGroup
./.jxta/cm/group-ID

Le directory contengono tre tipi di file
*.idx file, indice
record-offset.tbl, entry list
advertisement.tbl, advertisement
Programming with JXTA
Peer discovery



Per leggere i messaggi dalla cache si usa il metodo
getLocalAdvertisement();
Per scrivere nella cache si usa invece il metodo
getRemoteAdvertisement(); (il quale invia un
messaggio di discovery e attende la risposta).
Come si ottiene la risposta
Si può aspettare e successivamente controllare la propria
cache (getLocalAdvertisement())
Si può utilizzare un Discovery Listner (utilizzato nella demo).
Programming with JXTA
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 {
Per discovery
Per instanziare JXTA
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 JXTA
//
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 JXTA
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 JXTA
/**
* 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 JXTA
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 JXTA
* 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 JXTA
* 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 JXTA
/**
* 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();
}
}
Demo
Peer Discovery