You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.go 4.3 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package hub
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  7. "gitlink.org.cn/cloudream/common/utils/serder"
  8. "google.golang.org/grpc"
  9. "google.golang.org/grpc/credentials/insecure"
  10. )
  11. type Client struct {
  12. con *grpc.ClientConn
  13. cli HubClient
  14. }
  15. func NewClient(addr string) (*Client, error) {
  16. con, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
  17. if err != nil {
  18. return nil, err
  19. }
  20. return &Client{
  21. con: con,
  22. cli: NewHubClient(con),
  23. }, nil
  24. }
  25. func (c *Client) ExecuteIOPlan(ctx context.Context, plan exec.Plan) error {
  26. data, err := serder.ObjectToJSONEx(plan)
  27. if err != nil {
  28. return err
  29. }
  30. _, err = c.cli.ExecuteIOPlan(ctx, &ExecuteIOPlanReq{
  31. Plan: string(data),
  32. })
  33. return err
  34. }
  35. type grpcStreamReadCloser struct {
  36. io.ReadCloser
  37. stream Hub_GetStreamClient
  38. cancelFn context.CancelFunc
  39. readingData []byte
  40. recvEOF bool
  41. }
  42. func (s *grpcStreamReadCloser) Read(p []byte) (int, error) {
  43. if len(s.readingData) == 0 && !s.recvEOF {
  44. resp, err := s.stream.Recv()
  45. if err != nil {
  46. return 0, err
  47. }
  48. if resp.Type == StreamDataPacketType_Data {
  49. s.readingData = resp.Data
  50. } else if resp.Type == StreamDataPacketType_EOF {
  51. s.readingData = resp.Data
  52. s.recvEOF = true
  53. } else {
  54. return 0, fmt.Errorf("unsupported packt type: %v", resp.Type)
  55. }
  56. }
  57. cnt := copy(p, s.readingData)
  58. s.readingData = s.readingData[cnt:]
  59. if len(s.readingData) == 0 && s.recvEOF {
  60. return cnt, io.EOF
  61. }
  62. return cnt, nil
  63. }
  64. func (s *grpcStreamReadCloser) Close() error {
  65. s.cancelFn()
  66. return nil
  67. }
  68. func (c *Client) SendStream(ctx context.Context, planID exec.PlanID, varID exec.VarID, str io.Reader) error {
  69. sendCli, err := c.cli.SendStream(ctx)
  70. if err != nil {
  71. return err
  72. }
  73. err = sendCli.Send(&StreamDataPacket{
  74. Type: StreamDataPacketType_SendArgs,
  75. PlanID: string(planID),
  76. VarID: int32(varID),
  77. })
  78. if err != nil {
  79. return fmt.Errorf("sending first stream packet: %w", err)
  80. }
  81. buf := make([]byte, 1024*64)
  82. for {
  83. rd, err := str.Read(buf)
  84. if err == io.EOF {
  85. err := sendCli.Send(&StreamDataPacket{
  86. Type: StreamDataPacketType_EOF,
  87. Data: buf[:rd],
  88. })
  89. if err != nil {
  90. return fmt.Errorf("sending EOF packet: %w", err)
  91. }
  92. _, err = sendCli.CloseAndRecv()
  93. if err != nil {
  94. return fmt.Errorf("receiving response: %w", err)
  95. }
  96. return nil
  97. }
  98. if err != nil {
  99. return fmt.Errorf("reading stream data: %w", err)
  100. }
  101. err = sendCli.Send(&StreamDataPacket{
  102. Type: StreamDataPacketType_Data,
  103. Data: buf[:rd],
  104. })
  105. if err != nil {
  106. return fmt.Errorf("sending data packet: %w", err)
  107. }
  108. }
  109. }
  110. func (c *Client) GetStream(ctx context.Context, planID exec.PlanID, varID exec.VarID, signalID exec.VarID, signal exec.VarValue) (io.ReadCloser, error) {
  111. ctx, cancel := context.WithCancel(ctx)
  112. sdata, err := serder.ObjectToJSONEx(signal)
  113. if err != nil {
  114. cancel()
  115. return nil, err
  116. }
  117. stream, err := c.cli.GetStream(ctx, &GetStreamReq{
  118. PlanID: string(planID),
  119. VarID: int32(varID),
  120. SignalID: int32(signalID),
  121. Signal: string(sdata),
  122. })
  123. if err != nil {
  124. cancel()
  125. return nil, fmt.Errorf("request grpc failed, err: %w", err)
  126. }
  127. return &grpcStreamReadCloser{
  128. stream: stream,
  129. cancelFn: cancel,
  130. }, nil
  131. }
  132. func (c *Client) SendVar(ctx context.Context, planID exec.PlanID, id exec.VarID, value exec.VarValue) error {
  133. data, err := serder.ObjectToJSONEx(value)
  134. if err != nil {
  135. return err
  136. }
  137. _, err = c.cli.SendVar(ctx, &SendVarReq{
  138. PlanID: string(planID),
  139. VarID: int32(id),
  140. VarValue: string(data),
  141. })
  142. return err
  143. }
  144. func (c *Client) GetVar(ctx context.Context, planID exec.PlanID, varID exec.VarID, signalID exec.VarID, signal exec.VarValue) (exec.VarValue, error) {
  145. sdata, err := serder.ObjectToJSONEx(signal)
  146. if err != nil {
  147. return nil, err
  148. }
  149. resp, err := c.cli.GetVar(ctx, &GetVarReq{
  150. PlanID: string(planID),
  151. VarID: int32(varID),
  152. SignalID: int32(signalID),
  153. Signal: string(sdata),
  154. })
  155. if err != nil {
  156. return nil, err
  157. }
  158. getVar, err := serder.JSONToObjectEx[exec.VarValue]([]byte(resp.Var))
  159. if err != nil {
  160. return nil, err
  161. }
  162. return getVar, nil
  163. }
  164. func (c *Client) Ping() error {
  165. _, err := c.cli.Ping(context.Background(), &PingReq{})
  166. return err
  167. }
  168. func (c *Client) Close() {
  169. c.con.Close()
  170. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。