3 import "container/list"
12 var port = flag.Int("port", 21059, "Port to listen on")
14 var frame_count = expvar.NewInt("frame_count")
15 var speak_count = expvar.NewInt("speak_count")
16 var fetch_count = expvar.NewInt("fetch_count")
17 var fetch_wait_count = expvar.NewInt("fetch_wait_count")
18 var fetch_wake_count = expvar.NewInt("fetch_wake_count")
26 type StoreRequest struct {
28 Messages chan<- []Message
33 Get chan *StoreRequest
36 // TODO: Monotonic clock
38 func manage_store(store Store) {
39 messages := list.New()
46 case new_message, ok := <-store.Add:
51 for waiter := waiting.Front(); waiter != nil; waiter = waiter.Next() {
52 waiter.Value.(*StoreRequest).Messages <- []Message{*new_message}
53 close(waiter.Value.(*StoreRequest).Messages)
54 fetch_wake_count.Add(1)
57 messages.PushBack(new_message)
58 if message_count < max_messages {
61 messages.Remove(messages.Front())
63 case request, ok := <-store.Get:
68 if messages.Back() == nil || !request.StartTime.Before(messages.Back().Value.(*Message).Time) {
69 waiting.PushBack(request)
70 fetch_wait_count.Add(1)
72 start := messages.Back()
74 if messages.Front().Value.(*Message).Time.After(request.StartTime) {
75 start = messages.Front()
76 response_size = message_count
78 for start.Prev().Value.(*Message).Time.After(request.StartTime) {
83 response_messages := make([]Message, 0, response_size)
84 for m := start; m != nil; m = m.Next() {
85 response_messages = append(response_messages, *m.Value.(*Message))
87 request.Messages <- response_messages
93 func start_store() Store {
94 store := Store{make(chan *Message, 20), make(chan *StoreRequest, 20)}
95 go manage_store(store)
99 const frame_html = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
100 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
102 <html xmlns="http://www.w3.org/1999/xhtml">
104 <script type="text/javascript"><!--//--><![CDATA[//><!--
108 var xhr = new XMLHttpRequest();
109 xhr.onreadystatechange = function() {
110 if (this.readyState == this.DONE) {
111 if (this.status == 200) {
112 var rtxt = this.responseText;
114 var r = JSON.parse(rtxt);
116 window.parent.postMessage(rtxt, "*");
118 if (r.length >= 1 && "Time" in r[r.length-1]) {
119 since = r[r.length-1]["Time"];
124 window.setTimeout(go, delay);
129 uri += '?since="' + since + '"';
131 xhr.open("GET", uri);
141 func start_server(store Store) {
142 http.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
144 url_since := r.FormValue("since")
146 err := json.Unmarshal([]byte(url_since), &since)
148 log.Print("fetch: parse since: ", err)
149 w.WriteHeader(http.StatusBadRequest)
150 w.Write([]byte("Could not parse since as date"))
154 messages_from_store := make(chan []Message, 1)
155 store.Get <- &StoreRequest{since, messages_from_store}
157 json_encoded, err := json.Marshal(<-messages_from_store)
159 log.Print("json encode: ", err)
160 w.WriteHeader(http.StatusInternalServerError)
163 w.Header().Add("Content-Type", "application/json")
164 w.Header().Add("Access-Control-Allow-Origin", "*")
165 w.Write(json_encoded)
168 http.HandleFunc("/speak", func(w http.ResponseWriter, r *http.Request) {
169 store.Add <- &Message{
175 http.HandleFunc("/frame", func(w http.ResponseWriter, r *http.Request) {
177 w.Write([]byte(frame_html));
180 log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
185 store := start_store()