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