2015-01-13 05:31:14 -07:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 12:42:11 -07:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestModel struct {
|
|
|
|
data []byte
|
2014-09-28 04:05:25 -07:00
|
|
|
folder string
|
2014-09-22 12:42:11 -07:00
|
|
|
name string
|
|
|
|
offset int64
|
|
|
|
size int
|
2015-01-24 14:56:12 -07:00
|
|
|
hash []byte
|
|
|
|
flags uint32
|
|
|
|
options []Option
|
2014-09-22 12:42:11 -07:00
|
|
|
closedCh chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestModel() *TestModel {
|
|
|
|
return &TestModel{
|
|
|
|
closedCh: make(chan bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo) {
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) {
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
2015-01-24 14:56:12 -07:00
|
|
|
func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) {
|
2014-09-28 04:00:38 -07:00
|
|
|
t.folder = folder
|
2014-09-22 12:42:11 -07:00
|
|
|
t.name = name
|
|
|
|
t.offset = offset
|
|
|
|
t.size = size
|
2015-01-24 14:56:12 -07:00
|
|
|
t.hash = hash
|
|
|
|
t.flags = flags
|
|
|
|
t.options = options
|
2014-09-22 12:42:11 -07:00
|
|
|
return t.data, nil
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (t *TestModel) Close(deviceID DeviceID, err error) {
|
2014-09-22 12:42:11 -07:00
|
|
|
close(t.closedCh)
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestModel) isClosed() bool {
|
|
|
|
select {
|
|
|
|
case <-t.closedCh:
|
|
|
|
return true
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
return false // Timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrPipe struct {
|
|
|
|
io.PipeWriter
|
|
|
|
written int
|
|
|
|
max int
|
|
|
|
err error
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrPipe) Write(data []byte) (int, error) {
|
|
|
|
if e.closed {
|
|
|
|
return 0, e.err
|
|
|
|
}
|
|
|
|
if e.written+len(data) > e.max {
|
|
|
|
n, _ := e.PipeWriter.Write(data[:e.max-e.written])
|
|
|
|
e.PipeWriter.CloseWithError(e.err)
|
|
|
|
e.closed = true
|
|
|
|
return n, e.err
|
|
|
|
}
|
|
|
|
return e.PipeWriter.Write(data)
|
|
|
|
}
|