2014-10-22 00:02:20 +02:00
|
|
|
package ipc
|
|
|
|
|
|
|
|
import (
|
2014-10-25 03:33:23 +02:00
|
|
|
"bytes"
|
2014-10-22 00:02:20 +02:00
|
|
|
"io"
|
2014-10-25 03:33:23 +02:00
|
|
|
"io/ioutil"
|
2014-10-22 00:02:20 +02:00
|
|
|
"net"
|
|
|
|
"os"
|
2014-10-29 02:15:40 +01:00
|
|
|
"reflect"
|
2014-10-22 00:02:20 +02:00
|
|
|
|
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
|
|
|
"github.com/docker/libchan"
|
|
|
|
"github.com/docker/libchan/spdy"
|
|
|
|
)
|
|
|
|
|
2014-10-29 20:14:19 +01: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 02:15:40 +01:00
|
|
|
//
|
2014-10-29 20:14:19 +01:00
|
|
|
// To create a new out-of-process driver, create a main package which calls StorageDriverServer with
|
|
|
|
// a storagedriver.StorageDriver
|
2014-10-29 02:15:40 +01:00
|
|
|
func StorageDriverServer(driver storagedriver.StorageDriver) error {
|
2014-10-22 00:02:20 +02: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 20:14:19 +01: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-22 00:02:20 +02: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 20:14:19 +01:00
|
|
|
// handleRequest handles storagedriver.StorageDriver method requests as defined in client.go
|
2014-10-29 02:15:40 +01:00
|
|
|
// Responds to requests using the Request.ResponseChannel
|
2014-10-22 00:02:20 +02:00
|
|
|
func handleRequest(driver storagedriver.StorageDriver, request Request) {
|
|
|
|
switch request.Type {
|
2014-11-06 21:16:14 +01:00
|
|
|
case "Version":
|
|
|
|
err := request.ResponseChannel.Send(&VersionResponse{Version: storagedriver.CurrentVersion})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-22 00:02:20 +02:00
|
|
|
case "GetContent":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
|
|
|
content, err := driver.GetContent(path)
|
2014-10-25 03:33:23 +02:00
|
|
|
var response ReadStreamResponse
|
|
|
|
if err != nil {
|
|
|
|
response = ReadStreamResponse{Error: ResponseError(err)}
|
|
|
|
} else {
|
2014-10-31 19:50:02 +01:00
|
|
|
response = ReadStreamResponse{Reader: ioutil.NopCloser(bytes.NewReader(content))}
|
2014-10-22 00:02:20 +02:00
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "PutContent":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
2014-10-25 03:33:23 +02: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-22 00:02:20 +02:00
|
|
|
Error: ResponseError(err),
|
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "ReadStream":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
2014-10-29 02:15:40 +01: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-22 00:02:20 +02:00
|
|
|
reader, err := driver.ReadStream(path, offset)
|
|
|
|
var response ReadStreamResponse
|
|
|
|
if err != nil {
|
|
|
|
response = ReadStreamResponse{Error: ResponseError(err)}
|
|
|
|
} else {
|
2014-10-31 19:50:02 +01:00
|
|
|
response = ReadStreamResponse{Reader: ioutil.NopCloser(reader)}
|
2014-10-22 00:02:20 +02:00
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "WriteStream":
|
|
|
|
path, _ := request.Parameters["Path"].(string)
|
2014-10-29 02:15:40 +01: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-22 00:02:20 +02: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)
|
|
|
|
}
|
2014-11-07 21:58:48 +01:00
|
|
|
case "CurrentSize":
|
2014-10-22 00:02:20 +02:00
|
|
|
path, _ := request.Parameters["Path"].(string)
|
2014-11-07 21:58:48 +01:00
|
|
|
position, err := driver.CurrentSize(path)
|
|
|
|
response := CurrentSizeResponse{
|
2014-10-22 00:02:20 +02:00
|
|
|
Position: position,
|
|
|
|
Error: ResponseError(err),
|
|
|
|
}
|
|
|
|
err = request.ResponseChannel.Send(&response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
case "List":
|
2014-11-04 18:52:24 +01:00
|
|
|
path, _ := request.Parameters["Path"].(string)
|
|
|
|
keys, err := driver.List(path)
|
2014-10-22 00:02:20 +02: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)
|
|
|
|
}
|
|
|
|
}
|