package srvhnakra

import "log"
import "fmt"
import "net"
import "crypto/tls"
import "hnakra/router"
import "hnakra/config"

type Server struct {
	underlying net.Listener
	Config     config.Config
	Router     *router.Router
}

func (server *Server) ListenAndServe () (err error) {
	server.underlying, err = tls.Listen (
		"tcp", fmt.Sprint(":", server.Config.RouterPort()),
		config.TLSConfigFor(server.Config))
	if err != nil { return err }
	
	log.Println(".// router on", server.underlying.Addr())

	for {
		conn, err := server.underlying.Accept()
		if err != nil { return err }
		log.Println("-=E incoming connection from", conn.RemoteAddr())
		server.Router.Accept(conn)
	}
}

func (server *Server) Close () error {
	return server.underlying.Close()
}
