summaryrefslogtreecommitdiff
path: root/go/mixed-server.go
blob: 2408a44446375ef48b9315f3921196db602d2f3c (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
 
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"net/url"
"regexp"
"os"
"os/signal"
"sort"
"strings"
"sync"
)
 
type WordAndCount struct {
word string
count uint
}
 
func newWordsAndCount(w *map[string]uint) []WordAndCount {
ret := make([]WordAndCount, len(*w))
i := 0
for word, count := range *w {
ret[i] = WordAndCount{word,count}
i++
}
 
return ret
}
 
func (wac WordAndCount) lessThan(other WordAndCount) bool {
return wac.count < other.count
}
 
type Model struct {
lock  sync.RWMutex
words map[string]uint
}
 
func NewModel() Model {
return Model{words: make(map[string]uint)}
}
 
func (m *Model) AddWords(words []string) {
m.lock.Lock()
defer m.lock.Unlock()
for _, word := range words {
v, _ := m.words[word]
m.words[word] = v + 1
}
}
 
func (m *Model) GetMostCommonPairs(how_many uint) []WordAndCount {
m.lock.RLock()
defer m.lock.RUnlock()
wac := newWordsAndCount(&m.words)
sort.SliceStable(wac, func(i,j int) bool {
return ! wac[i].lessThan(wac[j])
})
if how_many >= uint(len(wac)) {
how_many = uint(len(wac))
}
return wac[:how_many]
}
 
type View struct{}
 
func NewView() View {
return View{}
}
func (v View) WordsFromInput(input string) []string {
return strings.Fields(input)
}
func (v View) TableFromRankedPairs(ranked_pairs []WordAndCount) string {
// we're not actually getting pairs, just strings
var b strings.Builder
for _, p := range ranked_pairs {
fmt.Fprintf(&b, "%-15s %3d\n", p.word,p.count)
}
return b.String()
}
 
func NewHttpController(m Model, v View) *http.ServeMux {
controller := http.NewServeMux()
controller.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
uri, _ := url.Parse(req.RequestURI)
q := uri.Query()
n, _ := q["n"]
var how_many uint
if len(n) > 0 {
fmt.Sscanf(n[0], "%d", &how_many)
}
if how_many == 0 {
how_many = 10
}
most_common_pairs := m.GetMostCommonPairs(how_many)
 
table := v.TableFromRankedPairs(most_common_pairs)
strings.NewReader(table + "\n").WriteTo(w)
} else if req.Method == "POST" {
var body strings.Builder
io.Copy(&body, req.Body)
words := v.WordsFromInput(body.String())
m.AddWords(words)
}
})
return controller
}
 
type LineController struct {m Model; v View}
func NewLineController(m Model, v View) LineController {
return LineController{m,v}
}
func (l *LineController) OnCommand(w io.Writer, command string, args string) {
if command == "get" {
l.getWords(w,args)
} else if command == "put" {
l.putWords(w,args)
} else {
l.reply(w,"bad command, only 'get' and 'put'")
        }
}
func (l *LineController) reply(w io.Writer, text string) {
strings.NewReader(text + "\n").WriteTo(w)
}
func (l *LineController) getWords(w io.Writer, args string) {
var how_many uint
fmt.Sscanf(args, "%d", &how_many)
if how_many == 0 {
how_many = 10
}
most_common_pairs := l.m.GetMostCommonPairs(how_many)
table := l.v.TableFromRankedPairs(most_common_pairs)
l.reply(w,table)
}
func (l *LineController) putWords(w io.Writer, args string) {
words := l.v.WordsFromInput(args)
l.m.AddWords(words)
l.reply(w,"ok")
}
 
type TextServer struct {
c LineController
l net.Listener
}
func TextListenAndServe(host string, controller LineController) error {
l, err := net.Listen("tcp",host)
if err != nil {
return err
}
ts := TextServer{controller,l}
ts.Serve()
return nil
}
func (ts *TextServer) Serve() {
for conn,err := ts.l.Accept(); err == nil; conn,err = ts.l.Accept() {
go ts.serveOne(conn)
}
}
func (ts *TextServer) serveOne(conn net.Conn) {
split := regexp.MustCompile(`\s+`)
s := bufio.NewScanner(conn)
for s.Scan() {
parts := split.Split(s.Text(),2)
if len(parts)>1 {
ts.c.OnCommand(conn,parts[0],parts[1])
} else if len(parts)==1 {
ts.c.OnCommand(conn,parts[0],"")
}
}
 
conn.Close()
return
}
 
func main() {
model := NewModel()
view := NewView()
httpcontroller := NewHttpController(model, view)
go http.ListenAndServe("localhost:8080", httpcontroller)
 
linecontroller := NewLineController(model,view)
go TextListenAndServe("localhost:2020",linecontroller)
 
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
<-sigs
}