cwtch/storage/message_store.go

80 lines
1.8 KiB
Go
Raw Normal View History

2018-03-09 20:44:13 +00:00
package storage
import (
"bufio"
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/protocol"
2018-03-09 20:44:13 +00:00
"encoding/json"
"fmt"
"log"
2018-03-09 20:44:13 +00:00
"os"
"sync"
)
2018-05-16 21:11:04 +00:00
// MessageStoreInterface defines an interface to interact with a store of cwtch messages.
2018-03-09 20:44:13 +00:00
type MessageStoreInterface interface {
AddMessage(protocol.GroupMessage)
FetchMessages() []*protocol.GroupMessage
}
2018-05-16 21:11:04 +00:00
// MessageStore is a file-backed implementation of MessageStoreInterface
2018-03-09 20:44:13 +00:00
type MessageStore struct {
file *os.File
lock sync.Mutex
messages []*protocol.GroupMessage
}
2018-05-16 21:11:04 +00:00
// Close closes the message store and underlying resources.
2018-03-09 20:44:13 +00:00
func (ms *MessageStore) Close() {
ms.lock.Lock()
2018-03-09 20:44:13 +00:00
ms.messages = nil
ms.file.Close()
ms.lock.Unlock()
2018-03-09 20:44:13 +00:00
}
2018-05-16 21:11:04 +00:00
// Init sets up a MessageStore backed by filename
2018-03-09 20:44:13 +00:00
func (ms *MessageStore) Init(filename string) {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
if err != nil {
panic(err)
}
ms.file = f
scanner := bufio.NewScanner(f)
for scanner.Scan() {
gms := scanner.Text()
gm := &protocol.GroupMessage{}
err := json.Unmarshal([]byte(gms), gm)
if err == nil {
ms.messages = append(ms.messages, gm)
} else {
panic(err)
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
}
2018-05-16 21:11:04 +00:00
// FetchMessages returns all messages from the backing file.
2018-03-09 20:44:13 +00:00
func (ms *MessageStore) FetchMessages() (messages []*protocol.GroupMessage) {
messages = make([]*protocol.GroupMessage, len(ms.messages))
ms.lock.Lock()
copy(messages, ms.messages)
ms.lock.Unlock()
return
}
2018-05-16 21:11:04 +00:00
// AddMessage adds a GroupMessage to the store
2018-03-09 20:44:13 +00:00
func (ms *MessageStore) AddMessage(gm protocol.GroupMessage) {
ms.lock.Lock()
ms.messages = append(ms.messages, &gm)
s, err := json.Marshal(gm)
if err != nil {
log.Printf("[ERROR] Failed to unmarshal group message %v\n", err)
}
2018-03-09 20:44:13 +00:00
fmt.Fprintf(ms.file, "%s\n", s)
ms.lock.Unlock()
}