-
Notifications
You must be signed in to change notification settings - Fork 2
/
lunchTracker.go
145 lines (128 loc) · 2.83 KB
/
lunchTracker.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
143
144
145
package main
import (
"os"
"crypto/rand"
"gob"
)
type LunchTracker chan LunchPoll
func (t *LunchTracker) AddPlace(args *StringArgs, place *int) os.Error {
valid, ive := verify(&args.Auth, args)
if !valid {
return ive
}
var ok bool
poll := t.getPoll()
*place, ok = poll.addPlace(args.String, args.Auth.Name)
if !ok {
*place = -1
}
t.persist(poll)
t.putPoll(poll)
return nil
}
func (t *LunchTracker) Comment(args *StringArgs, success *bool) os.Error {
valid, ive := verify(&args.Auth, args)
if !valid {
return ive
}
poll := t.getPoll()
*success = poll.comment(args.String, args.Auth.Name)
t.persist(poll)
t.putPoll(poll)
return nil
}
func (t *LunchTracker) DelPlace(args *IntArgs, success *bool) os.Error {
valid, ive := verify(&args.Auth, args)
if !valid {
return ive
}
poll := t.getPoll()
*success = poll.delPlace(args.Num, args.Auth.Name)
t.persist(poll)
t.putPoll(poll)
return nil
}
func (t *LunchTracker) Drive(args *IntArgs, success *bool) os.Error {
valid, ive := verify(&args.Auth, args)
if !valid {
return ive
}
poll := t.getPoll()
*success = poll.drive(args.Auth.Name, args.Num)
t.persist(poll)
t.putPoll(poll)
return nil
}
func (t *LunchTracker) UnDrive(args *EmptyArgs, success *bool) os.Error {
valid, ive := verify(&args.Auth, args)
if !valid {
return ive
}
poll := t.getPoll()
*success = poll.unDrive(args.Auth.Name)
t.persist(poll)
t.putPoll(poll)
return nil
}
func (t *LunchTracker) Vote(args *IntArgs, success *bool) os.Error {
valid, ive := verify(&args.Auth, args)
if !valid {
return ive
}
poll := t.getPoll()
*success = poll.vote(args.Auth.Name, args.Num)
t.persist(poll)
t.putPoll(poll)
return nil
}
func (t *LunchTracker) UnVote(args *EmptyArgs, success *bool) os.Error {
valid, ive := verify(&args.Auth, args)
if !valid {
return ive
}
poll := t.getPoll()
*success = poll.unVote(args.Auth.Name)
t.persist(poll)
t.putPoll(poll)
return nil
}
func (t *LunchTracker) DisplayPlaces(args *EmptyArgs, response *LunchPoll) os.Error {
valid, ive := verify(&args.Auth, args)
if !valid {
return ive
}
*response = t.getPoll()
t.putPoll(*response)
return nil
}
func (t *LunchTracker) Challenge(name *string, challenge *Bin) os.Error {
_, valid := userMap[(*name)]
if !valid {
valid = checkUser(*name)
if !valid {
return os.NewError("Unknown User")
}
}
*challenge = make(Bin, 512)
n, err := rand.Read(*challenge)
if err != nil || n != 512 {
panic("Challenge Generation Failed")
}
userMap[*name].SChallenge = challenge
return nil
}
func (t *LunchTracker) getPoll() LunchPoll {
return <-*t
}
func (t *LunchTracker) putPoll(l LunchPoll) {
*t <- l
}
func (t *LunchTracker) persist(poll LunchPoll) {
file, err := os.Open(*dataFile, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer file.Close()
encode := gob.NewEncoder(file)
encode.Encode(poll)
}