]> git.scottworley.com Git - reliable-chat/blame - server/server.go
Copy less: pass messages by pointer
[reliable-chat] / server / server.go
CommitLineData
92d1d6ac
SW
1package main
2
3import "container/list"
4import "encoding/json"
5import "log"
6import "net/http"
7import "time"
8
9type Message struct {
10 Time time.Time
11 Text string
12}
13
92d1d6ac
SW
14type StoreRequest struct {
15 StartTime time.Time
16 Messages chan<- []Message
17}
18
19type Store struct {
fa5e7c1b
SW
20 Add chan *Message
21 Get chan *StoreRequest
92d1d6ac
SW
22}
23
24// TODO: Monotonic clock
25
26func manage_store(store Store) {
27 messages := list.New()
28 message_count := 0
29 max_messages := 1000
30 waiting := list.New()
31 for {
32 select {
33 case new_message := <-store.Add:
34 messages.PushBack(new_message)
35 for waiter := waiting.Front(); waiter != nil; waiter = waiter.Next() {
fa5e7c1b
SW
36 waiter.Value.(*StoreRequest).Messages <- []Message{*new_message}
37 close(waiter.Value.(*StoreRequest).Messages)
92d1d6ac
SW
38 }
39 waiting.Init()
40 if message_count < max_messages {
41 message_count++
42 } else {
43 messages.Remove(messages.Front())
44 }
45 case request := <-store.Get:
fa5e7c1b 46 if messages.Back() == nil || !request.StartTime.Before(messages.Back().Value.(*Message).Time) {
92d1d6ac
SW
47 waiting.PushBack(request)
48 } else {
49 start := messages.Back()
50 response_size := 1
fa5e7c1b 51 if messages.Front().Value.(*Message).Time.After(request.StartTime) {
92d1d6ac
SW
52 start = messages.Front()
53 response_size = message_count
54 } else {
fa5e7c1b 55 for start.Prev().Value.(*Message).Time.After(request.StartTime) {
92d1d6ac
SW
56 start = start.Prev()
57 response_size++
58 }
59 }
60 response_messages := make([]Message, 0, response_size)
61 for m := start; m != nil; m = m.Next() {
fa5e7c1b 62 response_messages = append(response_messages, *m.Value.(*Message))
92d1d6ac
SW
63 }
64 request.Messages <- response_messages
65 }
66 }
67 }
68}
69
70func start_store() Store {
fa5e7c1b 71 store := Store{make(chan *Message, 20), make(chan *StoreRequest, 20)}
92d1d6ac
SW
72 go manage_store(store)
73 return store
74}
75
76func start_server(store Store) {
77 http.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
87ac1d98
SW
78 var since time.Time
79 url_since := r.FormValue("since")
80 if url_since != "" {
81 err := json.Unmarshal([]byte(url_since), &since)
82 if err != nil {
83 log.Print("fetch: parse since: ", err)
84 w.WriteHeader(http.StatusBadRequest)
85 w.Write([]byte("Could not parse since as date"))
86 return
87 }
88 }
92d1d6ac 89 messages_from_store := make(chan []Message, 1)
fa5e7c1b 90 store.Get <- &StoreRequest{since, messages_from_store}
92d1d6ac 91
9b33d853 92 json_encoded, err := json.Marshal(<-messages_from_store)
92d1d6ac
SW
93 if err != nil {
94 log.Print("json encode: ", err)
95 w.WriteHeader(http.StatusInternalServerError)
96 return
97 }
98 w.Header().Add("Content-Type", "application/json")
99 w.Write(json_encoded)
100 })
101
102 http.HandleFunc("/speak", func(w http.ResponseWriter, r *http.Request) {
fa5e7c1b 103 store.Add <- &Message{time.Now(), r.FormValue("text")}
92d1d6ac
SW
104 })
105
106 log.Fatal(http.ListenAndServe(":8080", nil))
107}
108
109func main() {
110 store := start_store()
111 start_server(store)
112}