Skip to content

Commit

Permalink
Fix bugs in sim dht script. Switch over to fast server.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsdotpm committed Mar 1, 2016
1 parent 3076f3e commit ff6d7ce
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
30 changes: 27 additions & 3 deletions pyp2p/dht_msg.py
Expand Up @@ -23,8 +23,7 @@
import time
import logging

dht_msg_endpoint = "http://185.61.148.22/dht_msg.php"
# dht_msg_endpoint = "http://localhost/dht_msg.php"
dht_msg_endpoint = "http://162.243.213.95/dht_msg.php"
logging.basicConfig()
log = logging.getLogger(__name__)

Expand Down Expand Up @@ -185,24 +184,31 @@ def do(args):
# Make API call.
ret = requests.get(call, timeout=5).text
ret = json.loads(ret)
#self.is_neighbours_ready.set()
#return
if type(ret) == dict:
ret = [ret]

# Convert to kademlia neighbours.
neighbours = []
for neighbour in ret:
if not is_ip_valid(neighbour["ip"]):
print("Invalid ip!")
print(neighbour["ip"])
continue

neighbour["port"] = int(neighbour["port"])
if not is_valid_port(neighbour["port"]):
print("Invalid port!")
print(neighbour["port"])
continue

neighbour["can_test"] = int(neighbour["can_test"])
knode = KadNode(
id=binascii.unhexlify(neighbour["id"].encode("ascii")),
ip=neighbour["ip"],
port=neighbour["port"],
can_test=int(neighbour["can_test"])
can_test=neighbour["can_test"]
)

neighbours.append(knode)
Expand Down Expand Up @@ -462,6 +468,24 @@ def get_messages(self):


if __name__ == "__main__":
node_1 = DHT(ip="127.0.0.1", port=1337)
node_2 = DHT(ip="127.0.0.1", port=1338)
node_3 = DHT(ip="127.0.0.1", port=1339)

print(node_1.ip)
print(node_1.port)
print(node_2.neighbours)

print("Node 1 has mutex")
print(node_1.has_mutex)
print()
print("Node 2 has mutex")
print(node_2.has_mutex)
print()
print("Node 3 has mutex")
print(node_3.has_mutex)


#node1 = DHT()
#print(node1.get_id())
#print(node1.node_id)
Expand Down
51 changes: 33 additions & 18 deletions server/dht_msg.php
Expand Up @@ -102,39 +102,50 @@
$nodes = array();

#Fetch one random node to reserve for testing.
$restrictions = "";
$restrictions = " AND `id`<>" . $node["id"];
if($node["has_mutex"] == 1)
{
# echo("HAS MUTEX");

#Fetch nodes to reserve.
$sql = "SELECT * FROM `nodes` WHERE (`reservation_expiry`<$timestamp OR `reservation_expiry`=0) AND `last_alive`>=$freshness AND `network_id`='$network_id' ORDER BY rand() LIMIT 1 FOR UPDATE";
#Potential issue here if there's an attack.
$sql = "SELECT * FROM `nodes` WHERE (`reservation_expiry`<$timestamp OR `reservation_expiry`=0) AND `last_alive`>=$freshness AND `network_id`='$network_id' AND `has_mutex`=0 $restrictions ORDER BY `LAST_ALIVE` DESC LIMIT 1 FOR UPDATE";
# echo($sql);
$result = mysql_query($sql, $con);
while($row = mysql_fetch_assoc($result))
{
# echo("Found node to reserve");
$row["can_test"] = 1;
$nodes[] = $row;
$node_id = mysql_real_escape_string($row["id"], $con);
$restrictions = " AND `id`<>$node_id";
}

#Reserve those nodes.
$expiry = time() + $config["reservation_timeout"];
$expiry = mysql_real_escape_string($expiry, $con);
foreach($nodes as $value)
{
$id = $value["id"];
$id = mysql_real_escape_string($id, $con);
$sql = "UPDATE `nodes` SET `reservation_expiry`=$expiry WHERE `id`=$id";
mysql_query($sql, $con);
$restrictions .= " AND `id`<>$node_id";

#Reserve those nodes.
$expiry = time() + $config["reservation_timeout"];
$expiry = mysql_real_escape_string($expiry, $con);
foreach($nodes as $value)
{
# echo("<p></p>UPdating set reservation expiry.");
$id = $value["id"];
$id = mysql_real_escape_string($id, $con);
$sql = "UPDATE `nodes` SET `reservation_expiry`=$expiry WHERE `id`=$id";
mysql_query($sql, $con);
}

#Reduce limit for next section (since we just got a node.)
$limit -= 1;
}

#Reduce limit for next section (since we just got a node.)
$limit -= 1;
}

#Fetch remaining nodes.
#Might not want to even do this part.
#Do you even want results from nodes who can't bandwidth test?
if($limit)
{
$sql = "SELECT * FROM `nodes` WHERE `last_alive`>=$freshness AND `network_id`='$network_id' $restrictions ORDER BY rand() LIMIT $limit FOR UPDATE";
# echo("<br>");
# echo($sql);
$sql = "SELECT * FROM `nodes` WHERE `last_alive`>=$freshness AND `network_id`='$network_id' AND `has_mutex`=1 $restrictions ORDER BY `LAST_ALIVE` DESC LIMIT $limit FOR UPDATE";
# echo($sql);
$result = mysql_query($sql, $con);
while($row = mysql_fetch_assoc($result))
{
Expand All @@ -145,6 +156,10 @@

end_transaction($con, 1);

# echo("<p>--------------------</p>");
# var_dump($nodes);
# echo("<p>--------------------</p>");

$neighbours = array();
foreach($nodes as $value)
{
Expand Down

0 comments on commit ff6d7ce

Please sign in to comment.