-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
62 lines (48 loc) · 1.1 KB
/
main.go
File metadata and controls
62 lines (48 loc) · 1.1 KB
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
package main
import (
"context"
"fmt"
"log"
"net"
"strings"
"github.com/tlgs/protohackers/internal/protohackers"
)
type UnusualDatabaseProgram struct{ *protohackers.Config }
func (UnusualDatabaseProgram) Setup() context.Context { return context.TODO() }
func (UnusualDatabaseProgram) Handle(_ context.Context, conn net.Conn) {
defer conn.Close()
udpConn, ok := conn.(*net.UDPConn)
if !ok {
log.Println("bad connection type")
return
}
var store = map[string]string{"version": "Ken's Key-Value Store 1.0.0"}
buf := make([]byte, 1024)
for {
n, addr, err := udpConn.ReadFrom(buf)
if err != nil {
log.Print(err)
continue
}
request := string(buf[:n])
log.Printf("%v %v", addr, request)
k, v, insert := strings.Cut(request, "=")
if insert {
if k == "version" {
continue
}
store[k] = v
} else {
response := fmt.Sprintf("%v=%v", k, store[k])
_, err := udpConn.WriteTo([]byte(response), addr)
if err != nil {
log.Print(err)
}
}
}
}
func main() {
cfg := protohackers.NewConfig(10004)
cfg.ParseFlags()
protohackers.RunUDP(UnusualDatabaseProgram{cfg})
}