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.

sync.go 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package ops
  2. import (
  3. "fmt"
  4. "io"
  5. "gitlink.org.cn/cloudream/common/pkgs/future"
  6. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  8. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/utils"
  9. )
  10. func init() {
  11. exec.UseOp[*OnStreamBegin]()
  12. exec.UseOp[*OnStreamEnd]()
  13. exec.UseOp[*HoldUntil]()
  14. exec.UseOp[*HangUntil]()
  15. exec.UseOp[*Broadcast]()
  16. }
  17. type OnStreamBegin struct {
  18. Raw exec.VarID `json:"raw"`
  19. New exec.VarID `json:"new"`
  20. Signal exec.SignalVar `json:"signal"`
  21. }
  22. func (o *OnStreamBegin) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  23. raw, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Raw)
  24. if err != nil {
  25. return err
  26. }
  27. e.PutVar(o.New, &exec.StreamValue{Stream: raw.Stream}).
  28. PutVar(o.Signal.ID, o.Signal.Value)
  29. return nil
  30. }
  31. func (o *OnStreamBegin) String() string {
  32. return fmt.Sprintf("OnStreamBegin %v->%v S:%v", o.Raw, o.New, o.Signal.ID)
  33. }
  34. type OnStreamEnd struct {
  35. Raw exec.VarID `json:"raw"`
  36. New exec.VarID `json:"new"`
  37. Signal *exec.SignalVar `json:"signal"`
  38. }
  39. type onStreamEnd struct {
  40. inner io.ReadCloser
  41. callback *future.SetVoidFuture
  42. }
  43. func (o *onStreamEnd) Read(p []byte) (n int, err error) {
  44. n, err = o.inner.Read(p)
  45. if err == io.EOF {
  46. o.callback.SetVoid()
  47. } else if err != nil {
  48. o.callback.SetError(err)
  49. }
  50. return n, err
  51. }
  52. func (o *onStreamEnd) Close() error {
  53. o.callback.SetError(fmt.Errorf("stream closed early"))
  54. return o.inner.Close()
  55. }
  56. func (o *OnStreamEnd) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  57. raw, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Raw)
  58. if err != nil {
  59. return err
  60. }
  61. cb := future.NewSetVoid()
  62. e.PutVar(o.New, &exec.StreamValue{Stream: &onStreamEnd{
  63. inner: raw.Stream,
  64. callback: cb,
  65. }})
  66. err = cb.Wait(ctx.Context)
  67. if err != nil {
  68. return err
  69. }
  70. e.PutVar(o.Signal.ID, o.Signal.Value)
  71. return nil
  72. }
  73. func (o *OnStreamEnd) String() string {
  74. return fmt.Sprintf("OnStreamEnd %v->%v S:%v", o.Raw, o.New, o.Signal.ID)
  75. }
  76. type HoldUntil struct {
  77. Waits []exec.VarID `json:"waits"`
  78. Holds []exec.VarID `json:"holds"`
  79. Emits []exec.VarID `json:"emits"`
  80. }
  81. func (w *HoldUntil) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  82. holds, err := exec.BindArray[exec.VarValue](e, ctx.Context, w.Holds)
  83. if err != nil {
  84. return err
  85. }
  86. _, err = exec.BindArray[exec.VarValue](e, ctx.Context, w.Waits)
  87. if err != nil {
  88. return err
  89. }
  90. exec.PutArray(e, w.Emits, holds)
  91. return nil
  92. }
  93. func (w *HoldUntil) String() string {
  94. return fmt.Sprintf("HoldUntil Waits: %v, (%v) -> (%v)", utils.FormatVarIDs(w.Waits), utils.FormatVarIDs(w.Holds), utils.FormatVarIDs(w.Emits))
  95. }
  96. type HangUntil struct {
  97. Waits []exec.VarID `json:"waits"`
  98. Op exec.Op `json:"op"`
  99. }
  100. func (h *HangUntil) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  101. _, err := exec.BindArray[exec.VarValue](e, ctx.Context, h.Waits)
  102. if err != nil {
  103. return err
  104. }
  105. return h.Op.Execute(ctx, e)
  106. }
  107. func (h *HangUntil) String() string {
  108. return "HangUntil"
  109. }
  110. type Broadcast struct {
  111. Source exec.VarID `json:"source"`
  112. Targets []exec.VarID `json:"targets"`
  113. }
  114. func (b *Broadcast) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  115. src, err := exec.BindVar[*exec.SignalValue](e, ctx.Context, b.Source)
  116. if err != nil {
  117. return err
  118. }
  119. targets := make([]exec.VarValue, len(b.Targets))
  120. for i := 0; i < len(b.Targets); i++ {
  121. targets[i] = src.Clone()
  122. }
  123. exec.PutArray(e, b.Targets, targets)
  124. return nil
  125. }
  126. func (b *Broadcast) String() string {
  127. return "Broadcast"
  128. }
  129. type HoldUntilNode struct {
  130. dag.NodeBase
  131. }
  132. func (b *GraphNodeBuilder) NewHoldUntil() *HoldUntilNode {
  133. node := &HoldUntilNode{}
  134. b.AddNode(node)
  135. return node
  136. }
  137. func (t *HoldUntilNode) SetSignal(s *dag.Var) {
  138. t.InputValues().EnsureSize(1)
  139. s.ValueTo(t, 0)
  140. }
  141. func (t *HoldUntilNode) HoldStream(str *dag.Var) *dag.Var {
  142. str.StreamTo(t, t.InputStreams().EnlargeOne())
  143. output := t.Graph().NewVar()
  144. t.OutputStreams().SetupNew(t, output)
  145. return output
  146. }
  147. func (t *HoldUntilNode) HoldVar(v *dag.Var) *dag.Var {
  148. v.ValueTo(t, t.InputValues().EnlargeOne())
  149. output := t.Graph().NewVar()
  150. t.OutputValues().SetupNew(t, output)
  151. return output
  152. }
  153. func (t *HoldUntilNode) GenerateOp() (exec.Op, error) {
  154. o := &HoldUntil{
  155. Waits: []exec.VarID{t.InputValues().Get(0).VarID},
  156. }
  157. for i := 0; i < t.OutputValues().Len(); i++ {
  158. o.Holds = append(o.Holds, t.InputValues().Get(i+1).VarID)
  159. o.Emits = append(o.Emits, t.OutputValues().Get(i).VarID)
  160. }
  161. for i := 0; i < t.OutputStreams().Len(); i++ {
  162. o.Holds = append(o.Holds, t.InputStreams().Get(i).VarID)
  163. o.Emits = append(o.Emits, t.OutputStreams().Get(i).VarID)
  164. }
  165. return o, nil
  166. }
  167. // func (t *HoldUntilType) String() string {
  168. // return fmt.Sprintf("HoldUntil[]%v%v", formatStreamIO(node), formatValueIO(node))
  169. // }