github.com/homburg/packer@v0.6.1-0.20140528012651-1dcaf1716848/packer/rpc/client.go (about)

     1  package rpc
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"github.com/ugorji/go/codec"
     6  	"io"
     7  	"log"
     8  	"net/rpc"
     9  )
    10  
    11  // Client is the client end that communicates with a Packer RPC server.
    12  // Establishing a connection is up to the user, the Client can just
    13  // communicate over any ReadWriteCloser.
    14  type Client struct {
    15  	mux      *MuxConn
    16  	client   *rpc.Client
    17  	closeMux bool
    18  }
    19  
    20  func NewClient(rwc io.ReadWriteCloser) (*Client, error) {
    21  	result, err := newClientWithMux(NewMuxConn(rwc), 0)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	result.closeMux = true
    27  	return result, err
    28  }
    29  
    30  func newClientWithMux(mux *MuxConn, streamId uint32) (*Client, error) {
    31  	clientConn, err := mux.Dial(streamId)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	var h codec.MsgpackHandle
    37  	clientCodec := codec.GoRpc.ClientCodec(clientConn, &h)
    38  
    39  	return &Client{
    40  		mux:      mux,
    41  		client:   rpc.NewClientWithCodec(clientCodec),
    42  		closeMux: false,
    43  	}, nil
    44  }
    45  
    46  func (c *Client) Close() error {
    47  	if err := c.client.Close(); err != nil {
    48  		return err
    49  	}
    50  
    51  	if c.closeMux {
    52  		log.Printf("[WARN] Client is closing mux")
    53  		return c.mux.Close()
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func (c *Client) Artifact() packer.Artifact {
    60  	return &artifact{
    61  		client:   c.client,
    62  		endpoint: DefaultArtifactEndpoint,
    63  	}
    64  }
    65  
    66  func (c *Client) Build() packer.Build {
    67  	return &build{
    68  		client: c.client,
    69  		mux:    c.mux,
    70  	}
    71  }
    72  
    73  func (c *Client) Builder() packer.Builder {
    74  	return &builder{
    75  		client: c.client,
    76  		mux:    c.mux,
    77  	}
    78  }
    79  
    80  func (c *Client) Cache() packer.Cache {
    81  	return &cache{
    82  		client: c.client,
    83  	}
    84  }
    85  
    86  func (c *Client) Command() packer.Command {
    87  	return &command{
    88  		client: c.client,
    89  		mux:    c.mux,
    90  	}
    91  }
    92  
    93  func (c *Client) Communicator() packer.Communicator {
    94  	return &communicator{
    95  		client: c.client,
    96  		mux:    c.mux,
    97  	}
    98  }
    99  
   100  func (c *Client) Environment() packer.Environment {
   101  	return &Environment{
   102  		client: c.client,
   103  		mux:    c.mux,
   104  	}
   105  }
   106  
   107  func (c *Client) Hook() packer.Hook {
   108  	return &hook{
   109  		client: c.client,
   110  		mux:    c.mux,
   111  	}
   112  }
   113  
   114  func (c *Client) PostProcessor() packer.PostProcessor {
   115  	return &postProcessor{
   116  		client: c.client,
   117  		mux:    c.mux,
   118  	}
   119  }
   120  
   121  func (c *Client) Provisioner() packer.Provisioner {
   122  	return &provisioner{
   123  		client: c.client,
   124  		mux:    c.mux,
   125  	}
   126  }
   127  
   128  func (c *Client) Ui() packer.Ui {
   129  	return &Ui{
   130  		client:   c.client,
   131  		endpoint: DefaultUiEndpoint,
   132  	}
   133  }