1 /* reliable-chat - multipath chat
2 * Copyright (C) 2012 Scott Worley <sworley@chkno.net>
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.
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.
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/>.
20 import "container/list"
21 import "encoding/json"
29 var port = flag.Int("port", 21059, "Port to listen on")
30 var localaddress = flag.String("localaddress", "", "Local address to bind to")
31 var max_messages = flag.Int("maxmessages", 1000, "Maximum number of messages to retain")
33 var start_time = expvar.NewInt("start_time")
34 var speak_count = expvar.NewInt("speak_count")
35 var fetch_count = expvar.NewInt("fetch_count")
36 var fetch_wait_count = expvar.NewInt("fetch_wait_count")
37 var fetch_wake_count = expvar.NewInt("fetch_wake_count")
38 var drop_due_to_limit_count = expvar.NewInt("drop_due_to_limit_count")
46 type StoreRequest struct {
48 Messages chan<- []Message
53 Get chan *StoreRequest
56 // TODO: Monotonic clock
58 func manage_store(store Store) {
59 messages := list.New()
65 case new_message, ok := <-store.Add:
70 for waiter := waiting.Front(); waiter != nil; waiter = waiter.Next() {
71 waiter.Value.(*StoreRequest).Messages <- []Message{*new_message}
72 close(waiter.Value.(*StoreRequest).Messages)
73 fetch_wake_count.Add(1)
76 messages.PushBack(new_message)
77 if message_count < *max_messages {
80 messages.Remove(messages.Front())
81 drop_due_to_limit_count.Add(1)
83 case request, ok := <-store.Get:
88 if messages.Back() == nil || !request.StartTime.Before(messages.Back().Value.(*Message).Time) {
89 waiting.PushBack(request)
90 fetch_wait_count.Add(1)
92 start := messages.Back()
94 if messages.Front().Value.(*Message).Time.After(request.StartTime) {
95 start = messages.Front()
96 response_size = message_count
98 for start.Prev().Value.(*Message).Time.After(request.StartTime) {
103 response_messages := make([]Message, 0, response_size)
104 for m := start; m != nil; m = m.Next() {
105 response_messages = append(response_messages, *m.Value.(*Message))
107 request.Messages <- response_messages
113 func start_store() Store {
114 store := Store{make(chan *Message, 20), make(chan *StoreRequest, 20)}
115 go manage_store(store)
119 const robots_txt = `User-agent: *
123 func start_server(store Store) {
124 http.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
126 url_since := r.FormValue("since")
128 err := json.Unmarshal([]byte(url_since), &since)
130 log.Print("fetch: parse since: ", err)
131 w.WriteHeader(http.StatusBadRequest)
132 w.Write([]byte("Could not parse since as date"))
136 messages_from_store := make(chan []Message, 1)
137 store.Get <- &StoreRequest{since, messages_from_store}
139 json_encoded, err := json.Marshal(<-messages_from_store)
141 log.Print("json encode: ", err)
142 w.WriteHeader(http.StatusInternalServerError)
145 w.Header().Add("Content-Type", "application/json")
146 w.Header().Add("Access-Control-Allow-Origin", "*")
147 w.Write(json_encoded)
150 http.HandleFunc("/speak", func(w http.ResponseWriter, r *http.Request) {
151 store.Add <- &Message{
157 http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
158 w.Write([]byte(robots_txt))
161 log.Fatal(http.ListenAndServe(*localaddress+":"+strconv.Itoa(*port), nil))
166 start_time.Set(time.Now().UnixNano())
167 store := start_store()