-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (61 loc) · 1.62 KB
/
main.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Butter tracker package enables the tracking of nodes deployed on a Butter network. It is
// mostly used for metrics and testing.
package main
import (
"database/sql"
"fmt"
"log"
"time"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "docker"
dbname = "world"
)
func ConnectToDB() *sql.DB {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
log.Fatal(err)
}
return db
}
// createSchema for postgres database of Butter nodes
func createSchema(db *sql.DB) {
const drop = `DROP TABLE IF EXISTS nodes;`
_, err := db.Exec(drop)
if err != nil {
log.Fatal(err)
}
const create string = `
CREATE TABLE IF NOT EXISTS nodes (
addr VARCHAR NOT NULL PRIMARY KEY,
peers VARCHAR NOT NULL,
groups VARCHAR,
last_seen TIMESTAMP NOT NULL DEFAULT NOW()
);`
_, err = db.Exec(create)
}
// This information could be used to generate a live graph of the network and its PCG overlay by
// integrating the database/TRacker server with websockets
func main() {
db := ConnectToDB()
defer db.Close()
createSchema(db)
// Nodes periodically ping the Tracker telling the server they are still alive and hence to
// maintain and update their entry in the databse
for {
time.Sleep(time.Second * 10)
// Remove all the nodes that haven't been seen in a while
const delete string = `
DELETE FROM nodes
WHERE last_seen < NOW() - INTERVAL '10 seconds';`
_, err := db.Exec(delete)
if err != nil {
log.Fatal(err)
}
}
}