2013-06-09 22:25:48 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2014-09-04 00:17:53 -04:00
|
|
|
"log"
|
2013-06-09 22:25:48 -04:00
|
|
|
"net/rpc"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-09 22:25:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Cache where the cache is actually executed
|
|
|
|
// over an RPC connection.
|
|
|
|
type cache struct {
|
|
|
|
client *rpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheServer wraps a packer.Cache implementation and makes it exportable
|
|
|
|
// as part of a Golang RPC server.
|
|
|
|
type CacheServer struct {
|
|
|
|
cache packer.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
type CacheRLockResponse struct {
|
|
|
|
Path string
|
|
|
|
Exists bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) Lock(key string) (result string) {
|
|
|
|
if err := c.client.Call("Cache.Lock", key, &result); err != nil {
|
2014-09-04 00:17:53 -04:00
|
|
|
log.Printf("[ERR] Cache.Lock error: %s", err)
|
|
|
|
return
|
2013-06-09 22:25:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) RLock(key string) (string, bool) {
|
|
|
|
var result CacheRLockResponse
|
|
|
|
if err := c.client.Call("Cache.RLock", key, &result); err != nil {
|
2014-09-04 00:17:53 -04:00
|
|
|
log.Printf("[ERR] Cache.RLock error: %s", err)
|
|
|
|
return "", false
|
2013-06-09 22:25:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result.Path, result.Exists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) Unlock(key string) {
|
|
|
|
if err := c.client.Call("Cache.Unlock", key, new(interface{})); err != nil {
|
2014-09-04 00:17:53 -04:00
|
|
|
log.Printf("[ERR] Cache.Unlock error: %s", err)
|
|
|
|
return
|
2013-06-09 22:25:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) RUnlock(key string) {
|
|
|
|
if err := c.client.Call("Cache.RUnlock", key, new(interface{})); err != nil {
|
2014-09-04 00:17:53 -04:00
|
|
|
log.Printf("[ERR] Cache.RUnlock error: %s", err)
|
|
|
|
return
|
2013-06-09 22:25:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheServer) Lock(key string, result *string) error {
|
|
|
|
*result = c.cache.Lock(key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheServer) Unlock(key string, result *interface{}) error {
|
|
|
|
c.cache.Unlock(key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheServer) RLock(key string, result *CacheRLockResponse) error {
|
|
|
|
path, exists := c.cache.RLock(key)
|
|
|
|
*result = CacheRLockResponse{path, exists}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheServer) RUnlock(key string, result *interface{}) error {
|
|
|
|
c.cache.RUnlock(key)
|
|
|
|
return nil
|
|
|
|
}
|