Commit 5007b240 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Server opens a port in the given range

parent dbe53602
...@@ -5,8 +5,8 @@ import ( ...@@ -5,8 +5,8 @@ import (
"net" "net"
) )
var portRangeMin int = 0 var portRangeMin int = 10000
var portRangeMax int = 0 var portRangeMax int = 11000
// This sets the port range that the RPC stuff will use when creating // This sets the port range that the RPC stuff will use when creating
// new temporary servers. Some RPC calls require the creation of temporary // new temporary servers. Some RPC calls require the creation of temporary
......
...@@ -10,21 +10,23 @@ import ( ...@@ -10,21 +10,23 @@ import (
// A Server is a Golang RPC server that has helper methods for automatically // A Server is a Golang RPC server that has helper methods for automatically
// setting up the endpoints for Packer interfaces. // setting up the endpoints for Packer interfaces.
type Server struct { type Server struct {
listener net.Listener
server *rpc.Server server *rpc.Server
started bool
doneChan chan bool
} }
// Creates and returns a new Server. // Creates and returns a new Server.
func NewServer() *Server { func NewServer() *Server {
return &Server{ return &Server{
server: rpc.NewServer(), server: rpc.NewServer(),
started: false,
} }
} }
func (s *Server) Address() string { 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) { func (s *Server) RegisterUi(ui packer.Ui) {
...@@ -32,29 +34,21 @@ func (s *Server) RegisterUi(ui packer.Ui) { ...@@ -32,29 +34,21 @@ func (s *Server) RegisterUi(ui packer.Ui) {
} }
func (s *Server) Start() error { func (s *Server) Start() error {
if s.started { if s.listener != nil {
return errors.New("Server already started.") 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 // Start the TCP listener and a goroutine responsible for cleaning up the
// listener. // listener.
listener, _ := net.Listen("tcp", address) s.listener = netListenerInRange(portRangeMin, portRangeMax)
go func() { if s.listener == nil {
<-s.doneChan return errors.New("Could not open a port ot listen on.")
listener.Close() }
}()
// Start accepting connections // Start accepting connections
go func() { go func() {
for { for {
conn, err := listener.Accept() conn, err := s.listener.Accept()
if err != nil { if err != nil {
break break
} }
...@@ -67,11 +61,8 @@ func (s *Server) Start() error { ...@@ -67,11 +61,8 @@ func (s *Server) Start() error {
} }
func (s *Server) Stop() { func (s *Server) Stop() {
if s.started { if s.listener != nil {
// TODO: There is a race condition here, we need to wait for s.listener.Close()
// the listener to REALLY close. s.listener = nil
s.doneChan <- true
s.started = false
s.doneChan = nil
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment