-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoenix.go
61 lines (51 loc) · 1.12 KB
/
phoenix.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
package main
import (
"encoding/binary"
. "fmt"
"log"
"net"
"os/exec"
t "time"
)
var counter uint64
var port = 9999
var buf = make([]byte, 16)
func spawnBackup() {
(exec.Command("gnome-terminal", "-x", "sh", "-c", "go run phoenix.go")).Run()
Println("New backup is online!")
}
func main() {
addr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:9999")
isPrimary := false
conn, err := net.ListenUDP("udp", addr)
if err != nil {
log.Println("Error, listeningUDP failed!")
}
log.Println("I'm the backup!")
for !(isPrimary) {
conn.SetReadDeadline(t.Now().Add(2 * t.Second))
n, _, err := conn.ReadFromUDP(buf)
if err != nil {
isPrimary = true
} else {
counter = binary.BigEndian.Uint64(buf[:n])
}
}
conn.Close()
Println("addr: ", addr)
spawnBackup()
Println("I'm now the primary!")
bcastConn, _ := net.DialUDP("udp", nil, addr)
for {
if counter%10 == 0 {
Println("\t*---------------*")
Println("\t| Number: ", counter, "\t|")
} else {
Println("\t| Number: ", counter, "\t|")
}
counter++
binary.BigEndian.PutUint64(buf, counter)
_, _ = bcastConn.Write(buf)
t.Sleep(50 * t.Millisecond)
}
}