Skip to content

Commit

Permalink
log/retrieve history
Browse files Browse the repository at this point in the history
  • Loading branch information
hyangah committed Sep 20, 2015
1 parent 59ce7a6 commit a06d3a7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
43 changes: 38 additions & 5 deletions chat/app/chatroom/chatroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package chatroom

import (
"container/list"
"fmt"
"strings"
"time"
)

Expand Down Expand Up @@ -74,13 +76,44 @@ func chatroom() {
ch <- Subscription{events, subscriber}

case event := <-publish:
for ch := subscribers.Front(); ch != nil; ch = ch.Next() {
ch.Value.(chan Event) <- event

send := func(e Event) {
for ch := subscribers.Front(); ch != nil; ch = ch.Next() {
ch.Value.(chan Event) <- event
}
}
if archive.Len() >= archiveSize {
archive.Remove(archive.Front())
s := strings.SplitN(event.Text, " ", 3)
var begin time.Time
if s[0] == "/replay" {
if len(s) > 1 {
d, err := time.ParseDuration(s[1])
if err == nil {
begin = time.Now().Add(-1 * d)
}
}
logs, err := Retrieve(begin, time.Now())
if err != nil {
event.Text = fmt.Sprintf("failed to retrieve the log: %v", err)
send(event)
} else {
for _, l := range logs {
event.Text = fmt.Sprintf("%s", l)
send(event)
}
}
} else {
// TODO: event.Type (join/leave)
if event.Text != "" {
Log(time.Now(), event.User+":"+event.Text)
}
for ch := subscribers.Front(); ch != nil; ch = ch.Next() {
ch.Value.(chan Event) <- event
}
if archive.Len() >= archiveSize {
archive.Remove(archive.Front())
}
archive.PushBack(event)
}
archive.PushBack(event)

case unsub := <-unsubscribe:
for ch := subscribers.Front(); ch != nil; ch = ch.Next() {
Expand Down
2 changes: 2 additions & 0 deletions chat/app/chatroom/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func Retrieve(begin, end time.Time) ([]string, error) {
max := []byte(end.Format(time.RFC3339))
for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
// TODO: k? (timestamp)
log.Printf("appending %s: %s", k, v)
res = append(res, fmt.Sprintf("%s: %s", k, v))
}
return nil
Expand All @@ -53,5 +54,6 @@ func Retrieve(begin, end time.Time) ([]string, error) {
if err != nil {
return nil, err
}
log.Print("returning data: ", res)
return res, nil
}
2 changes: 1 addition & 1 deletion chat/app/controllers/longpolling.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package controllers

import (
"github.com/revel/revel"
"github.com/revel/samples/chat/app/chatroom"
"github.com/womenwhogonyc/samples/chat/app/chatroom"
)

type LongPolling struct {
Expand Down
4 changes: 2 additions & 2 deletions chat/app/controllers/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package controllers

import (
"github.com/revel/revel"
"github.com/revel/samples/chat/app/chatroom"
"github.com/revel/samples/chat/app/routes"
"github.com/womenwhogonyc/samples/chat/app/chatroom"
"github.com/womenwhogonyc/samples/chat/app/routes"
)

type Refresh struct {
Expand Down
4 changes: 2 additions & 2 deletions chat/app/controllers/websocket.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package controllers

import (
"golang.org/x/net/websocket"
"github.com/revel/revel"
"github.com/revel/samples/chat/app/chatroom"
"github.com/womenwhogonyc/samples/chat/app/chatroom"
"golang.org/x/net/websocket"
)

type WebSocket struct {
Expand Down

0 comments on commit a06d3a7

Please sign in to comment.