Skip to content

Examples

Brad edited this page May 24, 2024 · 12 revisions

Creating your first project

This will allow you to join an already running Kademlia Node

Kademlia kad = new Kademlia();
kad.join(6881, InetAddress.getByName("HOSENAME"), 6881);

This will allow you to become a genesis for a network.

Kademlia kad = new Kademlia();
kad.bind(6881);

Node Information

Here is how you can get node information from Kademlia

kad.getRoutingTable().getDerivedUID(); //GETS DERIVED UID
kad.getRoutingTable().getConsensusExternalAddress(); //GETS THE ADDRESS BASED ON WHAT OTHER NODES RESPOND WITH (COULD BE LOCAL IF ALL NODES RESPOND WITH LOCAL IP)

UPnP port forwarding

kad.getUPnP().forward(6881);

Secure UID Requirement

Requiring Secure UID. By default Secure UID is set to true.

kad.getRoutingTable().setSecureOnly(false); //DISABLES SECURE UID REQUIREMENT

Bogon Packet Checks

For local networks and testing make sure to enable Bogon.

kad.getServer().setAllowBogon(true); //ALLOWS BOGON PACKET SENDING AND RECEIVING

Sending Message

Here is an example of how to send a request.

PingRequest request = new PingRequest();
request.setDestination(/*InetSocketAddress*/);

try{
    kad.getServer().send(request, new ResponseCallback(){
        @Override
        public void onResponse(ResponseEvent event){
        }

        @Override
        public void onErrorResponse(ErrorResponseEvent event){
        }

        @Override
        public void onStalled(StalledEvent event){
        }
    });

    kad.getServer().send(event);

}catch(IOException e){
    e.printStackTrace();
}