Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
define the interface of downloader, data_scheduler, client_stream, re…
Browse files Browse the repository at this point in the history
…gister, uploader, data_protocol and url protocol.

Signed-off-by: allen.wq <[email protected]>
  • Loading branch information
wangforthinker committed May 18, 2020
1 parent f131b5f commit d04b08c
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 0 deletions.
34 changes: 34 additions & 0 deletions dfget/corev2/basic/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package basic

// Response defines the response.
type Response interface {
Success() bool
Data() interface{}
}

// RangeRequest defines the range request.
type RangeRequest interface {
URL() string
Offset() int64
Size() int64
Header() map[string]string

// Extra gets the extra info.
Extra() interface{}
}
31 changes: 31 additions & 0 deletions dfget/corev2/clientwriter/client_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package clientwriter

import (
"github.com/dragonflyoss/Dragonfly/pkg/protocol"
)

// ClientStream defines how to organize distribution data for range request.
// An instance binds to a range request.
// It may receive a lot of distribution data.
// Developer could add a io.WriteCloser in constructor of instance, and the ClientWriter will
// write request data to io.Writer.
type ClientWriter interface {
// WriteData writes the distribution data from other peers, it may be called more times.
PutData(data protocol.DistributionData) error
}
65 changes: 65 additions & 0 deletions dfget/corev2/datascheduler/data_scheduler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package datascheduler

import (
"context"

"github.com/dragonflyoss/Dragonfly/dfget/corev2/basic"

strfmt "github.com/go-openapi/strfmt"
)

// PeerInfo represents the target address which is provided the download data.
type PeerInfo struct {
IP strfmt.IPv4
Port int32
Path string
// ID represents the client ID of peer.
ID string
}

// SchedulerResult defines the result of schedule of range data.
type SchedulePieceDataResult struct {
Off int64
Size int64

// PeerInfos represents the schedule peers which to get the range data.
PeerInfos []*PeerInfo
}

// SchedulerResult defines the schedule result of request range.
// For some implementation, developer could do more than one schedule for the same request range.
type SchedulerResult interface {
// Result get the schedule result for range data which may not include all data of request range.
Result() []*SchedulePieceDataResult

// State gets the temporary states of this schedule which binds to range request.
State() ScheduleState
}

// ScheduleState defines the state of this schedule.
type ScheduleState interface {
// Continue tells user if reschedule the request range again.
Continue() bool
}

// DataScheduler defines how to schedule peers for range request.
type DataScheduler interface {
// state should be got from SchedulerResult which is got from last caller for the same range request.
Schedule(ctx context.Context, rr basic.RangeRequest, state ScheduleState) (SchedulerResult, error)
}
28 changes: 28 additions & 0 deletions dfget/corev2/downloader/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package downloader

import (
"context"
"io"
)

// Downloader defines how to download file range from peer/cdn, an instance binds to <task, target address>.
type Downloader interface {
// Download downloads range data.
Download(ctx context.Context, off, size int64) (io.ReadCloser, error)
}
24 changes: 24 additions & 0 deletions dfget/corev2/regist/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package regist

import "github.com/dragonflyoss/Dragonfly/dfget/corev2/basic"

// SupernodeRegister encapsulates the Register steps into a struct.
type SupernodeRegister interface {
Register(peerPort int) (response basic.Response, err error)
}
27 changes: 27 additions & 0 deletions dfget/corev2/report/reporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package report

import (
"github.com/dragonflyoss/Dragonfly/dfget/corev2/basic"
"github.com/dragonflyoss/Dragonfly/dfget/locator"
)

// Reporter defines how to report resource to suprnode.
type Reporter interface {
Report(supernode *locator.Supernode) (basic.Response, error)
}
25 changes: 25 additions & 0 deletions dfget/corev2/uploader/uploader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uploader

import "io"

// Uploader defines how to upload range by path.
type Uploader interface {
// UploadRange defines how to upload range by path.
UploadRange(path string, off, size int64, opt interface{}) (io.ReadCloser, error)
}
88 changes: 88 additions & 0 deletions pkg/protocol/distributiondata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package protocol

import (
"context"
"io"
)

// DataEncoder defines how to encode/decode data.
type DataEncoder interface {
// Encode data.
Encode(io.Reader) (io.Reader, error)

// Decode data.
Decode(io.Reader) (io.Reader, error)
}

// DataType defines the type of DistributionData.
type DataType interface {
// String return the type string.
String() string

// Encoder return the encoder of the type.
Encoder() DataEncoder
}

// DistributionData defines the protocol of distribute data which is exchanged in peers.
type DistributionData interface {
// Type gets the data type.
Type() DataType

// Size gets the size of data.
Size() int64

// Metadata gets the metadata.
Metadata() interface{}

// Content gets the content of data.
Content(ctx context.Context) (io.Reader, error)
}

type eofDataType struct{}

func (ty eofDataType) String() string {
return "EOF"
}

func (ty *eofDataType) Encoder() DataEncoder {
return nil
}

// EOFDistributionData represents the eof of file.
type eofDistributionData struct{}

func (eof *eofDistributionData) Size() int64 {
return 0
}

func (eof *eofDistributionData) Metadata() interface{} {
return nil
}

func (eof *eofDistributionData) Content(ctx context.Context) (io.Reader, error) {
return nil, io.EOF
}

func (eof *eofDistributionData) Type() DataType {
return &eofDataType{}
}

func NewEoFDistributionData() DistributionData {
return &eofDistributionData{}
}
Loading

0 comments on commit d04b08c

Please sign in to comment.