|
package
easypastry.sample;
import
java.io.IOException;
import
easypastry.cast.CastHandler;
import
easypastry.core.PastryConnection;
import
easypastry.core.PastryKernel;
import
easypastry.dht.DHTException;
import
easypastry.dht.DHTHandler;
public
class
AppMain {
private PastryConnection
conn;
private DHTHandler
dht;
private CastHandler
cast;
public
void
initKBR(String filename)
throws
Exception {
PastryKernel.init(filename);
conn = PastryKernel.getPastryConnection();
}
public
void
initDHT(String context)
throws
DHTException {
dht = PastryKernel.getDHTHandler(context);
}
public
void
initCast(String group) {
cast = PastryKernel.getCastHandler();
cast.subscribe(group);
cast.addDeliverListener(group,
new
AppCastListener());
}
public
void start()
throws Exception {
conn.bootNode();
}
public DHTHandler getDHTHandler()
{
return
dht;
}
public CastHandler getCastHandler()
{
return
cast;
}
private
void
testDHT() throws
DHTException {
DHTHandler dht = getDHTHandler();
System.out.println("DHT
| Inserting : <key1, value1>");
dht.put("key1",
"value1");
System.out.println("DHT
| Inserting : <key1, value2>");
dht.put("key1",
"value2");
System.out.println("DHT
| Retrieving : <key1>");
String value = (String) dht.get("key1");
System.out.println("DHT
| Current value of <key1> : <"+value+">");
}
private
void
testCast() {
CastHandler cast = getCastHandler();
System.out.println("Cast
| Direct : text message 1");
cast.sendDirect(cast.getLocalNodeHandle(),
new
AppCastContent("text
message 1"));
System.out.println("Cast
| Muticast : text message 2");
cast.sendMulticast("p2p://test",
new
AppCastContent("text
message 2"));
}
public
static
void
main(String[] args) {
try {
AppMain app =
new
AppMain();
app.initKBR("easypastry-config.xml");
app.initDHT("test");
app.initCast("p2p://test");
app.start();
app.testDHT();
app.testCast();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
} |