spongy/spongyd/spongyd.go

108 lines
2.1 KiB
Go
Raw Normal View History

2014-07-18 18:09:59 -06:00
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
2015-02-11 14:47:34 -07:00
"path/filepath"
2014-07-20 23:03:53 -06:00
"time"
2014-07-18 18:09:59 -06:00
)
2014-07-23 21:15:04 -06:00
var running bool = true
2016-01-18 18:09:29 -07:00
var verbose bool = false
2014-10-24 22:21:53 -06:00
var maxlogsize uint
2014-07-23 21:15:04 -06:00
2016-01-18 18:09:29 -07:00
func debug(format string, a ...interface{}) {
if verbose {
log.Printf(format, a...)
}
}
func exists(filename string) bool {
_, err := os.Stat(filename); if err != nil {
2014-08-12 23:21:19 -06:00
return false
}
return true
2014-07-18 18:09:59 -06:00
}
func runsvdir(dirname string) {
services := make(map[string]*Network)
dir, err := os.Open(dirname)
2014-07-23 21:15:04 -06:00
if err != nil {
log.Fatal(err)
2014-07-20 23:03:53 -06:00
}
defer dir.Close()
2015-02-11 14:47:34 -07:00
log.Printf("Starting in %s\n", dirname)
2014-07-20 23:03:53 -06:00
for running {
dn, err := dir.Readdirnames(0); if err != nil {
log.Fatal(err)
}
found := make(map[string]bool)
for _, fn := range dn {
fpath := path.Join(dirname, fn)
if _, ok := services[fpath]; ! ok {
2015-02-11 15:56:23 -07:00
if exists(path.Join(fpath, "down")) {
continue
}
2015-02-11 15:54:23 -07:00
if ! exists(path.Join(fpath, "server")) {
continue
}
2015-02-11 14:47:34 -07:00
log.Printf("Found new network %s", fpath)
newnet := NewNetwork(fpath)
services[fpath] = newnet
go newnet.Connect()
}
found[fpath] = true
2014-07-20 23:03:53 -06:00
}
// If anything vanished, disconnect it
for fpath, nw := range services {
if _, ok := found[fpath]; ! ok {
2015-02-11 14:47:34 -07:00
log.Printf("Removing vanished network %s", fpath)
nw.Close()
2014-07-20 23:03:53 -06:00
}
}
_, _ = dir.Seek(0, 0)
time.Sleep(20 * time.Second)
2014-07-20 23:03:53 -06:00
}
}
2014-07-18 18:09:59 -06:00
func usage() {
2015-02-11 13:54:13 -07:00
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] BASEPATH\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "BASEPATH is the path to your IRC directory (see README)\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "OPTIONS:\n")
2014-07-18 18:09:59 -06:00
flag.PrintDefaults()
}
func main() {
2015-02-11 13:54:13 -07:00
flag.Usage = usage
2016-01-18 18:09:29 -07:00
flag.UintVar(&maxlogsize, "logsize", 6000, "Log entries before rotating")
flag.BoolVar(&verbose, "verbose", false, "Verbose logging")
notime := flag.Bool("notime", false, "Don't timestamp debugging messages")
2014-07-18 18:09:59 -06:00
flag.Parse()
2015-02-11 13:54:13 -07:00
if flag.NArg() != 1 {
usage()
os.Exit(2)
}
2015-02-11 14:47:34 -07:00
basePath, err := filepath.Abs(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
if *notime {
log.SetFlags(0)
}
runsvdir(basePath)
2014-07-20 23:03:53 -06:00
running = false
2014-07-18 18:09:59 -06:00
}