2014-10-21 22:02:20 +00:00
|
|
|
package ipc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-10-25 01:33:23 +00:00
|
|
|
"io/ioutil"
|
2014-10-21 22:02:20 +00:00
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/docker/libchan"
|
|
|
|
)
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Defines a remote method call request
|
|
|
|
// A return value struct is to be sent over the ResponseChannel
|
2014-10-21 22:02:20 +00:00
|
|
|
type Request struct {
|
|
|
|
Type string
|
|
|
|
Parameters map[string]interface{}
|
|
|
|
ResponseChannel libchan.Sender
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// A simple wrapper around an io.ReadCloser that implements the io.ReadWriteCloser interface
|
|
|
|
// Writes are disallowed and will return an error if ever called
|
2014-10-21 22:02:20 +00:00
|
|
|
type noWriteReadWriteCloser struct {
|
|
|
|
io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r noWriteReadWriteCloser) Write(p []byte) (n int, err error) {
|
|
|
|
return 0, errors.New("Write unsupported")
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Wraps an io.Reader as an io.ReadWriteCloser with a nop Close and unsupported Write method
|
|
|
|
// Has no effect when an io.ReadWriteCloser is passed in
|
2014-10-25 01:33:23 +00:00
|
|
|
func WrapReader(reader io.Reader) io.ReadWriteCloser {
|
|
|
|
if readWriteCloser, ok := reader.(io.ReadWriteCloser); ok {
|
|
|
|
return readWriteCloser
|
|
|
|
} else if readCloser, ok := reader.(io.ReadCloser); ok {
|
|
|
|
return noWriteReadWriteCloser{readCloser}
|
|
|
|
} else {
|
|
|
|
return noWriteReadWriteCloser{ioutil.NopCloser(reader)}
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type responseError struct {
|
|
|
|
Type string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Wraps an error in a serializable struct containing the error's type and message
|
2014-10-21 22:02:20 +00:00
|
|
|
func ResponseError(err error) *responseError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &responseError{
|
|
|
|
Type: reflect.TypeOf(err).String(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *responseError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s", err.Type, err.Message)
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// IPC method call response object definitions
|
|
|
|
|
|
|
|
// Response for a ReadStream request
|
2014-10-21 22:02:20 +00:00
|
|
|
type ReadStreamResponse struct {
|
|
|
|
Reader io.ReadWriteCloser
|
|
|
|
Error *responseError
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Response for a WriteStream request
|
2014-10-21 22:02:20 +00:00
|
|
|
type WriteStreamResponse struct {
|
|
|
|
Error *responseError
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Response for a ResumeWritePosition request
|
2014-10-21 22:02:20 +00:00
|
|
|
type ResumeWritePositionResponse struct {
|
|
|
|
Position uint64
|
|
|
|
Error *responseError
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Response for a List request
|
2014-10-21 22:02:20 +00:00
|
|
|
type ListResponse struct {
|
|
|
|
Keys []string
|
|
|
|
Error *responseError
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Response for a Move request
|
2014-10-21 22:02:20 +00:00
|
|
|
type MoveResponse struct {
|
|
|
|
Error *responseError
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Response for a Delete request
|
2014-10-21 22:02:20 +00:00
|
|
|
type DeleteResponse struct {
|
|
|
|
Error *responseError
|
|
|
|
}
|