// Originally taken from: https://github.com/mhchia/py-libp2p-daemon-bindings
// Licence: MIT
// Author: Kevin Mai-Husan Chia

syntax = "proto2";

package p2pclient.p2pd.pb;

message Request {
  enum Type {
    IDENTIFY                 = 0;
    CONNECT                  = 1;
    STREAM_OPEN              = 2;
    STREAM_HANDLER           = 3;
    REMOVE_STREAM_HANDLER    = 10;
    DHT                      = 4;
    LIST_PEERS               = 5;
    CONNMANAGER              = 6;
    DISCONNECT               = 7;
    PUBSUB                   = 8;
    PERSISTENT_CONN_UPGRADE  = 9;
  }

  required Type type = 1;

  optional ConnectRequest connect = 2;
  optional StreamOpenRequest streamOpen = 3;
  optional StreamHandlerRequest streamHandler = 4;
  optional RemoveStreamHandlerRequest removeStreamHandler = 9;
  optional DHTRequest dht = 5;
  optional ConnManagerRequest connManager = 6;
  optional DisconnectRequest disconnect = 7;
  optional PSRequest pubsub = 8;
}

message Response {
  enum Type {
    OK    = 0;
    ERROR = 1;
  }

  required Type type = 1;
  optional ErrorResponse error = 2;
  optional StreamInfo streamInfo = 3;
  optional IdentifyResponse identify = 4;
  optional DHTResponse dht = 5;
  repeated PeerInfo peers = 6;
  optional PSResponse pubsub = 7;
}

message PersistentConnectionRequest {
  required bytes callId = 1;

  oneof message {
    AddUnaryHandlerRequest addUnaryHandler = 2;
    RemoveUnaryHandlerRequest removeUnaryHandler = 6;
    CallUnaryRequest  callUnary = 3;
    CallUnaryResponse unaryResponse = 4;
    Cancel cancel = 5;
  }
}

message PersistentConnectionResponse {
  required bytes callId = 1;

  oneof message {
    CallUnaryResponse callUnaryResponse = 2;
    CallUnaryRequest requestHandling = 3;
    DaemonError daemonError = 4;
    Cancel cancel = 5;
  }
}


message IdentifyResponse {
  required bytes id = 1;
  repeated bytes addrs = 2;
}

message ConnectRequest {
  required bytes peer = 1;
  repeated bytes addrs = 2;
  optional int64 timeout = 3;
}

message StreamOpenRequest {
  required bytes peer = 1;
  repeated string proto = 2;
  optional int64 timeout = 3;
}

message StreamHandlerRequest {
  required bytes addr = 1;
  repeated string proto = 2;
  required bool balanced = 3;
}

message RemoveStreamHandlerRequest {
  required bytes addr = 1;
  repeated string proto = 2;
}

message ErrorResponse {
  required string msg = 1;
}

message StreamInfo {
  required bytes peer = 1;
  required bytes addr = 2;
  required string proto = 3;
}

message DHTRequest {
  enum Type {
    FIND_PEER                    = 0;
    FIND_PEERS_CONNECTED_TO_PEER = 1;
    FIND_PROVIDERS               = 2;
    GET_CLOSEST_PEERS            = 3;
    GET_PUBLIC_KEY               = 4;
    GET_VALUE                    = 5;
    SEARCH_VALUE                 = 6;
    PUT_VALUE                    = 7;
    PROVIDE                      = 8;
  }

  required Type type = 1;
  optional bytes peer = 2;
  optional bytes cid = 3;
  optional bytes key = 4;
  optional bytes value = 5;
  optional int32 count = 6;
  optional int64 timeout = 7;
}

message DHTResponse {
  enum Type {
    BEGIN = 0;
    VALUE = 1;
    END   = 2;
  }

  required Type type = 1;
  optional PeerInfo peer = 2;
  optional bytes value = 3;
}

message PeerInfo {
  required bytes id = 1;
  repeated bytes addrs = 2;
}

message ConnManagerRequest {
  enum Type {
    TAG_PEER        = 0;
    UNTAG_PEER      = 1;
    TRIM            = 2;
  }

  required Type type = 1;

  optional bytes peer = 2;
  optional string tag = 3;
  optional int64 weight = 4;
}

message DisconnectRequest {
  required bytes peer = 1;
}

message PSRequest {
  enum Type {
    GET_TOPICS = 0;
    LIST_PEERS = 1;
    PUBLISH    = 2;
    SUBSCRIBE  = 3;
  }

  required Type type = 1;
  optional string topic = 2;
  optional bytes data = 3;
}

message PSMessage {
  optional bytes from = 1;
  optional bytes data = 2;
  optional bytes seqno = 3;
  repeated string topicIDs = 4;
  optional bytes signature = 5;
  optional bytes key = 6;
}

message PSResponse {
  repeated string topics = 1;
  repeated bytes peerIDs = 2;
}

message CallUnaryRequest {
  required bytes peer = 1;
  required string proto = 2;
  required bytes data = 3;
}

message CallUnaryResponse {
  oneof result {
    bytes response = 1;
    bytes error = 2;
  }
}

message AddUnaryHandlerRequest {
  required string proto = 1;
  required bool balanced = 2;
}

message RemoveUnaryHandlerRequest {
  required string proto = 1;
}

message DaemonError {
  optional string message = 1;
}

message Cancel {
}

message RPCError {
  optional string message = 1;
}
