Commit 88f7b330 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: Rename Command to follow RPC style

parent ac83cf65
......@@ -5,15 +5,15 @@ import (
"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.
type ClientCommand struct {
type command struct {
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.
type ServerCommand struct {
type CommandServer struct {
command packer.Command
}
......@@ -24,11 +24,11 @@ type CommandRunArgs struct {
type CommandSynopsisArgs byte
func Command(client *rpc.Client) *ClientCommand {
return &ClientCommand{client}
func Command(client *rpc.Client) *command {
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
server := rpc.NewServer()
RegisterEnvironment(server, env)
......@@ -42,7 +42,7 @@ func (c *ClientCommand) Run(env packer.Environment, args []string) (result int)
return
}
func (c *ClientCommand) Synopsis() (result string) {
func (c *command) Synopsis() (result string) {
err := c.client.Call("Command.Synopsis", CommandSynopsisArgs(0), &result)
if err != nil {
panic(err)
......@@ -51,7 +51,7 @@ func (c *ClientCommand) Synopsis() (result string) {
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)
if err != nil {
return err
......@@ -63,7 +63,7 @@ func (c *ServerCommand) Run(args *CommandRunArgs, reply *int) error {
return nil
}
func (c *ServerCommand) Synopsis(args *CommandSynopsisArgs, reply *string) error {
func (c *CommandServer) Synopsis(args *CommandSynopsisArgs, reply *string) error {
*reply = c.command.Synopsis()
return nil
}
......@@ -3,7 +3,6 @@ package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net"
"net/rpc"
"testing"
)
......@@ -25,66 +24,23 @@ func (tc *TestCommand) Synopsis() string {
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) {
assert := asserts.NewTestingAsserts(t, true)
// Create the command
command := new(TestCommand)
serverCommand := &ServerCommand{command}
// Start the RPC server, and make sure to exit it at the end
// of the test.
readyChan := make(chan int)
stopChan := make(chan int)
defer func() { stopChan <- 1 }()
go testCommandRPCServer(":1234", serverCommand, readyChan, stopChan)
<-readyChan
// Start the server
server := rpc.NewServer()
RegisterCommand(server, command)
address := serveSingleConn(server)
// Create the command client over RPC and run some methods to verify
// we get the proper behavior.
client, err := rpc.Dial("tcp", ":1234")
if err != nil {
panic(err)
}
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be no error")
clientComm := &ClientCommand{client}
clientComm := Command(client)
// Test run
runArgs := []string{"foo", "bar"}
......
......@@ -20,7 +20,7 @@ func RegisterBuilder(s *rpc.Server, b packer.Builder) {
// Registers the appropriate endpoint on an RPC server to serve a
// 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
......
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