github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/cache.go (about)

     1  package rpc
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"log"
     6  	"net/rpc"
     7  )
     8  
     9  // An implementation of packer.Cache where the cache is actually executed
    10  // over an RPC connection.
    11  type cache struct {
    12  	client *rpc.Client
    13  }
    14  
    15  // CacheServer wraps a packer.Cache implementation and makes it exportable
    16  // as part of a Golang RPC server.
    17  type CacheServer struct {
    18  	cache packer.Cache
    19  }
    20  
    21  type CacheRLockResponse struct {
    22  	Path   string
    23  	Exists bool
    24  }
    25  
    26  func (c *cache) Lock(key string) (result string) {
    27  	if err := c.client.Call("Cache.Lock", key, &result); err != nil {
    28  		log.Printf("[ERR] Cache.Lock error: %s", err)
    29  		return
    30  	}
    31  
    32  	return
    33  }
    34  
    35  func (c *cache) RLock(key string) (string, bool) {
    36  	var result CacheRLockResponse
    37  	if err := c.client.Call("Cache.RLock", key, &result); err != nil {
    38  		log.Printf("[ERR] Cache.RLock error: %s", err)
    39  		return "", false
    40  	}
    41  
    42  	return result.Path, result.Exists
    43  }
    44  
    45  func (c *cache) Unlock(key string) {
    46  	if err := c.client.Call("Cache.Unlock", key, new(interface{})); err != nil {
    47  		log.Printf("[ERR] Cache.Unlock error: %s", err)
    48  		return
    49  	}
    50  }
    51  
    52  func (c *cache) RUnlock(key string) {
    53  	if err := c.client.Call("Cache.RUnlock", key, new(interface{})); err != nil {
    54  		log.Printf("[ERR] Cache.RUnlock error: %s", err)
    55  		return
    56  	}
    57  }
    58  
    59  func (c *CacheServer) Lock(key string, result *string) error {
    60  	*result = c.cache.Lock(key)
    61  	return nil
    62  }
    63  
    64  func (c *CacheServer) Unlock(key string, result *interface{}) error {
    65  	c.cache.Unlock(key)
    66  	return nil
    67  }
    68  
    69  func (c *CacheServer) RLock(key string, result *CacheRLockResponse) error {
    70  	path, exists := c.cache.RLock(key)
    71  	*result = CacheRLockResponse{path, exists}
    72  	return nil
    73  }
    74  
    75  func (c *CacheServer) RUnlock(key string, result *interface{}) error {
    76  	c.cache.RUnlock(key)
    77  	return nil
    78  }