Server opens a port in the given range

This commit is contained in:
Mitchell Hashimoto 2013-05-04 13:23:28 -07:00
parent dbe5360262
commit 5007b240dc
2 changed files with 17 additions and 26 deletions

View File

@ -5,8 +5,8 @@ import (
"net"
)
var portRangeMin int = 0
var portRangeMax int = 0
var portRangeMin int = 10000
var portRangeMax int = 11000
// This sets the port range that the RPC stuff will use when creating
// new temporary servers. Some RPC calls require the creation of temporary

View File

@ -10,21 +10,23 @@ import (
// A Server is a Golang RPC server that has helper methods for automatically
// setting up the endpoints for Packer interfaces.
type Server struct {
listener net.Listener
server *rpc.Server
started bool
doneChan chan bool
}
// Creates and returns a new Server.
func NewServer() *Server {
return &Server{
server: rpc.NewServer(),
started: false,
}
}
func (s *Server) Address() string {
return ":2345"
if s.listener == nil {
panic("Server not listening.")
}
return s.listener.Addr().String()
}
func (s *Server) RegisterUi(ui packer.Ui) {
@ -32,29 +34,21 @@ func (s *Server) RegisterUi(ui packer.Ui) {
}
func (s *Server) Start() error {
if s.started {
if s.listener != nil {
return errors.New("Server already started.")
}
// TODO: Address
address := ":2345"
// Mark that we started and setup the channel we'll use to mark exits
s.started = true
s.doneChan = make(chan bool)
// Start the TCP listener and a goroutine responsible for cleaning up the
// listener.
listener, _ := net.Listen("tcp", address)
go func() {
<-s.doneChan
listener.Close()
}()
s.listener = netListenerInRange(portRangeMin, portRangeMax)
if s.listener == nil {
return errors.New("Could not open a port ot listen on.")
}
// Start accepting connections
go func() {
for {
conn, err := listener.Accept()
conn, err := s.listener.Accept()
if err != nil {
break
}
@ -67,11 +61,8 @@ func (s *Server) Start() error {
}
func (s *Server) Stop() {
if s.started {
// TODO: There is a race condition here, we need to wait for
// the listener to REALLY close.
s.doneChan <- true
s.started = false
s.doneChan = nil
if s.listener != nil {
s.listener.Close()
s.listener = nil
}
}