2014-10-21 22:02:20 +00:00
|
|
|
package ipc
|
|
|
|
|
|
|
|
import (
|
2014-10-25 01:33:23 +00:00
|
|
|
"bytes"
|
2014-10-21 22:02:20 +00:00
|
|
|
"io"
|
2014-10-25 01:33:23 +00:00
|
|
|
"io/ioutil"
|
2014-10-21 22:02:20 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2014-10-29 01:15:40 +00:00
|
|
|
"reflect"
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
|
|
|
"github.com/docker/libchan"
|
|
|
|
"github.com/docker/libchan/spdy"
|
|
|
|
)
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// StorageDriverServer runs a new IPC server handling requests for the given
|
|
|
|
// storagedriver.StorageDriver
|
|
|
|
// This explicitly uses file descriptor 3 for IPC communication, as storage drivers are spawned in
|
|
|
|
// client.go
|
2014-10-29 01:15:40 +00:00
|
|
|
//
|
2014-10-29 19:14:19 +00:00
|
|
|
// To create a new out-of-process driver, create a main package which calls StorageDriverServer with
|
|
|
|
// a storagedriver.StorageDriver
|
2014-10-29 01:15:40 +00:00
|
|
|
func StorageDriverServer(driver storagedriver.StorageDriver) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
childSocket := os.NewFile(3, "childSocket")
|
|
|
|
defer childSocket.Close()
|
|
|
|
conn, err := net.FileConn(childSocket)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
if transport, err := spdy.NewServerTransport(conn); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
for {
|
|
|
|
receiver, err := transport.WaitReceiveChannel()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
go receive(driver, receiver)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// receive receives new storagedriver.StorageDriver method requests and creates a new goroutine to
|
|
|
|
// handle each request
|
|
|
|
// Requests are expected to be of type ipc.Request as the parameters are unknown until the request
|
|
|
|
// type is deserialized
|
2014-10-21 22:02:20 +00:00
|
|
|
func receive(driver storagedriver.StorageDriver, receiver libchan.Receiver) {
|
|
|
|
for {
|
|
|
|
var request Request
|
|
|
|
err := receiver.Receive(&request)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
go handleRequest(driver, request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// handleRequest handles storagedriver.StorageDriver method requests as defined in client.go
|
2014-10-29 01:15:40 +00:00
|
|
|
// Responds to requests using the Request.ResponseChannel
|
2014-10-21 22:02:20 +00:00
|
|
|
func handleRequest(driver storagedriver.StorageDriver, request Request) {
|
|
|
|
switch request.Type {
|
|
|
|
case "GetContent":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
|
|
|
content, err := driver.GetContent(path)
|
2014-10-25 01:33:23 +00:00
|
|
|
var response ReadStreamResponse
|
|
|
|
if err != nil {
|
|
|
|
response = ReadStreamResponse{Error: ResponseError(err)}
|
|
|
|
} else {
|
2014-10-31 18:50:02 +00:00
|
|
|
response = ReadStreamResponse{Reader: ioutil.NopCloser(bytes.NewReader(content))}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "PutContent":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
2014-10-25 01:33:23 +00:00
|
|
|
reader, _ := request.Parameters["Reader"].(io.ReadCloser)
|
|
|
|
contents, err := ioutil.ReadAll(reader)
|
|
|
|
defer reader.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = driver.PutContent(path, contents)
|
|
|
|
}
|
|
|
|
response := WriteStreamResponse{
|
2014-10-21 22:02:20 +00:00
|
|
|
Error: ResponseError(err),
|
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "ReadStream":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
2014-10-29 01:15:40 +00:00
|
|
|
// Depending on serialization method, Offset may be convereted to any int/uint type
|
|
|
|
offset := reflect.ValueOf(request.Parameters["Offset"]).Convert(reflect.TypeOf(uint64(0))).Uint()
|
2014-10-21 22:02:20 +00:00
|
|
|
reader, err := driver.ReadStream(path, offset)
|
|
|
|
var response ReadStreamResponse
|
|
|
|
if err != nil {
|
|
|
|
response = ReadStreamResponse{Error: ResponseError(err)}
|
|
|
|
} else {
|
2014-10-31 18:50:02 +00:00
|
|
|
response = ReadStreamResponse{Reader: ioutil.NopCloser(reader)}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "WriteStream":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
2014-10-29 01:15:40 +00:00
|
|
|
// Depending on serialization method, Offset may be convereted to any int/uint type
|
|
|
|
offset := reflect.ValueOf(request.Parameters["Offset"]).Convert(reflect.TypeOf(uint64(0))).Uint()
|
|
|
|
// Depending on serialization method, Size may be convereted to any int/uint type
|
|
|
|
size := reflect.ValueOf(request.Parameters["Size"]).Convert(reflect.TypeOf(uint64(0))).Uint()
|
2014-10-21 22:02:20 +00:00
|
|
|
reader, _ := request.Parameters["Reader"].(io.ReadCloser)
|
|
|
|
err := driver.WriteStream(path, offset, size, reader)
|
|
|
|
response := WriteStreamResponse{
|
|
|
|
Error: ResponseError(err),
|
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "ResumeWritePosition":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
|
|
|
position, err := driver.ResumeWritePosition(path)
|
|
|
|
response := ResumeWritePositionResponse{
|
|
|
|
Position: position,
|
|
|
|
Error: ResponseError(err),
|
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "List":
|
2014-11-04 17:52:24 +00:00
|
|
|
path, _ := request.Parameters["Path"].(string)
|
|
|
|
keys, err := driver.List(path)
|
2014-10-21 22:02:20 +00:00
|
|
|
response := ListResponse{
|
|
|
|
Keys: keys,
|
|
|
|
Error: ResponseError(err),
|
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "Move":
|
|
|
|
sourcePath, _ := request.Parameters["SourcePath"].(string)
|
|
|
|
destPath, _ := request.Parameters["DestPath"].(string)
|
|
|
|
err := driver.Move(sourcePath, destPath)
|
|
|
|
response := MoveResponse{
|
|
|
|
Error: ResponseError(err),
|
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "Delete":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
|
|
|
err := driver.Delete(path)
|
|
|
|
response := DeleteResponse{
|
|
|
|
Error: ResponseError(err),
|
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(request)
|
|
|
|
}
|
|
|
|
}
|