]> git.scottworley.com Git - reliable-chat/blame - server/server.go
Remove /frame support
[reliable-chat] / server / server.go
CommitLineData
520c21fd
SW
1/* reliable-chat - multipath chat
2 * Copyright (C) 2012 Scott Worley <sworley@chkno.net>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
92d1d6ac
SW
18package main
19
20import "container/list"
21import "encoding/json"
03513e5c 22import "expvar"
24a546cf 23import "flag"
92d1d6ac
SW
24import "log"
25import "net/http"
24a546cf 26import "strconv"
92d1d6ac
SW
27import "time"
28
24a546cf 29var port = flag.Int("port", 21059, "Port to listen on")
270d2ae5 30var localaddress = flag.String("localaddress", "", "Local address to bind to")
24a546cf 31
03513e5c
SW
32var speak_count = expvar.NewInt("speak_count")
33var fetch_count = expvar.NewInt("fetch_count")
34var fetch_wait_count = expvar.NewInt("fetch_wait_count")
35var fetch_wake_count = expvar.NewInt("fetch_wake_count")
36
92d1d6ac
SW
37type Message struct {
38 Time time.Time
b199796a 39 ID string
92d1d6ac
SW
40 Text string
41}
42
92d1d6ac
SW
43type StoreRequest struct {
44 StartTime time.Time
45 Messages chan<- []Message
46}
47
48type Store struct {
fa5e7c1b
SW
49 Add chan *Message
50 Get chan *StoreRequest
92d1d6ac
SW
51}
52
53// TODO: Monotonic clock
54
55func manage_store(store Store) {
56 messages := list.New()
57 message_count := 0
58 max_messages := 1000
59 waiting := list.New()
c282d878 60main:
92d1d6ac
SW
61 for {
62 select {
c282d878
SW
63 case new_message, ok := <-store.Add:
64 if !ok {
65 break main
66 }
03513e5c 67 speak_count.Add(1)
92d1d6ac 68 for waiter := waiting.Front(); waiter != nil; waiter = waiter.Next() {
fa5e7c1b
SW
69 waiter.Value.(*StoreRequest).Messages <- []Message{*new_message}
70 close(waiter.Value.(*StoreRequest).Messages)
03513e5c 71 fetch_wake_count.Add(1)
92d1d6ac
SW
72 }
73 waiting.Init()
03513e5c 74 messages.PushBack(new_message)
92d1d6ac
SW
75 if message_count < max_messages {
76 message_count++
77 } else {
78 messages.Remove(messages.Front())
79 }
c282d878
SW
80 case request, ok := <-store.Get:
81 if !ok {
82 break main
83 }
03513e5c 84 fetch_count.Add(1)
fa5e7c1b 85 if messages.Back() == nil || !request.StartTime.Before(messages.Back().Value.(*Message).Time) {
92d1d6ac 86 waiting.PushBack(request)
03513e5c 87 fetch_wait_count.Add(1)
92d1d6ac
SW
88 } else {
89 start := messages.Back()
90 response_size := 1
fa5e7c1b 91 if messages.Front().Value.(*Message).Time.After(request.StartTime) {
92d1d6ac
SW
92 start = messages.Front()
93 response_size = message_count
94 } else {
fa5e7c1b 95 for start.Prev().Value.(*Message).Time.After(request.StartTime) {
92d1d6ac
SW
96 start = start.Prev()
97 response_size++
98 }
99 }
100 response_messages := make([]Message, 0, response_size)
101 for m := start; m != nil; m = m.Next() {
fa5e7c1b 102 response_messages = append(response_messages, *m.Value.(*Message))
92d1d6ac
SW
103 }
104 request.Messages <- response_messages
105 }
106 }
107 }
108}
109
110func start_store() Store {
fa5e7c1b 111 store := Store{make(chan *Message, 20), make(chan *StoreRequest, 20)}
92d1d6ac
SW
112 go manage_store(store)
113 return store
114}
115
e3e35bbe
SW
116const robots_txt = `User-agent: *
117Disallow: /
118`
119
92d1d6ac
SW
120func start_server(store Store) {
121 http.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
87ac1d98
SW
122 var since time.Time
123 url_since := r.FormValue("since")
124 if url_since != "" {
125 err := json.Unmarshal([]byte(url_since), &since)
126 if err != nil {
127 log.Print("fetch: parse since: ", err)
128 w.WriteHeader(http.StatusBadRequest)
129 w.Write([]byte("Could not parse since as date"))
130 return
131 }
132 }
92d1d6ac 133 messages_from_store := make(chan []Message, 1)
fa5e7c1b 134 store.Get <- &StoreRequest{since, messages_from_store}
92d1d6ac 135
9b33d853 136 json_encoded, err := json.Marshal(<-messages_from_store)
92d1d6ac
SW
137 if err != nil {
138 log.Print("json encode: ", err)
139 w.WriteHeader(http.StatusInternalServerError)
140 return
141 }
142 w.Header().Add("Content-Type", "application/json")
79ced6f1 143 w.Header().Add("Access-Control-Allow-Origin", "*")
92d1d6ac
SW
144 w.Write(json_encoded)
145 })
146
147 http.HandleFunc("/speak", func(w http.ResponseWriter, r *http.Request) {
b199796a
SW
148 store.Add <- &Message{
149 time.Now(),
150 r.FormValue("id"),
151 r.FormValue("text")}
92d1d6ac
SW
152 })
153
e3e35bbe
SW
154 http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
155 w.Write([]byte(robots_txt));
156 })
157
270d2ae5 158 log.Fatal(http.ListenAndServe(*localaddress+":"+strconv.Itoa(*port), nil))
92d1d6ac
SW
159}
160
161func main() {
bc44b6bc 162 flag.Parse()
92d1d6ac
SW
163 store := start_store()
164 start_server(store)
165}