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 var monotonic_clock chan chan time.Time
58 const minimum_clock_increment = time.Millisecond
61 internal_monotonic_clock := make(chan chan time.Time, 1)
63 last_time := time.Now()
67 case request, ok := <-internal_monotonic_clock:
71 earliest_acceptable_time := last_time.Add(minimum_clock_increment)
72 current_time := time.Now()
73 if current_time.Before(earliest_acceptable_time) {
74 current_time = earliest_acceptable_time
76 request <- current_time
77 last_time = current_time
81 monotonic_clock = internal_monotonic_clock
84 func now() time.Time {
85 c := make(chan time.Time, 0)
90 func manage_store(store Store) {
91 messages := list.New()
97 case new_message, ok := <-store.Add:
102 for waiter := waiting.Front(); waiter != nil; waiter = waiter.Next() {
103 waiter.Value.(*StoreRequest).Messages <- []Message{*new_message}
104 close(waiter.Value.(*StoreRequest).Messages)
105 fetch_wake_count.Add(1)
108 messages.PushBack(new_message)
109 if message_count < *max_messages {
112 messages.Remove(messages.Front())
113 drop_due_to_limit_count.Add(1)
115 case request, ok := <-store.Get:
120 if messages.Back() == nil || !request.StartTime.Before(messages.Back().Value.(*Message).Time) {
121 waiting.PushBack(request)
122 fetch_wait_count.Add(1)
124 start := messages.Back()
126 if messages.Front().Value.(*Message).Time.After(request.StartTime) {
127 start = messages.Front()
128 response_size = message_count
130 for start.Prev().Value.(*Message).Time.After(request.StartTime) {
135 response_messages := make([]Message, 0, response_size)
136 for m := start; m != nil; m = m.Next() {
137 response_messages = append(response_messages, *m.Value.(*Message))
139 request.Messages <- response_messages
145 func start_store() Store {
146 store := Store{make(chan *Message, 20), make(chan *StoreRequest, 20)}
147 go manage_store(store)
151 const robots_txt = `User-agent: *
155 func start_server(store Store) {
156 http.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
158 url_since := r.FormValue("since")
160 err := json.Unmarshal([]byte(url_since), &since)
162 log.Print("fetch: parse since: ", err)
163 w.WriteHeader(http.StatusBadRequest)
164 w.Write([]byte("Could not parse since as date"))
168 messages_from_store := make(chan []Message, 1)
169 store.Get <- &StoreRequest{since, messages_from_store}
171 json_encoded, err := json.Marshal(<-messages_from_store)
173 log.Print("json encode: ", err)
174 w.WriteHeader(http.StatusInternalServerError)
177 w.Header().Add("Content-Type", "application/json")
178 w.Header().Add("Access-Control-Allow-Origin", "*")
179 w.Write(json_encoded)
182 http.HandleFunc("/speak", func(w http.ResponseWriter, r *http.Request) {
183 store.Add <- &Message{
187 w.Header().Add("Access-Control-Allow-Origin", "*")
190 http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
191 w.Write([]byte(robots_txt))
194 log.Fatal(http.ListenAndServe(*localaddress+":"+strconv.Itoa(*port), nil))
200 start_time.Set(now().UnixNano())
201 store := start_store()