-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
85 lines (75 loc) · 1.73 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
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// Checks given room codes manually, in parallel.
// Parallelism of len(roomCodes) threads.
func checkRoomCodesManual(roomCodes []string) error {
channel := make(chan Result, len(roomCodes))
for i := 0; i < len(roomCodes); i++ {
go func(i int) {
channel <- checkRoomCode(roomCodes[i])
}(i)
}
for i := 0; i < len(roomCodes); i++ {
result := <-channel
fmt.Printf("%s: ", result.roomInfo.RoomCode)
if result.err != nil {
return result.err
} else if result.success {
fmt.Println("**** FOUND ROOM ****")
} else {
fmt.Println("EMPTY")
}
}
return nil
}
// Randomly generate room codes and test them for activity in parallel.
// Parallelism of N_WORKERS threads.
func checkRoomCodesRandom() error {
channel := make(chan Result, N_WORKERS*10)
done := false
for i := 0; i < N_WORKERS; i++ {
go func() {
for {
if done {
return
}
channel <- checkRoomCode(genRoomCode())
}
}()
}
for codesChecked := 1; ; codesChecked++ {
result := <-channel
if result.err != nil {
done = true
return result.err
} else if result.success {
fmt.Printf("\nFOUND ROOM CODE: %s\n", strings.ToUpper(result.roomInfo.RoomCode))
pretty, _ := json.MarshalIndent(result.roomInfo, "", " ")
fmt.Println(string(pretty))
if FIND_FIRST {
done = true
return nil
}
} else {
fmt.Printf("\rRoom codes checked: %d", codesChecked)
}
}
}
func run() error {
// User can manually supply room codes to be checked as positional CLI arguments.
if len(argv) > 0 {
return checkRoomCodesManual(argv)
}
return checkRoomCodesRandom()
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
}