-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (113 loc) · 3.32 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import "flag"
// golParams provides the details of how to run the Game of Life and which image to load.
type golParams struct {
turns int
threads int
imageWidth int
imageHeight int
}
// ioCommand allows requesting behaviour from the io (pgm) goroutine.
type ioCommand uint8
// This is a way of creating enums in Go.
// It will evaluate to:
// ioOutput = 0
// ioInput = 1
// ioCheckIdle = 2
const (
ioOutput ioCommand = iota
ioInput
ioCheckIdle
)
// cell is used as the return type for the testing framework.
type cell struct {
x, y int
}
// distributorToIo defines all chans that the distributor goroutine will have to communicate with the io goroutine.
// Note the restrictions on chans being send-only or receive-only to prevent bugs.
type distributorToIo struct {
command chan<- ioCommand
idle <-chan bool
filename chan<- string
inputVal <-chan uint8
outputVal chan<- uint8
keyChan <-chan rune
timeChan chan bool
}
// ioToDistributor defines all chans that the io goroutine will have to communicate with the distributor goroutine.
// Note the restrictions on chans being send-only or receive-only to prevent bugs.
type ioToDistributor struct {
command <-chan ioCommand
idle chan<- bool
filename <-chan string
inputVal chan<- uint8
outputVal <-chan uint8
}
// distributorChans stores all the chans that the distributor goroutine will use.
type distributorChans struct {
io distributorToIo
}
// ioChans stores all the chans that the io goroutine will use.
type ioChans struct {
distributor ioToDistributor
}
// gameOfLife is the function called by the testing framework.
// It makes some channels and starts relevant goroutines.
// It places the created channels in the relevant structs.
// It returns an array of alive cells returned by the distributor.
func gameOfLife(p golParams, keyChan <-chan rune, timeChan chan bool) []cell {
var dChans distributorChans
var ioChans ioChans
ioCommand := make(chan ioCommand)
dChans.io.command = ioCommand
ioChans.distributor.command = ioCommand
ioIdle := make(chan bool)
dChans.io.idle = ioIdle
ioChans.distributor.idle = ioIdle
ioFilename := make(chan string)
dChans.io.filename = ioFilename
ioChans.distributor.filename = ioFilename
inputVal := make(chan uint8)
dChans.io.inputVal = inputVal
ioChans.distributor.inputVal = inputVal
outputVal := make(chan uint8)
dChans.io.outputVal = outputVal
ioChans.distributor.outputVal = outputVal
dChans.io.keyChan = keyChan
dChans.io.timeChan = timeChan
aliveCells := make(chan []cell)
go distributor(p, dChans, aliveCells)
go pgmIo(p, ioChans)
alive := <-aliveCells
return alive
}
// main is the function called when starting Game of Life with 'make gol'
// Do not edit until Stage 2.
func main() {
var params golParams
keyChan := make(chan rune)
timeChan := make(chan bool)
go getKeyboardCommand(keyChan)
go timerControl(timeChan)
flag.IntVar(
¶ms.threads,
"t",
8,
"Specify the number of worker threads to use. Defaults to 8.")
flag.IntVar(
¶ms.imageWidth,
"w",
512,
"Specify the width of the image. Defaults to 512.")
flag.IntVar(
¶ms.imageHeight,
"h",
512,
"Specify the height of the image. Defaults to 512.")
flag.Parse()
params.turns = 10000000000
startControlServer(params)
gameOfLife(params, keyChan, timeChan)
StopControlServer()
}
//Onion