3 import "container/list"
12 var port = flag.Int("port", 21059, "Port to listen on")
14 var speak_count = expvar.NewInt("speak_count")
15 var fetch_count = expvar.NewInt("fetch_count")
16 var fetch_wait_count = expvar.NewInt("fetch_wait_count")
17 var fetch_wake_count = expvar.NewInt("fetch_wake_count")
25 type StoreRequest struct {
27 Messages chan<- []Message
32 Get chan *StoreRequest
35 // TODO: Monotonic clock
37 func manage_store(store Store) {
38 messages := list.New()
45 case new_message, ok := <-store.Add:
50 for waiter := waiting.Front(); waiter != nil; waiter = waiter.Next() {
51 waiter.Value.(*StoreRequest).Messages <- []Message{*new_message}
52 close(waiter.Value.(*StoreRequest).Messages)
53 fetch_wake_count.Add(1)
56 messages.PushBack(new_message)
57 if message_count < max_messages {
60 messages.Remove(messages.Front())
62 case request, ok := <-store.Get:
67 if messages.Back() == nil || !request.StartTime.Before(messages.Back().Value.(*Message).Time) {
68 waiting.PushBack(request)
69 fetch_wait_count.Add(1)
71 start := messages.Back()
73 if messages.Front().Value.(*Message).Time.After(request.StartTime) {
74 start = messages.Front()
75 response_size = message_count
77 for start.Prev().Value.(*Message).Time.After(request.StartTime) {
82 response_messages := make([]Message, 0, response_size)
83 for m := start; m != nil; m = m.Next() {
84 response_messages = append(response_messages, *m.Value.(*Message))
86 request.Messages <- response_messages
92 func start_store() Store {
93 store := Store{make(chan *Message, 20), make(chan *StoreRequest, 20)}
94 go manage_store(store)
98 const frame_html = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
99 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
101 <html xmlns="http://www.w3.org/1999/xhtml">
103 <script type="text/javascript"><!--//--><![CDATA[//><!--
107 var xhr = new XMLHttpRequest();
108 xhr.onreadystatechange = function() {
109 if (this.readyState == this.DONE) {
110 if (this.status == 200) {
111 var rtxt = this.responseText;
113 var r = JSON.parse(rtxt);
115 window.parent.postMessage(rtxt, "*");
117 if (r.length >= 1 && "Time" in r[r.length-1]) {
118 since = r[r.length-1]["Time"];
123 window.setTimeout(go, delay);
128 uri += '?since="' + since + '"';
130 xhr.open("GET", uri);
140 func start_server(store Store) {
141 http.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
143 url_since := r.FormValue("since")
145 err := json.Unmarshal([]byte(url_since), &since)
147 log.Print("fetch: parse since: ", err)
148 w.WriteHeader(http.StatusBadRequest)
149 w.Write([]byte("Could not parse since as date"))
153 messages_from_store := make(chan []Message, 1)
154 store.Get <- &StoreRequest{since, messages_from_store}
156 json_encoded, err := json.Marshal(<-messages_from_store)
158 log.Print("json encode: ", err)
159 w.WriteHeader(http.StatusInternalServerError)
162 w.Header().Add("Content-Type", "application/json")
163 w.Write(json_encoded)
166 http.HandleFunc("/speak", func(w http.ResponseWriter, r *http.Request) {
167 store.Add <- &Message{
173 http.HandleFunc("/frame", func(w http.ResponseWriter, r *http.Request) {
174 w.Write([]byte(frame_html));
177 log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
181 store := start_store()