/*
 *     Copyright 2022 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.
 */

syntax = "proto3";

package cdnsystem;

import "pkg/apis/common/v1/common.proto";
import "validate/validate.proto";

option go_package = "d7y.io/api/v2/pkg/apis/cdnsystem/v1;cdnsystem";

message SeedRequest{
  string task_id = 1 [(validate.rules).string.min_len = 1];
  string url = 2 [(validate.rules).string.uri = true];
  common.UrlMeta url_meta = 3;
}

// keep piece meta and data separately
// check piece md5, md5s sign and total content length
message PieceSeed{
  // reuse already downloaded peer
  bool reuse = 1;
  // peer id for cdn node, need suffix with _CDN
  string peer_id = 2 [(validate.rules).string.min_len = 1];
  // cdn host id
  string host_id = 3 [(validate.rules).string.min_len = 1];
  // piece info
  common.PieceInfo piece_info = 4;
  // whether or not all seeds are downloaded
  bool done = 5;
  // content total length for the url, content_length < 0 represent content length is unknown
  int64 content_length = 6;
  // total piece count, -1 represents task is downloading or failed
  int32 total_piece_count = 7;
  // begin time for the piece downloading
  uint64 begin_time = 8;
  // end time for the piece downloading
  uint64 end_time = 9;
  // task extend attribute
  common.ExtendAttribute extend_attribute = 10;
}

// CDN System RPC Service
service Seeder{
  // Generate seeds and return to scheduler
  rpc ObtainSeeds(SeedRequest)returns(stream PieceSeed);
  // Get piece tasks from cdn
  rpc GetPieceTasks(common.PieceTaskRequest)returns(common.PiecePacket);
  // Sync piece tasks with other peers
  rpc SyncPieceTasks(stream common.PieceTaskRequest)returns(stream common.PiecePacket);
}

