nadeemabdulhamid / PeerBase

http://cs.berry.edu/~nhamid/p2p
MIT License
23 stars 10 forks source link

Question for the Router usage #6

Open hiimanget opened 1 year ago

hiimanget commented 1 year ago

Hi @nadeemabdulhamid, I'm using your framework to build some p2p apps. But I have a question about how to use the router in other ways.

Can u show me what kind of routers can be used for other purposes with some examples? (like routing with limited hops)

nadeemabdulhamid commented 1 year ago

Yeah, good question - it has been 10 years though since I worked with this code (though I just moved it onto Github last year), so I don't have any good examples available, sorry. Looking through it, I think I was intending for the router interface to be used for something like a distributed shortest path algorithm, or experimenting with implementing distance-vector/link-state algorithms. In the example code provided, if I remember correctly, generally, the idea is that every peer eventually knows about every other peer and thus there is always a direct route. But, if you wanted to implement a routing technique where peer nodes would only know a limited number of other direct neighbors in the network, then you would implement a more elaborate router.

hiimanget commented 1 year ago

Thank you for your reply.

But don't you think I should rewrite the router interface to implement the hops like below?

public interface Router {
    // NodeInfo route(String id); <- origin
    NodeInfo route(String id, int hops);
    List<NodeInfo> list(int hops);
}

And the router will create a connection to send a msg to the neighbors and there may be a HopHandler to handle it. Is this fine or am I using it wrong way for the router interface?

I think I still couldn't understand exactly how to implement the router interface structure in other ways without changing the structure.

nadeemabdulhamid commented 1 year ago

I imagine that any additional information - like a routing table - would be kept in the Node class. And for things like hops remaining, those could be part of the messages being exchanged. The Router could be implemented as an inner class in the Node (just like the on in the Java sample) so that it would have access to the routing table and other information that the node knows about. At least that's my thinking without actually trying to implement it. Because if you start changing the interface, there might be lots of different parameters that any routing algorithm would want to incorporate, besides hops, and so then we'd have to keep on changing the interface of the route method. ??

hiimanget commented 1 year ago

Thank u, understood a little for me. But I have decided to use SimpleRouter as it is cuz I thought I don't have to implement the hops to make p2p nodes work normally.

Btw, why did u set server socket timeout and reopen it on every 2 seconds? (Node#mainLoop()) Is it for preventing to block establishing with only a specific node for a long time?

nadeemabdulhamid commented 1 year ago

That time out setting is not related to establishing up the connection. It controls the behavior for read() after a client connects. https://stackoverflow.com/questions/12820874/what-is-the-functionality-of-setsotimeout-and-how-it-works

hiimanget commented 1 year ago

But looks like the catch block is used to catch an exception of ServerSocket#setSoTimeout().