mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-13 07:04:44 +08:00
Compare commits
10 Commits
v2.1.10
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9705cad1aa | ||
|
|
eaf20c4e3a | ||
|
|
027e83b706 | ||
|
|
f9be55e98d | ||
|
|
d7c4cfb1ef | ||
|
|
4cb6882a1a | ||
|
|
b78d9721f2 | ||
|
|
f8953d1764 | ||
|
|
fac7a323e1 | ||
|
|
1995d91cfc |
@@ -3,13 +3,14 @@ package cluster
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/rpc"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var configDir = "./config/"
|
||||
|
||||
@@ -10,7 +10,7 @@ const (
|
||||
Sys_Event_Tcp EventType = -3
|
||||
Sys_Event_Http_Event EventType = -4
|
||||
Sys_Event_WebSocket EventType = -5
|
||||
Sys_Event_Kcp EventType = -6
|
||||
Sys_Event_Kcp EventType = -6
|
||||
Sys_Event_Node_Conn_Event EventType = -7
|
||||
Sys_Event_Nats_Conn_Event EventType = -8
|
||||
Sys_Event_DiscoverService EventType = -9
|
||||
@@ -18,6 +18,6 @@ const (
|
||||
Sys_Event_EtcdDiscovery EventType = -11
|
||||
Sys_Event_Gin_Event EventType = -12
|
||||
Sys_Event_FrameTick EventType = -13
|
||||
|
||||
Sys_Event_User_Define EventType = 1
|
||||
Sys_Event_ReloadBlueprint EventType = -14
|
||||
Sys_Event_User_Define EventType = 1
|
||||
)
|
||||
|
||||
14
go.mod
14
go.mod
@@ -1,8 +1,6 @@
|
||||
module github.com/duanhf2012/origin/v2
|
||||
|
||||
go 1.22
|
||||
|
||||
toolchain go1.22.7
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/IBM/sarama v1.43.3
|
||||
@@ -84,11 +82,11 @@ require (
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.13 // indirect
|
||||
go.uber.org/multierr v1.10.0 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/crypto v0.26.0 // indirect
|
||||
golang.org/x/net v0.28.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.23.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
golang.org/x/crypto v0.45.0 // indirect
|
||||
golang.org/x/net v0.47.0 // indirect
|
||||
golang.org/x/sync v0.18.0 // indirect
|
||||
golang.org/x/sys v0.38.0 // indirect
|
||||
golang.org/x/text v0.31.0 // indirect
|
||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||
|
||||
20
go.sum
20
go.sum
@@ -231,8 +231,8 @@ golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPh
|
||||
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
|
||||
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
@@ -254,8 +254,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
|
||||
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
|
||||
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
|
||||
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -263,8 +263,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
|
||||
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -277,8 +277,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
|
||||
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
@@ -288,8 +288,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
|
||||
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
|
||||
@@ -3,12 +3,13 @@ package network
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/gorilla/websocket"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type WSServer struct {
|
||||
@@ -16,13 +17,16 @@ type WSServer struct {
|
||||
MaxConnNum int
|
||||
PendingWriteNum int
|
||||
MaxMsgLen uint32
|
||||
HTTPTimeout time.Duration
|
||||
CertFile string
|
||||
KeyFile string
|
||||
NewAgent func(*WSConn) Agent
|
||||
ln net.Listener
|
||||
handler *WSHandler
|
||||
messageType int
|
||||
|
||||
HandshakeTimeout time.Duration
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
}
|
||||
|
||||
type WSHandler struct {
|
||||
@@ -73,14 +77,14 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
handler.conns[conn] = struct{}{}
|
||||
handler.mutexConns.Unlock()
|
||||
c,ok:=conn.NetConn().(*net.TCPConn)
|
||||
c, ok := conn.NetConn().(*net.TCPConn)
|
||||
if !ok {
|
||||
tlsConn,ok := conn.NetConn().(*tls.Conn)
|
||||
tlsConn, ok := conn.NetConn().(*tls.Conn)
|
||||
if !ok {
|
||||
log.Error("conn error")
|
||||
return
|
||||
}
|
||||
c,ok = tlsConn.NetConn().(*net.TCPConn)
|
||||
c, ok = tlsConn.NetConn().(*net.TCPConn)
|
||||
if !ok {
|
||||
log.Error("conn error")
|
||||
return
|
||||
@@ -127,10 +131,19 @@ func (server *WSServer) Start() error {
|
||||
server.MaxMsgLen = 4096
|
||||
log.Info("invalid MaxMsgLen", log.Uint32("reset", server.MaxMsgLen))
|
||||
}
|
||||
if server.HTTPTimeout <= 0 {
|
||||
server.HTTPTimeout = 10 * time.Second
|
||||
log.Info("invalid HTTPTimeout", log.Duration("reset", server.HTTPTimeout))
|
||||
if server.HandshakeTimeout <= 0 {
|
||||
server.HandshakeTimeout = 15 * time.Second
|
||||
log.Info("invalid HandshakeTimeout", log.Duration("reset", server.HandshakeTimeout))
|
||||
}
|
||||
if server.ReadTimeout <= 0 {
|
||||
server.ReadTimeout = 15 * time.Second
|
||||
log.Info("invalid ReadTimeout", log.Duration("reset", server.ReadTimeout))
|
||||
}
|
||||
if server.WriteTimeout <= 0 {
|
||||
server.WriteTimeout = 15 * time.Second
|
||||
log.Info("invalid WriteTimeout", log.Duration("reset", server.WriteTimeout))
|
||||
}
|
||||
|
||||
if server.NewAgent == nil {
|
||||
log.Error("NewAgent must not be nil")
|
||||
return errors.New("NewAgent must not be nil")
|
||||
@@ -159,7 +172,7 @@ func (server *WSServer) Start() error {
|
||||
conns: make(WebsocketConnSet),
|
||||
messageType: server.messageType,
|
||||
upgrader: websocket.Upgrader{
|
||||
HandshakeTimeout: server.HTTPTimeout,
|
||||
HandshakeTimeout: server.HandshakeTimeout,
|
||||
CheckOrigin: func(_ *http.Request) bool { return true },
|
||||
},
|
||||
}
|
||||
@@ -167,8 +180,8 @@ func (server *WSServer) Start() error {
|
||||
httpServer := &http.Server{
|
||||
Addr: server.Addr,
|
||||
Handler: server.handler,
|
||||
ReadTimeout: server.HTTPTimeout,
|
||||
WriteTimeout: server.HTTPTimeout,
|
||||
ReadTimeout: server.ReadTimeout,
|
||||
WriteTimeout: server.WriteTimeout,
|
||||
MaxHeaderBytes: 1024,
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,15 @@ package wsmodule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type WSModule struct {
|
||||
@@ -36,6 +38,10 @@ type WSCfg struct {
|
||||
LittleEndian bool //是否小端序
|
||||
KeyFile string
|
||||
CertFile string
|
||||
|
||||
HandshakeTimeoutSecond time.Duration
|
||||
ReadTimeoutSecond time.Duration
|
||||
WriteTimeoutSecond time.Duration
|
||||
}
|
||||
|
||||
type WSPackType int8
|
||||
@@ -63,6 +69,9 @@ func (ws *WSModule) OnInit() error {
|
||||
ws.WSServer.PendingWriteNum = ws.wsCfg.PendingWriteNum
|
||||
ws.WSServer.MaxMsgLen = ws.wsCfg.MaxMsgLen
|
||||
ws.WSServer.Addr = ws.wsCfg.ListenAddr
|
||||
ws.WSServer.HandshakeTimeout = ws.wsCfg.HandshakeTimeoutSecond*time.Second
|
||||
ws.WSServer.ReadTimeout = ws.wsCfg.ReadTimeoutSecond*time.Second
|
||||
ws.WSServer.WriteTimeout = ws.wsCfg.WriteTimeoutSecond*time.Second
|
||||
|
||||
if ws.wsCfg.KeyFile != "" && ws.wsCfg.CertFile != "" {
|
||||
ws.WSServer.KeyFile = ws.wsCfg.KeyFile
|
||||
|
||||
@@ -3,78 +3,154 @@ package blueprint
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
type Blueprint struct {
|
||||
execNodes []IExecNode // 注册的定义执行结点
|
||||
|
||||
execPool ExecPool
|
||||
graphPool GraphPool
|
||||
execNodeList []func() IExecNode
|
||||
execPool *ExecPool
|
||||
graphPool *GraphPool
|
||||
|
||||
blueprintModule IBlueprintModule
|
||||
mapGraph map[int64]IGraph
|
||||
seedID int64
|
||||
cancelTimer func(*uint64)bool
|
||||
mapGraph map[int64]IGraph
|
||||
seedID int64
|
||||
cancelTimer func(*uint64) bool
|
||||
|
||||
execDefFilePath string // 执行结点定义文件路径
|
||||
graphFilePath string // 蓝图文件路径
|
||||
}
|
||||
|
||||
func (bm *Blueprint) RegExecNode(execNode IExecNode) {
|
||||
bm.execNodes = append(bm.execNodes, execNode)
|
||||
func (bm *Blueprint) RegisterExecNode(execNodeFunc func() IExecNode) {
|
||||
bm.execNodeList = append(bm.execNodeList, execNodeFunc)
|
||||
}
|
||||
|
||||
func (bm *Blueprint) regSysNode(){
|
||||
bm.RegExecNode(&AddInt{})
|
||||
bm.RegExecNode(&SubInt{})
|
||||
bm.RegExecNode(&MulInt{})
|
||||
bm.RegExecNode(&DivInt{})
|
||||
bm.RegExecNode(&ModInt{})
|
||||
bm.RegExecNode(&RandNumber{})
|
||||
|
||||
bm.RegExecNode(&Entrance_ArrayParam{})
|
||||
bm.RegExecNode(&Entrance_IntParam{})
|
||||
bm.RegExecNode(&Entrance_Timer{})
|
||||
bm.RegExecNode(&Output{})
|
||||
bm.RegExecNode(&Sequence{})
|
||||
bm.RegExecNode(&Foreach{})
|
||||
bm.RegExecNode(&ForeachIntArray{})
|
||||
|
||||
bm.RegExecNode(&GetArrayInt{})
|
||||
bm.RegExecNode(&GetArrayString{})
|
||||
bm.RegExecNode(&GetArrayLen{})
|
||||
bm.RegExecNode(&CreateIntArray{})
|
||||
bm.RegExecNode(&CreateStringArray{})
|
||||
bm.RegExecNode(&AppendIntegerToArray{})
|
||||
bm.RegExecNode(&AppendStringToArray{})
|
||||
|
||||
bm.RegExecNode(&BoolIf{})
|
||||
bm.RegExecNode(&GreaterThanInteger{})
|
||||
bm.RegExecNode(&LessThanInteger{})
|
||||
bm.RegExecNode(&EqualInteger{})
|
||||
bm.RegExecNode(&RangeCompare{})
|
||||
bm.RegExecNode(&Probability{})
|
||||
bm.RegExecNode(&CreateTimer{})
|
||||
type IExecNodeType[T any] interface {
|
||||
*T
|
||||
IExecNode
|
||||
}
|
||||
|
||||
func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule,cancelTimer func(*uint64)bool) error {
|
||||
bm.regSysNode()
|
||||
err := bm.execPool.Load(execDefFilePath)
|
||||
// 生成一个泛型函数,返回func() IExecNode类型
|
||||
func NewExecNode[T any, P IExecNodeType[T]]() func() IExecNode {
|
||||
return func() IExecNode {
|
||||
var t T
|
||||
return P(&t)
|
||||
}
|
||||
}
|
||||
|
||||
func (bm *Blueprint) regSysNodes() {
|
||||
bm.RegisterExecNode(NewExecNode[AddInt]())
|
||||
bm.RegisterExecNode(NewExecNode[SubInt]())
|
||||
bm.RegisterExecNode(NewExecNode[MulInt]())
|
||||
bm.RegisterExecNode(NewExecNode[DivInt]())
|
||||
bm.RegisterExecNode(NewExecNode[ModInt]())
|
||||
bm.RegisterExecNode(NewExecNode[RandNumber]())
|
||||
|
||||
bm.RegisterExecNode(NewExecNode[Entrance_ArrayParam]())
|
||||
bm.RegisterExecNode(NewExecNode[Entrance_IntParam]())
|
||||
bm.RegisterExecNode(NewExecNode[Entrance_Timer]())
|
||||
bm.RegisterExecNode(NewExecNode[DebugOutput]())
|
||||
bm.RegisterExecNode(NewExecNode[Sequence]())
|
||||
bm.RegisterExecNode(NewExecNode[Foreach]())
|
||||
bm.RegisterExecNode(NewExecNode[ForeachIntArray]())
|
||||
|
||||
bm.RegisterExecNode(NewExecNode[GetArrayInt]())
|
||||
bm.RegisterExecNode(NewExecNode[GetArrayString]())
|
||||
bm.RegisterExecNode(NewExecNode[GetArrayLen]())
|
||||
bm.RegisterExecNode(NewExecNode[CreateIntArray]())
|
||||
bm.RegisterExecNode(NewExecNode[CreateStringArray]())
|
||||
bm.RegisterExecNode(NewExecNode[AppendIntegerToArray]())
|
||||
bm.RegisterExecNode(NewExecNode[AppendStringToArray]())
|
||||
bm.RegisterExecNode(NewExecNode[BoolIf]())
|
||||
bm.RegisterExecNode(NewExecNode[GreaterThanInteger]())
|
||||
bm.RegisterExecNode(NewExecNode[LessThanInteger]())
|
||||
bm.RegisterExecNode(NewExecNode[EqualInteger]())
|
||||
bm.RegisterExecNode(NewExecNode[RangeCompare]())
|
||||
bm.RegisterExecNode(NewExecNode[EqualSwitch]())
|
||||
bm.RegisterExecNode(NewExecNode[Probability]())
|
||||
bm.RegisterExecNode(NewExecNode[CreateTimer]())
|
||||
}
|
||||
|
||||
|
||||
func (bm *Blueprint) StartHotReload() (func(),error) {
|
||||
var execPool ExecPool
|
||||
var graphPool GraphPool
|
||||
|
||||
// 加载配置结点生成名字对应的innerExecNode
|
||||
err := execPool.Load(bm.execDefFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 将注册的实际执行结点与innerExecNode进行关联
|
||||
for _, newExec := range bm.execNodeList {
|
||||
e := newExec()
|
||||
if !execPool.Register(e) {
|
||||
return nil,fmt.Errorf("register exec failed,exec:%s", e.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
// 加载所有的vgf蓝图文件
|
||||
err = graphPool.Load(&execPool, bm.graphFilePath, bm.blueprintModule)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回配置加载后的刷新内存处理
|
||||
return func() {
|
||||
// 替换旧的执行池和图池
|
||||
bm.execPool = &execPool
|
||||
bm.graphPool = &graphPool
|
||||
|
||||
for _, gh := range bm.mapGraph {
|
||||
gFileName := gh.GetGraphFileName()
|
||||
bGraph := bm.graphPool.GetBaseGraph(gFileName)
|
||||
if bGraph == nil {
|
||||
log.Warn("GetBaseGraph fail", log.String("graph file name", gFileName))
|
||||
bGraph = &baseGraph{entrance: map[int64]*execNode{}}
|
||||
}
|
||||
|
||||
gh.HotReload(bGraph)
|
||||
}
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule, cancelTimer func(*uint64) bool) error {
|
||||
var execPool ExecPool
|
||||
var graphPool GraphPool
|
||||
|
||||
// 加载配置结点生成名字对应的innerExecNode
|
||||
err := execPool.Load(execDefFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, e := range bm.execNodes {
|
||||
if !bm.execPool.Register(e) {
|
||||
// 注册系统执行结点
|
||||
bm.regSysNodes()
|
||||
|
||||
// 将注册的实际执行结点与innerExecNode进行关联
|
||||
for _, newExec := range bm.execNodeList {
|
||||
e := newExec()
|
||||
if !execPool.Register(e) {
|
||||
return fmt.Errorf("register exec failed,exec:%s", e.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
err = bm.graphPool.Load(&bm.execPool, graphFilePath, blueprintModule)
|
||||
// 加载所有的vgf蓝图文件
|
||||
err = graphPool.Load(&execPool, graphFilePath, blueprintModule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bm.execPool = &execPool
|
||||
bm.graphPool = &graphPool
|
||||
bm.cancelTimer = cancelTimer
|
||||
bm.blueprintModule = blueprintModule
|
||||
bm.mapGraph = make(map[int64]IGraph,128)
|
||||
bm.mapGraph = make(map[int64]IGraph, 128)
|
||||
bm.execDefFilePath = execDefFilePath
|
||||
bm.graphFilePath = graphFilePath
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -82,32 +158,41 @@ func (bm *Blueprint) Create(graphName string) int64 {
|
||||
if graphName == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
graphID := atomic.AddInt64(&bm.seedID, 1)
|
||||
bm.mapGraph[graphID] = bm.graphPool.Create(graphName, graphID)
|
||||
gr := bm.graphPool.Create(graphName, graphID)
|
||||
if gr == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
bm.mapGraph[graphID] = gr
|
||||
return graphID
|
||||
}
|
||||
|
||||
func (bm *Blueprint) TriggerEvent(graphID int64, eventID int64, args ...any) error{
|
||||
func (bm *Blueprint) TriggerEvent(graphID int64, eventID int64, args ...any) error {
|
||||
graph := bm.mapGraph[graphID]
|
||||
if graph == nil {
|
||||
return fmt.Errorf("can not find graph:%d", graphID)
|
||||
}
|
||||
|
||||
_,err:= graph.Do(eventID, args...)
|
||||
_, err := graph.Do(eventID, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (bm *Blueprint) Do(graphID int64, entranceID int64, args ...any) (Port_Array,error){
|
||||
func (bm *Blueprint) Do(graphID int64, entranceID int64, args ...any) (Port_Array, error) {
|
||||
graph := bm.mapGraph[graphID]
|
||||
if graph == nil {
|
||||
return nil,fmt.Errorf("can not find graph:%d", graphID)
|
||||
return nil, fmt.Errorf("can not find graph:%d", graphID)
|
||||
}
|
||||
|
||||
return graph.Do(entranceID, args...)
|
||||
}
|
||||
|
||||
func (bm *Blueprint) ReleaseGraph(graphID int64) {
|
||||
if graphID == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
defer delete(bm.mapGraph, graphID)
|
||||
graph := bm.mapGraph[graphID]
|
||||
if graph == nil {
|
||||
@@ -117,7 +202,7 @@ func (bm *Blueprint) ReleaseGraph(graphID int64) {
|
||||
graph.Release()
|
||||
}
|
||||
|
||||
func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool{
|
||||
func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool {
|
||||
tId := *timerId
|
||||
bm.cancelTimer(timerId)
|
||||
|
||||
@@ -126,11 +211,11 @@ func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool{
|
||||
return false
|
||||
}
|
||||
|
||||
gr,ok := graph.(*Graph)
|
||||
gr, ok := graph.(*Graph)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
delete(gr.mapTimerID, tId)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@ import (
|
||||
)
|
||||
|
||||
func TestExecMgr(t *testing.T) {
|
||||
var bp Blueprint
|
||||
err := bp.Init("E:\\WorkSpace\\c4\\OriginNodeEditor\\json", "E:\\WorkSpace\\c4\\OriginNodeEditor\\vgf", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Init failed,err:%v", err)
|
||||
}
|
||||
|
||||
//graphTest2 := bp.Create("testForeach")
|
||||
//err = graphTest2.Do(EntranceID_IntParam, 1, 2, 3)
|
||||
|
||||
@@ -2,15 +2,7 @@ package blueprint
|
||||
|
||||
import "fmt"
|
||||
|
||||
type IBaseExecNode interface {
|
||||
initInnerExecNode(innerNode *innerExecNode)
|
||||
initExecNode(gr *Graph, en *execNode) error
|
||||
GetPorts() ([]IPort, []IPort)
|
||||
getExecNodeInfo() (*ExecContext, *execNode)
|
||||
setExecNodeInfo(gr *ExecContext, en *execNode)
|
||||
GetBlueprintModule() IBlueprintModule
|
||||
}
|
||||
|
||||
// IInnerExecNode 配置生成的结点
|
||||
type IInnerExecNode interface {
|
||||
GetName() string
|
||||
SetExec(exec IExecNode)
|
||||
@@ -26,16 +18,27 @@ type IInnerExecNode interface {
|
||||
GetOutPortParamStartIndex() int
|
||||
}
|
||||
|
||||
// IBaseExecNode 实际注册的执行结点的基础结构体
|
||||
type IBaseExecNode interface {
|
||||
initInnerExecNode(innerNode *innerExecNode)
|
||||
initExecNode(gr *Graph, en *execNode) error
|
||||
GetPorts() ([]IPort, []IPort)
|
||||
getExecNodeInfo() (*ExecContext, *execNode)
|
||||
setExecNodeInfo(gr *ExecContext, en *execNode)
|
||||
GetBlueprintModule() IBlueprintModule
|
||||
}
|
||||
|
||||
// IExecNode 实际注册的执行结点
|
||||
type IExecNode interface {
|
||||
IBaseExecNode
|
||||
GetName() string
|
||||
DoNext(index int) error
|
||||
Exec() (int, error) // 返回后续执行的Node的Index
|
||||
GetNextExecLen() int
|
||||
getInnerExecNode() IInnerExecNode
|
||||
|
||||
setVariableName(name string) bool
|
||||
}
|
||||
|
||||
// 配置对应的基础信息+端口数据
|
||||
type innerExecNode struct {
|
||||
Name string
|
||||
Title string
|
||||
@@ -47,11 +50,11 @@ type innerExecNode struct {
|
||||
|
||||
outPortParamStartIndex int // 输出参数的起始索引,用于排除执行出口
|
||||
|
||||
IExecNode
|
||||
IExecNode // 实际注册的执行结点
|
||||
}
|
||||
|
||||
type BaseExecNode struct {
|
||||
*innerExecNode
|
||||
*innerExecNode // 内部注册的执行结点
|
||||
|
||||
// 执行时初始化的数据
|
||||
*ExecContext
|
||||
@@ -108,26 +111,12 @@ func (bc *BaseExecConfig) GetMaxOutPortId() int {
|
||||
return maxPortId
|
||||
}
|
||||
|
||||
//func (em *innerExecNode) AppendInPort(port ...IPort) {
|
||||
// if len(em.inPort) == 0 {
|
||||
// em.inPortParamStartIndex = -1
|
||||
// }
|
||||
//
|
||||
// for i := 0; i < len(port); i++ {
|
||||
// if !port[i].IsPortExec() && em.inPortParamStartIndex < 0 {
|
||||
// em.inPortParamStartIndex = len(em.inPort)
|
||||
// }
|
||||
//
|
||||
// em.inPort = append(em.inPort, port[i])
|
||||
// }
|
||||
//}
|
||||
|
||||
func (em *innerExecNode) PrepareMaxInPortId(maxInPortId int) {
|
||||
em.inPort = make([]IPort, 0, maxInPortId+1)
|
||||
em.inPort = make([]IPort, maxInPortId+1)
|
||||
}
|
||||
|
||||
func (em *innerExecNode) PrepareMaxOutPortId(maxOutPortId int) {
|
||||
em.outPort = make([]IPort, 0, maxOutPortId+1)
|
||||
em.outPort = make([]IPort, maxOutPortId+1)
|
||||
}
|
||||
|
||||
func (em *innerExecNode) SetInPortById(id int, port IPort) bool {
|
||||
@@ -243,6 +232,10 @@ func (em *innerExecNode) GetOutPort(index int) IPort {
|
||||
return em.outPort[index]
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetVariableName() string {
|
||||
return en.execNode.variableName
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetBluePrintModule() IBlueprintModule {
|
||||
return en.gr.IBlueprintModule
|
||||
}
|
||||
@@ -579,10 +572,6 @@ func (en *BaseExecNode) getInnerExecNode() IInnerExecNode {
|
||||
return en.innerExecNode.IExecNode.(IInnerExecNode)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) setVariableName(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetBlueprintModule() IBlueprintModule {
|
||||
if en.gr == nil {
|
||||
return nil
|
||||
|
||||
@@ -5,19 +5,19 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// 格式说明Entrance_ID
|
||||
// Entrance 格式:Entrance_XXXX_ID
|
||||
const (
|
||||
Entrance = "Entrance_"
|
||||
)
|
||||
|
||||
type ExecPool struct {
|
||||
innerExecNodeMap map[string]IInnerExecNode
|
||||
execNodeMap map[string]IExecNode
|
||||
innerExecNodeMap map[string]IInnerExecNode // 所有配置对应的结点信息
|
||||
execNodeMap map[string]IExecNode // 实际注册的执行结点
|
||||
}
|
||||
|
||||
func (em *ExecPool) Load(execDefFilePath string) error {
|
||||
@@ -49,16 +49,19 @@ func (em *ExecPool) Load(execDefFilePath string) error {
|
||||
|
||||
// 只处理JSON文件
|
||||
if filepath.Ext(path) == ".json" {
|
||||
// 将配置的结点初始化为innerExecNode将加入到innerExecNodeMap中
|
||||
return em.processJSONFile(path)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to walk path %s: %v", execDefFilePath, err)
|
||||
}
|
||||
|
||||
return em.loadSysExec()
|
||||
// 生成变量配置对应的配置结点GetVar_类型、SetVar_类型
|
||||
return em.regVariablesNode()
|
||||
}
|
||||
|
||||
// 处理单个JSON文件
|
||||
@@ -84,21 +87,24 @@ func (em *ExecPool) processJSONFile(filePath string) error {
|
||||
}
|
||||
|
||||
for i := range baseExecConfig {
|
||||
// 排序
|
||||
// 对PortId进行排序
|
||||
sort.Slice(baseExecConfig[i].Inputs, func(left, right int) bool {
|
||||
return baseExecConfig[i].Inputs[left].PortId < baseExecConfig[i].Inputs[right].PortId
|
||||
})
|
||||
|
||||
// 对PortId进行排序
|
||||
sort.Slice(baseExecConfig[i].Outputs, func(left, right int) bool {
|
||||
return baseExecConfig[i].Outputs[left].PortId < baseExecConfig[i].Outputs[right].PortId
|
||||
})
|
||||
|
||||
exec, err := em.createExecFromJSON(baseExecConfig[i])
|
||||
if err != nil {
|
||||
return err
|
||||
// 根据配置的结点信息,创建innerExecNode
|
||||
var execError error
|
||||
exec, execError := em.createExecFromJSON(baseExecConfig[i])
|
||||
if execError != nil {
|
||||
return execError
|
||||
}
|
||||
|
||||
if !em.loadBaseExec(exec) {
|
||||
// 加载到innerExecNodeMap中
|
||||
if !em.addInnerExec(exec) {
|
||||
return fmt.Errorf("exec %s already registered", exec.GetName())
|
||||
}
|
||||
}
|
||||
@@ -126,6 +132,7 @@ func (em *ExecPool) createPortByDataType(nodeName, portName, dataType string) (I
|
||||
func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExecNode, error) {
|
||||
var baseExec innerExecNode
|
||||
|
||||
// 如果是入口名,则按入口名Entrance_ArrayParam_000002生成结点名:Entrance_ArrayParam
|
||||
entranceName, _, ok := getEntranceNodeNameAndID(baseExecConfig.Name)
|
||||
if ok {
|
||||
baseExec.Name = entranceName
|
||||
@@ -138,7 +145,7 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
|
||||
baseExec.PrepareMaxInPortId(baseExecConfig.GetMaxInPortId())
|
||||
baseExec.PrepareMaxOutPortId(baseExecConfig.GetMaxOutPortId())
|
||||
|
||||
// exec数量
|
||||
// 初始化所有的输入端口
|
||||
inExecNum := 0
|
||||
for index, input := range baseExecConfig.Inputs {
|
||||
portType := strings.ToLower(input.PortType)
|
||||
@@ -146,6 +153,7 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
|
||||
return nil, fmt.Errorf("input %s data type %s not support", input.Name, input.DataType)
|
||||
}
|
||||
|
||||
// 输入执行结点只能有一个,且只能放在第一个
|
||||
if portType == Config_PortType_Exec {
|
||||
if inExecNum > 0 {
|
||||
return nil, fmt.Errorf("inPort only allows one Execute,node name %s", baseExec.Name)
|
||||
@@ -155,19 +163,22 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
|
||||
}
|
||||
|
||||
inExecNum++
|
||||
// 设置执行端口
|
||||
baseExec.SetInPortById(input.PortId, NewPortExec())
|
||||
// baseExec.AppendInPort(NewPortExec())
|
||||
continue
|
||||
}
|
||||
|
||||
// 根据类型设置对应的端口
|
||||
port, err := em.createPortByDataType(baseExec.Name, input.Name, input.DataType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 根据PortId设置端口
|
||||
baseExec.SetInPortById(input.PortId, port)
|
||||
}
|
||||
|
||||
// 初始化所有的输出端口
|
||||
hasData := false
|
||||
for _, output := range baseExecConfig.Outputs {
|
||||
portType := strings.ToLower(output.PortType)
|
||||
@@ -180,11 +191,13 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
|
||||
return nil, fmt.Errorf("the exec port can only be placed at the front,node name %s", baseExec.Name)
|
||||
}
|
||||
|
||||
// 设置执行端口
|
||||
if portType == Config_PortType_Exec {
|
||||
baseExec.SetOutPortById(output.PortId, NewPortExec())
|
||||
continue
|
||||
}
|
||||
|
||||
// 根据类型设置数据端口
|
||||
hasData = true
|
||||
port, err := em.createPortByDataType(baseExec.Name, output.Name, output.DataType)
|
||||
if err != nil {
|
||||
@@ -196,7 +209,7 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
|
||||
return &baseExec, nil
|
||||
}
|
||||
|
||||
func (em *ExecPool) loadBaseExec(exec IInnerExecNode) bool {
|
||||
func (em *ExecPool) addInnerExec(exec IInnerExecNode) bool {
|
||||
if _, ok := em.innerExecNodeMap[exec.GetName()]; ok {
|
||||
return false
|
||||
}
|
||||
@@ -224,10 +237,14 @@ func (em *ExecPool) Register(exec IExecNode) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// 设置实际执行结点中innerExecNode变量,BaseExecNode.innerExecNode = innerNode
|
||||
baseExecNode.initInnerExecNode(innerNode.(*innerExecNode))
|
||||
|
||||
// innerNode设置实际的exec变量,innerExecNode.IExecNode = exec
|
||||
innerNode.SetExec(exec)
|
||||
|
||||
em.execNodeMap[baseExec.GetName()] = baseExec
|
||||
// 将实际的执行结点保存到execNodeMap中
|
||||
em.execNodeMap[baseExec.GetName()] = exec
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -238,7 +255,8 @@ func (em *ExecPool) GetExec(name string) IInnerExecNode {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (em *ExecPool) loadSysExec() error {
|
||||
// regVariablesNode 注册变量结点GetVar_类型、SetVar_类型
|
||||
func (em *ExecPool) regVariablesNode() error {
|
||||
var err error
|
||||
if err = em.regGetVariables(Config_DataType_Int); err != nil {
|
||||
return err
|
||||
@@ -300,7 +318,7 @@ func (em *ExecPool) regGetVariables(typ string) error {
|
||||
var getVariablesNode GetVariablesNode
|
||||
getVariablesNode.nodeName = baseExec.GetName()
|
||||
|
||||
if !em.loadBaseExec(&baseExec) {
|
||||
if !em.addInnerExec(&baseExec) {
|
||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
||||
}
|
||||
if !em.Register(&getVariablesNode) {
|
||||
@@ -336,7 +354,7 @@ func (em *ExecPool) regSetVariables(typ string) error {
|
||||
baseExec.SetOutPortById(1, outPort)
|
||||
|
||||
baseExec.IExecNode = &SetVariablesNode{nodeName: baseExec.GetName()}
|
||||
if !em.loadBaseExec(&baseExec) {
|
||||
if !em.addInnerExec(&baseExec) {
|
||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
||||
}
|
||||
if !em.Register(baseExec.IExecNode) {
|
||||
|
||||
@@ -4,15 +4,20 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
const ReturnVarial = "g_Return"
|
||||
|
||||
var IsDebug = false
|
||||
|
||||
type IGraph interface {
|
||||
Do(entranceID int64, args ...any) (Port_Array, error)
|
||||
Release()
|
||||
GetGraphFileName() string
|
||||
HotReload(newBaseGraph *baseGraph)
|
||||
}
|
||||
|
||||
type IBlueprintModule interface {
|
||||
@@ -28,7 +33,8 @@ type baseGraph struct {
|
||||
}
|
||||
|
||||
type Graph struct {
|
||||
graphID int64
|
||||
graphFileName string
|
||||
graphID int64
|
||||
*baseGraph
|
||||
graphContext
|
||||
IBlueprintModule
|
||||
@@ -137,12 +143,18 @@ func (gc *graphConfig) GetNodeByID(nodeID string) *nodeConfig {
|
||||
}
|
||||
|
||||
func (gr *Graph) Do(entranceID int64, args ...any) (Port_Array, error) {
|
||||
if IsDebug {
|
||||
log.Debug("Graph Do", log.String("graphName", gr.graphFileName), log.Int64("graphID", gr.graphID), log.Int64("entranceID", entranceID))
|
||||
}
|
||||
|
||||
entranceNode := gr.entrance[entranceID]
|
||||
if entranceNode == nil {
|
||||
return nil, fmt.Errorf("entranceID:%d not found", entranceID)
|
||||
}
|
||||
|
||||
gr.variables = map[string]IPort{}
|
||||
gr.context = map[string]*ExecContext{}
|
||||
|
||||
if gr.globalVariables == nil {
|
||||
gr.globalVariables = map[string]IPort{}
|
||||
}
|
||||
@@ -196,3 +208,11 @@ func (gr *Graph) Release() {
|
||||
// 清理掉所有数据
|
||||
*gr = Graph{}
|
||||
}
|
||||
|
||||
func (gr *Graph) HotReload(newBaseGraph *baseGraph) {
|
||||
gr.baseGraph = newBaseGraph
|
||||
}
|
||||
|
||||
func (gr *Graph) GetGraphFileName() string{
|
||||
return gr.graphFileName
|
||||
}
|
||||
|
||||
@@ -52,6 +52,15 @@ func (gp *GraphPool) Load(execPool *ExecPool, graphFilePath string, blueprintMod
|
||||
})
|
||||
}
|
||||
|
||||
func (gp *GraphPool) GetBaseGraph(graphName string) *baseGraph {
|
||||
gr, ok := gp.mapGraphs[graphName]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return gr
|
||||
}
|
||||
|
||||
func (gp *GraphPool) Create(graphName string, graphID int64) IGraph {
|
||||
gr, ok := gp.mapGraphs[graphName]
|
||||
if !ok {
|
||||
@@ -61,6 +70,7 @@ func (gp *GraphPool) Create(graphName string, graphID int64) IGraph {
|
||||
var graph Graph
|
||||
graph.baseGraph = gr
|
||||
graph.graphID = graphID
|
||||
graph.graphFileName = graphName
|
||||
graph.context = make(map[string]*ExecContext, 4)
|
||||
graph.IBlueprintModule = gp.blueprintModule
|
||||
return &graph
|
||||
@@ -72,21 +82,25 @@ func (gp *GraphPool) processJSONFile(filePath string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
if err = file.Close(); err != nil {
|
||||
fmt.Printf("关闭文件 %s 时出错: %v\n", filePath, err)
|
||||
}
|
||||
}()
|
||||
|
||||
fileName := filepath.Base(filePath)
|
||||
ext := filepath.Ext(fileName) // 获取".html"
|
||||
ext := filepath.Ext(fileName) // 获取".vgf"
|
||||
name := strings.TrimSuffix(fileName, ext) // 获取"name"
|
||||
|
||||
// 解析文件
|
||||
var gConfig graphConfig
|
||||
decoder := json.NewDecoder(file)
|
||||
if err := decoder.Decode(&gConfig); err != nil {
|
||||
if err = decoder.Decode(&gConfig); err != nil {
|
||||
return fmt.Errorf("failed to decode JSON from file %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
// 预处理蓝图
|
||||
return gp.prepareGraph(name, &gConfig)
|
||||
}
|
||||
|
||||
@@ -137,8 +151,6 @@ func (gp *GraphPool) genVarExec(nodeCfg *nodeConfig, graphConfig *graphConfig) (
|
||||
}
|
||||
|
||||
e := gp.execPool.GetExec(nodeName)
|
||||
e.(IExecNode).setVariableName(varName)
|
||||
|
||||
return e, varName
|
||||
}
|
||||
|
||||
@@ -174,7 +186,7 @@ func (gp *GraphPool) genAllNode(graphConfig *graphConfig) (map[string]*execNode,
|
||||
|
||||
func (gp *GraphPool) prepareOneNode(mapNodeExec map[string]*execNode, nodeExec *execNode, graphConfig *graphConfig, recursion *int) error {
|
||||
*recursion++
|
||||
if *recursion > 100 {
|
||||
if *recursion > 256 {
|
||||
return fmt.Errorf("recursion too deep")
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package blueprint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
type prePortNode struct {
|
||||
@@ -102,20 +103,6 @@ func (en *execNode) exec(gr *Graph) (int, error) {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
//defer func() {
|
||||
inPort, outPort := node.GetPorts()
|
||||
debugString := "inPort:"
|
||||
for i := 0; i < len(inPort); i++ {
|
||||
debugString += fmt.Sprintf("%+v,", inPort[i])
|
||||
}
|
||||
debugString += " outPort:"
|
||||
for i := 0; i < len(outPort); i++ {
|
||||
debugString += fmt.Sprintf("%+v,", outPort[i])
|
||||
}
|
||||
|
||||
fmt.Printf("exec node %s,%s\n", en.execNode.GetName(), debugString)
|
||||
//}()
|
||||
|
||||
return e.Exec()
|
||||
}
|
||||
|
||||
@@ -155,6 +142,10 @@ func (en *execNode) doSetInPort(gr *Graph, index int, inPort IPort) error {
|
||||
}
|
||||
|
||||
func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
|
||||
if IsDebug {
|
||||
log.Debug("Start ExecNode", log.String("Name",en.execNode.GetName()))
|
||||
}
|
||||
|
||||
// 重新初始化上下文
|
||||
inPorts, outPorts := en.execNode.CloneInOutPort()
|
||||
gr.context[en.Id] = &ExecContext{
|
||||
@@ -164,7 +155,7 @@ func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
|
||||
|
||||
startOutIdx := en.execNode.GetOutPortParamStartIndex()
|
||||
for i := 0; i < len(outPortArgs); i++ {
|
||||
if i >= len(outPorts) {
|
||||
if i+startOutIdx >= len(outPorts) {
|
||||
return fmt.Errorf("args %d not found in node %s", i, en.execNode.GetName())
|
||||
}
|
||||
|
||||
@@ -194,6 +185,10 @@ func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if IsDebug {
|
||||
log.Debug("End ExecNode", log.String("Name",en.execNode.GetName()),log.Any("InPort",inPorts ),log.Any("OutPort",outPorts))
|
||||
}
|
||||
|
||||
if nextIndex == -1 || en.nextNode == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ func (em *Port[T]) convertInt64(v any) (int64, bool) {
|
||||
|
||||
func (em *Port[T]) setAnyVale(v any) error {
|
||||
switch v.(type) {
|
||||
case int, int64:
|
||||
case int8,int16,int32,int, int64,uint8,uint16,uint32,uint, uint64:
|
||||
val, ok := em.convertInt64(v)
|
||||
if !ok {
|
||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package blueprint
|
||||
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@ const (
|
||||
EntranceID_Timer = 3
|
||||
)
|
||||
|
||||
|
||||
|
||||
type Entrance_ArrayParam struct {
|
||||
BaseExecNode
|
||||
}
|
||||
@@ -53,15 +51,15 @@ func (em *Entrance_Timer) Exec() (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
type Output struct {
|
||||
type DebugOutput struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *Output) GetName() string {
|
||||
return "Output"
|
||||
func (em *DebugOutput) GetName() string {
|
||||
return "DebugOutput"
|
||||
}
|
||||
|
||||
func (em *Output) Exec() (int, error) {
|
||||
func (em *DebugOutput) Exec() (int, error) {
|
||||
val, ok := em.GetInPortInt(1)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("output Exec inParam not found")
|
||||
@@ -77,7 +75,7 @@ func (em *Output) Exec() (int, error) {
|
||||
return 0, fmt.Errorf("output Exec inParam not found")
|
||||
}
|
||||
|
||||
fmt.Printf("output Exec inParam [%d] [%s] [%v]\n", val, valStr, valArray)
|
||||
log.Debug("DebugOutput Exec", log.Any("param1", val), log.Any("param2", valStr), log.Any("param3", valArray))
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
@@ -119,7 +117,8 @@ func (em *ForeachIntArray) Exec() (int, error) {
|
||||
}
|
||||
|
||||
for i := range array {
|
||||
em.ExecContext.OutputPorts[2].SetInt(array[i].IntVal)
|
||||
em.ExecContext.OutputPorts[2].SetInt(Port_Int(i))
|
||||
em.ExecContext.OutputPorts[3].SetInt(array[i].IntVal)
|
||||
err := em.DoNext(0)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
@@ -471,6 +470,40 @@ func (em *RangeCompare) Exec() (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// EqualSwitch 等于分支==
|
||||
type EqualSwitch struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *EqualSwitch) GetName() string {
|
||||
return "EqualSwitch"
|
||||
}
|
||||
|
||||
func (em *EqualSwitch) Exec() (int, error) {
|
||||
inPortA := em.GetInPort(1)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
ret, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 error")
|
||||
}
|
||||
|
||||
intArray := em.execNode.GetInPortDefaultIntArrayValue(2)
|
||||
if intArray == nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
for i := 0; i < len(intArray) && i < em.GetOutPortCount()-2; i++ {
|
||||
if ret == intArray[i] {
|
||||
return i + 2, nil
|
||||
}
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Probability 概率判断(万分比)
|
||||
type Probability struct {
|
||||
BaseExecNode
|
||||
|
||||
@@ -12,13 +12,11 @@ const globalVariablesPrefix = "g_"
|
||||
type GetVariablesNode struct {
|
||||
BaseExecNode
|
||||
nodeName string
|
||||
varName string
|
||||
}
|
||||
|
||||
type SetVariablesNode struct {
|
||||
BaseExecNode
|
||||
nodeName string
|
||||
varName string
|
||||
}
|
||||
|
||||
func (g *GetVariablesNode) GetName() string {
|
||||
@@ -27,14 +25,14 @@ func (g *GetVariablesNode) GetName() string {
|
||||
|
||||
func (g *GetVariablesNode) Exec() (int, error) {
|
||||
var port IPort
|
||||
if strings.HasPrefix(g.varName, globalVariablesPrefix) {
|
||||
port = g.gr.globalVariables[g.varName]
|
||||
if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
|
||||
port = g.gr.globalVariables[g.GetVariableName()]
|
||||
} else {
|
||||
port = g.gr.variables[g.varName]
|
||||
port = g.gr.variables[g.GetVariableName()]
|
||||
}
|
||||
|
||||
if port == nil {
|
||||
return -1, fmt.Errorf("variable %s not found,node name %s", g.varName, g.nodeName)
|
||||
return -1, fmt.Errorf("variable %s not found,node name %s", g.GetVariableName(), g.nodeName)
|
||||
}
|
||||
|
||||
if !g.SetOutPort(0, port) {
|
||||
@@ -44,10 +42,7 @@ func (g *GetVariablesNode) Exec() (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (g *GetVariablesNode) setVariableName(name string) bool {
|
||||
g.varName = name
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
func (g *SetVariablesNode) GetName() string {
|
||||
return g.nodeName
|
||||
@@ -60,10 +55,10 @@ func (g *SetVariablesNode) Exec() (int, error) {
|
||||
}
|
||||
|
||||
varPort := port.Clone()
|
||||
if strings.HasPrefix(g.varName, globalVariablesPrefix) {
|
||||
g.gr.globalVariables[g.varName] = varPort
|
||||
if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
|
||||
g.gr.globalVariables[g.GetVariableName()] = varPort
|
||||
} else {
|
||||
g.gr.variables[g.varName] = varPort
|
||||
g.gr.variables[g.GetVariableName()] = varPort
|
||||
}
|
||||
|
||||
if !g.SetOutPort(1, varPort) {
|
||||
@@ -73,7 +68,3 @@ func (g *SetVariablesNode) Exec() (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (g *SetVariablesNode) setVariableName(name string) bool {
|
||||
g.varName = name
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user