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, version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import "container/list"
20 import "encoding/json"
28 var port = flag.Int("port", 21059, "Port to listen on")
29 var localaddress = flag.String("localaddress", "", "Local address to bind to")
30 var max_messages = flag.Int("maxmessages", 1000, "Maximum number of messages to retain")
32 var start_time = expvar.NewInt("start_time")
33 var speak_count = expvar.NewInt("speak_count")
34 var fetch_count = expvar.NewInt("fetch_count")
35 var fetch_wait_count = expvar.NewInt("fetch_wait_count")
36 var fetch_wake_count = expvar.NewInt("fetch_wake_count")
37 var drop_due_to_limit_count = expvar.NewInt("drop_due_to_limit_count")
45 type StoreRequest struct {
47 Messages chan<- []Message
52 Get chan *StoreRequest
55 var monotonic_clock chan chan time.Time
57 const minimum_clock_increment = time.Millisecond
60 internal_monotonic_clock := make(chan chan time.Time, 1)
62 last_time := time.Now()
66 case request, ok := <-internal_monotonic_clock:
70 earliest_acceptable_time := last_time.Add(minimum_clock_increment)
71 current_time := time.Now()
72 if current_time.Before(earliest_acceptable_time) {
73 current_time = earliest_acceptable_time
75 request <- current_time
76 last_time = current_time
80 monotonic_clock = internal_monotonic_clock
83 func now() time.Time {
84 c := make(chan time.Time, 0)
89 func manage_store(store Store) {
90 messages := list.New()
96 case new_message, ok := <-store.Add:
101 for waiter := waiting.Front(); waiter != nil; waiter = waiter.Next() {
102 waiter.Value.(*StoreRequest).Messages <- []Message{*new_message}
103 close(waiter.Value.(*StoreRequest).Messages)
104 fetch_wake_count.Add(1)
107 messages.PushBack(new_message)
108 if message_count < *max_messages {
111 messages.Remove(messages.Front())
112 drop_due_to_limit_count.Add(1)
114 case request, ok := <-store.Get:
119 if messages.Back() == nil || !request.StartTime.Before(messages.Back().Value.(*Message).Time) {
120 waiting.PushBack(request)
121 fetch_wait_count.Add(1)
123 start := messages.Back()
125 if messages.Front().Value.(*Message).Time.After(request.StartTime) {
126 start = messages.Front()
127 response_size = message_count
129 for start.Prev().Value.(*Message).Time.After(request.StartTime) {
134 response_messages := make([]Message, 0, response_size)
135 for m := start; m != nil; m = m.Next() {
136 response_messages = append(response_messages, *m.Value.(*Message))
138 request.Messages <- response_messages
144 func start_store() Store {
145 store := Store{make(chan *Message, 20), make(chan *StoreRequest, 20)}
146 go manage_store(store)
150 const robots_txt = `User-agent: *
154 func start_server(store Store) {
155 http.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
157 url_since := r.FormValue("since")
159 err := json.Unmarshal([]byte(url_since), &since)
161 log.Print("fetch: parse since: ", err)
162 w.WriteHeader(http.StatusBadRequest)
163 w.Write([]byte("Could not parse since as date"))
167 messages_from_store := make(chan []Message, 1)
168 store.Get <- &StoreRequest{since, messages_from_store}
170 json_encoded, err := json.Marshal(<-messages_from_store)
172 log.Print("json encode: ", err)
173 w.WriteHeader(http.StatusInternalServerError)
176 w.Header().Add("Content-Type", "application/json")
177 w.Header().Add("Access-Control-Allow-Origin", "*")
178 w.Write(json_encoded)
181 http.HandleFunc("/speak", func(w http.ResponseWriter, r *http.Request) {
182 store.Add <- &Message{
186 w.Header().Add("Access-Control-Allow-Origin", "*")
189 http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
190 w.Write([]byte(robots_txt))
193 log.Fatal(http.ListenAndServe(*localaddress+":"+strconv.Itoa(*port), nil))
199 start_time.Set(now().UnixNano())
200 store := start_store()