packer/rpc: Rename Command to follow RPC style

This commit is contained in:
Mitchell Hashimoto 2013-05-08 13:42:25 -07:00
parent ac83cf652a
commit 88f7b33082
3 changed files with 18 additions and 62 deletions

View File

@ -5,15 +5,15 @@ import (
"net/rpc" "net/rpc"
) )
// A ClientCommand is an implementation of the Command interface where the // A Command is an implementation of the packer.Command interface where the
// command is actually executed over an RPC connection. // command is actually executed over an RPC connection.
type ClientCommand struct { type command struct {
client *rpc.Client client *rpc.Client
} }
// A ServerCommand wraps a Command and makes it exportable as part // A CommandServer wraps a packer.Command and makes it exportable as part
// of a Golang RPC server. // of a Golang RPC server.
type ServerCommand struct { type CommandServer struct {
command packer.Command command packer.Command
} }
@ -24,11 +24,11 @@ type CommandRunArgs struct {
type CommandSynopsisArgs byte type CommandSynopsisArgs byte
func Command(client *rpc.Client) *ClientCommand { func Command(client *rpc.Client) *command {
return &ClientCommand{client} return &command{client}
} }
func (c *ClientCommand) Run(env packer.Environment, args []string) (result int) { func (c *command) Run(env packer.Environment, args []string) (result int) {
// Create and start the server for the Environment // Create and start the server for the Environment
server := rpc.NewServer() server := rpc.NewServer()
RegisterEnvironment(server, env) RegisterEnvironment(server, env)
@ -42,7 +42,7 @@ func (c *ClientCommand) Run(env packer.Environment, args []string) (result int)
return return
} }
func (c *ClientCommand) Synopsis() (result string) { func (c *command) Synopsis() (result string) {
err := c.client.Call("Command.Synopsis", CommandSynopsisArgs(0), &result) err := c.client.Call("Command.Synopsis", CommandSynopsisArgs(0), &result)
if err != nil { if err != nil {
panic(err) panic(err)
@ -51,7 +51,7 @@ func (c *ClientCommand) Synopsis() (result string) {
return return
} }
func (c *ServerCommand) Run(args *CommandRunArgs, reply *int) error { func (c *CommandServer) Run(args *CommandRunArgs, reply *int) error {
client, err := rpc.Dial("tcp", args.RPCAddress) client, err := rpc.Dial("tcp", args.RPCAddress)
if err != nil { if err != nil {
return err return err
@ -63,7 +63,7 @@ func (c *ServerCommand) Run(args *CommandRunArgs, reply *int) error {
return nil return nil
} }
func (c *ServerCommand) Synopsis(args *CommandSynopsisArgs, reply *string) error { func (c *CommandServer) Synopsis(args *CommandSynopsisArgs, reply *string) error {
*reply = c.command.Synopsis() *reply = c.command.Synopsis()
return nil return nil
} }

View File

@ -3,7 +3,6 @@ package rpc
import ( import (
"cgl.tideland.biz/asserts" "cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"net"
"net/rpc" "net/rpc"
"testing" "testing"
) )
@ -25,66 +24,23 @@ func (tc *TestCommand) Synopsis() string {
return "foo" return "foo"
} }
// This starts a RPC server for the given command listening on the
// given address. The RPC server is ready when "readyChan" receives a message
// and the RPC server will quit when "stopChan" receives a message.
//
// This function should be run in a goroutine.
func testCommandRPCServer(laddr string, command interface{}, readyChan chan int, stopChan <-chan int) {
listener, err := net.Listen("tcp", laddr)
if err != nil {
panic(err)
}
// Close the listener when we exit so that the RPC server ends
defer listener.Close()
// Start the RPC server
server := rpc.NewServer()
server.RegisterName("Command", command)
go func() {
for {
conn, err := listener.Accept()
if err != nil {
// If there is an error, just ignore it.
break
}
go server.ServeConn(conn)
}
}()
// We're ready!
readyChan <- 1
// Block on waiting to receive from the channel
<-stopChan
}
func TestRPCCommand(t *testing.T) { func TestRPCCommand(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
// Create the command // Create the command
command := new(TestCommand) command := new(TestCommand)
serverCommand := &ServerCommand{command}
// Start the RPC server, and make sure to exit it at the end // Start the server
// of the test. server := rpc.NewServer()
readyChan := make(chan int) RegisterCommand(server, command)
stopChan := make(chan int) address := serveSingleConn(server)
defer func() { stopChan <- 1 }()
go testCommandRPCServer(":1234", serverCommand, readyChan, stopChan)
<-readyChan
// Create the command client over RPC and run some methods to verify // Create the command client over RPC and run some methods to verify
// we get the proper behavior. // we get the proper behavior.
client, err := rpc.Dial("tcp", ":1234") client, err := rpc.Dial("tcp", address)
if err != nil { assert.Nil(err, "should be no error")
panic(err)
}
clientComm := &ClientCommand{client} clientComm := Command(client)
// Test run // Test run
runArgs := []string{"foo", "bar"} runArgs := []string{"foo", "bar"}

View File

@ -20,7 +20,7 @@ func RegisterBuilder(s *rpc.Server, b packer.Builder) {
// Registers the appropriate endpoint on an RPC server to serve a // Registers the appropriate endpoint on an RPC server to serve a
// Packer Command. // Packer Command.
func RegisterCommand(s *rpc.Server, c packer.Command) { func RegisterCommand(s *rpc.Server, c packer.Command) {
s.RegisterName("Command", &ServerCommand{c}) s.RegisterName("Command", &CommandServer{c})
} }
// Registers the appropriate endpoint on an RPC server to serve a // Registers the appropriate endpoint on an RPC server to serve a