writer.go 3.04 KB
Newer Older
Jan Winkelmann's avatar
Jan Winkelmann committed
1 2 3
package cmds

import (
4
	"encoding/json"
Jan Winkelmann's avatar
Jan Winkelmann committed
5 6 7
	"fmt"
	"io"
	"reflect"
8
	"sync"
9 10

	"github.com/ipfs/go-ipfs-cmds/cmdsutil"
Jan Winkelmann's avatar
Jan Winkelmann committed
11 12
)

Jan Winkelmann's avatar
Jan Winkelmann committed
13
func NewWriterResponseEmitter(w io.WriteCloser, req Request, enc func(Request) func(io.Writer) Encoder) *WriterResponseEmitter {
14 15 16
	return &WriterResponseEmitter{
		w:   w,
		c:   w,
Jan Winkelmann's avatar
Jan Winkelmann committed
17 18
		enc: enc(req)(w),
		req: req,
Jan Winkelmann's avatar
Jan Winkelmann committed
19 20 21 22
	}
}

func NewReaderResponse(r io.Reader, encType EncodingType, req Request) Response {
23 24
	emitted := make(chan struct{})

Jan Winkelmann's avatar
Jan Winkelmann committed
25 26 27 28 29
	return &readerResponse{
		req:     req,
		r:       r,
		encType: encType,
		dec:     Decoders[encType](r),
30
		emitted: emitted,
Jan Winkelmann's avatar
Jan Winkelmann committed
31 32 33 34 35 36 37 38 39 40 41
	}
}

type readerResponse struct {
	r       io.Reader
	encType EncodingType
	dec     Decoder

	req Request

	length uint64
42
	err    *cmdsutil.Error
Jan Winkelmann's avatar
Jan Winkelmann committed
43

44 45
	emitted chan struct{}
	once    sync.Once
Jan Winkelmann's avatar
Jan Winkelmann committed
46 47
}

Jan Winkelmann's avatar
Jan Winkelmann committed
48 49
func (r *readerResponse) Request() Request {
	return r.req
Jan Winkelmann's avatar
Jan Winkelmann committed
50 51
}

52
func (r *readerResponse) Error() *cmdsutil.Error {
53 54
	<-r.emitted

Jan Winkelmann's avatar
Jan Winkelmann committed
55 56 57 58
	return r.err
}

func (r *readerResponse) Length() uint64 {
59 60
	<-r.emitted

Jan Winkelmann's avatar
Jan Winkelmann committed
61 62 63 64
	return r.length
}

func (r *readerResponse) Next() (interface{}, error) {
65 66 67
	a := &Any{}
	a.Add(cmdsutil.Error{})
	a.Add(r.req.Command().Type)
Jan Winkelmann's avatar
Jan Winkelmann committed
68

69 70 71 72 73
	err := r.dec.Decode(a)
	if err != nil {
		return nil, err
	}

74 75
	r.once.Do(func() { close(r.emitted) })

76
	v := a.Interface()
Jan Winkelmann's avatar
Jan Winkelmann committed
77 78 79 80 81 82 83
	if err, ok := v.(cmdsutil.Error); ok {
		r.err = &err
		return nil, ErrRcvdError
	}
	if err, ok := v.(*cmdsutil.Error); ok {
		r.err = err
		return nil, ErrRcvdError
84 85 86
	}

	return v, nil
Jan Winkelmann's avatar
Jan Winkelmann committed
87 88
}

89
type WriterResponseEmitter struct {
Jan Winkelmann's avatar
Jan Winkelmann committed
90
	// TODO maybe make those public?
91 92 93
	w   io.Writer
	c   io.Closer
	enc Encoder
Jan Winkelmann's avatar
Jan Winkelmann committed
94
	req Request
Jan Winkelmann's avatar
Jan Winkelmann committed
95 96

	length *uint64
97
	err    *cmdsutil.Error
Jan Winkelmann's avatar
Jan Winkelmann committed
98 99 100 101

	emitted bool
}

102
func (re *WriterResponseEmitter) SetError(v interface{}, errType cmdsutil.ErrorType) error {
Jan Winkelmann's avatar
Jan Winkelmann committed
103
	log.Debugf("re.SetError(%v, %v)", v, errType)
104
	return re.Emit(&cmdsutil.Error{Message: fmt.Sprint(v), Code: errType})
Jan Winkelmann's avatar
Jan Winkelmann committed
105 106
}

107
func (re *WriterResponseEmitter) SetLength(length uint64) {
Jan Winkelmann's avatar
Jan Winkelmann committed
108 109 110 111 112 113 114
	if re.emitted {
		return
	}

	*re.length = length
}

115 116 117 118 119 120 121 122 123 124 125 126
func (re *WriterResponseEmitter) Close() error {
	return re.c.Close()
}

func (re *WriterResponseEmitter) Head() Head {
	return Head{
		Len: *re.length,
		Err: re.err,
	}
}

func (re *WriterResponseEmitter) Emit(v interface{}) error {
127 128 129
	if ch, ok := v.(chan interface{}); ok {
		v = (<-chan interface{})(ch)
	}
Jan Winkelmann's avatar
Jan Winkelmann committed
130

131 132 133 134 135 136 137 138
	if ch, isChan := v.(<-chan interface{}); isChan {
		for v = range ch {
			err := re.Emit(v)
			if err != nil {
				return err
			}
		}
		return nil
Jan Winkelmann's avatar
Jan Winkelmann committed
139 140
	}

141 142 143
	re.emitted = true

	return re.enc.Encode(v)
Jan Winkelmann's avatar
Jan Winkelmann committed
144 145
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
type Any struct {
	types []reflect.Type

	v interface{}
}

func (a *Any) UnmarshalJSON(data []byte) error {
	var (
		iv  interface{}
		err error
	)

	for _, t := range a.types {
		v := reflect.New(t)

		err = json.Unmarshal(data, v.Interface())
		if err == nil && v.Elem().Interface() != reflect.Zero(t).Interface() {
			a.v = v.Elem().Interface()
			return nil
		}
	}

	err = json.Unmarshal(data, &iv)
	a.v = iv

	return err
}

func (a *Any) Add(v interface{}) {
	t := reflect.TypeOf(v)
	if t.Kind() == reflect.Ptr || t.Kind() == reflect.Interface {
		t = t.Elem()
	}

	a.types = append(a.types, t)
}

func (a *Any) Interface() interface{} {
	return a.v
}