2015-02-11 20:19:46 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2015-02-18 20:33:17 -07:00
|
|
|
"github.com/go-fsnotify/fsnotify"
|
|
|
|
"io/ioutil"
|
2015-02-11 20:19:46 -07:00
|
|
|
"os"
|
2015-02-18 20:33:17 -07:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2015-02-11 20:19:46 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Network struct {
|
|
|
|
name string
|
|
|
|
currentLog string
|
|
|
|
lineno int64
|
|
|
|
|
|
|
|
basePath string
|
2015-02-18 20:33:17 -07:00
|
|
|
seq int
|
2015-02-11 20:19:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Update struct {
|
|
|
|
Lines []string
|
|
|
|
LastEventId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNetwork(basePath string) (*Network) {
|
|
|
|
return &Network{
|
|
|
|
basePath: basePath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nw *Network) LastEventId() string {
|
|
|
|
return fmt.Sprintf("%s:%s:%d", nw.name, nw.currentLog, nw.lineno)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nw *Network) SetPosition(filename string, lineno int64) {
|
|
|
|
nw.currentLog = filename
|
|
|
|
nw.lineno = lineno
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nw *Network) Tail(out chan<- *Update) error {
|
|
|
|
if nw.currentLog == "" {
|
2015-02-18 20:33:17 -07:00
|
|
|
var err error
|
|
|
|
|
2015-02-11 20:19:46 -07:00
|
|
|
currentfn := path.Join(nw.basePath, "log", "current")
|
2015-02-18 20:33:17 -07:00
|
|
|
nw.currentLog, err = os.Readlink(currentfn)
|
2015-02-11 20:19:46 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath := path.Join(nw.basePath, "log", nw.currentLog)
|
|
|
|
f, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
2015-02-18 20:33:17 -07:00
|
|
|
watcher.Add(filepath)
|
2015-02-11 20:19:46 -07:00
|
|
|
|
2015-02-20 16:48:52 -07:00
|
|
|
// XXX: some way to stop this?
|
2015-02-11 20:19:46 -07:00
|
|
|
for {
|
|
|
|
lines := make([]string, 0)
|
|
|
|
bf := bufio.NewScanner(f)
|
|
|
|
for bf.Scan() {
|
|
|
|
t := bf.Text()
|
|
|
|
nw.lineno += 1
|
|
|
|
|
|
|
|
parts := strings.Split(t, " ")
|
|
|
|
if (len(parts) >= 4) && (parts[2] == "NEXTLOG") {
|
|
|
|
watcher.Remove(filepath)
|
2015-02-18 20:33:17 -07:00
|
|
|
filename := parts[3]
|
2015-02-20 16:48:52 -07:00
|
|
|
filepath = path.Join(nw.basePath, "log", filename)
|
2015-02-11 20:19:46 -07:00
|
|
|
f.Close()
|
|
|
|
f, err = os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
watcher.Add(filepath)
|
|
|
|
nw.lineno = 0
|
|
|
|
}
|
2015-02-18 20:33:17 -07:00
|
|
|
lines = append(lines, t)
|
2015-02-11 20:19:46 -07:00
|
|
|
}
|
2015-02-18 20:33:17 -07:00
|
|
|
if len(lines) > 0 {
|
2015-02-11 20:19:46 -07:00
|
|
|
update := Update{
|
|
|
|
Lines: lines,
|
|
|
|
LastEventId: nw.LastEventId(),
|
|
|
|
}
|
|
|
|
out <- &update
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _ = <-watcher.Events:
|
|
|
|
// Somethin' happened!
|
|
|
|
case err := <-watcher.Errors:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-18 20:33:17 -07:00
|
|
|
func (nw *Network) Write(data []byte) {
|
|
|
|
epoch := time.Now().Unix()
|
|
|
|
pid := os.Getpid()
|
|
|
|
filename := fmt.Sprintf("%d-%d-%d.txt", epoch, pid, nw.seq)
|
|
|
|
|
|
|
|
filepath := path.Join(nw.basePath, "outq", filename)
|
|
|
|
ioutil.WriteFile(filepath, data, 0750)
|
|
|
|
nw.seq += 1
|
|
|
|
}
|
2015-02-20 16:48:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
func Networks(basePath string) (found []*Network) {
|
|
|
|
|
|
|
|
dir, err := os.Open(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
|
|
|
|
entities, _ := dir.Readdirnames(0)
|
|
|
|
for _, fn := range entities {
|
|
|
|
netdir := path.Join(basePath, fn)
|
|
|
|
|
|
|
|
_, err = os.Stat(path.Join(netdir, "nick"))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nw := NewNetwork(netdir)
|
|
|
|
found = append(found, nw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|