mtsatellite/main.go

72 lines
1.2 KiB
Go
Raw Normal View History

2014-08-03 00:01:14 +02:00
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
)
func main() {
var port int
var host string
var cacheSize int
flag.IntVar(&port, "port", 6379, "port to bind")
flag.StringVar(&host, "host", "", "host to bind")
flag.IntVar(&cacheSize, "cache", 32, "cache size in MB")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
log.Fatal("Missing path to world")
}
var err error
var backend Backend
2014-08-03 00:01:14 +02:00
if backend, err = NewLeveDBBackend(args[0], cacheSize); err != nil {
2014-08-03 00:01:14 +02:00
log.Fatal(err)
}
defer backend.Shutdown()
2014-08-03 00:01:14 +02:00
var listener net.Listener
listener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
log.Fatal(err)
}
defer listener.Close()
log.Printf("Server started at %s", listener.Addr())
2014-08-03 00:01:14 +02:00
connChan := make(chan net.Conn)
defer close(connChan)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, os.Kill)
go func() {
for {
conn, err := listener.Accept()
if err != nil {
log.Fatal(err)
}
log.Printf("Client accepted from: %s", conn.RemoteAddr())
2014-08-03 00:01:14 +02:00
connChan <- conn
}
}()
for {
select {
case conn := <-connChan:
go NewConnection(conn, backend).Run()
2014-08-03 00:01:14 +02:00
case <-sigChan:
log.Println("Shutting down")
return
}
}
}