-
Notifications
You must be signed in to change notification settings - Fork 4
/
vfindcallrequest.go
186 lines (148 loc) · 5.27 KB
/
vfindcallrequest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//go:generate sm-uml-gen -f $GOFILE
package handlers
import (
"context"
"github.com/insolar/assured-ledger/ledger-core/conveyor"
"github.com/insolar/assured-ledger/ledger-core/conveyor/smachine"
"github.com/insolar/assured-ledger/ledger-core/network/messagesender"
messageSenderAdapter "github.com/insolar/assured-ledger/ledger-core/network/messagesender/adapter"
"github.com/insolar/assured-ledger/ledger-core/rms"
"github.com/insolar/assured-ledger/ledger-core/vanilla/injector"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
"github.com/insolar/assured-ledger/ledger-core/virtual/callsummary"
)
type SMVFindCallRequest struct {
// input arguments
Meta *rms.Meta
Payload *rms.VFindCallRequest
// dependencies
messageSender messageSenderAdapter.MessageSender
pulseSlot *conveyor.PulseSlot
syncLinkAccessor callsummary.SyncAccessor
callResult *rms.VCallResult
status rms.VFindCallResponse_CallState
}
/* -------- Declaration ------------- */
var dSMVFindCallRequestInstance smachine.StateMachineDeclaration = &dSMVFindCallRequest{}
type dSMVFindCallRequest struct {
smachine.StateMachineDeclTemplate
}
func (*dSMVFindCallRequest) InjectDependencies(sm smachine.StateMachine, _ smachine.SlotLink, injector injector.DependencyInjector) {
s := sm.(*SMVFindCallRequest)
injector.MustInject(&s.pulseSlot)
injector.MustInject(&s.messageSender)
}
func (*dSMVFindCallRequest) GetInitStateFor(sm smachine.StateMachine) smachine.InitFunc {
s := sm.(*SMVFindCallRequest)
return s.Init
}
/* -------- Instance ------------- */
func (s *SMVFindCallRequest) GetStateMachineDeclaration() smachine.StateMachineDeclaration {
return dSMVFindCallRequestInstance
}
func (s *SMVFindCallRequest) Init(ctx smachine.InitializationContext) smachine.StateUpdate {
if s.pulseSlot.State() == conveyor.Present {
return ctx.JumpExt(smachine.SlotStep{
Transition: s.stepWait,
Migration: s.migrateFutureMessage,
})
}
return ctx.Jump(s.stepProcessRequest)
}
func (s *SMVFindCallRequest) stepWait(ctx smachine.ExecutionContext) smachine.StateUpdate {
return ctx.Sleep().ThenRepeat()
}
func (s *SMVFindCallRequest) migrateFutureMessage(ctx smachine.MigrationContext) smachine.StateUpdate {
return ctx.Jump(s.stepProcessRequest)
}
func (s *SMVFindCallRequest) stepProcessRequest(ctx smachine.ExecutionContext) smachine.StateUpdate {
accessor, isCondPublished := callsummary.GetSummarySMSyncAccessor(ctx, s.Payload.Callee.GetValue())
if isCondPublished {
// if cond is still published we need to wait for object to register itself in call summary
s.syncLinkAccessor = accessor
return ctx.Jump(s.stepWaitCallResult)
}
return ctx.Jump(s.stepGetRequestData)
}
func (s *SMVFindCallRequest) stepWaitCallResult(ctx smachine.ExecutionContext) smachine.StateUpdate {
var cond smachine.SyncLink
switch s.syncLinkAccessor.Prepare(func(link *smachine.SyncLink) {
cond = *link
}).TryUse(ctx).GetDecision() {
case smachine.NotPassed:
return ctx.WaitShared(s.syncLinkAccessor.SharedDataLink).ThenRepeat()
case smachine.Impossible:
panic(throw.NotImplemented())
case smachine.Passed:
// go further
default:
panic(throw.Impossible())
}
if ctx.Acquire(cond).IsNotPassed() {
return ctx.Sleep().ThenRepeat()
}
return ctx.Jump(s.stepGetRequestData)
}
func (s *SMVFindCallRequest) stepGetRequestData(ctx smachine.ExecutionContext) smachine.StateUpdate {
summarySharedStateAccessor, ok := callsummary.GetSummarySMSharedAccessor(ctx, s.pulseSlot.PulseData().PulseNumber)
// no result
if !ok {
return ctx.Jump(s.stepNotFoundResponse)
}
var (
foundResult bool
)
action := func(sharedCallSummary *callsummary.SharedCallSummary) {
requests, ok := sharedCallSummary.Requests.GetObjectCallResults(s.Payload.Callee.GetValue())
if !ok {
return
}
objectCallResult, ok := requests.CallResults[s.Payload.Outgoing.GetValue()]
if !ok {
return
}
foundResult = true
s.callResult = objectCallResult.Result
}
switch summarySharedStateAccessor.Prepare(action).TryUse(ctx).GetDecision() {
case smachine.NotPassed:
return ctx.WaitShared(summarySharedStateAccessor.SharedDataLink).ThenRepeat()
case smachine.Impossible:
panic(throw.NotImplemented())
case smachine.Passed:
// go further
default:
panic(throw.Impossible())
}
if !foundResult {
return ctx.Jump(s.stepNotFoundResponse)
}
s.status = rms.CallStateFound
return ctx.Jump(s.stepSendResponse)
}
func (s *SMVFindCallRequest) stepNotFoundResponse(ctx smachine.ExecutionContext) smachine.StateUpdate {
s.status = rms.CallStateMissing
if s.Payload.Outgoing.GetPulseOfLocal() < s.Payload.LookAt {
s.status = rms.CallStateUnknown
}
return ctx.Jump(s.stepSendResponse)
}
func (s *SMVFindCallRequest) stepSendResponse(ctx smachine.ExecutionContext) smachine.StateUpdate {
target := s.Meta.Sender.GetValue()
msg := rms.VFindCallResponse{
LookedAt: s.Payload.LookAt,
Callee: s.Payload.Callee,
Outgoing: s.Payload.Outgoing,
Status: s.status,
CallResult: s.callResult,
}
s.messageSender.PrepareAsync(ctx, func(goCtx context.Context, svc messagesender.Service) smachine.AsyncResultFunc {
err := svc.SendTarget(goCtx, &msg, target)
return func(ctx smachine.AsyncResultContext) {
if err != nil {
ctx.Log().Error("failed to send message", err)
}
}
}).WithoutAutoWakeUp().Start()
return ctx.Stop()
}