-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.go
1438 lines (1189 loc) · 39.9 KB
/
class.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package pces
// file class.go holds structures, methods, functions, data structures, and event handlers
// related to the 'class' specialization of instances of computational functions
import (
"fmt"
"github.com/iti/evt/evtm"
"github.com/iti/evt/vrtime"
"github.com/iti/mrnes"
"gopkg.in/yaml.v3"
"strconv"
"encoding/json"
"math"
"strings"
)
// FuncClassCfg represents the methods used to simulate the effect
// of executing a function, different types of input generate different
// types of responses, so we use a map whose key selects the start, end pair of methods
type FuncClassCfg interface {
FuncClassName() string
CreateCfg(string) any
InitCfg(*evtm.EventManager, *CmpPtnFuncInst, string, bool)
ValidateCfg(*CmpPtnFuncInst) error
}
// StartMethod gives the signature of functions called to implement
// a function's entry point
type StartMethod func(*evtm.EventManager, *CmpPtnFuncInst, string, *CmpPtnMsg)
// RespMethod associates two RespFunc that implement a function's response,
// one when it starts, the other when it ends
type RespMethod struct {
Start StartMethod
End evtm.EventHandlerFunction
}
// FuncClassNames needs to have an indexing key for every class of function that
// might be included in a model. Map exists just for checking validity of
// a function class name when building a model. Any new class introduced
// in this file needs to have an entry placed in this dictionary.
var FuncClassNames map[string]bool = map[string]bool{
// "connSrc": true,
"measure": true,
"processPckt": true,
"srvRsp": true,
"srvReq": true,
"transfer": true,
"start": true,
"finish": true,
"bckgrndLd": true}
// RegisterFuncClass is called to tell the system that a particular
// function class exists, and gives a point to its description.
// The idea is to register only those function classes actually used, or at least,
// provide clear separation between classes. Reflect of bool allows call to RegisterFuncClass
// as part of a variable assignment outside of a function body
func RegisterFuncClass(fc FuncClassCfg) bool {
className := fc.FuncClassName()
_, present := FuncClasses[className]
if present {
return true
}
FuncClasses[className] = fc
FuncClassNames[className] = true
return true
}
// FuncClasses is a map that takes us from the name of a FuncClass
var FuncClasses map[string]FuncClassCfg = make(map[string]FuncClassCfg)
// HndlrMap maps a string name for an event handling function to the function itself
var HndlrMap map[string]evtm.EventHandlerFunction = map[string]evtm.EventHandlerFunction{"empty": EmptyInitFunc}
// AddHndlrMap includes a new association between string and event handler
func AddHndlrMap(name string, hndlr evtm.EventHandlerFunction) {
if HndlrMap == nil {
HndlrMap = make(map[string]evtm.EventHandlerFunction)
}
HndlrMap[name] = hndlr
}
// ClassMethods maps the function class name to a map indexed by (string) operation code to get to
// a pair of functions to handle the entry and exit to the function
var ClassMethods map[string]map[string]RespMethod = make(map[string]map[string]RespMethod)
var ClassMethodsBuilt bool = CreateClassMethods()
// CreateClassMethods fills in the ClassMethods table for each of the defined function classes
func CreateClassMethods() bool {
// ClassMethod key is the string identity of a function class, points to
// a map whose key is a methodCode.
// build table for processPckt class
fmap := make(map[string]RespMethod)
fmap["default"] = RespMethod{Start: processPcktEnter, End: processPcktExit}
fmap["processOp"] = RespMethod{Start: processPcktEnter, End: processPcktExit}
ClassMethods["processPckt"] = fmap
// build table for start class
fmap = make(map[string]RespMethod)
fmap["default"] = RespMethod{Start: startEnter, End: ExitFunc}
ClassMethods["start"] = fmap
// build table for finish class
fmap = make(map[string]RespMethod)
fmap["default"] = RespMethod{Start: finishEnter, End: ExitFunc}
fmap["finishOp"] = RespMethod{Start: finishEnter, End: ExitFunc}
ClassMethods["finish"] = fmap
fmap = make(map[string]RespMethod)
fmap["default"] = RespMethod{Start: measureEnter, End: ExitFunc}
fmap["measure"] = RespMethod{Start: measureEnter, End: ExitFunc}
ClassMethods["measure"] = fmap
fmap = make(map[string]RespMethod)
fmap["default"] = RespMethod{Start: srvRspEnter, End: ExitFunc}
ClassMethods["srvRsp"] = fmap
fmap = make(map[string]RespMethod)
fmap["default"] = RespMethod{Start: srvReqEnter, End: ExitFunc}
fmap["request"] = RespMethod{Start: srvReqEnter, End: ExitFunc}
fmap["return"] = RespMethod{Start: srvReqRtn, End: ExitFunc}
ClassMethods["srvReq"] = fmap
fmap = make(map[string]RespMethod)
fmap["default"] = RespMethod{Start: transferEnter, End: ExitFunc}
ClassMethods["transfer"] = fmap
// need to have bckgrndLd as a key to ClassMethods, but it
// doesn't use the RespMethod mechanisms
ClassMethods["bckgrndLd"] = make(map[string]RespMethod)
return true
}
func checkMCValidity(funcClass string, mc string) bool {
fcm, present := ClassMethods[funcClass]
if !present {
return false
}
_, present = fcm[mc]
return present
}
func CreateNewClass(className string) {
fmap := make(map[string]RespMethod)
ClassMethods[className] = fmap
}
func AddClassMethod(className string, methodName string, rm RespMethod) {
ClassMethods[className][methodName] = rm
}
// UpdateMsg copies the pattern, label coordinates of the message's current
// position to be the previous one and labels the next coordinates with
// the values given as arguments
func UpdateMsg(msg *CmpPtnMsg, nxtCPID int, nxtLabel, msgType string) {
msg.MsgType = msgType
msg.CPID = nxtCPID
msg.Label = nxtLabel
}
// validFunctionClass checks whether the string argument is a function class
// name that appears as an index in the FuncClasses map to an entity that
// meets the FuncClassCfg interface
func validFuncClass(class string) bool {
_, present := FuncClasses[class]
return present
}
func advanceMsg(cpfi *CmpPtnFuncInst, msg *CmpPtnMsg, msgTypeOut string) *CmpPtnMsg {
var nxtCPID int
var nxtMsgType string
var nxtLabel string
var eeidx int
var present bool
// if there is only one outedge we'll choose that. Throw a warning if
// the msgType passed in differs from the msgType on the edge
if len(cpfi.OutEdges) == 1 {
eeidx = 0
nxtMsgType = cpfi.OutEdges[0].MsgType
} else if len(msgTypeOut) > 0 {
// Get the index of the OutEdge associated with the passed-in msgType.
eeidx, present = cpfi.Msg2Idx[msgTypeOut]
if !present {
panic(fmt.Errorf("expected output edge for funcion %s", cpfi.Label))
}
nxtMsgType = cpfi.OutEdges[eeidx].MsgType
}
// pull the next message's type, computation pattern, and label of target function from
// the outedge structure
nxtLabel = cpfi.OutEdges[eeidx].FuncLabel
nxtCPID = cpfi.OutEdges[eeidx].CPID
// modify the message in place
UpdateMsg(msg, nxtCPID, nxtLabel, nxtMsgType)
// put the response where ExitFunc will find it
cpfi.AddResponse(msg.ExecID, []*CmpPtnMsg{msg})
return msg
}
func copyDict(dict1, dict2 map[string]string) {
for key, value := range dict2 {
dict1[key] = value
}
}
//-------- methods and state for function class processPckt
var ppVar *processPcktCfg = ClassCreateProcessPcktCfg()
var processPcktLoaded bool = RegisterFuncClass(ppVar)
type processPcktCfg struct {
// map method code to an operation for the timing lookup
TimingCode map[string]string `yaml:"timingcode" json:"timingcode"`
Msg2MC map[string]string `yaml:"msg2mc" json:"msg2mc"`
Msg2Msg map[string]string `yaml:"msg2msg" json:"msg2msg"`
// if the packet is processed through an accelerator, its name in the destination endpoint
AccelName string `yaml:"accelname" json:"accelname"`
Trace string `yaml:"trace" json:"trace"`
}
type processPcktState struct {
msgTypeIn string
calls int
}
// ClassCreateProcessPcktCfg is a constructor called just to create an instance, fields unimportant
func ClassCreateProcessPcktCfg() *processPcktCfg {
pp := new(processPcktCfg)
pp.TimingCode = make(map[string]string)
pp.Msg2MC = make(map[string]string)
pp.Msg2Msg = make(map[string]string)
pp.AccelName = ""
pp.Trace = "0"
return pp
}
func createProcessPcktState(ppc *processPcktCfg) *processPcktState {
pps := new(processPcktState)
pps.calls = 0
return pps
}
func (pp *processPcktCfg) FuncClassName() string {
return "processPckt"
}
func (pp *processPcktCfg) CreateCfg(cfgStr string) any {
useYAML := (cfgStr[0] != '{')
ppVarAny, err := pp.Deserialize(cfgStr, useYAML)
if err != nil {
panic(fmt.Errorf("processPckt.InitCfg sees deserialization error"))
}
return ppVarAny
}
func (pp *processPcktCfg) InitCfg(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, cfgStr string, useYAML bool) {
ppVarAny := pp.CreateCfg(cfgStr)
ppv := ppVarAny.(*processPcktCfg)
cpfi.Cfg = ppv
cpfi.State = createProcessPcktState(ppv)
copyDict(cpfi.Msg2MC, ppv.Msg2MC)
cpfi.Trace = (ppv.Trace != "0")
}
func (pp *processPcktCfg) ValidateCfg(cpfi *CmpPtnFuncInst) error {
return nil
}
// Serialize transforms the processPckt into string form for
// inclusion through a file
func (pp *processPcktCfg) Serialize(useYAML bool) (string, error) {
var bytes []byte
var merr error
if useYAML {
bytes, merr = yaml.Marshal(*pp)
} else {
bytes, merr = json.Marshal(*pp)
}
if merr != nil {
return "", merr
}
return string(bytes[:]), nil
}
func (pp *processPcktCfg) CfgStr() string {
rtn, err := pp.Serialize(true)
if err != nil {
panic(fmt.Errorf("process packet cfg serialization error"))
}
return rtn
}
// Deserialize recovers a serialized representation of a processPckt structure
func (pp *processPcktCfg) Deserialize(fss string, useYAML bool) (any, error) {
// turn the string into a slice of bytes
var err error
fsb := []byte(fss)
tc := make(map[string]string)
example := processPcktCfg{TimingCode: tc, Trace: "0"}
// Select whether we read in json or yaml
if useYAML {
err = yaml.Unmarshal(fsb, &example)
} else {
err = json.Unmarshal(fsb, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// processPcktEnter schedules the simulation of processing one packet.
// Default handler
func processPcktEnter(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, methodCode string, msg *CmpPtnMsg) {
pps := cpfi.State.(*processPcktState)
ppc := cpfi.Cfg.(*processPcktCfg)
pps.calls += 1
// determine whether an accelerator call is part of this cpfi and if so get the right time and scheduler
var genTime float64
var scheduler *mrnes.TaskScheduler
pps.msgTypeIn = msg.MsgType
opCode := msg.MsgType
if len(ppc.AccelName) > 0 {
// look up the model associated with this name
genTime = AccelFuncExecTime(cpfi, ppc.AccelName, ppc.TimingCode[opCode], msg)
scheduler = mrnes.AccelSchedulersByHostName[cpfi.Host][ppc.AccelName]
} else {
// not an accelerator call. look up the generation service requirement.
genTime = HostFuncExecTime(cpfi, ppc.TimingCode[opCode], msg)
scheduler = mrnes.TaskSchedulerByHostName[cpfi.Host]
}
endPtID := mrnes.EndptDevByName[cpfi.Host].DevID()
execID := msg.ExecID
// call the scheduler.
scheduler.Schedule(evtMgr, methodCode, genTime, cpfi.Priority, math.MaxFloat64, cpfi, msg, execID, endPtID, processPcktExit)
}
// processPcktExit executes when the associated message did not get served immediately on being scheduled,
// but now has finished.
func processPcktExit(evtMgr *evtm.EventManager, context any, data any) any {
cpfi := context.(*CmpPtnFuncInst)
task := data.(*mrnes.Task)
ppc := cpfi.Cfg.(*processPcktCfg)
pps := cpfi.State.(*processPcktState)
// get transformed message type as function of inbound message type
outMsgType := ppc.Msg2Msg[pps.msgTypeIn]
msg := advanceMsg(cpfi, task.Msg.(*CmpPtnMsg), outMsgType)
// schedule the exitFunc handler
evtMgr.Schedule(cpfi, msg, ExitFunc, vrtime.SecondsToTime(0.0))
return nil
}
//-------- methods and state for function class start
var srtVar *StartCfg = ClassCreateStartCfg()
var startLoaded bool = RegisterFuncClass(srtVar)
type startState struct {
pcktLen int
msgLen int
msgType string
startTime float64
calls int
}
type StartCfg struct {
PcktLen string `yaml:"pcktlen" json:"pcktlen"`
MsgLen string `yaml:"msglen" json:"msglen"`
MsgType string `yaml:"msgtype" json:"msgtype"`
Data string `yaml:"data" json:"data"`
StartTime string `yaml:"starttime" json:"starttime"`
Msg2MC map[string]string `yaml:"msg2mc" json:"msg2mc"`
Trace string `yaml:"trace" json:"trace"`
}
func ClassCreateStartCfg() *StartCfg {
srt := new(StartCfg)
srt.Msg2MC = make(map[string]string)
srt.Trace = "0"
return srt
}
func createStartState(scfg *StartCfg) *startState {
srt := new(startState)
srt.msgLen, _ = strconv.Atoi(scfg.MsgLen)
srt.pcktLen, _ = strconv.Atoi(scfg.PcktLen)
srt.msgType = scfg.MsgType
srt.startTime, _ = strconv.ParseFloat(scfg.StartTime, 64)
return srt
}
func (srt *StartCfg) FuncClassName() string {
return "start"
}
func (srt *StartCfg) CreateCfg(cfgStr string) any {
useYAML := cfgStr[0] != '{'
srtVarAny, err := srt.Deserialize(cfgStr, useYAML)
if err != nil {
panic(fmt.Errorf("start.InitCfg sees deserialization error"))
}
return srtVarAny
}
func (srt *StartCfg) InitCfg(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, cfgStr string, useYAML bool) {
srtVarAny := srt.CreateCfg(cfgStr)
srtv := srtVarAny.(*StartCfg)
cpfi.Cfg = srtv
copyDict(cpfi.Msg2MC, srtv.Msg2MC)
cpfi.State = createStartState(srtv)
cpfi.Trace = (srtv.Trace != "0")
}
func (srt *StartCfg) ValidateCfg(cpfi *CmpPtnFuncInst) error {
return nil
}
// Serialize transforms the start into string form for
// inclusion through a file
func (srt *StartCfg) Serialize(useYAML bool) (string, error) {
var bytes []byte
var merr error
if useYAML {
bytes, merr = yaml.Marshal(*srt)
} else {
bytes, merr = json.Marshal(*srt)
}
if merr != nil {
return "", merr
}
return string(bytes[:]), nil
}
func (srt *StartCfg) CfgStr() string {
rtn, err := srt.Serialize(true)
if err != nil {
panic(fmt.Errorf("start cfg serialization error"))
}
return rtn
}
// Deserialize recovers a serialized representation of a start structure
func (srt *StartCfg) Deserialize(fss string, useYAML bool) (any, error) {
// turn the string into a slice of bytes
var err error
fsb := []byte(fss)
example := StartCfg{Trace: "0"}
// Select whether we read in json or yaml
if useYAML {
err = yaml.Unmarshal(fsb, &example)
} else {
err = json.Unmarshal(fsb, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// start an execution thread, main thing here is creating the initial
// message and giving it an execID
func startEnter(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, methodCode string, msg *CmpPtnMsg) {
srts := cpfi.State.(*startState)
srts.calls += 1
cpm := new(CmpPtnMsg)
cpm.PcktLen = srts.pcktLen
cpm.MsgLen = srts.msgLen
cpm.MsgType = srts.msgType
numExecThreads += 1
cpm.ExecID = numExecThreads
endptName := cpfi.Host
endpt := mrnes.EndptDevByName[endptName]
AddCPTrace(traceMgr, evtMgr.CurrentTime(), cpm.ExecID, endpt.DevID(), "enter", cpm)
srtTime := srts.startTime
// out edge destination a function of the message type
cpm = advanceMsg(cpfi, cpm, srts.msgType)
cpfi.AddResponse(cpm.ExecID, []*CmpPtnMsg{cpm})
evtMgr.Schedule(cpfi, cpm, ExitFunc, vrtime.SecondsToTime(srtTime))
}
//-------- methods and state for function class start
var fnshVar *FinishCfg = ClassCreateFinishCfg()
var finishLoaded bool = RegisterFuncClass(fnshVar)
type finishState struct {
calls int
}
type FinishCfg struct {
Msg2MC map[string]string `yaml:"msg2mc" json:"msg2mc"`
Trace string `yaml:"trace" json:"trace"`
}
func ClassCreateFinishCfg() *FinishCfg {
fnsh := new(FinishCfg)
fnsh.Trace = "0"
fnsh.Msg2MC = make(map[string]string)
return fnsh
}
func createFinishState(fcfg *FinishCfg) *finishState {
fnsh := new(finishState)
return fnsh
}
func (fnsh *FinishCfg) FuncClassName() string {
return "finish"
}
func (fnsh *FinishCfg) CreateCfg(cfgStr string) any {
useYAML := cfgStr[0] != '{'
fnshVarAny, err := fnsh.Deserialize(cfgStr, useYAML)
if err != nil {
panic(fmt.Errorf("finish.InitCfg sees deserialization error"))
}
return fnshVarAny
}
func (fnsh *FinishCfg) InitCfg(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, cfgStr string, useYAML bool) {
fnshVarAny := fnsh.CreateCfg(cfgStr)
fnshv := fnshVarAny.(*FinishCfg)
cpfi.Cfg = fnshv
copyDict(cpfi.Msg2MC, fnshv.Msg2MC)
cpfi.State = createFinishState(fnshv)
cpfi.Trace = (fnshv.Trace != "0")
}
func (fnsh *FinishCfg) ValidateCfg(cpfi *CmpPtnFuncInst) error {
return nil
}
// Serialize transforms the finish into string form for
// inclusion through a file
func (fnsh *FinishCfg) Serialize(useYAML bool) (string, error) {
var bytes []byte
var merr error
if useYAML {
bytes, merr = yaml.Marshal(*fnsh)
} else {
bytes, merr = json.Marshal(*fnsh)
}
if merr != nil {
return "", merr
}
return string(bytes[:]), nil
}
func (fnsh *FinishCfg) CfgStr() string {
rtn, err := fnsh.Serialize(true)
if err != nil {
panic(fmt.Errorf("finish cfg serialization error"))
}
return rtn
}
// Deserialize recovers a serialized representation of a finish structure
func (fnsh *FinishCfg) Deserialize(fss string, useYAML bool) (any, error) {
// turn the string into a slice of bytes
var err error
fsb := []byte(fss)
example := FinishCfg{Trace: "0"}
// Select whether we read in json or yaml
if useYAML {
err = yaml.Unmarshal(fsb, &example)
} else {
err = json.Unmarshal(fsb, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// finishEnter drops information in the logs, and by not scheduling anything lets the execution thread finish
func finishEnter(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, methodCode string, msg *CmpPtnMsg) {
fns := cpfi.State.(*finishState)
fns.calls += 1
endptName := cpfi.Host
endpt := mrnes.EndptDevByName[endptName]
AddCPTrace(traceMgr, evtMgr.CurrentTime(), msg.ExecID, endpt.DevID(), "exit", msg)
}
// -------- methods and state for function class srvRsp
// srvRsp receives a request to perform some service, from
// a different comp pattern and label. Those are found in the message's
// PrevCPID, prevLabel, and the requesting message type is
// in prevMsgType. The srvRsp function delays based on the (configured) service operation
// and then responds back to the requesting source.
var srvRspVar *srvRspCfg = ClassCreateSrvRspCfg()
var srvRspLoaded bool = RegisterFuncClass(srvRspVar)
type srvRspState struct {
calls int
}
type srvRspCfg struct {
// chosen op, for timing
TimingCode map[string]string `yaml:"timingcode" json:"timingcode"`
Msg2MC map[string]string `yaml:"msg2mc" json:"msg2mc"`
DirectPrefix []string `yaml:"directprefix" json:"directprefix"`
Trace string `yaml:"trace" json:"trace"`
}
func ClassCreateSrvRspCfg() *srvRspCfg {
srvRsp := new(srvRspCfg)
srvRsp.TimingCode = make(map[string]string)
srvRsp.DirectPrefix = make([]string, 0)
srvRsp.Msg2MC = make(map[string]string)
srvRsp.Trace = "0"
return srvRsp
}
func createSrvRspState(srvRsp *srvRspCfg) *srvRspState {
srvRsps := new(srvRspState)
return srvRsps
}
func (srvRsp *srvRspCfg) FuncClassName() string {
return "srvRsp"
}
func (srvRsp *srvRspCfg) CreateCfg(cfgStr string) any {
useYAML := (cfgStr[0] != '{')
srvRspVarAny, err := srvRsp.Deserialize(cfgStr, useYAML)
if err != nil {
panic(fmt.Errorf("srvRsp.InitCfg sees deserialization error"))
}
return srvRspVarAny
}
func (srvRsp *srvRspCfg) Populate(tc map[string]string, trace bool) {
for key, value := range tc {
srvRsp.TimingCode[key] = value
}
}
func (srvRsp *srvRspCfg) InitCfg(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, cfgStr string, useYAML bool) {
srvRspVarAny := srvRsp.CreateCfg(cfgStr)
srvRspv := srvRspVarAny.(*srvRspCfg)
cpfi.Cfg = srvRspv
copyDict(cpfi.Msg2MC, srvRspv.Msg2MC)
cpfi.IsService = true
cpfi.State = createSrvRspState(srvRspv)
// the srventication response function is a service, meaning
// that its selection doesn't require identification of the source CP
cpfi.Trace = (srvRspv.Trace != "0")
}
func (srvRsp *srvRspCfg) ValidateCfg(cpfi *CmpPtnFuncInst) error {
return nil
}
// Serialize transforms the srvRsp into string form for
// inclusion through a file
func (srvRsp *srvRspCfg) Serialize(useYAML bool) (string, error) {
var bytes []byte
var merr error
if useYAML {
bytes, merr = yaml.Marshal(*srvRsp)
} else {
bytes, merr = json.Marshal(*srvRsp)
}
if merr != nil {
return "", merr
}
return string(bytes[:]), nil
}
func (srvRsp *srvRspCfg) CfgStr() string {
rtn, err := srvRsp.Serialize(true)
if err != nil {
panic(fmt.Errorf("service response cfg serialization error"))
}
return rtn
}
// Deserialize recovers a serialized representation of a srvRsp structure
func (srvRsp *srvRspCfg) Deserialize(fss string, useYAML bool) (any, error) {
// turn the string into a slice of bytes
var err error
fsb := []byte(fss)
example := srvRspCfg{Trace: "0"}
// Select whether we read in json or yaml
if useYAML {
err = yaml.Unmarshal(fsb, &example)
} else {
err = json.Unmarshal(fsb, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// srvRspEnter flags
func srvRspEnter(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, methodCode string, msg *CmpPtnMsg) {
arpc := cpfi.Cfg.(*srvRspCfg)
arps := cpfi.State.(*srvRspState)
arps.calls += 1
endptName := cpfi.Host
endpt := mrnes.EndptDevByName[endptName]
AddCPTrace(traceMgr, evtMgr.CurrentTime(), msg.ExecID, endpt.DevID(), "enter", msg)
// look up response delay. If message type prefix matches the direct prefix list
// use it directly
pieces := strings.Split(msg.MsgType, "-")
var tcCode string
if len(pieces) > 1 {
found := false
opPrefix := pieces[0]
for _, prefix := range arpc.DirectPrefix {
if opPrefix == prefix {
found = true
break
}
}
if !found {
panic(fmt.Errorf("expected server to have prefix %s for msg type %s", pieces[0], msg.MsgType))
}
tcCode = msg.MsgType
} else {
tcCode = arpc.TimingCode[msg.MsgType]
}
genTime := HostFuncExecTime(cpfi, tcCode, msg)
msg.CPID = msg.RtnCPID
msg.Label = msg.RtnLabel
msg.MsgType = msg.RtnMsgType
// put where ExitFunc will find it
cpfi.AddResponse(msg.ExecID, []*CmpPtnMsg{msg})
// schedule ExitFunc to happen after the genTime delay
evtMgr.Schedule(cpfi, msg, ExitFunc, vrtime.SecondsToTime(genTime))
}
// -------- methods and state for function class srvReq
var srvReqVar *srvReqCfg = ClassCreateSrvReqCfg()
var srvReqLoaded bool = RegisterFuncClass(srvReqVar)
type srvReqState struct {
rspEdgeIdx int // index of edge pointing to service response function
msgTypeIn string // type of message on receipt
calls int
}
type srvReqCfg struct {
// map the service request to the message type on departure
Bypass string `yaml:"bypass" json:"bypass"`
SrvCP string `yaml:"srvcp" json:"srvcp"`
SrvOp string `yaml:"srvop" json:"srvop"`
RspOp string `yaml:"rspop" json:"rspop"`
SrvLabel string `yaml:"srvlabel" json:"srvlabel"`
Msg2MC map[string]string `yaml:"msg2mc" json:"msg2mc"`
Msg2Msg map[string]string `yaml:"op2msg" json:"op2msg"`
Trace string `yaml:"trace" json:"trace"`
}
func ClassCreateSrvReqCfg() *srvReqCfg {
srvReq := new(srvReqCfg)
srvReq.Msg2MC = make(map[string]string)
srvReq.Msg2Msg = make(map[string]string)
return srvReq
}
func createSrvReqState(srvReq *srvReqCfg) *srvReqState {
srqs := new(srvReqState)
srqs.rspEdgeIdx = -1 // flag for replacement on first encounter
return srqs
}
func (srvReq *srvReqCfg) FuncClassName() string {
return "srvReq"
}
func (srvReq *srvReqCfg) CreateCfg(cfgStr string) any {
useYAML := (cfgStr[0] != '{')
srvReqVarAny, err := srvReq.Deserialize(cfgStr, useYAML)
if err != nil {
panic(fmt.Errorf("srvReq.InitCfg sees deserialization error"))
}
return srvReqVarAny
}
func (srvReq *srvReqCfg) Populate(bypass bool, trace bool) {
if trace {
srvReq.Trace = "1"
} else {
srvReq.Trace = "0"
}
}
func (srvReq *srvReqCfg) InitCfg(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, cfgStr string, useYAML bool) {
srvReqVarAny := srvReq.CreateCfg(cfgStr)
srvReqv := srvReqVarAny.(*srvReqCfg)
cpfi.Cfg = srvReqv
copyDict(cpfi.Msg2MC, srvReq.Msg2MC)
cpfi.State = createSrvReqState(srvReqv)
cpfi.Trace = (srvReqv.Trace != "0")
}
func (srvReq *srvReqCfg) ValidateCfg(cpfi *CmpPtnFuncInst) error {
return nil
}
// Serialize transforms the srvReq into string form for
// inclusion through a file
func (srvReq *srvReqCfg) Serialize(useYAML bool) (string, error) {
var bytes []byte
var merr error
if useYAML {
bytes, merr = yaml.Marshal(*srvReq)
} else {
bytes, merr = json.Marshal(*srvReq)
}
if merr != nil {
return "", merr
}
return string(bytes[:]), nil
}
func (srvReq *srvReqCfg) CfgStr() string {
rtn, err := srvReq.Serialize(true)
if err != nil {
panic(fmt.Errorf("service request cfg serialization error"))
}
return rtn
}
// Deserialize recovers a serialized representation of a srvReq structure
func (srvReq *srvReqCfg) Deserialize(fss string, useYAML bool) (any, error) {
// turn the string into a slice of bytes
var err error
fsb := []byte(fss)
example := srvReqCfg{Trace: "0"}
// Select whether we read in json or yaml
if useYAML {
err = yaml.Unmarshal(fsb, &example)
} else {
err = json.Unmarshal(fsb, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// srvReqEnter flags
func srvReqEnter(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, methodCode string, msg *CmpPtnMsg) {
srqs := cpfi.State.(*srvReqState)
srqc := cpfi.Cfg.(*srvReqCfg)
srqs.calls += 1
if srqc.Bypass != "0" {
// the outbound message type is the same as the inbound.
// Find the outbound edge that matches
outMsg := advanceMsg(cpfi, msg, msg.MsgType)
// put msg where ExitFunc will find it
cpfi.AddResponse(msg.ExecID, []*CmpPtnMsg{outMsg})
// schedule ExitFunc to happen after the genTime delay
evtMgr.Schedule(cpfi, msg, ExitFunc, vrtime.SecondsToTime(0.0))
return
}
// remember incoming message type
srqs.msgTypeIn = msg.MsgType
var found bool
if srqs.rspEdgeIdx == -1 {
// find edge whose target func is from the "srvRsp" class
for idx, edge := range cpfi.OutEdges {
cp := CmpPtnInstByID[edge.CPID]
for _, funcInst := range cp.Funcs {
if funcInst.Class == "srvRsp" {
srqs.rspEdgeIdx = idx
found = true
break
}
}
if found {
break
}
}
}
if found {
var eidx = srqs.rspEdgeIdx
edge := cpfi.OutEdges[eidx]
msg.CPID = edge.CPID
msg.Label = edge.FuncLabel
} else {
var srvCPID int
var srvLabel string
// the server CP needs to be given
if len(srqc.SrvCP) > 0 {
// srqc.SrvCP points to the CP and srvc.SrvOp gives the index in the Services table
srvCPID = CmpPtnInstByName[srqc.SrvCP].ID
} else {
// for the use case of the first function on a processor asking for an authentication
// service from the CmpPtn that called it
srvCPID = CmpPtnInstByID[msg.PrevCPID].ID
}
// the server label is either given, or is looked up from the server CP services table
if len(srqc.SrvLabel) > 0 {
srvLabel = srqc.SrvLabel
} else {
// use the service op prefix as a code to look in the services table
pieces := strings.Split(srqc.SrvOp, "-")
srvOp := pieces[0]
srvDesc := CmpPtnInstByID[srvCPID].Services[srvOp]
if len(srvDesc.CP) > 0 {
srvCPID = CmpPtnInstByName[srvDesc.CP].ID
}
srvLabel = srvDesc.Label
}
msg.CPID = srvCPID
msg.Label = srvLabel
msg.MsgType = srqc.SrvOp
msg.RtnCPID = cpfi.CPID
msg.RtnLabel = cpfi.Label
msg.RtnMsgType = "return-" + msg.MsgType
// set up the return msgType to point to the return function
cpfi.Msg2MC[msg.RtnMsgType] = "return"
}
// put msg where ExitFunc will find it
cpfi.AddResponse(msg.ExecID, []*CmpPtnMsg{msg})
endptName := cpfi.Host
endpt := mrnes.EndptDevByName[endptName]
AddCPTrace(traceMgr, evtMgr.CurrentTime(), msg.ExecID, endpt.DevID(), "enter", msg)
// schedule ExitFunc to happen after the genTime delay
evtMgr.Schedule(cpfi, msg, ExitFunc, vrtime.SecondsToTime(0.0))
}
func srvReqRtn(evtMgr *evtm.EventManager, cpfi *CmpPtnFuncInst, methodCode string, msg *CmpPtnMsg) {
srqs := cpfi.State.(*srvReqState)
srqc := cpfi.Cfg.(*srvReqCfg)