-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
55 lines (48 loc) · 2.06 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const DHT = require('bittorrent-dht')
const net = require('net');
const dht = new DHT()
const baseHash = "ab280ff80cd7451beec35ad73398"
const driverHash = Buffer.from("driver", "utf8").toString("hex")
var infoHash = baseHash + driverHash
var DRIVER_LIST = {}
var isContacted = []
dht.listen(21000, function () {
console.log('now listening for ' + infoHash)
})
dht.lookup(infoHash)
dht.on('peer', function (peer, infoHash, from) {
if (isContacted.indexOf(peer.host) == -1){
console.log('found potential peer ' + peer.host + ':' + peer.port + ' through ' + from.address + ':' + from.port)
isContacted.push(peer.host)
DRIVER_LIST["0.0.0.0" + ':' + peer.port] = "driver"
var client = new net.Socket();
client.connect(parseInt(peer.port), "0.0.0.0", function(){
console.log("<=========================")
console.log("connected to " + peer.host + ":" + peer.port)
client.write("ack")
client.on("data", function (data) {
responseObj = JSON.parse(data.toString())
if (responseObj.hasOwnProperty("status")){
// first response
if (responseObj.status == "free"){
console.log("<=========================")
console.log("reaching out to available driver")
currentLatLon = {
lat: responseObj.location.lat - 0.1,
lon: responseObj.location.lon - 0.1
}
bookRequest = {
location: currentLatLon
}
client.write(Buffer.from(JSON.stringify(bookRequest)))
}
}
if (responseObj.hasOwnProperty("cost")){
console.log("<=========================")
console.log("price quoted by driver is Rs " + Math.floor(responseObj.cost))
client.write("ack2")
}
})
})
}
})