mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-06 22:27:36 +08:00
Compare commits
2 Commits
v2.2.0
...
e6232453ef
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e6232453ef | ||
|
|
0467548acb |
74
README.md
74
README.md
@@ -80,22 +80,8 @@ Etcd方式示例:
|
|||||||
"DialTimeoutMillisecond": 3000,
|
"DialTimeoutMillisecond": 3000,
|
||||||
"EtcdList": [
|
"EtcdList": [
|
||||||
{
|
{
|
||||||
"LocalNetworkName": "network_Area1",
|
"NetworkName": ["network1"],
|
||||||
"Endpoints": ["http://127.0.0.1:12379"],
|
"Endpoints": ["http://192.168.13.24:12379"]
|
||||||
"UserName": "",
|
|
||||||
"Password": "",
|
|
||||||
"Cert": "",
|
|
||||||
"CertKey": "",
|
|
||||||
"Ca": ""
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"NeighborNetworkName": ["network_Area2"],
|
|
||||||
"Endpoints": ["http://127.0.0.1:12379"],
|
|
||||||
"UserName": "",
|
|
||||||
"Password": "",
|
|
||||||
"Cert": "",
|
|
||||||
"CertKey": "",
|
|
||||||
"Ca": ""
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -106,16 +92,12 @@ TTLSecond:表示健康检查TTL失效时间10秒
|
|||||||
|
|
||||||
DialTimeoutMillisecond: 与etcd连接超时时间
|
DialTimeoutMillisecond: 与etcd连接超时时间
|
||||||
|
|
||||||
EtcdList:Etcd列表,可以多个Etcd服务器连接(注意:列表中必需有一个LocalNetworkName项,表示当前所有的Node归属当前网络名为network_Area1)Node下所有的服务会往network_Area1中注册。监听该网络的结点可以发现该网络中的Service。本地网络会默认监听本地网络中所有的服务。
|
EtcdList:Etcd列表,可以多个Etcd服务器连接
|
||||||
|
|
||||||
NeighborNetworkName:表示监听的邻居网络名,可以发现该网络中所有Service
|
NetworkName:所在的网络名称,可以配置多个。node会往对应的网络名称中注册、监听发现Service。NetworkName也起到发现隔离的作用。
|
||||||
|
|
||||||
Endpoints:Etcd服务器地址
|
Endpoints:Etcd服务器地址
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Origin方式示例:
|
Origin方式示例:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
@@ -123,15 +105,10 @@ Origin方式示例:
|
|||||||
"Discovery": {
|
"Discovery": {
|
||||||
"Origin":{
|
"Origin":{
|
||||||
"TTLSecond": 10,
|
"TTLSecond": 10,
|
||||||
"LocalMasterNodeId": "bot",
|
|
||||||
"MasterNodeList": [
|
"MasterNodeList": [
|
||||||
{
|
{
|
||||||
"NodeId": "bot",
|
"NodeId": "test_1",
|
||||||
"ListenAddr": "127.0.0.1:11001"
|
"ListenAddr": "127.0.0.1:8801"
|
||||||
},
|
|
||||||
{
|
|
||||||
"NodeId": "mp1server",
|
|
||||||
"ListenAddr": "127.0.0.1:11000"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -141,12 +118,8 @@ Origin方式示例:
|
|||||||
|
|
||||||
TTLSecond:表示健康检查TTL失效时间10秒
|
TTLSecond:表示健康检查TTL失效时间10秒
|
||||||
|
|
||||||
LocalMasterNodeId:本地所有的Node归属当前Master NodeId。归属当前的所有的Node会往该Master Node中注册服务。MasterNode会自动同步给所有的监听结点。本地Node会默认监听本地Master Node中所有的服务。注意LocalMasterNodeId配置的NodeId要在MasterNodeList列表中。
|
|
||||||
|
|
||||||
MasterNodeList:指定哪些Node为服务发现Master结点,需要配置NodeId与ListenAddr,注意它们要与实际的Node配置一致。
|
MasterNodeList:指定哪些Node为服务发现Master结点,需要配置NodeId与ListenAddr,注意它们要与实际的Node配置一致。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### RpcMode部分
|
### RpcMode部分
|
||||||
|
|
||||||
默认模式
|
默认模式
|
||||||
@@ -974,45 +947,20 @@ origin引擎默认使用读取所有结点配置的进行确认结点有哪些Se
|
|||||||
"Private": false,
|
"Private": false,
|
||||||
"remark": "//以_打头的,表示只在本机进程,不对整个子网开发",
|
"remark": "//以_打头的,表示只在本机进程,不对整个子网开发",
|
||||||
"ServiceList": ["_TestService1", "TestService9", "TestService10"],
|
"ServiceList": ["_TestService1", "TestService9", "TestService10"],
|
||||||
"AllowDiscovery": [
|
"DiscoveryService": [
|
||||||
{
|
{
|
||||||
"MasterNodeId": "nodeid_1",
|
"MasterNodeId": "nodeid_1",
|
||||||
"NetworkName":"networkname1",
|
"NetworkName":"networkname1"
|
||||||
"NodeIdList":[".*server"],
|
"DiscoveryService": ["TestService8"]
|
||||||
"ServiceList": ["TestService8"]
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
以上,如果是使用Etcd发现模式,则表示可以发现网络名networkname1,NodeId为server结尾,服务名为TestService8的服务。
|
DiscoveryService:在当前nodeid为nodeid_test的结点中,只发现 MasterNodeId为nodeid_1或NetworkName为networkname1网络中的TestService8服务。
|
||||||
|
|
||||||
AllowDiscovery:可以配置发现的规则,如果只配置MasterNodeId或NetworkName时(如果使用Etcd则只配置NetworkName,Origin则只配置MasterNodeId),则会筛选指定网络的所有服务,如下:
|
|
||||||
|
|
||||||
```
|
|
||||||
"AllowDiscovery": [
|
|
||||||
{
|
|
||||||
"NetworkName":"networkname1",
|
|
||||||
}
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
则以上,只匹配networkname1网络名的所有服务,支持正则表达式,例如可以配置为"NetworkName":".*name1",则可以发现网络名为name1结尾的所有服务。
|
|
||||||
|
|
||||||
如果只发现NodeId为server结尾的所有服务,可以使用以下配置方式:
|
|
||||||
|
|
||||||
```
|
|
||||||
"AllowDiscovery": [
|
|
||||||
{
|
|
||||||
"NodeIdList":[".*server"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
筛选服务也是同上。也可以组合配置NetworkName和NodeIdList配置。
|
|
||||||
|
|
||||||
|
|
||||||
|
**注意**:MasterNodeId与NetworkName只配置一个,分别在模式为origin或者etcd服务发现类型时。
|
||||||
|
|
||||||
第八章:HttpService使用
|
第八章:HttpService使用
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|||||||
@@ -3,16 +3,13 @@ package cluster
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"github.com/duanhf2012/origin/v2/event"
|
"github.com/duanhf2012/origin/v2/event"
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
"github.com/duanhf2012/origin/v2/rpc"
|
"github.com/duanhf2012/origin/v2/rpc"
|
||||||
"github.com/duanhf2012/origin/v2/service"
|
"github.com/duanhf2012/origin/v2/service"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var configDir = "./config/"
|
var configDir = "./config/"
|
||||||
@@ -26,23 +23,21 @@ const (
|
|||||||
Discard NodeStatus = 1 //丢弃
|
Discard NodeStatus = 1 //丢弃
|
||||||
)
|
)
|
||||||
|
|
||||||
// AllowDiscovery 允许发现的网络服务
|
type DiscoveryService struct {
|
||||||
type AllowDiscovery struct {
|
MasterNodeId string //要筛选的主结点Id,如果不配置或者配置成0,表示针对所有的主结点
|
||||||
MasterNodeId string // 支持正则表达式
|
NetworkName string //如果是etcd,指定要筛选的网络名中的服务,不配置,表示所有的网络
|
||||||
NetworkName string // 支持正则表达式
|
ServiceList []string //只发现的服务列表
|
||||||
NodeIdList []string // 支持正则表达式
|
|
||||||
ServiceList []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeInfo struct {
|
type NodeInfo struct {
|
||||||
NodeId string
|
NodeId string
|
||||||
Private bool
|
Private bool
|
||||||
ListenAddr string
|
ListenAddr string
|
||||||
MaxRpcParamLen uint32 //最大Rpc参数长度
|
MaxRpcParamLen uint32 //最大Rpc参数长度
|
||||||
CompressBytesLen int //超过字节进行压缩的长度
|
CompressBytesLen int //超过字节进行压缩的长度
|
||||||
ServiceList []string //所有的有序服务列表
|
ServiceList []string //所有的有序服务列表
|
||||||
PublicServiceList []string //对外公开的服务列表
|
PublicServiceList []string //对外公开的服务列表
|
||||||
AllowDiscovery []AllowDiscovery //允许发现的网络服务
|
DiscoveryService []DiscoveryService //筛选发现的服务,如果不配置,不进行筛选
|
||||||
status NodeStatus
|
status NodeStatus
|
||||||
Retire bool
|
Retire bool
|
||||||
}
|
}
|
||||||
@@ -227,9 +222,9 @@ func (cls *Cluster) serviceDiscoverySetNodeInfo(nodeInfo *NodeInfo) {
|
|||||||
}
|
}
|
||||||
cls.mapRpc[nodeInfo.NodeId] = &rpcInfo
|
cls.mapRpc[nodeInfo.NodeId] = &rpcInfo
|
||||||
if cls.IsNatsMode() == true || cls.discoveryInfo.discoveryType != OriginType {
|
if cls.IsNatsMode() == true || cls.discoveryInfo.discoveryType != OriginType {
|
||||||
log.Info("Discovery node and new rpc client", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire))
|
log.Info("Discovery nodeId and new rpc client", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire))
|
||||||
} else {
|
} else {
|
||||||
log.Info("Discovery node and new rpc client", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire), log.String("nodeListenAddr", nodeInfo.ListenAddr))
|
log.Info("Discovery nodeId and new rpc client", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire), log.String("nodeListenAddr", nodeInfo.ListenAddr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -466,76 +461,28 @@ func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo, bool) {
|
|||||||
return nodeInfo.nodeInfo, true
|
return nodeInfo.nodeInfo, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cls *Cluster) CanDiscoveryService(fromNetworkName string, fromMasterNodeId string, fromNodeId string, serviceName string) bool {
|
func (cls *Cluster) CanDiscoveryService(fromMasterNodeId string, serviceName string) bool {
|
||||||
canDiscovery := true
|
canDiscovery := true
|
||||||
// 筛选允许的服务
|
|
||||||
splitServiceName := strings.Split(serviceName, ":")
|
splitServiceName := strings.Split(serviceName, ":")
|
||||||
if len(splitServiceName) == 2 {
|
if len(splitServiceName) == 2 {
|
||||||
serviceName = splitServiceName[0]
|
serviceName = splitServiceName[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// 先筛选允许的网络,有配置才会检测
|
for i := 0; i < len(cls.GetLocalNodeInfo().DiscoveryService); i++ {
|
||||||
if len(cls.GetLocalNodeInfo().AllowDiscovery) > 0 {
|
masterNodeId := cls.GetLocalNodeInfo().DiscoveryService[i].MasterNodeId
|
||||||
allowNetwork := false
|
//无效的配置,则跳过
|
||||||
for i := 0; i < len(cls.GetLocalNodeInfo().AllowDiscovery); i++ {
|
if masterNodeId == rpc.NodeIdNull && len(cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList) == 0 {
|
||||||
masterNodeId := cls.GetLocalNodeInfo().AllowDiscovery[i].MasterNodeId
|
continue
|
||||||
networkName := cls.GetLocalNodeInfo().AllowDiscovery[i].NetworkName
|
|
||||||
nodeIdList := cls.GetLocalNodeInfo().AllowDiscovery[i].NodeIdList
|
|
||||||
serviceList := cls.GetLocalNodeInfo().AllowDiscovery[i].ServiceList
|
|
||||||
|
|
||||||
// 如果配置了网络及Master结点,则匹配之
|
|
||||||
if fromNetworkName != "" {
|
|
||||||
matchNetWork, _ := regexp.MatchString(networkName, fromNetworkName)
|
|
||||||
if !matchNetWork {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else if fromMasterNodeId != "" {
|
|
||||||
matchMasterNode, _ := regexp.MatchString(masterNodeId, fromMasterNodeId)
|
|
||||||
if !matchMasterNode {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果配置了
|
|
||||||
if len(nodeIdList) > 0 {
|
|
||||||
hasNode := false
|
|
||||||
for _, nodeId := range nodeIdList {
|
|
||||||
matchNodeId, _ := regexp.MatchString(nodeId, fromNodeId)
|
|
||||||
if !matchNodeId {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
hasNode = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if !hasNode {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果配置了服务,则匹配之
|
|
||||||
if len(serviceList) > 0 {
|
|
||||||
hasService := false
|
|
||||||
for _, service := range serviceList {
|
|
||||||
// service按正则表达式匹配serviceName
|
|
||||||
matched, _ := regexp.MatchString(service, serviceName)
|
|
||||||
if matched {
|
|
||||||
hasService = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !hasService {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
allowNetwork = true
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !allowNetwork {
|
canDiscovery = false
|
||||||
return false
|
if masterNodeId == fromMasterNodeId || masterNodeId == rpc.NodeIdNull {
|
||||||
|
for _, discoveryService := range cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList {
|
||||||
|
if discoveryService == serviceName {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,6 @@ import (
|
|||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/duanhf2012/origin/v2/event"
|
"github.com/duanhf2012/origin/v2/event"
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
"github.com/duanhf2012/origin/v2/rpc"
|
"github.com/duanhf2012/origin/v2/rpc"
|
||||||
@@ -20,15 +14,19 @@ import (
|
|||||||
"go.etcd.io/etcd/api/v3/mvccpb"
|
"go.etcd.io/etcd/api/v3/mvccpb"
|
||||||
"go.etcd.io/etcd/client/v3"
|
"go.etcd.io/etcd/client/v3"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const originDir = "/origin"
|
const originDir = "/origin"
|
||||||
|
|
||||||
type etcdClientInfo struct {
|
type etcdClientInfo struct {
|
||||||
isLocalNetwork bool
|
watchKeys []string
|
||||||
watchKeys []string
|
leaseID clientv3.LeaseID
|
||||||
leaseID clientv3.LeaseID
|
keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse
|
||||||
keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EtcdDiscoveryService struct {
|
type EtcdDiscoveryService struct {
|
||||||
@@ -95,7 +93,6 @@ func (ed *EtcdDiscoveryService) OnInit() error {
|
|||||||
return errors.New("etcd discovery config is nil.")
|
return errors.New("etcd discovery config is nil.")
|
||||||
}
|
}
|
||||||
|
|
||||||
var hasLocalNetwork bool
|
|
||||||
for i := 0; i < len(etcdDiscoveryCfg.EtcdList); i++ {
|
for i := 0; i < len(etcdDiscoveryCfg.EtcdList); i++ {
|
||||||
var client *clientv3.Client
|
var client *clientv3.Client
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
@@ -144,25 +141,13 @@ func (ed *EtcdDiscoveryService) OnInit() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ec := &etcdClientInfo{}
|
ec := &etcdClientInfo{}
|
||||||
|
for _, networkName := range etcdDiscoveryCfg.EtcdList[i].NetworkName {
|
||||||
if etcdDiscoveryCfg.EtcdList[i].LocalNetworkName != "" {
|
ec.watchKeys = append(ec.watchKeys, fmt.Sprintf("%s/%s", originDir, networkName))
|
||||||
hasLocalNetwork = true
|
|
||||||
ec.isLocalNetwork = true
|
|
||||||
ec.watchKeys = append(ec.watchKeys, fmt.Sprintf("%s/%s", originDir, etcdDiscoveryCfg.EtcdList[i].LocalNetworkName))
|
|
||||||
} else {
|
|
||||||
ec.isLocalNetwork = false
|
|
||||||
for _, networkName := range etcdDiscoveryCfg.EtcdList[i].NeighborNetworkName {
|
|
||||||
ec.watchKeys = append(ec.watchKeys, fmt.Sprintf("%s/%s", originDir, networkName))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ed.mapClient[client] = ec
|
ed.mapClient[client] = ec
|
||||||
}
|
}
|
||||||
|
|
||||||
if !hasLocalNetwork {
|
|
||||||
return errors.New("etcd discovery init fail,cannot find local network")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,18 +167,14 @@ func (ed *EtcdDiscoveryService) registerServiceByClient(client *clientv3.Client,
|
|||||||
}
|
}
|
||||||
|
|
||||||
etcdClient.leaseID = resp.ID
|
etcdClient.leaseID = resp.ID
|
||||||
|
for _, watchKey := range etcdClient.watchKeys {
|
||||||
// 注册服务节点到 etcd,LocalNetwork时才会注册,且etcdClient.watchKeys必然>0
|
// 注册服务节点到 etcd
|
||||||
if len(etcdClient.watchKeys) != 1 {
|
_, err = client.Put(context.Background(), ed.getRegisterKey(watchKey), ed.byteLocalNodeInfo, clientv3.WithLease(resp.ID))
|
||||||
log.Error("LocalNetwork watchkey is error")
|
if err != nil {
|
||||||
return
|
log.Error("etcd Put fail", log.ErrorField("err", err))
|
||||||
}
|
ed.tryRegisterService(client, etcdClient)
|
||||||
|
return
|
||||||
_, err = client.Put(context.Background(), ed.getRegisterKey(etcdClient.watchKeys[0]), ed.byteLocalNodeInfo, clientv3.WithLease(resp.ID))
|
}
|
||||||
if err != nil {
|
|
||||||
log.Error("etcd Put fail", log.ErrorField("err", err))
|
|
||||||
ed.tryRegisterService(client, etcdClient)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
etcdClient.keepAliveChan, err = client.KeepAlive(context.Background(), etcdClient.leaseID)
|
etcdClient.keepAliveChan, err = client.KeepAlive(context.Background(), etcdClient.leaseID)
|
||||||
@@ -223,10 +204,6 @@ func (ed *EtcdDiscoveryService) tryRegisterService(client *clientv3.Client, etcd
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !etcdClient.isLocalNetwork {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ed.AfterFunc(time.Second*3, func(t *timer.Timer) {
|
ed.AfterFunc(time.Second*3, func(t *timer.Timer) {
|
||||||
ed.registerServiceByClient(client, etcdClient)
|
ed.registerServiceByClient(client, etcdClient)
|
||||||
})
|
})
|
||||||
@@ -252,18 +229,13 @@ func (ed *EtcdDiscoveryService) tryLaterRetire() {
|
|||||||
func (ed *EtcdDiscoveryService) retire() error {
|
func (ed *EtcdDiscoveryService) retire() error {
|
||||||
//从etcd中更新
|
//从etcd中更新
|
||||||
for c, ec := range ed.mapClient {
|
for c, ec := range ed.mapClient {
|
||||||
if !ec.isLocalNetwork {
|
for _, watchKey := range ec.watchKeys {
|
||||||
continue
|
// 注册服务节点到 etcd
|
||||||
}
|
_, err := c.Put(context.Background(), ed.getRegisterKey(watchKey), ed.byteLocalNodeInfo, clientv3.WithLease(ec.leaseID))
|
||||||
|
if err != nil {
|
||||||
if len(ec.watchKeys)!=1 {
|
log.Error("etcd Put fail", log.ErrorField("err", err))
|
||||||
log.Error("LocalNetwork watchkey is error")
|
return err
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
_, err := c.Put(context.Background(), ed.getRegisterKey(ec.watchKeys[0]), ed.byteLocalNodeInfo, clientv3.WithLease(ec.leaseID))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("etcd Put fail", log.ErrorField("err", err))
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,7 +292,7 @@ func (ed *EtcdDiscoveryService) setNodeInfo(networkName string, nodeInfo *rpc.No
|
|||||||
//筛选关注的服务
|
//筛选关注的服务
|
||||||
var discoverServiceSlice = make([]string, 0, 24)
|
var discoverServiceSlice = make([]string, 0, 24)
|
||||||
for _, pubService := range nodeInfo.PublicServiceList {
|
for _, pubService := range nodeInfo.PublicServiceList {
|
||||||
if cluster.CanDiscoveryService(networkName, "", nodeInfo.NodeId, pubService) == true {
|
if cluster.CanDiscoveryService(networkName, pubService) == true {
|
||||||
discoverServiceSlice = append(discoverServiceSlice, pubService)
|
discoverServiceSlice = append(discoverServiceSlice, pubService)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -531,16 +503,11 @@ func (ed *EtcdDiscoveryService) RPC_ServiceRecord(etcdServiceRecord *service.Etc
|
|||||||
|
|
||||||
//写入到etcd中
|
//写入到etcd中
|
||||||
for c, info := range ed.mapClient {
|
for c, info := range ed.mapClient {
|
||||||
if !info.isLocalNetwork {
|
for _, watchKey := range info.watchKeys {
|
||||||
continue
|
if ed.getNetworkNameByWatchKey(watchKey) == etcdServiceRecord.NetworkName {
|
||||||
}
|
client = c
|
||||||
|
break
|
||||||
if len(info.watchKeys)!=1 {
|
}
|
||||||
log.Error("")
|
|
||||||
}
|
|
||||||
if ed.getNetworkNameByWatchKey(info.watchKeys[0]) == etcdServiceRecord.NetworkName {
|
|
||||||
client = c
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -508,17 +508,11 @@ func (dc *OriginDiscoveryClient) regServiceDiscover(nodeId string) {
|
|||||||
if nodeId == cluster.GetLocalNodeInfo().NodeId {
|
if nodeId == cluster.GetLocalNodeInfo().NodeId {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeInfo := cluster.getOriginMasterDiscoveryNodeInfo(nodeId)
|
nodeInfo := cluster.getOriginMasterDiscoveryNodeInfo(nodeId)
|
||||||
if nodeInfo == nil {
|
if nodeInfo == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 只能往本地Master结点上注册
|
|
||||||
if !cluster.IsLocalMasterNodeId(nodeId) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var req rpc.RegServiceDiscoverReq
|
var req rpc.RegServiceDiscoverReq
|
||||||
req.NodeInfo = &rpc.NodeInfo{}
|
req.NodeInfo = &rpc.NodeInfo{}
|
||||||
req.NodeInfo.NodeId = cluster.localNodeInfo.NodeId
|
req.NodeInfo.NodeId = cluster.localNodeInfo.NodeId
|
||||||
@@ -554,7 +548,7 @@ func (dc *OriginDiscoveryClient) setNodeInfo(masterNodeId string, nodeInfo *rpc.
|
|||||||
//筛选关注的服务
|
//筛选关注的服务
|
||||||
var discoverServiceSlice = make([]string, 0, 24)
|
var discoverServiceSlice = make([]string, 0, 24)
|
||||||
for _, pubService := range nodeInfo.PublicServiceList {
|
for _, pubService := range nodeInfo.PublicServiceList {
|
||||||
if cluster.CanDiscoveryService("",masterNodeId, nodeInfo.NodeId,pubService) == true {
|
if cluster.CanDiscoveryService(masterNodeId, pubService) == true {
|
||||||
discoverServiceSlice = append(discoverServiceSlice, pubService)
|
discoverServiceSlice = append(discoverServiceSlice, pubService)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -623,22 +617,13 @@ func (cls *Cluster) IsOriginMasterDiscoveryNode(nodeId string) bool {
|
|||||||
return cls.getOriginMasterDiscoveryNodeInfo(nodeId) != nil
|
return cls.getOriginMasterDiscoveryNodeInfo(nodeId) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cls *Cluster) IsLocalMasterNodeId(nodeId string) bool {
|
|
||||||
if cls.discoveryInfo.Origin == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return cls.discoveryInfo.Origin.LocalMasterNodeId == nodeId
|
|
||||||
}
|
|
||||||
func (cls *Cluster) getOriginMasterDiscoveryNodeInfo(nodeId string) *NodeInfo {
|
func (cls *Cluster) getOriginMasterDiscoveryNodeInfo(nodeId string) *NodeInfo {
|
||||||
if cls.discoveryInfo.Origin == nil {
|
if cls.discoveryInfo.Origin == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(cls.discoveryInfo.Origin.MasterNodeList); i++ {
|
for i := 0; i < len(cls.discoveryInfo.Origin.MasterNodeList); i++ {
|
||||||
|
if cls.discoveryInfo.Origin.MasterNodeList[i].NodeId == nodeId {
|
||||||
|
|
||||||
if cls.discoveryInfo.Origin.MasterNodeList[i].NodeId == nodeId {
|
|
||||||
return &cls.discoveryInfo.Origin.MasterNodeList[i]
|
return &cls.discoveryInfo.Origin.MasterNodeList[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,27 +16,27 @@ import (
|
|||||||
|
|
||||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
|
|
||||||
type EtcdList struct {
|
type EtcdList struct {
|
||||||
LocalNetworkName string // 如果配置,则为本地网络,必需配置一个本地网络
|
NetworkName []string
|
||||||
NeighborNetworkName []string
|
Endpoints []string
|
||||||
Endpoints []string
|
UserName string
|
||||||
UserName string
|
Password string
|
||||||
Password string
|
Cert string
|
||||||
Cert string
|
CertKey string
|
||||||
CertKey string
|
Ca string
|
||||||
Ca string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EtcdDiscovery struct {
|
type EtcdDiscovery struct {
|
||||||
DialTimeoutMillisecond time.Duration
|
DialTimeoutMillisecond time.Duration
|
||||||
TTLSecond int64
|
TTLSecond int64
|
||||||
EtcdList []EtcdList
|
|
||||||
|
EtcdList []EtcdList
|
||||||
}
|
}
|
||||||
|
|
||||||
type OriginDiscovery struct {
|
type OriginDiscovery struct {
|
||||||
TTLSecond int64
|
TTLSecond int64
|
||||||
LocalMasterNodeId string
|
MasterNodeList []NodeInfo
|
||||||
MasterNodeList []NodeInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiscoveryType int
|
type DiscoveryType int
|
||||||
@@ -72,7 +72,7 @@ type NodeInfoList struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validConfigFile(f string) bool {
|
func validConfigFile(f string) bool {
|
||||||
return strings.HasSuffix(f, ".json") || strings.HasSuffix(f, ".yml") || strings.HasSuffix(f, ".yaml")
|
return strings.HasSuffix(f, ".json")|| strings.HasSuffix(f, ".yml") || strings.HasSuffix(f, ".yaml")
|
||||||
}
|
}
|
||||||
|
|
||||||
func yamlToJson(data []byte, v interface{}) ([]byte, error) {
|
func yamlToJson(data []byte, v interface{}) ([]byte, error) {
|
||||||
@@ -131,20 +131,21 @@ func (d *DiscoveryInfo) setEtcd(etcd *EtcdDiscovery) error {
|
|||||||
return fmt.Errorf("repeat configuration of Discovery")
|
return fmt.Errorf("repeat configuration of Discovery")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Endpoints不允许重复
|
||||||
|
mapAddr := make(map[string]struct{})
|
||||||
for _, n := range etcd.EtcdList {
|
for _, n := range etcd.EtcdList {
|
||||||
//networkName不允许重复
|
for _, endPoint := range n.Endpoints {
|
||||||
mapNetworkName := make(map[string]struct{})
|
if _, ok := mapAddr[endPoint]; ok == true {
|
||||||
if n.LocalNetworkName != "" {
|
return fmt.Errorf("etcd discovery config Etcd.EtcdList.Endpoints %+v is repeat", endPoint)
|
||||||
if _, ok := mapNetworkName[n.LocalNetworkName]; ok == true {
|
|
||||||
return fmt.Errorf("etcd discovery config Etcd.EtcdList.LocalNetworkName %+v is repeat", n.LocalNetworkName)
|
|
||||||
}
|
}
|
||||||
mapNetworkName[n.LocalNetworkName] = struct{}{}
|
mapAddr[endPoint] = struct{}{}
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, netName := range n.NeighborNetworkName {
|
//networkName不允许重复
|
||||||
|
mapNetworkName := make(map[string]struct{})
|
||||||
|
for _, netName := range n.NetworkName {
|
||||||
if _, ok := mapNetworkName[netName]; ok == true {
|
if _, ok := mapNetworkName[netName]; ok == true {
|
||||||
return fmt.Errorf("etcd discovery config Etcd.EtcdList.NetworkName %+v is repeat", netName)
|
return fmt.Errorf("etcd discovery config Etcd.EtcdList.NetworkName %+v is repeat", n.NetworkName)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapNetworkName[netName] = struct{}{}
|
mapNetworkName[netName] = struct{}{}
|
||||||
@@ -274,7 +275,7 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
|||||||
var rpcMode RpcMode
|
var rpcMode RpcMode
|
||||||
|
|
||||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||||
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error) error {
|
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error)error {
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -339,7 +340,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
|||||||
nodeService := map[string]interface{}{}
|
nodeService := map[string]interface{}{}
|
||||||
|
|
||||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||||
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error) error {
|
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error)error{
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -431,7 +432,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cls *Cluster) parseLocalCfg() error {
|
func (cls *Cluster) parseLocalCfg() error{
|
||||||
rpcInfo := NodeRpcInfo{}
|
rpcInfo := NodeRpcInfo{}
|
||||||
rpcInfo.nodeInfo = cls.localNodeInfo
|
rpcInfo.nodeInfo = cls.localNodeInfo
|
||||||
rpcInfo.client = rpc.NewLClient(rpcInfo.nodeInfo.NodeId, &cls.callSet)
|
rpcInfo.client = rpc.NewLClient(rpcInfo.nodeInfo.NodeId, &cls.callSet)
|
||||||
@@ -453,7 +454,7 @@ func (cls *Cluster) parseLocalCfg() error {
|
|||||||
cls.mapServiceNode[serviceName] = make(map[string]struct{})
|
cls.mapServiceNode[serviceName] = make(map[string]struct{})
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId]; ok {
|
if _,ok:=cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId];ok {
|
||||||
return fmt.Errorf("duplicate service %s is configured in node %s", serviceName, cls.localNodeInfo.NodeId)
|
return fmt.Errorf("duplicate service %s is configured in node %s", serviceName, cls.localNodeInfo.NodeId)
|
||||||
}
|
}
|
||||||
cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId] = struct{}{}
|
cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId] = struct{}{}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ const (
|
|||||||
Sys_Event_Tcp EventType = -3
|
Sys_Event_Tcp EventType = -3
|
||||||
Sys_Event_Http_Event EventType = -4
|
Sys_Event_Http_Event EventType = -4
|
||||||
Sys_Event_WebSocket EventType = -5
|
Sys_Event_WebSocket EventType = -5
|
||||||
Sys_Event_Kcp EventType = -6
|
Sys_Event_Kcp EventType = -6
|
||||||
Sys_Event_Node_Conn_Event EventType = -7
|
Sys_Event_Node_Conn_Event EventType = -7
|
||||||
Sys_Event_Nats_Conn_Event EventType = -8
|
Sys_Event_Nats_Conn_Event EventType = -8
|
||||||
Sys_Event_DiscoverService EventType = -9
|
Sys_Event_DiscoverService EventType = -9
|
||||||
@@ -18,6 +18,6 @@ const (
|
|||||||
Sys_Event_EtcdDiscovery EventType = -11
|
Sys_Event_EtcdDiscovery EventType = -11
|
||||||
Sys_Event_Gin_Event EventType = -12
|
Sys_Event_Gin_Event EventType = -12
|
||||||
Sys_Event_FrameTick EventType = -13
|
Sys_Event_FrameTick EventType = -13
|
||||||
Sys_Event_ReloadBlueprint EventType = -14
|
|
||||||
Sys_Event_User_Define EventType = 1
|
Sys_Event_User_Define EventType = 1
|
||||||
)
|
)
|
||||||
|
|||||||
17
go.mod
17
go.mod
@@ -1,18 +1,16 @@
|
|||||||
module github.com/duanhf2012/origin/v2
|
module github.com/duanhf2012/origin/v2
|
||||||
|
|
||||||
go 1.22
|
go 1.22
|
||||||
|
toolchain go1.23.7
|
||||||
toolchain go1.22.7
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/IBM/sarama v1.43.3
|
github.com/IBM/sarama v1.43.3
|
||||||
github.com/duanhf2012/rotatelogs v0.0.0-20250124024205-39765c212d8a
|
github.com/duanhf2012/rotatelogs v0.0.0-20250124024205-39765c212d8a
|
||||||
github.com/gin-gonic/gin v1.10.0
|
github.com/gin-gonic/gin v1.10.0
|
||||||
github.com/go-sql-driver/mysql v1.6.0
|
github.com/go-sql-driver/mysql v1.6.0
|
||||||
github.com/goccy/go-json v0.10.2
|
|
||||||
github.com/gomodule/redigo v1.8.8
|
github.com/gomodule/redigo v1.8.8
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.0
|
||||||
github.com/json-iterator/go v1.1.12
|
github.com/json-iterator/go v1.1.12
|
||||||
github.com/nats-io/nats.go v1.34.1
|
github.com/nats-io/nats.go v1.34.1
|
||||||
github.com/pierrec/lz4/v4 v4.1.21
|
github.com/pierrec/lz4/v4 v4.1.21
|
||||||
@@ -45,6 +43,7 @@ require (
|
|||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||||
github.com/go-stack/stack v1.8.0 // indirect
|
github.com/go-stack/stack v1.8.0 // indirect
|
||||||
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/gogo/protobuf v1.3.2 // indirect
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
github.com/golang/protobuf v1.5.4 // indirect
|
github.com/golang/protobuf v1.5.4 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
@@ -84,11 +83,11 @@ require (
|
|||||||
go.etcd.io/etcd/client/pkg/v3 v3.5.13 // indirect
|
go.etcd.io/etcd/client/pkg/v3 v3.5.13 // indirect
|
||||||
go.uber.org/multierr v1.10.0 // indirect
|
go.uber.org/multierr v1.10.0 // indirect
|
||||||
golang.org/x/arch v0.8.0 // indirect
|
golang.org/x/arch v0.8.0 // indirect
|
||||||
golang.org/x/crypto v0.26.0 // indirect
|
golang.org/x/crypto v0.35.0 // indirect
|
||||||
golang.org/x/net v0.28.0 // indirect
|
golang.org/x/net v0.36.0 // indirect
|
||||||
golang.org/x/sync v0.8.0 // indirect
|
golang.org/x/sync v0.11.0 // indirect
|
||||||
golang.org/x/sys v0.23.0 // indirect
|
golang.org/x/sys v0.30.0 // indirect
|
||||||
golang.org/x/text v0.17.0 // indirect
|
golang.org/x/text v0.22.0 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // 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/api v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
|
||||||
|
|||||||
24
go.sum
24
go.sum
@@ -88,8 +88,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||||
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||||
@@ -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-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.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.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
|
||||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
|
||||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
|
||||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
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-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
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.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.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.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
|
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
|
||||||
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
|
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
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-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/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-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-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.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
|
||||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
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-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
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.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.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.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.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
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-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
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.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.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.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.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
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-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
|
|||||||
@@ -3,13 +3,12 @@ package network
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type WSServer struct {
|
type WSServer struct {
|
||||||
@@ -17,16 +16,13 @@ type WSServer struct {
|
|||||||
MaxConnNum int
|
MaxConnNum int
|
||||||
PendingWriteNum int
|
PendingWriteNum int
|
||||||
MaxMsgLen uint32
|
MaxMsgLen uint32
|
||||||
|
HTTPTimeout time.Duration
|
||||||
CertFile string
|
CertFile string
|
||||||
KeyFile string
|
KeyFile string
|
||||||
NewAgent func(*WSConn) Agent
|
NewAgent func(*WSConn) Agent
|
||||||
ln net.Listener
|
ln net.Listener
|
||||||
handler *WSHandler
|
handler *WSHandler
|
||||||
messageType int
|
messageType int
|
||||||
|
|
||||||
HandshakeTimeout time.Duration
|
|
||||||
ReadTimeout time.Duration
|
|
||||||
WriteTimeout time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WSHandler struct {
|
type WSHandler struct {
|
||||||
@@ -77,22 +73,9 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
handler.conns[conn] = struct{}{}
|
handler.conns[conn] = struct{}{}
|
||||||
handler.mutexConns.Unlock()
|
handler.mutexConns.Unlock()
|
||||||
c, ok := conn.NetConn().(*net.TCPConn)
|
|
||||||
if !ok {
|
|
||||||
tlsConn, ok := conn.NetConn().(*tls.Conn)
|
|
||||||
if !ok {
|
|
||||||
log.Error("conn error")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c, ok = tlsConn.NetConn().(*net.TCPConn)
|
|
||||||
if !ok {
|
|
||||||
log.Error("conn error")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
c.SetLinger(0)
|
conn.UnderlyingConn().(*net.TCPConn).SetLinger(0)
|
||||||
c.SetNoDelay(true)
|
conn.UnderlyingConn().(*net.TCPConn).SetNoDelay(true)
|
||||||
wsConn := newWSConn(conn, r.Header, handler.pendingWriteNum, handler.maxMsgLen, handler.messageType)
|
wsConn := newWSConn(conn, r.Header, handler.pendingWriteNum, handler.maxMsgLen, handler.messageType)
|
||||||
agent := handler.newAgent(wsConn)
|
agent := handler.newAgent(wsConn)
|
||||||
agent.Run()
|
agent.Run()
|
||||||
@@ -131,19 +114,10 @@ func (server *WSServer) Start() error {
|
|||||||
server.MaxMsgLen = 4096
|
server.MaxMsgLen = 4096
|
||||||
log.Info("invalid MaxMsgLen", log.Uint32("reset", server.MaxMsgLen))
|
log.Info("invalid MaxMsgLen", log.Uint32("reset", server.MaxMsgLen))
|
||||||
}
|
}
|
||||||
if server.HandshakeTimeout <= 0 {
|
if server.HTTPTimeout <= 0 {
|
||||||
server.HandshakeTimeout = 15 * time.Second
|
server.HTTPTimeout = 10 * time.Second
|
||||||
log.Info("invalid HandshakeTimeout", log.Duration("reset", server.HandshakeTimeout))
|
log.Info("invalid HTTPTimeout", log.Duration("reset", server.HTTPTimeout))
|
||||||
}
|
}
|
||||||
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 {
|
if server.NewAgent == nil {
|
||||||
log.Error("NewAgent must not be nil")
|
log.Error("NewAgent must not be nil")
|
||||||
return errors.New("NewAgent must not be nil")
|
return errors.New("NewAgent must not be nil")
|
||||||
@@ -172,7 +146,7 @@ func (server *WSServer) Start() error {
|
|||||||
conns: make(WebsocketConnSet),
|
conns: make(WebsocketConnSet),
|
||||||
messageType: server.messageType,
|
messageType: server.messageType,
|
||||||
upgrader: websocket.Upgrader{
|
upgrader: websocket.Upgrader{
|
||||||
HandshakeTimeout: server.HandshakeTimeout,
|
HandshakeTimeout: server.HTTPTimeout,
|
||||||
CheckOrigin: func(_ *http.Request) bool { return true },
|
CheckOrigin: func(_ *http.Request) bool { return true },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -180,8 +154,8 @@ func (server *WSServer) Start() error {
|
|||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: server.Addr,
|
Addr: server.Addr,
|
||||||
Handler: server.handler,
|
Handler: server.handler,
|
||||||
ReadTimeout: server.ReadTimeout,
|
ReadTimeout: server.HTTPTimeout,
|
||||||
WriteTimeout: server.WriteTimeout,
|
WriteTimeout: server.HTTPTimeout,
|
||||||
MaxHeaderBytes: 1024,
|
MaxHeaderBytes: 1024,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func init() {
|
|||||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, SingleStop, SignalRetire)
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, SingleStop, SignalRetire)
|
||||||
|
|
||||||
console.RegisterCommandBool("help", false, "<-help> This help.", usage)
|
console.RegisterCommandBool("help", false, "<-help> This help.", usage)
|
||||||
console.RegisterCommandString("name", "", "<-name nodeName> node's name.", setName)
|
console.RegisterCommandString("name", "", "<-name nodeName> Node's name.", setName)
|
||||||
console.RegisterCommandString("start", "", "<-start nodeid=nodeid> Run originserver.", startNode)
|
console.RegisterCommandString("start", "", "<-start nodeid=nodeid> Run originserver.", startNode)
|
||||||
console.RegisterCommandString("stop", "", "<-stop nodeid=nodeid> Stop originserver process.", stopNode)
|
console.RegisterCommandString("stop", "", "<-stop nodeid=nodeid> Stop originserver process.", stopNode)
|
||||||
console.RegisterCommandString("retire", "", "<-retire nodeid=nodeid> retire originserver process.", retireNode)
|
console.RegisterCommandString("retire", "", "<-retire nodeid=nodeid> retire originserver process.", retireNode)
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
package blueprintmodule
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/duanhf2012/origin/v2/service"
|
|
||||||
"github.com/duanhf2012/origin/v2/util/blueprint"
|
|
||||||
"sync/atomic"
|
|
||||||
)
|
|
||||||
|
|
||||||
type BlueprintModule struct {
|
|
||||||
service.Module
|
|
||||||
|
|
||||||
bp blueprint.Blueprint
|
|
||||||
|
|
||||||
execDefFilePath string
|
|
||||||
graphFilePath string
|
|
||||||
|
|
||||||
seedGraphID int64
|
|
||||||
|
|
||||||
mapGraph map[int64]blueprint.IGraph
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *BlueprintModule) Init(execDefFilePath string, graphFilePath string) error {
|
|
||||||
m.execDefFilePath = execDefFilePath
|
|
||||||
m.graphFilePath = graphFilePath
|
|
||||||
|
|
||||||
m.mapGraph = make(map[int64]blueprint.IGraph, 1024)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *BlueprintModule) OnInit() error {
|
|
||||||
if m.execDefFilePath == "" || m.graphFilePath == "" {
|
|
||||||
return fmt.Errorf("execDefFilePath or graphFilePath is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
m.seedGraphID = 1
|
|
||||||
return m.bp.Init(m.execDefFilePath, m.graphFilePath, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *BlueprintModule) CreateGraph(graphName string) int64 {
|
|
||||||
graphID := atomic.AddInt64(&m.seedGraphID, 1)
|
|
||||||
graph := m.bp.Create(graphName, graphID)
|
|
||||||
if graph == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
m.mapGraph[graphID] = graph
|
|
||||||
|
|
||||||
return graphID
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *BlueprintModule) GetGraph(graphID int64) (blueprint.IGraph, error) {
|
|
||||||
graph, ok := m.mapGraph[graphID]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("graph not found,graphID:%d", graphID)
|
|
||||||
}
|
|
||||||
return graph, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *BlueprintModule) Do(graphID int64, entranceID int64, args ...any) error {
|
|
||||||
graph, err := m.GetGraph(graphID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return graph.Do(entranceID, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *BlueprintModule) TriggerEvent(graphID int64, eventID int64, args ...any) error {
|
|
||||||
graph, err := m.GetGraph(graphID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return graph.Do(eventID, args...)
|
|
||||||
}
|
|
||||||
@@ -248,7 +248,7 @@ func (ch *ConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession
|
|||||||
select {
|
select {
|
||||||
case msg := <-claim.Messages():
|
case msg := <-claim.Messages():
|
||||||
if msg == nil {
|
if msg == nil {
|
||||||
log.SWarn("claim will exit", log.Any("topic", claim.Topic()), log.Any("Partition", claim.Partition()))
|
log.SWarning("claim will exit", log.Any("topic", claim.Topic()), log.Any("Partition", claim.Partition()))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ch.AppendMsg(session, msg)
|
ch.AppendMsg(session, msg)
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ func (p *Producer) asyncRun() {
|
|||||||
asyncReturn := sm.Metadata.(*AsyncReturn)
|
asyncReturn := sm.Metadata.(*AsyncReturn)
|
||||||
asyncReturn.chanReturn <- asyncReturn
|
asyncReturn.chanReturn <- asyncReturn
|
||||||
case em := <-p.Errors():
|
case em := <-p.Errors():
|
||||||
log.Error("async kafkamodule error", log.ErrorField("err", em.Err))
|
log.Error("async kafkamodule error", log.ErrorAttr("err", em.Err))
|
||||||
if em.Msg.Metadata == nil {
|
if em.Msg.Metadata == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ func (m *MySQLModule) Begin() (*Tx, error) {
|
|||||||
var txDBModule Tx
|
var txDBModule Tx
|
||||||
txDb, err := m.db.Begin()
|
txDb, err := m.db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Begin error", log.ErrorField("err",err))
|
log.Error("Begin error:%s", err.Error())
|
||||||
return &txDBModule, err
|
return &txDBModule, err
|
||||||
}
|
}
|
||||||
txDBModule.slowDuration = m.slowDuration
|
txDBModule.slowDuration = m.slowDuration
|
||||||
@@ -155,7 +155,7 @@ func (m *MySQLModule) runPing() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-m.pingCoroutine.pintExit:
|
case <-m.pingCoroutine.pintExit:
|
||||||
log.Error("RunPing stopping",log.String("url", m.url),log.String("dbname", m.dbname))
|
log.Error("RunPing stopping %s...", fmt.Sprintf("%T", m))
|
||||||
return
|
return
|
||||||
case <-m.pingCoroutine.tickerPing.C:
|
case <-m.pingCoroutine.tickerPing.C:
|
||||||
if m.db != nil {
|
if m.db != nil {
|
||||||
@@ -221,12 +221,12 @@ func query(slowDuration time.Duration, db dbControl, strQuery string, args ...in
|
|||||||
datasetList.blur = true
|
datasetList.blur = true
|
||||||
|
|
||||||
if checkArgs(args) != nil {
|
if checkArgs(args) != nil {
|
||||||
log.Error("CheckArgs is error",log.String("sql",strQuery))
|
log.Error("CheckArgs is error :%s", strQuery)
|
||||||
return &datasetList, fmt.Errorf("checkArgs is error")
|
return &datasetList, fmt.Errorf("checkArgs is error")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db == nil {
|
if db == nil {
|
||||||
log.Error("cannot connect database",log.String("sql", strQuery))
|
log.Error("cannot connect database:%s", strQuery)
|
||||||
return &datasetList, fmt.Errorf("cannot connect database")
|
return &datasetList, fmt.Errorf("cannot connect database")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,10 +235,10 @@ func query(slowDuration time.Duration, db dbControl, strQuery string, args ...in
|
|||||||
timeFuncPass := time.Since(TimeFuncStart)
|
timeFuncPass := time.Since(TimeFuncStart)
|
||||||
|
|
||||||
if checkSlow(slowDuration, timeFuncPass) {
|
if checkSlow(slowDuration, timeFuncPass) {
|
||||||
log.Error("Query slow",log.Int64("time_ms",timeFuncPass.Milliseconds()),log.String("sql", strQuery), log.Any("args",args))
|
log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", timeFuncPass, strQuery, args)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Query error", log.String("sql",strQuery),log.ErrorField("err",err))
|
log.Error("Query:%s(%v)", strQuery, err)
|
||||||
if rows != nil {
|
if rows != nil {
|
||||||
rows.Close()
|
rows.Close()
|
||||||
}
|
}
|
||||||
@@ -278,8 +278,8 @@ func query(slowDuration time.Duration, db dbControl, strQuery string, args ...in
|
|||||||
hasRet := rows.NextResultSet()
|
hasRet := rows.NextResultSet()
|
||||||
|
|
||||||
if hasRet == false {
|
if hasRet == false {
|
||||||
if rowErr :=rows.Err();rowErr != nil {
|
if rows.Err() != nil {
|
||||||
log.Error("NextResultSet error", log.String("sql",strQuery), log.ErrorField("err",rowErr))
|
log.Error("Query:%s(%+v)", strQuery, rows)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -291,12 +291,12 @@ func query(slowDuration time.Duration, db dbControl, strQuery string, args ...in
|
|||||||
func exec(slowDuration time.Duration, db dbControl, strSql string, args ...interface{}) (*DBResult, error) {
|
func exec(slowDuration time.Duration, db dbControl, strSql string, args ...interface{}) (*DBResult, error) {
|
||||||
ret := &DBResult{}
|
ret := &DBResult{}
|
||||||
if db == nil {
|
if db == nil {
|
||||||
log.Error("cannot connect database", log.String("sql",strSql))
|
log.Error("cannot connect database:%s", strSql)
|
||||||
return ret, fmt.Errorf("cannot connect database")
|
return ret, fmt.Errorf("cannot connect database")
|
||||||
}
|
}
|
||||||
|
|
||||||
if checkArgs(args) != nil {
|
if checkArgs(args) != nil {
|
||||||
log.Error("CheckArgs is error", log.String("sql",strSql))
|
log.Error("CheckArgs is error :%s", strSql)
|
||||||
return ret, fmt.Errorf("checkArgs is error")
|
return ret, fmt.Errorf("checkArgs is error")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,10 +304,10 @@ func exec(slowDuration time.Duration, db dbControl, strSql string, args ...inter
|
|||||||
res, err := db.Exec(strSql, args...)
|
res, err := db.Exec(strSql, args...)
|
||||||
timeFuncPass := time.Since(TimeFuncStart)
|
timeFuncPass := time.Since(TimeFuncStart)
|
||||||
if checkSlow(slowDuration, timeFuncPass) {
|
if checkSlow(slowDuration, timeFuncPass) {
|
||||||
log.Error("Exec slow",log.Int64("time_ms",timeFuncPass.Milliseconds()),log.String("sql",strSql),log.Any("args",args) )
|
log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", timeFuncPass, strSql, args)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Exec error",log.String("sql",strSql),log.ErrorField("err", err))
|
log.Error("Exec:%s(%v)", strSql, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
package mysqlmodule
|
|
||||||
@@ -2,15 +2,13 @@ package wsmodule
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/duanhf2012/origin/v2/event"
|
"github.com/duanhf2012/origin/v2/event"
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
"github.com/duanhf2012/origin/v2/network"
|
"github.com/duanhf2012/origin/v2/network"
|
||||||
"github.com/duanhf2012/origin/v2/network/processor"
|
"github.com/duanhf2012/origin/v2/network/processor"
|
||||||
"github.com/duanhf2012/origin/v2/service"
|
"github.com/duanhf2012/origin/v2/service"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WSModule struct {
|
type WSModule struct {
|
||||||
@@ -36,12 +34,6 @@ type WSCfg struct {
|
|||||||
PendingWriteNum int
|
PendingWriteNum int
|
||||||
MaxMsgLen uint32
|
MaxMsgLen uint32
|
||||||
LittleEndian bool //是否小端序
|
LittleEndian bool //是否小端序
|
||||||
KeyFile string
|
|
||||||
CertFile string
|
|
||||||
|
|
||||||
HandshakeTimeoutSecond time.Duration
|
|
||||||
ReadTimeoutSecond time.Duration
|
|
||||||
WriteTimeoutSecond time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WSPackType int8
|
type WSPackType int8
|
||||||
@@ -69,22 +61,14 @@ func (ws *WSModule) OnInit() error {
|
|||||||
ws.WSServer.PendingWriteNum = ws.wsCfg.PendingWriteNum
|
ws.WSServer.PendingWriteNum = ws.wsCfg.PendingWriteNum
|
||||||
ws.WSServer.MaxMsgLen = ws.wsCfg.MaxMsgLen
|
ws.WSServer.MaxMsgLen = ws.wsCfg.MaxMsgLen
|
||||||
ws.WSServer.Addr = ws.wsCfg.ListenAddr
|
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 != "" {
|
//3.设置解析处理器
|
||||||
ws.WSServer.KeyFile = ws.wsCfg.KeyFile
|
|
||||||
ws.WSServer.CertFile = ws.wsCfg.CertFile
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置解析处理器
|
|
||||||
ws.process.SetByteOrder(ws.wsCfg.LittleEndian)
|
ws.process.SetByteOrder(ws.wsCfg.LittleEndian)
|
||||||
|
|
||||||
ws.mapClient = make(map[string]*WSClient, ws.WSServer.MaxConnNum)
|
ws.mapClient = make(map[string]*WSClient, ws.WSServer.MaxConnNum)
|
||||||
ws.WSServer.NewAgent = ws.NewWSClient
|
ws.WSServer.NewAgent = ws.NewWSClient
|
||||||
|
|
||||||
// 设置网络事件处理
|
//4.设置网络事件处理
|
||||||
ws.GetEventProcessor().RegEventReceiverFunc(event.Sys_Event_WebSocket, ws.GetEventHandler(), ws.wsEventHandler)
|
ws.GetEventProcessor().RegEventReceiverFunc(event.Sys_Event_WebSocket, ws.GetEventHandler(), ws.wsEventHandler)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -119,6 +103,7 @@ func (ws *WSModule) recyclerReaderBytes([]byte) {
|
|||||||
func (ws *WSModule) NewWSClient(conn *network.WSConn) network.Agent {
|
func (ws *WSModule) NewWSClient(conn *network.WSConn) network.Agent {
|
||||||
ws.mapClientLocker.Lock()
|
ws.mapClientLocker.Lock()
|
||||||
defer ws.mapClientLocker.Unlock()
|
defer ws.mapClientLocker.Unlock()
|
||||||
|
|
||||||
pClient := &WSClient{wsConn: conn, id: primitive.NewObjectID().Hex()}
|
pClient := &WSClient{wsConn: conn, id: primitive.NewObjectID().Hex()}
|
||||||
pClient.wsModule = ws
|
pClient.wsModule = ws
|
||||||
ws.mapClient[pClient.id] = pClient
|
ws.mapClient[pClient.id] = pClient
|
||||||
@@ -158,22 +143,6 @@ func (ws *WSModule) GetProcessor() processor.IRawProcessor {
|
|||||||
return ws.process
|
return ws.process
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WSModule) GetClientHeader(clientId string,key string) string {
|
|
||||||
ws.mapClientLocker.Lock()
|
|
||||||
defer ws.mapClientLocker.Unlock()
|
|
||||||
|
|
||||||
pClient, ok := ws.mapClient[clientId]
|
|
||||||
if ok == false || pClient.wsConn == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
if pClient.wsConn.GetHeader() == nil {
|
|
||||||
log.Warn("clientId header is nil", log.String("clientId", clientId))
|
|
||||||
}
|
|
||||||
|
|
||||||
return pClient.wsConn.GetHeader().Get(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ws *WSModule) GetClientIp(clientId string) string {
|
func (ws *WSModule) GetClientIp(clientId string) string {
|
||||||
ws.mapClientLocker.Lock()
|
ws.mapClientLocker.Lock()
|
||||||
defer ws.mapClientLocker.Unlock()
|
defer ws.mapClientLocker.Unlock()
|
||||||
|
|||||||
@@ -104,11 +104,11 @@ func (cs *CustomerSubscriber) UnSubscribe() {
|
|||||||
func (cs *CustomerSubscriber) LoadLastIndex() {
|
func (cs *CustomerSubscriber) LoadLastIndex() {
|
||||||
for {
|
for {
|
||||||
if atomic.LoadInt32(&cs.isStop) != 0 {
|
if atomic.LoadInt32(&cs.isStop) != 0 {
|
||||||
log.SInfo("topic ", cs.topic, " out of subscription")
|
log.Info("topic ", cs.topic, " out of subscription")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SInfo("customer ", cs.customerId, " start load last index ")
|
log.Info("customer ", cs.customerId, " start load last index ")
|
||||||
lastIndex, ret := cs.subscriber.dataPersist.LoadCustomerIndex(cs.topic, cs.customerId)
|
lastIndex, ret := cs.subscriber.dataPersist.LoadCustomerIndex(cs.topic, cs.customerId)
|
||||||
if ret == true {
|
if ret == true {
|
||||||
if lastIndex > 0 {
|
if lastIndex > 0 {
|
||||||
@@ -116,18 +116,18 @@ func (cs *CustomerSubscriber) LoadLastIndex() {
|
|||||||
} else {
|
} else {
|
||||||
//否则直接使用客户端发回来的
|
//否则直接使用客户端发回来的
|
||||||
}
|
}
|
||||||
log.SInfo("customer ", cs.customerId, " load finish,start index is ", cs.StartIndex)
|
log.Info("customer ", cs.customerId, " load finish,start index is ", cs.StartIndex)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SInfo("customer ", cs.customerId, " load last index is fail...")
|
log.Info("customer ", cs.customerId, " load last index is fail...")
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CustomerSubscriber) SubscribeRun() {
|
func (cs *CustomerSubscriber) SubscribeRun() {
|
||||||
defer cs.subscriber.queueWait.Done()
|
defer cs.subscriber.queueWait.Done()
|
||||||
log.SInfo("topic ", cs.topic, " start subscription")
|
log.Info("topic ", cs.topic, " start subscription")
|
||||||
|
|
||||||
//加载之前的位置
|
//加载之前的位置
|
||||||
if cs.subscribeMethod == MethodLast {
|
if cs.subscribeMethod == MethodLast {
|
||||||
@@ -136,7 +136,7 @@ func (cs *CustomerSubscriber) SubscribeRun() {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
if atomic.LoadInt32(&cs.isStop) != 0 {
|
if atomic.LoadInt32(&cs.isStop) != 0 {
|
||||||
log.SInfo("topic ", cs.topic, " out of subscription")
|
log.Info("topic ", cs.topic, " out of subscription")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,14 +146,14 @@ func (cs *CustomerSubscriber) SubscribeRun() {
|
|||||||
|
|
||||||
//todo 检测退出
|
//todo 检测退出
|
||||||
if cs.subscribe() == false {
|
if cs.subscribe() == false {
|
||||||
log.SInfo("topic ", cs.topic, " out of subscription")
|
log.Info("topic ", cs.topic, " out of subscription")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除订阅关系
|
//删除订阅关系
|
||||||
cs.subscriber.removeCustomer(cs.customerId, cs)
|
cs.subscriber.removeCustomer(cs.customerId, cs)
|
||||||
log.SInfo("topic ", cs.topic, " unsubscription")
|
log.Info("topic ", cs.topic, " unsubscription")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CustomerSubscriber) subscribe() bool {
|
func (cs *CustomerSubscriber) subscribe() bool {
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ func (ms *MessageQueueService) ReadCfg() error {
|
|||||||
maxProcessTopicBacklogNum, ok := mapDBServiceCfg["MaxProcessTopicBacklogNum"]
|
maxProcessTopicBacklogNum, ok := mapDBServiceCfg["MaxProcessTopicBacklogNum"]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
ms.maxProcessTopicBacklogNum = DefaultMaxTopicBacklogNum
|
ms.maxProcessTopicBacklogNum = DefaultMaxTopicBacklogNum
|
||||||
log.SInfo("MaxProcessTopicBacklogNum config is set to the default value of ", maxProcessTopicBacklogNum)
|
log.Info("MaxProcessTopicBacklogNum config is set to the default value of ", maxProcessTopicBacklogNum)
|
||||||
} else {
|
} else {
|
||||||
ms.maxProcessTopicBacklogNum = int32(maxProcessTopicBacklogNum.(float64))
|
ms.maxProcessTopicBacklogNum = int32(maxProcessTopicBacklogNum.(float64))
|
||||||
}
|
}
|
||||||
@@ -71,7 +71,7 @@ func (ms *MessageQueueService) ReadCfg() error {
|
|||||||
memoryQueueLen, ok := mapDBServiceCfg["MemoryQueueLen"]
|
memoryQueueLen, ok := mapDBServiceCfg["MemoryQueueLen"]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
ms.memoryQueueLen = DefaultMemoryQueueLen
|
ms.memoryQueueLen = DefaultMemoryQueueLen
|
||||||
log.SInfo("MemoryQueueLen config is set to the default value of ", DefaultMemoryQueueLen)
|
log.Info("MemoryQueueLen config is set to the default value of ", DefaultMemoryQueueLen)
|
||||||
} else {
|
} else {
|
||||||
ms.memoryQueueLen = int32(memoryQueueLen.(float64))
|
ms.memoryQueueLen = int32(memoryQueueLen.(float64))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -237,7 +237,7 @@ func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int
|
|||||||
defer cancelAll()
|
defer cancelAll()
|
||||||
err = cursor.All(ctxAll, &res)
|
err = cursor.All(ctxAll, &res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("find collect name error",log.String("topic",topic) ,log.ErrorField("err",err))
|
log.Error("find collect name ", topic, " is error", log.ErrorAttr("err", err))
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,7 +246,7 @@ func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int
|
|||||||
rawData, errM := bson.Marshal(res[i])
|
rawData, errM := bson.Marshal(res[i])
|
||||||
if errM != nil {
|
if errM != nil {
|
||||||
if errM != nil {
|
if errM != nil {
|
||||||
log.Error("Marshal error",log.String("topic",topic) , log.ErrorField("err", err))
|
log.Error("collect name ", topic, " Marshal is error", log.ErrorAttr("err", err))
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@@ -391,7 +391,7 @@ func (mp *MongoPersist) GetIndex(topicData *TopicData) uint64 {
|
|||||||
if e.Key == "_id" {
|
if e.Key == "_id" {
|
||||||
errC, seq := convertToNumber[uint64](e.Value)
|
errC, seq := convertToNumber[uint64](e.Value)
|
||||||
if errC != nil {
|
if errC != nil {
|
||||||
log.Error("value is error", log.ErrorField("err",errC), log.Any("val",e.Value))
|
log.Error("value is error:%s,%+v, ", errC.Error(), e.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
return seq
|
return seq
|
||||||
|
|||||||
@@ -56,9 +56,9 @@ func (ss *Subscriber) TopicSubscribe(rpcHandler rpc.IRpcHandler, subScribeType r
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ok == true {
|
if ok == true {
|
||||||
log.SInfo("repeat subscription for customer ", customerId)
|
log.Info("repeat subscription for customer ", customerId)
|
||||||
} else {
|
} else {
|
||||||
log.SInfo("subscription for customer ", customerId)
|
log.Info("subscription for customer ", customerId)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ func (ss *Subscriber) UnSubscribe(customerId string) {
|
|||||||
|
|
||||||
customerSubscriber, ok := ss.mapCustomer[customerId]
|
customerSubscriber, ok := ss.mapCustomer[customerId]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
log.SWarn("failed to unsubscribe customer ", customerId)
|
log.SWarning("failed to unsubscribe customer " + customerId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ func (tr *TopicRoom) Stop() {
|
|||||||
func (tr *TopicRoom) topicRoomRun() {
|
func (tr *TopicRoom) topicRoomRun() {
|
||||||
defer tr.queueWait.Done()
|
defer tr.queueWait.Done()
|
||||||
|
|
||||||
log.SInfo("topic room ", tr.topic, " is running..")
|
log.Info("topic room ", tr.topic, " is running..")
|
||||||
for {
|
for {
|
||||||
if atomic.LoadInt32(&tr.isStop) != 0 {
|
if atomic.LoadInt32(&tr.isStop) != 0 {
|
||||||
break
|
break
|
||||||
@@ -145,5 +145,5 @@ func (tr *TopicRoom) topicRoomRun() {
|
|||||||
}
|
}
|
||||||
tr.customerLocker.Unlock()
|
tr.customerLocker.Unlock()
|
||||||
|
|
||||||
log.SInfo("topic room ", tr.topic, " is stop")
|
log.Info("topic room ", tr.topic, " is stop")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,13 +142,13 @@ func (mp *MongoPersist) OnSetupRank(manual bool, rankSkip *RankSkip) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SInfo("start load rank ", rankSkip.GetRankName(), " from mongodb.")
|
log.Info("start load rank ", rankSkip.GetRankName(), " from mongodb.")
|
||||||
err := mp.loadFromDB(rankSkip.GetRankID(), rankSkip.GetRankName())
|
err := mp.loadFromDB(rankSkip.GetRankID(), rankSkip.GetRankName())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.SError("load from db is fail :%s", err.Error())
|
log.SError("load from db is fail :%s", err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.SInfo("finish load rank ", rankSkip.GetRankName(), " from mongodb.")
|
log.Info("finish load rank ", rankSkip.GetRankName(), " from mongodb.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,7 +296,7 @@ func (mp *MongoPersist) saveToDB() {
|
|||||||
buf := make([]byte, 4096)
|
buf := make([]byte, 4096)
|
||||||
l := runtime.Stack(buf, false)
|
l := runtime.Stack(buf, false)
|
||||||
errString := fmt.Sprint(r)
|
errString := fmt.Sprint(r)
|
||||||
log.StackError(string(buf[:l]), log.String("error", errString))
|
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -1,223 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sync/atomic"
|
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Blueprint struct {
|
|
||||||
execNodes []IExecNode // 注册的定义执行结点
|
|
||||||
execNodeList []func() IExecNode
|
|
||||||
execPool *ExecPool
|
|
||||||
graphPool *GraphPool
|
|
||||||
|
|
||||||
blueprintModule IBlueprintModule
|
|
||||||
mapGraph map[int64]IGraph
|
|
||||||
seedID int64
|
|
||||||
cancelTimer func(*uint64) bool
|
|
||||||
|
|
||||||
execDefFilePath string // 执行结点定义文件路径
|
|
||||||
graphFilePath string // 蓝图文件路径
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bm *Blueprint) RegisterExecNode(execNodeFunc func() IExecNode) {
|
|
||||||
bm.execNodeList = append(bm.execNodeList, execNodeFunc)
|
|
||||||
}
|
|
||||||
|
|
||||||
type IExecNodeType[T any] interface {
|
|
||||||
*T
|
|
||||||
IExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成一个泛型函数,返回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]())
|
|
||||||
bm.RegisterExecNode(NewExecNode[AppendIntReturn]())
|
|
||||||
bm.RegisterExecNode(NewExecNode[AppendStringReturn]())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 注册系统执行结点
|
|
||||||
bm.regSysNodes()
|
|
||||||
|
|
||||||
// 将注册的实际执行结点与innerExecNode进行关联
|
|
||||||
for _, newExec := range bm.execNodeList {
|
|
||||||
e := newExec()
|
|
||||||
if !execPool.Register(e) {
|
|
||||||
return fmt.Errorf("register exec failed,exec:%s", e.GetName())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 加载所有的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.execDefFilePath = execDefFilePath
|
|
||||||
bm.graphFilePath = graphFilePath
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bm *Blueprint) Create(graphName string) int64 {
|
|
||||||
if graphName == "" {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
graphID := atomic.AddInt64(&bm.seedID, 1)
|
|
||||||
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 {
|
|
||||||
graph := bm.mapGraph[graphID]
|
|
||||||
if graph == nil {
|
|
||||||
return fmt.Errorf("can not find graph:%d", graphID)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := graph.Do(eventID, args...)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
graph.Release()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool {
|
|
||||||
tId := *timerId
|
|
||||||
bm.cancelTimer(timerId)
|
|
||||||
|
|
||||||
graph := bm.mapGraph[graphID]
|
|
||||||
if graph == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
gr, ok := graph.(*Graph)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(gr.mapTimerID, tId)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
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)
|
|
||||||
//if err != nil {
|
|
||||||
// t.Fatalf("Do EntranceID_IntParam failed,err:%v", err)
|
|
||||||
//}
|
|
||||||
|
|
||||||
//graphTest2 := bp.Create("test2")
|
|
||||||
//
|
|
||||||
//err = graphTest2.Do(EntranceID_IntParam, 1, 2, 3)
|
|
||||||
//if err != nil {
|
|
||||||
// t.Fatalf("Do EntranceID_IntParam failed,err:%v", err)
|
|
||||||
//}
|
|
||||||
|
|
||||||
//graph := bp.Create("test1")
|
|
||||||
//err = graph.Do(EntranceID_IntParam, 1, 2, 3)
|
|
||||||
//if err != nil {
|
|
||||||
// t.Fatalf("do failed,err:%v", err)
|
|
||||||
//}
|
|
||||||
|
|
||||||
//graph.Release()
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
type ExecContext struct {
|
|
||||||
InputPorts []IPort
|
|
||||||
OutputPorts []IPort
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ec *ExecContext) Reset() {
|
|
||||||
*ec = ExecContext{}
|
|
||||||
}
|
|
||||||
@@ -1,581 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// IInnerExecNode 配置生成的结点
|
|
||||||
type IInnerExecNode interface {
|
|
||||||
GetName() string
|
|
||||||
SetExec(exec IExecNode)
|
|
||||||
IsInPortExec(index int) bool
|
|
||||||
IsOutPortExec(index int) bool
|
|
||||||
GetInPortCount() int
|
|
||||||
GetOutPortCount() int
|
|
||||||
CloneInOutPort() ([]IPort, []IPort)
|
|
||||||
|
|
||||||
GetInPort(index int) IPort
|
|
||||||
GetOutPort(index int) IPort
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 配置对应的基础信息+端口数据
|
|
||||||
type innerExecNode struct {
|
|
||||||
Name string
|
|
||||||
Title string
|
|
||||||
Package string
|
|
||||||
Description string
|
|
||||||
|
|
||||||
inPort []IPort // 下标即为portId
|
|
||||||
outPort []IPort // 下标即为portId
|
|
||||||
|
|
||||||
outPortParamStartIndex int // 输出参数的起始索引,用于排除执行出口
|
|
||||||
|
|
||||||
IExecNode // 实际注册的执行结点
|
|
||||||
}
|
|
||||||
|
|
||||||
type BaseExecNode struct {
|
|
||||||
*innerExecNode // 内部注册的执行结点
|
|
||||||
|
|
||||||
// 执行时初始化的数据
|
|
||||||
*ExecContext
|
|
||||||
gr *Graph
|
|
||||||
execNode *execNode
|
|
||||||
}
|
|
||||||
|
|
||||||
type InputConfig struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
PortType string `json:"type"`
|
|
||||||
DataType string `json:"data_type"`
|
|
||||||
HasInput bool `json:"has_input"`
|
|
||||||
PinWidget string `json:"pin_widget"`
|
|
||||||
PortId int `json:"port_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type OutputConfig struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
PortType string `json:"type"`
|
|
||||||
DataType string `json:"data_type"`
|
|
||||||
HasInput bool `json:"has_input"`
|
|
||||||
PortId int `json:"port_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type BaseExecConfig struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Package string `json:"package"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
IsPure bool `json:"is_pure"`
|
|
||||||
Inputs []InputConfig `json:"inputs"`
|
|
||||||
Outputs []OutputConfig `json:"outputs"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bc *BaseExecConfig) GetMaxInPortId() int {
|
|
||||||
maxPortId := -1
|
|
||||||
for i := range bc.Inputs {
|
|
||||||
if bc.Inputs[i].PortId > maxPortId {
|
|
||||||
maxPortId = bc.Inputs[i].PortId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return maxPortId
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bc *BaseExecConfig) GetMaxOutPortId() int {
|
|
||||||
maxPortId := -1
|
|
||||||
for i := range bc.Outputs {
|
|
||||||
if bc.Outputs[i].PortId > maxPortId {
|
|
||||||
maxPortId = bc.Outputs[i].PortId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return maxPortId
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) PrepareMaxInPortId(maxInPortId int) {
|
|
||||||
em.inPort = make([]IPort, maxInPortId+1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) PrepareMaxOutPortId(maxOutPortId int) {
|
|
||||||
em.outPort = make([]IPort, maxOutPortId+1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) SetInPortById(id int, port IPort) bool {
|
|
||||||
if id < 0 || id >= len(em.inPort) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
em.inPort[id] = port
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) SetOutPortById(id int, port IPort) bool {
|
|
||||||
if id < 0 || id >= len(em.outPort) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
em.outPort[id] = port
|
|
||||||
|
|
||||||
// 分析执行的
|
|
||||||
em.outPortParamStartIndex = -1
|
|
||||||
for i := range em.outPort {
|
|
||||||
if em.outPort[i] == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 遇到非Exec结点,即为输出参数开始位置
|
|
||||||
if !em.outPort[i].IsPortExec() {
|
|
||||||
em.outPortParamStartIndex = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) GetOutPortParamStartIndex() int {
|
|
||||||
return em.outPortParamStartIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) GetName() string {
|
|
||||||
return em.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) SetExec(exec IExecNode) {
|
|
||||||
em.IExecNode = exec
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) CloneInOutPort() ([]IPort, []IPort) {
|
|
||||||
inPorts := make([]IPort, 0, 2)
|
|
||||||
for _, port := range em.inPort {
|
|
||||||
if port == nil {
|
|
||||||
inPorts = append(inPorts, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
if port.IsPortExec() {
|
|
||||||
// 执行入口, 不需要克隆,占位处理
|
|
||||||
inPorts = append(inPorts, nil)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
inPorts = append(inPorts, port.Clone())
|
|
||||||
}
|
|
||||||
outPorts := make([]IPort, 0, 2)
|
|
||||||
|
|
||||||
for _, port := range em.outPort {
|
|
||||||
if port == nil {
|
|
||||||
outPorts = append(outPorts, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
if port.IsPortExec() {
|
|
||||||
outPorts = append(outPorts, nil)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
outPorts = append(outPorts, port.Clone())
|
|
||||||
}
|
|
||||||
|
|
||||||
return inPorts, outPorts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) IsInPortExec(index int) bool {
|
|
||||||
if index >= len(em.inPort) || index < 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return em.inPort[index].IsPortExec()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) IsOutPortExec(index int) bool {
|
|
||||||
if index >= len(em.outPort) || index < 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return em.outPort[index].IsPortExec()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) GetInPortCount() int {
|
|
||||||
return len(em.inPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) GetOutPortCount() int {
|
|
||||||
return len(em.outPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) GetInPort(index int) IPort {
|
|
||||||
if index >= len(em.inPort) || index < 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return em.inPort[index]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *innerExecNode) GetOutPort(index int) IPort {
|
|
||||||
if index >= len(em.outPort) || index < 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return em.outPort[index]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetVariableName() string {
|
|
||||||
return en.execNode.variableName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetBluePrintModule() IBlueprintModule {
|
|
||||||
return en.gr.IBlueprintModule
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) initInnerExecNode(innerNode *innerExecNode) {
|
|
||||||
en.innerExecNode = innerNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) getExecNodeInfo() (*ExecContext, *execNode) {
|
|
||||||
return en.ExecContext, en.execNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) setExecNodeInfo(c *ExecContext, e *execNode) {
|
|
||||||
en.ExecContext = c
|
|
||||||
en.execNode = e
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) initExecNode(gr *Graph, node *execNode) error {
|
|
||||||
ctx, ok := gr.context[node.Id]
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("node %s not found", node.Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
en.ExecContext = ctx
|
|
||||||
en.gr = gr
|
|
||||||
en.execNode = node
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetPorts() ([]IPort, []IPort) {
|
|
||||||
return en.InputPorts, en.OutputPorts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPort(index int) IPort {
|
|
||||||
if en.InputPorts == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if index >= len(en.InputPorts) || index < 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return en.InputPorts[index]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetOutPort(index int) IPort {
|
|
||||||
if en.OutputPorts == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if index >= len(en.OutputPorts) || index < 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return en.OutputPorts[index]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetOutPort(index int, val IPort) bool {
|
|
||||||
if index >= len(en.OutputPorts) || index < 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
en.OutputPorts[index].SetValue(val)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPortInt(index int) (Port_Int, bool) {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return port.GetInt()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPortFloat(index int) (Port_Float, bool) {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
return port.GetFloat()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPortStr(index int) (Port_Str, bool) {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
return port.GetStr()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPortArray(index int) (Port_Array, bool) {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
return port.GetArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPortArrayValInt(index int, idx int) (Port_Int, bool) {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
return port.GetArrayValInt(idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPortArrayValStr(idx int) (Port_Str, bool) {
|
|
||||||
port := en.GetInPort(idx)
|
|
||||||
if port == nil {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
return port.GetArrayValStr(idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPortBool(index int) (Port_Bool, bool) {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false, false
|
|
||||||
}
|
|
||||||
return port.GetBool()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetOutPortInt(index int) (Port_Int, bool) {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
return port.GetInt()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetOutPortFloat(index int) (Port_Float, bool) {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
return port.GetFloat()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetOutPortStr(index int) (Port_Str, bool) {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
return port.GetStr()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetOutPortArrayValInt(index int, idx int) (Port_Int, bool) {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
return port.GetArrayValInt(idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetOutPortArrayValStr(index int, idx int) (Port_Str, bool) {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
return port.GetArrayValStr(idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetOutPortBool(index int) (Port_Bool, bool) {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false, false
|
|
||||||
}
|
|
||||||
return port.GetBool()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetInPortInt(index int, val Port_Int) bool {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetInt(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetInPortFloat(index int, val Port_Float) bool {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetFloat(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetInPortStr(index int, val Port_Str) bool {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetStr(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetInBool(index int, val Port_Bool) bool {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetBool(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetInPortArrayValInt(index int, idx int, val Port_Int) bool {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetArrayValInt(idx, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetInPortArrayValStr(index int, idx int, val Port_Str) bool {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetArrayValStr(idx, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) AppendInPortArrayValInt(index int, val Port_Int) bool {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.AppendArrayValInt(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) AppendInPortArrayValStr(index int, val Port_Str) bool {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.AppendArrayValStr(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetInPortArrayLen(index int) Port_Int {
|
|
||||||
port := en.GetInPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return port.GetArrayLen()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetOutPortInt(index int, val Port_Int) bool {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetInt(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetOutPortFloat(index int, val Port_Float) bool {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetFloat(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetOutPortStr(index int, val Port_Str) bool {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetStr(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetOutPortBool(index int, val Port_Bool) bool {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetBool(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetOutPortArrayValInt(index int, idx int, val Port_Int) bool {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetArrayValInt(idx, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) SetOutPortArrayValStr(index int, idx int, val Port_Str) bool {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.SetArrayValStr(idx, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) AppendOutPortArrayValInt(index int, val Port_Int) bool {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.AppendArrayValInt(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) AppendOutPortArrayValStr(index int, val Port_Str) bool {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return port.AppendArrayValStr(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetOutPortArrayLen(index int) Port_Int {
|
|
||||||
port := en.GetOutPort(index)
|
|
||||||
if port == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return port.GetArrayLen()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) DoNext(index int) error {
|
|
||||||
// -1 表示中断运行
|
|
||||||
if index == -1 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if index < 0 || index >= len(en.execNode.nextNode) {
|
|
||||||
return fmt.Errorf("next index %d not found", index)
|
|
||||||
}
|
|
||||||
|
|
||||||
if en.execNode.nextNode[index] == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return en.execNode.nextNode[index].Do(en.gr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetNextExecLen() int {
|
|
||||||
return len(en.execNode.nextNode)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) getInnerExecNode() IInnerExecNode {
|
|
||||||
return en.innerExecNode.IExecNode.(IInnerExecNode)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *BaseExecNode) GetBlueprintModule() IBlueprintModule {
|
|
||||||
if en.gr == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return en.gr.IBlueprintModule
|
|
||||||
}
|
|
||||||
@@ -1,383 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Entrance 格式:Entrance_XXXX_ID
|
|
||||||
const (
|
|
||||||
Entrance = "Entrance_"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ExecPool struct {
|
|
||||||
innerExecNodeMap map[string]IInnerExecNode // 所有配置对应的结点信息
|
|
||||||
execNodeMap map[string]IExecNode // 实际注册的执行结点
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ExecPool) Load(execDefFilePath string) error {
|
|
||||||
em.innerExecNodeMap = make(map[string]IInnerExecNode, 512)
|
|
||||||
em.execNodeMap = make(map[string]IExecNode, 512)
|
|
||||||
|
|
||||||
// 检查路径是否存在
|
|
||||||
stat, err := os.Stat(execDefFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to access path %s: %v", execDefFilePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是单个文件,直接处理
|
|
||||||
if !stat.IsDir() {
|
|
||||||
return fmt.Errorf("%s is not a directory", execDefFilePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 遍历目录及其子目录
|
|
||||||
err = filepath.Walk(execDefFilePath, func(path string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("访问路径出错 %s: %v\n", path, err)
|
|
||||||
return nil // 继续遍历其他文件
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是目录,继续遍历
|
|
||||||
if info.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 只处理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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成变量配置对应的配置结点GetVar_类型、SetVar_类型
|
|
||||||
return em.regVariablesNode()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理单个JSON文件
|
|
||||||
func (em *ExecPool) processJSONFile(filePath string) error {
|
|
||||||
// 打开文件
|
|
||||||
file, err := os.Open(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to open file %s: %v", filePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func(file *os.File) {
|
|
||||||
err = file.Close()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to close file %s: %v\n", filePath, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}(file)
|
|
||||||
|
|
||||||
var baseExecConfig []BaseExecConfig
|
|
||||||
decoder := json.NewDecoder(file)
|
|
||||||
if err = decoder.Decode(&baseExecConfig); err != nil {
|
|
||||||
return fmt.Errorf("failed to decode JSON from file %s: %v", filePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
})
|
|
||||||
|
|
||||||
// 根据配置的结点信息,创建innerExecNode
|
|
||||||
var execError error
|
|
||||||
exec, execError := em.createExecFromJSON(baseExecConfig[i])
|
|
||||||
if execError != nil {
|
|
||||||
return execError
|
|
||||||
}
|
|
||||||
|
|
||||||
// 加载到innerExecNodeMap中
|
|
||||||
if !em.addInnerExec(exec) {
|
|
||||||
return fmt.Errorf("exec %s already registered", exec.GetName())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ExecPool) createPortByDataType(nodeName, portName, dataType string) (IPort, error) {
|
|
||||||
switch strings.ToLower(dataType) {
|
|
||||||
case Config_DataType_Int, Config_DataType_Integer:
|
|
||||||
return NewPortInt(), nil
|
|
||||||
case Config_DataType_Float:
|
|
||||||
return NewPortFloat(), nil
|
|
||||||
case Config_DataType_Str:
|
|
||||||
return NewPortStr(), nil
|
|
||||||
case Config_DataType_Boolean, Config_DataType_Bool:
|
|
||||||
return NewPortBool(), nil
|
|
||||||
case Config_DataType_Array:
|
|
||||||
return NewPortArray(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("invalid data type %s,node %s port %s", dataType, nodeName, portName)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
} else {
|
|
||||||
baseExec.Name = baseExecConfig.Name
|
|
||||||
}
|
|
||||||
baseExec.Title = baseExecConfig.Title
|
|
||||||
baseExec.Package = baseExecConfig.Package
|
|
||||||
baseExec.Description = baseExecConfig.Description
|
|
||||||
baseExec.PrepareMaxInPortId(baseExecConfig.GetMaxInPortId())
|
|
||||||
baseExec.PrepareMaxOutPortId(baseExecConfig.GetMaxOutPortId())
|
|
||||||
|
|
||||||
// 初始化所有的输入端口
|
|
||||||
inExecNum := 0
|
|
||||||
for index, input := range baseExecConfig.Inputs {
|
|
||||||
portType := strings.ToLower(input.PortType)
|
|
||||||
if portType != Config_PortType_Exec && portType != Config_PortType_Data {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
if index > 0 {
|
|
||||||
return nil, fmt.Errorf("the exec port is only allowed to be placed on the first one,node name %s", baseExec.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
inExecNum++
|
|
||||||
// 设置执行端口
|
|
||||||
baseExec.SetInPortById(input.PortId, 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)
|
|
||||||
if portType != Config_PortType_Exec && portType != Config_PortType_Data {
|
|
||||||
return nil, fmt.Errorf("output %s data type %s not support,node name %s", output.Name, output.DataType, baseExec.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exec出口只能先Exec,再Data,不能穿插,如果是Data类型,但遇到Exec入口则不允许
|
|
||||||
if hasData && portType == Config_PortType_Exec {
|
|
||||||
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 {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
baseExec.SetOutPortById(output.PortId, port)
|
|
||||||
}
|
|
||||||
return &baseExec, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ExecPool) addInnerExec(exec IInnerExecNode) bool {
|
|
||||||
if _, ok := em.innerExecNodeMap[exec.GetName()]; ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
em.innerExecNodeMap[exec.GetName()] = exec
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ExecPool) Register(exec IExecNode) bool {
|
|
||||||
baseExec, ok := exec.(IExecNode)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
innerNode, ok := em.innerExecNodeMap[baseExec.GetName()]
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok = em.execNodeMap[innerNode.GetName()]; ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
baseExecNode, ok := exec.(IBaseExecNode)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置实际执行结点中innerExecNode变量,BaseExecNode.innerExecNode = innerNode
|
|
||||||
baseExecNode.initInnerExecNode(innerNode.(*innerExecNode))
|
|
||||||
|
|
||||||
// innerNode设置实际的exec变量,innerExecNode.IExecNode = exec
|
|
||||||
innerNode.SetExec(exec)
|
|
||||||
|
|
||||||
// 将实际的执行结点保存到execNodeMap中
|
|
||||||
em.execNodeMap[baseExec.GetName()] = exec
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ExecPool) GetExec(name string) IInnerExecNode {
|
|
||||||
if exec, ok := em.execNodeMap[name]; ok {
|
|
||||||
return exec.getInnerExecNode()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// regVariablesNode 注册变量结点GetVar_类型、SetVar_类型
|
|
||||||
func (em *ExecPool) regVariablesNode() error {
|
|
||||||
var err error
|
|
||||||
if err = em.regGetVariables(Config_DataType_Int); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regGetVariables(Config_DataType_Integer); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regGetVariables(Config_DataType_Float); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regGetVariables(Config_DataType_Str); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regGetVariables(Config_DataType_Boolean); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regGetVariables(Config_DataType_Bool); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regGetVariables(Config_DataType_Array); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = em.regSetVariables(Config_DataType_Int); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regSetVariables(Config_DataType_Integer); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regSetVariables(Config_DataType_Float); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regSetVariables(Config_DataType_Str); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regSetVariables(Config_DataType_Boolean); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regSetVariables(Config_DataType_Bool); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = em.regSetVariables(Config_DataType_Array); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ExecPool) regGetVariables(typ string) error {
|
|
||||||
var baseExec innerExecNode
|
|
||||||
baseExec.Name = genGetVariablesNodeName(typ)
|
|
||||||
baseExec.PrepareMaxOutPortId(0)
|
|
||||||
|
|
||||||
outPort := NewPortByType(typ)
|
|
||||||
if outPort == nil {
|
|
||||||
return fmt.Errorf("invalid type %s", typ)
|
|
||||||
}
|
|
||||||
baseExec.SetOutPortById(0, outPort)
|
|
||||||
|
|
||||||
var getVariablesNode GetVariablesNode
|
|
||||||
getVariablesNode.nodeName = baseExec.GetName()
|
|
||||||
|
|
||||||
if !em.addInnerExec(&baseExec) {
|
|
||||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
|
||||||
}
|
|
||||||
if !em.Register(&getVariablesNode) {
|
|
||||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func genSetVariablesNodeName(typ string) string {
|
|
||||||
return fmt.Sprintf("%s_%s", SetVariables, typ)
|
|
||||||
}
|
|
||||||
|
|
||||||
func genGetVariablesNodeName(typ string) string {
|
|
||||||
return fmt.Sprintf("%s_%s", GetVariables, typ)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ExecPool) regSetVariables(typ string) error {
|
|
||||||
var baseExec innerExecNode
|
|
||||||
baseExec.Name = genSetVariablesNodeName(typ)
|
|
||||||
|
|
||||||
inExecPort := NewPortByType(Config_PortType_Exec)
|
|
||||||
inPort := NewPortByType(typ)
|
|
||||||
outExecPort := NewPortByType(Config_PortType_Exec)
|
|
||||||
outPort := NewPortByType(typ)
|
|
||||||
baseExec.PrepareMaxInPortId(1)
|
|
||||||
baseExec.PrepareMaxOutPortId(1)
|
|
||||||
|
|
||||||
baseExec.SetInPortById(0, inExecPort)
|
|
||||||
baseExec.SetInPortById(1, inPort)
|
|
||||||
|
|
||||||
baseExec.SetOutPortById(0, outExecPort)
|
|
||||||
baseExec.SetOutPortById(1, outPort)
|
|
||||||
|
|
||||||
baseExec.IExecNode = &SetVariablesNode{nodeName: baseExec.GetName()}
|
|
||||||
if !em.addInnerExec(&baseExec) {
|
|
||||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
|
||||||
}
|
|
||||||
if !em.Register(baseExec.IExecNode) {
|
|
||||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getEntranceNodeNameAndID(className string) (string, int64, bool) {
|
|
||||||
if !strings.HasPrefix(className, Entrance) {
|
|
||||||
return "", 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := strings.Split(className, "_")
|
|
||||||
if len(parts) != 3 {
|
|
||||||
return "", 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
entranceID, err := strconv.Atoi(parts[2])
|
|
||||||
if err != nil {
|
|
||||||
return "", 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return parts[0] + "_" + parts[1], int64(entranceID), true
|
|
||||||
}
|
|
||||||
@@ -1,231 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
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 {
|
|
||||||
SafeAfterFunc(timerId *uint64, d time.Duration, AdditionData interface{}, cb func(uint64, interface{}))
|
|
||||||
TriggerEvent(graphID int64, eventID int64, args ...any) error
|
|
||||||
CancelTimerId(graphID int64, timerId *uint64) bool
|
|
||||||
GetGameService() service.IService
|
|
||||||
GetBattleService() service.IService
|
|
||||||
}
|
|
||||||
|
|
||||||
type baseGraph struct {
|
|
||||||
entrance map[int64]*execNode // 入口
|
|
||||||
}
|
|
||||||
|
|
||||||
type Graph struct {
|
|
||||||
graphFileName string
|
|
||||||
graphID int64
|
|
||||||
*baseGraph
|
|
||||||
graphContext
|
|
||||||
IBlueprintModule
|
|
||||||
mapTimerID map[uint64]struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type graphContext struct {
|
|
||||||
context map[string]*ExecContext // 上下文
|
|
||||||
variables map[string]IPort // 变量
|
|
||||||
globalVariables map[string]IPort // 全局变量,g_Return,为执行返回值
|
|
||||||
}
|
|
||||||
|
|
||||||
type nodeConfig struct {
|
|
||||||
Id string `json:"id"`
|
|
||||||
Class string `json:"class"`
|
|
||||||
Module string `json:"module"`
|
|
||||||
//Pos []float64 `json:"pos"`
|
|
||||||
PortDefault map[string]interface{} `json:"port_defaultv"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type edgeConfig struct {
|
|
||||||
EdgeID string `json:"edge_id"`
|
|
||||||
SourceNodeID string `json:"source_node_id"`
|
|
||||||
DesNodeId string `json:"des_node_id"`
|
|
||||||
|
|
||||||
SourcePortId int `json:"source_port_id"`
|
|
||||||
DesPortId int `json:"des_port_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type MultiTypeValue struct {
|
|
||||||
Value any
|
|
||||||
}
|
|
||||||
|
|
||||||
// 实现json.Unmarshaler接口,自定义解码逻辑
|
|
||||||
func (v *MultiTypeValue) UnmarshalJSON(data []byte) error {
|
|
||||||
// 尝试将数据解析为字符串
|
|
||||||
var strVal string
|
|
||||||
if err := json.Unmarshal(data, &strVal); err == nil {
|
|
||||||
v.Value = strVal
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果不是字符串,尝试解析为数字
|
|
||||||
var intVal int
|
|
||||||
if err := json.Unmarshal(data, &intVal); err == nil {
|
|
||||||
v.Value = intVal
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果不是字符串,尝试解析为数字
|
|
||||||
var boolVal bool
|
|
||||||
if err := json.Unmarshal(data, &boolVal); err == nil {
|
|
||||||
v.Value = boolVal
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果不是字符串,尝试解析为数字
|
|
||||||
var float64Val float64
|
|
||||||
if err := json.Unmarshal(data, &float64Val); err == nil {
|
|
||||||
v.Value = float64Val
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var arrayVal []any
|
|
||||||
if err := json.Unmarshal(data, &arrayVal); err == nil {
|
|
||||||
v.Value = arrayVal
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// 如果都失败,返回错误
|
|
||||||
return fmt.Errorf("cannot unmarshal JSON value: %s", string(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
type variablesConfig struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Value MultiTypeValue `json:"value"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type graphConfig struct {
|
|
||||||
GraphName string `json:"graph_name"`
|
|
||||||
Time string `json:"time"`
|
|
||||||
|
|
||||||
Nodes []nodeConfig `json:"nodes"`
|
|
||||||
Edges []edgeConfig `json:"edges"`
|
|
||||||
Variables []variablesConfig `json:"variables"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gc *graphConfig) GetVariablesByName(varName string) *variablesConfig {
|
|
||||||
for _, varCfg := range gc.Variables {
|
|
||||||
if varCfg.Name == varName {
|
|
||||||
return &varCfg
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gc *graphConfig) GetNodeByID(nodeID string) *nodeConfig {
|
|
||||||
for _, node := range gc.Nodes {
|
|
||||||
if node.Id == nodeID {
|
|
||||||
return &node
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gr *Graph) GetAndCreateReturnPort() IPort {
|
|
||||||
p, ok := gr.globalVariables[ReturnVarial]
|
|
||||||
if ok && p != nil {
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
p = NewPortArray()
|
|
||||||
gr.globalVariables[ReturnVarial] = p
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
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{}
|
|
||||||
}else {
|
|
||||||
gr.globalVariables[ReturnVarial] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err := entranceNode.Do(gr, args...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if gr.globalVariables != nil {
|
|
||||||
port := gr.globalVariables[ReturnVarial]
|
|
||||||
if port != nil {
|
|
||||||
array, ok := port.GetArray()
|
|
||||||
if ok {
|
|
||||||
return array, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gr *Graph) GetNodeInPortValue(nodeID string, inPortIndex int) IPort {
|
|
||||||
if ctx, ok := gr.context[nodeID]; ok {
|
|
||||||
if inPortIndex >= len(ctx.InputPorts) || inPortIndex < 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx.InputPorts[inPortIndex]
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gr *Graph) GetNodeOutPortValue(nodeID string, outPortIndex int) IPort {
|
|
||||||
if ctx, ok := gr.context[nodeID]; ok {
|
|
||||||
if outPortIndex >= len(ctx.OutputPorts) || outPortIndex < 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return ctx.OutputPorts[outPortIndex]
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gr *Graph) Release() {
|
|
||||||
// 有定时器关闭定时器
|
|
||||||
for timerID := range gr.mapTimerID {
|
|
||||||
gr.CancelTimerId(gr.graphID, &timerID)
|
|
||||||
}
|
|
||||||
gr.mapTimerID = nil
|
|
||||||
|
|
||||||
// 清理掉所有数据
|
|
||||||
*gr = Graph{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gr *Graph) HotReload(newBaseGraph *baseGraph) {
|
|
||||||
gr.baseGraph = newBaseGraph
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gr *Graph) GetGraphFileName() string{
|
|
||||||
return gr.graphFileName
|
|
||||||
}
|
|
||||||
@@ -1,315 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/goccy/go-json"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GraphPool struct {
|
|
||||||
mapGraphs map[string]*baseGraph
|
|
||||||
execPool *ExecPool
|
|
||||||
blueprintModule IBlueprintModule
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) Load(execPool *ExecPool, graphFilePath string, blueprintModule IBlueprintModule) error {
|
|
||||||
gp.execPool = execPool
|
|
||||||
gp.mapGraphs = make(map[string]*baseGraph, 1024)
|
|
||||||
gp.blueprintModule = blueprintModule
|
|
||||||
|
|
||||||
// 检查路径是否存在
|
|
||||||
stat, err := os.Stat(graphFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to access path %s: %v", graphFilePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是单个文件,直接处理
|
|
||||||
if !stat.IsDir() {
|
|
||||||
return fmt.Errorf("%s is not a directory", graphFilePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 遍历目录及其子目录
|
|
||||||
return filepath.Walk(graphFilePath, func(path string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("访问路径出错 %s: %v\n", path, err)
|
|
||||||
return nil // 继续遍历其他文件
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是目录,继续遍历
|
|
||||||
if info.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 只处理JSON文件
|
|
||||||
if filepath.Ext(path) == ".vgf" {
|
|
||||||
return gp.processJSONFile(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) processJSONFile(filePath string) error {
|
|
||||||
// 打开文件
|
|
||||||
file, err := os.Open(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to open file %s: %v", filePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if err = file.Close(); err != nil {
|
|
||||||
fmt.Printf("关闭文件 %s 时出错: %v\n", filePath, err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
fileName := filepath.Base(filePath)
|
|
||||||
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 {
|
|
||||||
return fmt.Errorf("failed to decode JSON from file %s: %v", filePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 预处理蓝图
|
|
||||||
return gp.prepareGraph(name, &gConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) prepareGraph(graphName string, graphConfig *graphConfig) error {
|
|
||||||
// 找到所有的入口
|
|
||||||
for _, node := range graphConfig.Nodes {
|
|
||||||
_, entranceID, ok := getEntranceNodeNameAndID(node.Class)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 对入口进行预处理
|
|
||||||
err := gp.prepareOneEntrance(graphName, entranceID, &node, graphConfig)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) genVarExec(nodeCfg *nodeConfig, graphConfig *graphConfig) (IInnerExecNode, string) {
|
|
||||||
// 是否为Get_或Set_开头
|
|
||||||
if !strings.HasPrefix(nodeCfg.Class, "Get_") && !strings.HasPrefix(nodeCfg.Class, "Set_") {
|
|
||||||
return gp.execPool.GetExec(nodeCfg.Class), ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取Get_或Set_结尾字符串
|
|
||||||
var nodeName string
|
|
||||||
var varName string
|
|
||||||
if strings.HasPrefix(nodeCfg.Class, "Get_") {
|
|
||||||
var typ string
|
|
||||||
varName = strings.TrimPrefix(nodeCfg.Class, "Get_")
|
|
||||||
varCfg := graphConfig.GetVariablesByName(varName)
|
|
||||||
if varCfg != nil {
|
|
||||||
typ = varCfg.Type
|
|
||||||
}
|
|
||||||
nodeName = genGetVariablesNodeName(typ)
|
|
||||||
} else if strings.HasPrefix(nodeCfg.Class, "Set_") {
|
|
||||||
var typ string
|
|
||||||
varName = strings.TrimPrefix(nodeCfg.Class, "Set_")
|
|
||||||
varCfg := graphConfig.GetVariablesByName(varName)
|
|
||||||
if varCfg != nil {
|
|
||||||
typ = varCfg.Type
|
|
||||||
}
|
|
||||||
nodeName = genSetVariablesNodeName(typ)
|
|
||||||
}
|
|
||||||
|
|
||||||
e := gp.execPool.GetExec(nodeName)
|
|
||||||
return e, varName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) genAllNode(graphConfig *graphConfig) (map[string]*execNode, error) {
|
|
||||||
nodes := make(map[string]*execNode)
|
|
||||||
for _, node := range graphConfig.Nodes {
|
|
||||||
var varName string
|
|
||||||
className := node.Class
|
|
||||||
if name, _, ok := getEntranceNodeNameAndID(className); ok {
|
|
||||||
className = name
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取不到node,则获取变量node
|
|
||||||
exec := gp.execPool.GetExec(className)
|
|
||||||
if exec == nil {
|
|
||||||
exec, varName = gp.genVarExec(&node, graphConfig)
|
|
||||||
if exec == nil {
|
|
||||||
return nil, fmt.Errorf("%s node has not been registered", node.Class)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
nodes[node.Id] = &execNode{
|
|
||||||
Id: node.Id,
|
|
||||||
execNode: exec,
|
|
||||||
preInPort: make([]*prePortNode, exec.GetInPortCount()),
|
|
||||||
inPortDefaultValue: node.PortDefault,
|
|
||||||
variableName: varName,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nodes, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) prepareOneNode(mapNodeExec map[string]*execNode, nodeExec *execNode, graphConfig *graphConfig, recursion *int) error {
|
|
||||||
*recursion++
|
|
||||||
if *recursion > 256 {
|
|
||||||
return fmt.Errorf("recursion too deep")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 找到所有出口
|
|
||||||
var idx int
|
|
||||||
for ; nodeExec.execNode.IsOutPortExec(idx) && idx < nodeExec.execNode.GetOutPortCount(); idx++ {
|
|
||||||
// 找到出口结点
|
|
||||||
nextExecNode := gp.findOutNextNode(graphConfig, mapNodeExec, nodeExec.Id, idx)
|
|
||||||
nodeExec.nextNode = append(nodeExec.nextNode, nextExecNode)
|
|
||||||
if nextExecNode != nil {
|
|
||||||
nextExecNode.beConnect = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将所有的next填充next
|
|
||||||
for _, nextOne := range nodeExec.nextNode {
|
|
||||||
if nextOne == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// 对出口进行预处理
|
|
||||||
err := gp.prepareOneNode(mapNodeExec, nextOne, graphConfig, recursion)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) findOutNextNode(graphConfig *graphConfig, mapNodeExec map[string]*execNode, sourceNodeID string, sourcePortIdx int) *execNode {
|
|
||||||
// 找到出口的NodeID
|
|
||||||
for _, edge := range graphConfig.Edges {
|
|
||||||
if edge.SourceNodeID == sourceNodeID && edge.SourcePortId == sourcePortIdx {
|
|
||||||
return mapNodeExec[edge.DesNodeId]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// prepareOneEntrance 先处理执行Exec入出口连线
|
|
||||||
func (gp *GraphPool) prepareOneEntrance(graphName string, entranceID int64, nodeCfg *nodeConfig, graphConfig *graphConfig) error {
|
|
||||||
// 将所有的Node执行结点生成出来
|
|
||||||
mapNodes, err := gp.genAllNode(graphConfig)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 从入口结点开始做预处理,将next结点都统一生成
|
|
||||||
nodeExec := mapNodes[nodeCfg.Id]
|
|
||||||
if nodeExec == nil {
|
|
||||||
return fmt.Errorf("entrance node %s not found", nodeCfg.Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
nodeExec.isEntrance = true
|
|
||||||
err = gp.prepareOneNode(mapNodes, nodeExec, graphConfig, new(int))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理inPort前置结点
|
|
||||||
err = gp.prepareInPort(mapNodes, graphConfig)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
gr, ok := gp.mapGraphs[graphName]
|
|
||||||
if !ok {
|
|
||||||
gr = &baseGraph{}
|
|
||||||
gr.entrance = make(map[int64]*execNode, 16)
|
|
||||||
gp.mapGraphs[graphName] = gr
|
|
||||||
}
|
|
||||||
|
|
||||||
gr.entrance[entranceID] = nodeExec
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) findPreInPortNode(mapNodes map[string]*execNode, nodeExec *execNode, graphConfig *graphConfig, portIdx int) *prePortNode {
|
|
||||||
for _, edge := range graphConfig.Edges {
|
|
||||||
if edge.DesNodeId == nodeExec.Id && edge.DesPortId == portIdx {
|
|
||||||
srcNode := mapNodes[edge.SourceNodeID]
|
|
||||||
if srcNode == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var preNode prePortNode
|
|
||||||
preNode.node = srcNode
|
|
||||||
preNode.outPortId = edge.SourcePortId
|
|
||||||
|
|
||||||
return &preNode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) preparePreInPortNode(mapNodes map[string]*execNode, nodeExec *execNode, graphConfig *graphConfig) error {
|
|
||||||
// 找到当前结点的所有inPort的前一个端口
|
|
||||||
for i := 0; i < nodeExec.execNode.GetInPortCount(); i++ {
|
|
||||||
// 如果是执行结点,则跳过
|
|
||||||
if nodeExec.execNode.IsInPortExec(i) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 找到入口的上一个结点
|
|
||||||
preNode := gp.findPreInPortNode(mapNodes, nodeExec, graphConfig, i)
|
|
||||||
if preNode == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
nodeExec.preInPort[i] = preNode
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gp *GraphPool) prepareInPort(mapNodeExec map[string]*execNode, graphConfig *graphConfig) error {
|
|
||||||
for _, e := range mapNodeExec {
|
|
||||||
// 对当前结点的入口进行预处理
|
|
||||||
err := gp.preparePreInPortNode(mapNodeExec, e, graphConfig)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,296 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
// AddInt 加(int)
|
|
||||||
type AddInt struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AddInt) GetName() string {
|
|
||||||
return "AddInt"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AddInt) Exec() (int, error) {
|
|
||||||
inPortA := em.GetInPort(0)
|
|
||||||
if inPortA == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortB := em.GetInPort(1)
|
|
||||||
if inPortB == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet := em.GetOutPort(0)
|
|
||||||
if outPortRet == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt outParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inA, ok := inPortA.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
inB, ok := inPortB.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
ret := inA + inB
|
|
||||||
outPortRet.SetInt(ret)
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SubInt 减(int)
|
|
||||||
type SubInt struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *SubInt) GetName() string {
|
|
||||||
return "SubInt"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *SubInt) Exec() (int, error) {
|
|
||||||
inPortA := em.GetInPort(0)
|
|
||||||
if inPortA == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortB := em.GetInPort(1)
|
|
||||||
if inPortB == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortAbs := em.GetInPort(2)
|
|
||||||
if inPortAbs == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet := em.GetOutPort(0)
|
|
||||||
if outPortRet == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt outParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inA, ok := inPortA.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
inB, ok := inPortB.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
isAbs, ok := inPortAbs.GetBool()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
ret := inA - inB
|
|
||||||
if isAbs && ret < 0 {
|
|
||||||
ret *= -1
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet.SetInt(ret)
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MulInt 乘(int)
|
|
||||||
type MulInt struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *MulInt) GetName() string {
|
|
||||||
return "MulInt"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *MulInt) Exec() (int, error) {
|
|
||||||
inPortA := em.GetInPort(0)
|
|
||||||
if inPortA == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortB := em.GetInPort(1)
|
|
||||||
if inPortB == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet := em.GetOutPort(0)
|
|
||||||
if outPortRet == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt outParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inA, ok := inPortA.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
inB, ok := inPortB.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet.SetInt(inA * inB)
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DivInt 除(int)
|
|
||||||
type DivInt struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *DivInt) GetName() string {
|
|
||||||
return "DivInt"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *DivInt) Exec() (int, error) {
|
|
||||||
inPortA := em.GetInPort(0)
|
|
||||||
if inPortA == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortB := em.GetInPort(1)
|
|
||||||
if inPortB == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortRound := em.GetInPort(2)
|
|
||||||
if inPortRound == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet := em.GetOutPort(0)
|
|
||||||
if outPortRet == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt outParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inA, ok := inPortA.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
inB, ok := inPortB.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
isRound, ok := inPortRound.GetBool()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if inB == 0 {
|
|
||||||
return -1, fmt.Errorf("div zero error")
|
|
||||||
}
|
|
||||||
|
|
||||||
var ret int64
|
|
||||||
if isRound {
|
|
||||||
ret = (inA + inB/2) / inB
|
|
||||||
} else {
|
|
||||||
ret = inA / inB
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet.SetInt(ret)
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ModInt 取模(int)
|
|
||||||
type ModInt struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ModInt) GetName() string {
|
|
||||||
return "ModInt"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ModInt) Exec() (int, error) {
|
|
||||||
inPortA := em.GetInPort(0)
|
|
||||||
if inPortA == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortB := em.GetInPort(1)
|
|
||||||
if inPortB == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet := em.GetOutPort(0)
|
|
||||||
if outPortRet == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt outParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inA, ok := inPortA.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
inB, ok := inPortB.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
if inB == 0 {
|
|
||||||
return -1, fmt.Errorf("mod zero error")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet.SetInt(inA % inB)
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandNumber 范围随机[0,99]
|
|
||||||
type RandNumber struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *RandNumber) GetName() string {
|
|
||||||
return "RandNumber"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *RandNumber) Exec() (int, error) {
|
|
||||||
inPortSeed := em.GetInPort(0)
|
|
||||||
if inPortSeed == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortMin := em.GetInPort(1)
|
|
||||||
if inPortMin == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortMax := em.GetInPort(2)
|
|
||||||
if inPortMax == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet := em.GetOutPort(0)
|
|
||||||
if outPortRet == nil {
|
|
||||||
return -1, fmt.Errorf("AddInt outParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inSeed, ok := inPortSeed.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
inMin, ok := inPortMin.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
inMax, ok := inPortMax.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
var ret int64
|
|
||||||
if inSeed > 0 {
|
|
||||||
r := rand.New(rand.NewSource(inSeed))
|
|
||||||
if r == nil {
|
|
||||||
return -1, fmt.Errorf("RandNumber fail")
|
|
||||||
}
|
|
||||||
ret = int64(r.Intn(int(inMax-inMin+1))) + inMin
|
|
||||||
} else {
|
|
||||||
ret = int64(rand.Intn(int(inMax-inMin+1))) + inMin
|
|
||||||
}
|
|
||||||
|
|
||||||
outPortRet.SetInt(ret)
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
@@ -1,204 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type prePortNode struct {
|
|
||||||
node *execNode // 上个结点
|
|
||||||
outPortId int // 对应上一个结点的OutPortId
|
|
||||||
}
|
|
||||||
|
|
||||||
type execNode struct {
|
|
||||||
Id string
|
|
||||||
execNode IInnerExecNode
|
|
||||||
|
|
||||||
nextNode []*execNode
|
|
||||||
nextIdx int
|
|
||||||
|
|
||||||
preInPort []*prePortNode // Port的上一个结点
|
|
||||||
inPortDefaultValue map[string]any
|
|
||||||
|
|
||||||
variableName string // 如果是变量,则有变量名
|
|
||||||
beConnect bool // 是否有被连线
|
|
||||||
isEntrance bool // 是否是入口结点
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasBeConnectLine 是否有被连线
|
|
||||||
func (en *execNode) HasBeConnectLine() bool {
|
|
||||||
return en.beConnect
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasInPortExec 有前置执行入口
|
|
||||||
func (en *execNode) HasInPortExec() bool {
|
|
||||||
return en.execNode.IsInPortExec(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasOutPortExec 有前置执行入口
|
|
||||||
func (en *execNode) HasOutPortExec() bool {
|
|
||||||
return en.execNode.IsOutPortExec(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *execNode) GetInPortDefaultValue(index int) any {
|
|
||||||
key := fmt.Sprintf("%d", index)
|
|
||||||
v, ok := en.inPortDefaultValue[key]
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *execNode) GetInPortDefaultIntArrayValue(index int) []int64 {
|
|
||||||
val := en.GetInPortDefaultValue(index)
|
|
||||||
if val == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var arrayInt []int64
|
|
||||||
arrayVal := val.([]any)
|
|
||||||
for i := range arrayVal {
|
|
||||||
if intVal, ok := arrayVal[i].(float64); ok {
|
|
||||||
arrayInt = append(arrayInt, int64(intVal))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return arrayInt
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *execNode) GetInPortDefaultStringArrayValue(index int) []string {
|
|
||||||
val := en.GetInPortDefaultValue(index)
|
|
||||||
if val == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return val.([]string)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *execNode) Next() *execNode {
|
|
||||||
if en.nextIdx >= len(en.nextNode) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return en.nextNode[en.nextIdx]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *execNode) exec(gr *Graph) (int, error) {
|
|
||||||
e, ok := en.execNode.(IExecNode)
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("exec node %s not exec", en.execNode.GetName())
|
|
||||||
}
|
|
||||||
|
|
||||||
node, ok := en.execNode.(IBaseExecNode)
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("exec node %s not exec", en.execNode.GetName())
|
|
||||||
}
|
|
||||||
|
|
||||||
// 执行完,要恢复上下文,结点的BaseExecNode会被执行时修改
|
|
||||||
ctx, exNode := node.getExecNodeInfo()
|
|
||||||
defer func() {
|
|
||||||
node.setExecNodeInfo(ctx, exNode)
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := node.initExecNode(gr, en); err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return e.Exec()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (en *execNode) doSetInPort(gr *Graph, index int, inPort IPort) error {
|
|
||||||
// 找到当前Node的InPort的index的前一个结点
|
|
||||||
preNode := en.preInPort[index]
|
|
||||||
// 如果前一个结点为空,则填充默认值
|
|
||||||
if preNode == nil {
|
|
||||||
err := inPort.setAnyVale(en.GetInPortDefaultValue(index))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := gr.context[preNode.node.Id]; !ok ||
|
|
||||||
(!preNode.node.HasBeConnectLine() && !preNode.node.isEntrance) {
|
|
||||||
// 如果前一个结点没有执行过,则递归执行前一个结点
|
|
||||||
err := preNode.node.Do(gr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断上一个结点是否已经执行过
|
|
||||||
if _, ok := gr.context[preNode.node.Id]; ok {
|
|
||||||
outPort := gr.GetNodeOutPortValue(preNode.node.Id, preNode.outPortId)
|
|
||||||
if outPort == nil {
|
|
||||||
return fmt.Errorf("pre node %s out port index %d not found", preNode.node.Id, preNode.outPortId)
|
|
||||||
}
|
|
||||||
|
|
||||||
inPort.SetValue(outPort)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("pre node %s not exec", preNode.node.Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
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{
|
|
||||||
InputPorts: inPorts,
|
|
||||||
OutputPorts: outPorts,
|
|
||||||
}
|
|
||||||
|
|
||||||
startOutIdx := en.execNode.GetOutPortParamStartIndex()
|
|
||||||
for i := 0; i < len(outPortArgs); i++ {
|
|
||||||
if i+startOutIdx >= len(outPorts) {
|
|
||||||
return fmt.Errorf("args %d not found in node %s", i, en.execNode.GetName())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := outPorts[i+startOutIdx].setAnyVale(outPortArgs[i]); err != nil {
|
|
||||||
return fmt.Errorf("args %d set value error: %w", i, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理InPort结点值
|
|
||||||
var err error
|
|
||||||
for portId := range inPorts {
|
|
||||||
if en.execNode.IsInPortExec(portId) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
err = en.doSetInPort(gr, portId, inPorts[portId])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置执行器相关的上下文信息
|
|
||||||
// 如果是变量设置变量名
|
|
||||||
// 执行本结点
|
|
||||||
nextIndex, err := en.exec(gr)
|
|
||||||
if err != nil {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
if nextIndex < 0 || nextIndex >= len(en.nextNode) {
|
|
||||||
return fmt.Errorf("next index %d not found", nextIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
if en.nextNode[nextIndex] == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return en.nextNode[nextIndex].Do(gr)
|
|
||||||
}
|
|
||||||
@@ -1,420 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
Config_PortType_Exec = "exec"
|
|
||||||
Config_PortType_Data = "data"
|
|
||||||
Config_DataType_Int = "int"
|
|
||||||
Config_DataType_Integer = "integer"
|
|
||||||
Config_DataType_Float = "float"
|
|
||||||
Config_DataType_Str = "string"
|
|
||||||
Config_DataType_Boolean = "boolean"
|
|
||||||
Config_DataType_Bool = "bool"
|
|
||||||
Config_DataType_Array = "array"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Port[T iPortType] struct {
|
|
||||||
PortVal T
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) Clone() IPort {
|
|
||||||
arrayData, ok := any(em.PortVal).(Port_Array)
|
|
||||||
if !ok {
|
|
||||||
return &Port[T]{
|
|
||||||
PortVal: em.PortVal,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
portArray := Port[Port_Array]{}
|
|
||||||
portArray.PortVal = append(portArray.PortVal, arrayData...)
|
|
||||||
return &portArray
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) Reset() {
|
|
||||||
var v T
|
|
||||||
em.PortVal = v
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) GetInt() (Port_Int, bool) {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Int); ok {
|
|
||||||
return t, true
|
|
||||||
}
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) GetFloat() (Port_Float, bool) {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Float); ok {
|
|
||||||
return t, true
|
|
||||||
}
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) GetStr() (Port_Str, bool) {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Str); ok {
|
|
||||||
return t, true
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) GetArrayValInt(idx int) (Port_Int, bool) {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
|
||||||
if idx >= 0 && idx < len(t) {
|
|
||||||
return t[idx].IntVal, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) GetArrayValStr(idx int) (string, bool) {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
|
||||||
if idx >= 0 && idx < len(t) {
|
|
||||||
return t[idx].StrVal, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) GetBool() (Port_Bool, bool) {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Bool); ok {
|
|
||||||
return t, true
|
|
||||||
}
|
|
||||||
return false, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) GetArray() (Port_Array, bool) {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
|
||||||
return t, true
|
|
||||||
}
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) SetInt(val Port_Int) bool {
|
|
||||||
if t, ok := any(&em.PortVal).(*Port_Int); ok {
|
|
||||||
*t = val
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) SetFloat(val Port_Float) bool {
|
|
||||||
if t, ok := any(&em.PortVal).(*Port_Float); ok {
|
|
||||||
*t = val
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) SetStr(val Port_Str) bool {
|
|
||||||
if t, ok := any(&em.PortVal).(*Port_Str); ok {
|
|
||||||
*t = val
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) SetBool(val Port_Bool) bool {
|
|
||||||
if t, ok := any(&em.PortVal).(*Port_Bool); ok {
|
|
||||||
*t = val
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) SetArrayValInt(idx int, val Port_Int) bool {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
|
||||||
if idx >= 0 && idx < len(t) {
|
|
||||||
t[idx].IntVal = val
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) SetArrayValStr(idx int, val Port_Str) bool {
|
|
||||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
|
||||||
if idx >= 0 && idx < len(t) {
|
|
||||||
(t)[idx].StrVal = val
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) AppendArrayValInt(val Port_Int) bool {
|
|
||||||
if t, ok := any(&em.PortVal).(*Port_Array); ok {
|
|
||||||
*t = append(*t, ArrayData{IntVal: val})
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) AppendArrayValStr(val Port_Str) bool {
|
|
||||||
if t, ok := any(&em.PortVal).(*Port_Array); ok {
|
|
||||||
*t = append(*t, ArrayData{StrVal: val})
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) AppendArrayData(val ArrayData) bool {
|
|
||||||
if t, ok := any(&em.PortVal).(*Port_Array); ok {
|
|
||||||
*t = append(*t, val)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) GetArrayLen() Port_Int {
|
|
||||||
if t, ok := any(&em.PortVal).(*Port_Array); ok {
|
|
||||||
return Port_Int(len(*t))
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) IsPortExec() bool {
|
|
||||||
_, ok := any(em.PortVal).(Port_Exec)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) convertInt64(v any) (int64, bool) {
|
|
||||||
switch v.(type) {
|
|
||||||
case int:
|
|
||||||
return int64(v.(int)), true
|
|
||||||
case int64:
|
|
||||||
return v.(int64), true
|
|
||||||
case int32:
|
|
||||||
return int64(v.(int32)), true
|
|
||||||
case int16:
|
|
||||||
return int64(v.(int16)), true
|
|
||||||
case int8:
|
|
||||||
return int64(v.(int8)), true
|
|
||||||
case uint64:
|
|
||||||
return int64(v.(uint64)), true
|
|
||||||
case uint32:
|
|
||||||
return int64(v.(uint32)), true
|
|
||||||
case uint16:
|
|
||||||
return int64(v.(uint16)), true
|
|
||||||
case uint8:
|
|
||||||
return int64(v.(uint8)), true
|
|
||||||
case uint:
|
|
||||||
return int64(v.(uint)), true
|
|
||||||
default:
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) setAnyVale(v any) error {
|
|
||||||
switch v.(type) {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch any(em.PortVal).(type) {
|
|
||||||
case Port_Int:
|
|
||||||
em.SetInt(val)
|
|
||||||
case Port_Float:
|
|
||||||
em.SetFloat(Port_Float(val))
|
|
||||||
case Port_Str:
|
|
||||||
em.SetStr(fmt.Sprintf("%d", int64(val)))
|
|
||||||
case Port_Bool:
|
|
||||||
em.SetBool(int64(val) != 0)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
|
||||||
}
|
|
||||||
case float64:
|
|
||||||
fV := v.(float64)
|
|
||||||
switch any(em.PortVal).(type) {
|
|
||||||
case Port_Int:
|
|
||||||
em.SetInt(Port_Int(fV))
|
|
||||||
case Port_Float:
|
|
||||||
em.SetFloat(fV)
|
|
||||||
case Port_Str:
|
|
||||||
em.SetStr(fmt.Sprintf("%d", int64(fV)))
|
|
||||||
case Port_Bool:
|
|
||||||
em.SetBool(int64(fV) != 0)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
|
||||||
}
|
|
||||||
case string:
|
|
||||||
strV := v.(string)
|
|
||||||
switch any(em.PortVal).(type) {
|
|
||||||
case Port_Int:
|
|
||||||
val, err := strconv.Atoi(strV)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
em.SetInt(Port_Int(val))
|
|
||||||
case Port_Float:
|
|
||||||
fV, err := strconv.ParseFloat(strV, 64)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
em.SetFloat(fV)
|
|
||||||
case Port_Str:
|
|
||||||
em.SetStr(strV)
|
|
||||||
case Port_Bool:
|
|
||||||
val, err := strconv.ParseBool(strV)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
em.SetBool(val)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
|
||||||
}
|
|
||||||
case bool:
|
|
||||||
strV := v.(bool)
|
|
||||||
switch any(em.PortVal).(type) {
|
|
||||||
case Port_Int:
|
|
||||||
return fmt.Errorf("port type is int, but value is %v", strV)
|
|
||||||
case Port_Float:
|
|
||||||
return fmt.Errorf("port type is float, but value is %v", strV)
|
|
||||||
case Port_Str:
|
|
||||||
return fmt.Errorf("port type is string, but value is %v", strV)
|
|
||||||
case Port_Bool:
|
|
||||||
em.SetBool(strV)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
|
||||||
}
|
|
||||||
case []int64:
|
|
||||||
arr := v.([]int64)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(val)
|
|
||||||
}
|
|
||||||
case []int32:
|
|
||||||
arr := v.([]int32)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(Port_Int(val))
|
|
||||||
}
|
|
||||||
case []int16:
|
|
||||||
arr := v.([]int16)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(Port_Int(val))
|
|
||||||
}
|
|
||||||
case []int8:
|
|
||||||
arr := v.([]int8)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(Port_Int(val))
|
|
||||||
}
|
|
||||||
case []uint64:
|
|
||||||
arr := v.([]uint64)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(Port_Int(val))
|
|
||||||
}
|
|
||||||
case []uint32:
|
|
||||||
arr := v.([]uint32)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(Port_Int(val))
|
|
||||||
}
|
|
||||||
case []uint16:
|
|
||||||
arr := v.([]uint16)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(Port_Int(val))
|
|
||||||
}
|
|
||||||
case []uint8:
|
|
||||||
arr := v.([]uint8)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(Port_Int(val))
|
|
||||||
}
|
|
||||||
case []string:
|
|
||||||
arr := v.([]string)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValStr(val)
|
|
||||||
}
|
|
||||||
case Port_Array:
|
|
||||||
arr := v.(Port_Array)
|
|
||||||
for _, val := range arr {
|
|
||||||
em.AppendArrayValInt(val.IntVal)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Port[T]) SetValue(val IPort) bool {
|
|
||||||
valT, ok := val.(*Port[T])
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
em.PortVal = valT.PortVal
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
type IPort interface {
|
|
||||||
GetInt() (Port_Int, bool)
|
|
||||||
GetFloat() (Port_Float, bool)
|
|
||||||
GetStr() (Port_Str, bool)
|
|
||||||
GetArrayValInt(idx int) (Port_Int, bool)
|
|
||||||
GetArrayValStr(idx int) (Port_Str, bool)
|
|
||||||
GetBool() (Port_Bool, bool)
|
|
||||||
GetArray() (Port_Array, bool)
|
|
||||||
|
|
||||||
SetInt(val Port_Int) bool
|
|
||||||
SetFloat(val Port_Float) bool
|
|
||||||
SetStr(val Port_Str) bool
|
|
||||||
SetBool(val Port_Bool) bool
|
|
||||||
SetArrayValInt(idx int, val Port_Int) bool
|
|
||||||
SetArrayValStr(idx int, val Port_Str) bool
|
|
||||||
AppendArrayValInt(val Port_Int) bool
|
|
||||||
AppendArrayValStr(val Port_Str) bool
|
|
||||||
GetArrayLen() Port_Int
|
|
||||||
Clone() IPort
|
|
||||||
Reset()
|
|
||||||
|
|
||||||
IsPortExec() bool
|
|
||||||
|
|
||||||
setAnyVale(v any) error
|
|
||||||
SetValue(val IPort) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPortExec() IPort {
|
|
||||||
return &Port[Port_Exec]{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPortInt() IPort {
|
|
||||||
return &Port[Port_Int]{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPortFloat() IPort {
|
|
||||||
return &Port[Port_Float]{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPortStr() IPort {
|
|
||||||
return &Port[Port_Str]{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPortBool() IPort {
|
|
||||||
return &Port[Port_Bool]{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPortArray() IPort {
|
|
||||||
return &Port[Port_Array]{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPortByType(typ string) IPort {
|
|
||||||
switch typ {
|
|
||||||
case Config_PortType_Exec:
|
|
||||||
return NewPortExec()
|
|
||||||
case Config_DataType_Int, Config_DataType_Integer:
|
|
||||||
return NewPortInt()
|
|
||||||
case Config_DataType_Float:
|
|
||||||
return NewPortFloat()
|
|
||||||
case Config_DataType_Str:
|
|
||||||
return NewPortStr()
|
|
||||||
case Config_DataType_Bool, Config_DataType_Boolean:
|
|
||||||
return NewPortBool()
|
|
||||||
case Config_DataType_Array:
|
|
||||||
return NewPortArray()
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,784 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math/rand/v2"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 系统入口ID定义,1000以内
|
|
||||||
const (
|
|
||||||
EntranceID_IntParam = 1
|
|
||||||
EntranceID_ArrayParam = 2
|
|
||||||
EntranceID_Timer = 3
|
|
||||||
)
|
|
||||||
|
|
||||||
type Entrance_ArrayParam struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Entrance_ArrayParam) GetName() string {
|
|
||||||
return "Entrance_ArrayParam"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Entrance_ArrayParam) Exec() (int, error) {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Entrance_IntParam struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Entrance_IntParam) GetName() string {
|
|
||||||
return "Entrance_IntParam"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Entrance_IntParam) Exec() (int, error) {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Entrance_Timer struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Entrance_Timer) GetName() string {
|
|
||||||
return "Entrance_Timer"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Entrance_Timer) Exec() (int, error) {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type DebugOutput struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *DebugOutput) GetName() string {
|
|
||||||
return "DebugOutput"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *DebugOutput) Exec() (int, error) {
|
|
||||||
val, ok := em.GetInPortInt(1)
|
|
||||||
if !ok {
|
|
||||||
return 0, fmt.Errorf("output Exec inParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
valStr, ok := em.GetInPortStr(2)
|
|
||||||
if !ok {
|
|
||||||
return 0, fmt.Errorf("output Exec inParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
valArray, ok := em.GetInPortArray(3)
|
|
||||||
if !ok {
|
|
||||||
return 0, fmt.Errorf("output Exec inParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debug("DebugOutput Exec", log.Any("param1", val), log.Any("param2", valStr), log.Any("param3", valArray))
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Sequence struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Sequence) GetName() string {
|
|
||||||
return "Sequence"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Sequence) Exec() (int, error) {
|
|
||||||
for i := range em.outPort {
|
|
||||||
if !em.outPort[i].IsPortExec() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
err := em.DoNext(i)
|
|
||||||
if err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ForeachIntArray struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ForeachIntArray) GetName() string {
|
|
||||||
return "ForeachIntArray"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *ForeachIntArray) Exec() (int, error) {
|
|
||||||
array, ok := em.GetInPortArray(1)
|
|
||||||
if !ok {
|
|
||||||
return 0, fmt.Errorf("ForeachIntArray Exec inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range array {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err := em.DoNext(1)
|
|
||||||
if err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Foreach struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Foreach) GetName() string {
|
|
||||||
return "Foreach"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Foreach) Exec() (int, error) {
|
|
||||||
startIndex, ok := em.ExecContext.InputPorts[1].GetInt()
|
|
||||||
if !ok {
|
|
||||||
return 0, fmt.Errorf("foreach Exec inParam not found")
|
|
||||||
}
|
|
||||||
endIndex, ok := em.ExecContext.InputPorts[2].GetInt()
|
|
||||||
if !ok {
|
|
||||||
return 0, fmt.Errorf("foreach Exec inParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := startIndex; i < endIndex; i++ {
|
|
||||||
em.ExecContext.OutputPorts[2].SetInt(i)
|
|
||||||
err := em.DoNext(0)
|
|
||||||
if err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err := em.DoNext(1)
|
|
||||||
if err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetArrayInt struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *GetArrayInt) GetName() string {
|
|
||||||
return "GetArrayInt"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *GetArrayInt) Exec() (int, error) {
|
|
||||||
inPort := em.GetInPort(0)
|
|
||||||
if inPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt inParam not found")
|
|
||||||
}
|
|
||||||
outPort := em.GetOutPort(0)
|
|
||||||
if outPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt outParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
arrIndexPort := em.GetInPort(1)
|
|
||||||
if arrIndexPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam not found")
|
|
||||||
}
|
|
||||||
arrIndex, ok := arrIndexPort.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if arrIndex < 0 || arrIndex >= inPort.GetArrayLen() {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam out of range,index %d", arrIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
val, ok := inPort.GetArrayValInt(int(arrIndex))
|
|
||||||
if !ok {
|
|
||||||
log.Errorf("GetArrayValInt failed, idx:%d", arrIndex)
|
|
||||||
return -1, fmt.Errorf("GetArrayInt inParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPort.SetInt(val)
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetArrayString struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *GetArrayString) GetName() string {
|
|
||||||
return "GetArrayString"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *GetArrayString) Exec() (int, error) {
|
|
||||||
inPort := em.GetInPort(0)
|
|
||||||
if inPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt inParam 0 not found")
|
|
||||||
}
|
|
||||||
outPort := em.GetOutPort(0)
|
|
||||||
if outPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
arrIndexPort := em.GetInPort(1)
|
|
||||||
if arrIndexPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam 1 not found")
|
|
||||||
}
|
|
||||||
arrIndex, ok := arrIndexPort.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if arrIndex < 0 || arrIndex >= inPort.GetArrayLen() {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam out of range,index %d", arrIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
val, ok := inPort.GetArrayValStr(int(arrIndex))
|
|
||||||
if !ok {
|
|
||||||
log.Errorf("GetArrayValStr failed, idx:%d", arrIndex)
|
|
||||||
return -1, fmt.Errorf("GetArrayInt inParam not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPort.SetStr(val)
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetArrayLen struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *GetArrayLen) GetName() string {
|
|
||||||
return "GetArrayLen"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *GetArrayLen) Exec() (int, error) {
|
|
||||||
inPort := em.GetInPort(0)
|
|
||||||
if inPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt inParam 0 not found")
|
|
||||||
}
|
|
||||||
outPort := em.GetOutPort(0)
|
|
||||||
if outPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPort.SetInt(inPort.GetArrayLen())
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BoolIf 布尔判断
|
|
||||||
type BoolIf struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *BoolIf) GetName() string {
|
|
||||||
return "BoolIf"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *BoolIf) Exec() (int, error) {
|
|
||||||
inPort := em.GetInPort(1)
|
|
||||||
if inPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
ret, ok := inPort.GetBool()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("BoolIf inParam error")
|
|
||||||
}
|
|
||||||
|
|
||||||
if ret {
|
|
||||||
return 1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GreaterThanInteger 大于(整型) >
|
|
||||||
type GreaterThanInteger struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *GreaterThanInteger) GetName() string {
|
|
||||||
return "GreaterThanInteger"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *GreaterThanInteger) Exec() (int, error) {
|
|
||||||
inPortEqual := em.GetInPort(1)
|
|
||||||
if inPortEqual == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortA := em.GetInPort(2)
|
|
||||||
if inPortA == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPorB := em.GetInPort(3)
|
|
||||||
if inPorB == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
ret, ok := inPortEqual.GetBool()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
inA, ok := inPortA.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 2 error")
|
|
||||||
}
|
|
||||||
inB, ok := inPorB.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 3 error")
|
|
||||||
}
|
|
||||||
if ret {
|
|
||||||
if inA >= inB {
|
|
||||||
return 1, nil
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if inA > inB {
|
|
||||||
return 1, nil
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LessThanInteger 小于(整型) <
|
|
||||||
type LessThanInteger struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *LessThanInteger) GetName() string {
|
|
||||||
return "LessThanInteger"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *LessThanInteger) Exec() (int, error) {
|
|
||||||
inPortEqual := em.GetInPort(1)
|
|
||||||
if inPortEqual == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortA := em.GetInPort(2)
|
|
||||||
if inPortA == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPorB := em.GetInPort(3)
|
|
||||||
if inPorB == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
ret, ok := inPortEqual.GetBool()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
inA, ok := inPortA.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 2 error")
|
|
||||||
}
|
|
||||||
inB, ok := inPorB.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 3 error")
|
|
||||||
}
|
|
||||||
if ret {
|
|
||||||
if inA <= inB {
|
|
||||||
return 1, nil
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if inA < inB {
|
|
||||||
return 1, nil
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// EqualInteger 等于(整型)==
|
|
||||||
type EqualInteger struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *EqualInteger) GetName() string {
|
|
||||||
return "EqualInteger"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *EqualInteger) Exec() (int, error) {
|
|
||||||
|
|
||||||
inPortA := em.GetInPort(1)
|
|
||||||
if inPortA == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPorB := em.GetInPort(2)
|
|
||||||
if inPorB == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inA, ok := inPortA.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 2 error")
|
|
||||||
}
|
|
||||||
inB, ok := inPorB.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 3 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
if inA == inB {
|
|
||||||
return 1, nil
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RangeCompare 范围比较<=
|
|
||||||
type RangeCompare struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *RangeCompare) GetName() string {
|
|
||||||
return "RangeCompare"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *RangeCompare) 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Probability) GetName() string {
|
|
||||||
return "Probability"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *Probability) Exec() (int, error) {
|
|
||||||
inPortProbability := em.GetInPort(1)
|
|
||||||
if inPortProbability == nil {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inProbability, ok := inPortProbability.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
if inProbability > rand.Int64N(10000) {
|
|
||||||
return 1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateIntArray 创建整型数组
|
|
||||||
type CreateIntArray struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *CreateIntArray) GetName() string {
|
|
||||||
return "CreateIntArray"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *CreateIntArray) Exec() (int, error) {
|
|
||||||
intArray := em.execNode.GetInPortDefaultIntArrayValue(0)
|
|
||||||
if intArray == nil {
|
|
||||||
return -1, fmt.Errorf("CreateIntArray inParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPort := em.GetOutPort(0)
|
|
||||||
if outPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range intArray {
|
|
||||||
outPort.AppendArrayValInt(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateStringArray 创建字符串数组
|
|
||||||
type CreateStringArray struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *CreateStringArray) GetName() string {
|
|
||||||
return "CreateStringArray"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *CreateStringArray) Exec() (int, error) {
|
|
||||||
intArray := em.execNode.GetInPortDefaultStringArrayValue(0)
|
|
||||||
if intArray == nil {
|
|
||||||
return -1, fmt.Errorf("CreateIntArray inParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPort := em.GetOutPort(0)
|
|
||||||
if outPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range intArray {
|
|
||||||
outPort.AppendArrayValStr(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendIntegerToArray 数组追加整型
|
|
||||||
type AppendIntegerToArray struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AppendIntegerToArray) GetName() string {
|
|
||||||
return "AppendIntegerToArray"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AppendIntegerToArray) Exec() (int, error) {
|
|
||||||
inPortArray := em.GetInPort(0)
|
|
||||||
if inPortArray == nil {
|
|
||||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortVal := em.GetInPort(1)
|
|
||||||
if inPortVal == nil {
|
|
||||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPort := em.GetOutPort(0)
|
|
||||||
if outPort == nil {
|
|
||||||
return -1, fmt.Errorf("AppendIntegerToArray outParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
intArray, ok := inPortArray.GetArray()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 0 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
intVal, ok := inPortVal.GetInt()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 1 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range intArray {
|
|
||||||
outPort.AppendArrayValInt(intArray[i].IntVal)
|
|
||||||
}
|
|
||||||
outPort.AppendArrayValInt(intVal)
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendStringToArray 数组追加字符串
|
|
||||||
type AppendStringToArray struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AppendStringToArray) GetName() string {
|
|
||||||
return "AppendStringToArray"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AppendStringToArray) Exec() (int, error) {
|
|
||||||
inPortArray := em.GetInPort(0)
|
|
||||||
if inPortArray == nil {
|
|
||||||
return -1, fmt.Errorf("AppendStringToArray inParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
inPortVal := em.GetInPort(1)
|
|
||||||
if inPortVal == nil {
|
|
||||||
return -1, fmt.Errorf("AppendStringToArray inParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPort := em.GetOutPort(0)
|
|
||||||
if outPort == nil {
|
|
||||||
return -1, fmt.Errorf("AppendStringToArray outParam 0 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
intArray, ok := inPortArray.GetArray()
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AppendStringToArray inParam 0 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range intArray {
|
|
||||||
outPort.AppendArrayValStr(intArray[i].StrVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateTimer 创建定时器
|
|
||||||
type CreateTimer struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *CreateTimer) GetName() string {
|
|
||||||
return "CreateTimer"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *CreateTimer) Exec() (int, error) {
|
|
||||||
delay, ok := em.GetInPortInt(0)
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("CreateTimer inParam 0 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
array, ok := em.GetInPortArray(1)
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("CreateTimer inParam 1 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
var timerId uint64
|
|
||||||
graphID := em.gr.graphID
|
|
||||||
em.gr.IBlueprintModule.SafeAfterFunc(&timerId, time.Duration(delay)*time.Millisecond, nil, func(timerId uint64, additionData interface{}) {
|
|
||||||
err := em.gr.IBlueprintModule.TriggerEvent(graphID, EntranceID_Timer, array)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("CreateTimer SafeAfterFunc error timerId:%d err:%v", timerId, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
em.gr.IBlueprintModule.CancelTimerId(graphID, &timerId)
|
|
||||||
})
|
|
||||||
|
|
||||||
em.gr.mapTimerID[timerId] = struct{}{}
|
|
||||||
|
|
||||||
outPort := em.GetOutPort(1)
|
|
||||||
if outPort == nil {
|
|
||||||
return -1, fmt.Errorf("CreateTimer outParam 1 not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
outPort.SetInt(int64(timerId))
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CloseTimer 关闭定时器
|
|
||||||
type CloseTimer struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *CloseTimer) GetName() string {
|
|
||||||
return "CloseTimer"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *CloseTimer) Exec() (int, error) {
|
|
||||||
timerID, ok := em.GetInPortInt(1)
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("CreateTimer inParam 1 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
id := uint64(timerID)
|
|
||||||
ok = em.gr.IBlueprintModule.CancelTimerId(em.gr.graphID, &id)
|
|
||||||
if !ok {
|
|
||||||
log.Warnf("CloseTimer CancelTimerId:%d", id)
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// AppendIntReturn 追加返回结果(Int)
|
|
||||||
type AppendIntReturn struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AppendIntReturn) GetName() string {
|
|
||||||
return "AppendIntReturn"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AppendIntReturn) Exec() (int, error) {
|
|
||||||
val, ok := em.GetInPortInt(1)
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AppendIntReturn inParam 1 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
returnPort := em.gr.GetAndCreateReturnPort()
|
|
||||||
if returnPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetAndCreateReturnPort fail")
|
|
||||||
}
|
|
||||||
returnPort.AppendArrayValInt(val)
|
|
||||||
|
|
||||||
return -0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// AppendStringReturn 追加返回结果(String)
|
|
||||||
type AppendStringReturn struct {
|
|
||||||
BaseExecNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AppendStringReturn) GetName() string {
|
|
||||||
return "AppendStringReturn"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (em *AppendStringReturn) Exec() (int, error) {
|
|
||||||
val, ok := em.GetInPortStr(1)
|
|
||||||
if !ok {
|
|
||||||
return -1, fmt.Errorf("AppendStringReturn inParam 1 error")
|
|
||||||
}
|
|
||||||
|
|
||||||
returnPort := em.gr.GetAndCreateReturnPort()
|
|
||||||
if returnPort == nil {
|
|
||||||
return -1, fmt.Errorf("GetAndCreateReturnPort fail")
|
|
||||||
}
|
|
||||||
returnPort.AppendArrayValStr(val)
|
|
||||||
|
|
||||||
return -0, nil
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
type ArrayElement struct {
|
|
||||||
IntVal int64
|
|
||||||
StrVal string
|
|
||||||
}
|
|
||||||
|
|
||||||
type PortExec struct {
|
|
||||||
}
|
|
||||||
type ArrayData struct {
|
|
||||||
IntVal int64
|
|
||||||
StrVal string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Port_Exec = PortExec
|
|
||||||
type Port_Int = int64
|
|
||||||
type Port_Float = float64
|
|
||||||
type Port_Str = string
|
|
||||||
type Port_Bool = bool
|
|
||||||
|
|
||||||
type Port_Array []ArrayData
|
|
||||||
|
|
||||||
type iPortType interface {
|
|
||||||
Port_Exec | Port_Int | Port_Float | Port_Str | Port_Bool | Port_Array
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
package blueprint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const GetVariables = "GetVar"
|
|
||||||
const SetVariables = "SetVar"
|
|
||||||
const globalVariablesPrefix = "g_"
|
|
||||||
|
|
||||||
type GetVariablesNode struct {
|
|
||||||
BaseExecNode
|
|
||||||
nodeName string
|
|
||||||
}
|
|
||||||
|
|
||||||
type SetVariablesNode struct {
|
|
||||||
BaseExecNode
|
|
||||||
nodeName string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GetVariablesNode) GetName() string {
|
|
||||||
return g.nodeName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GetVariablesNode) Exec() (int, error) {
|
|
||||||
var port IPort
|
|
||||||
if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
|
|
||||||
port = g.gr.globalVariables[g.GetVariableName()]
|
|
||||||
} else {
|
|
||||||
port = g.gr.variables[g.GetVariableName()]
|
|
||||||
}
|
|
||||||
|
|
||||||
if port == nil {
|
|
||||||
return -1, fmt.Errorf("variable %s not found,node name %s", g.GetVariableName(), g.nodeName)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !g.SetOutPort(0, port) {
|
|
||||||
return -1, fmt.Errorf("set out port failed,node name %s", g.nodeName)
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func (g *SetVariablesNode) GetName() string {
|
|
||||||
return g.nodeName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *SetVariablesNode) Exec() (int, error) {
|
|
||||||
port := g.GetInPort(1)
|
|
||||||
if port == nil {
|
|
||||||
return -1, fmt.Errorf("get in port failed,node name %s", g.nodeName)
|
|
||||||
}
|
|
||||||
|
|
||||||
varPort := port.Clone()
|
|
||||||
if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
|
|
||||||
g.gr.globalVariables[g.GetVariableName()] = varPort
|
|
||||||
} else {
|
|
||||||
g.gr.variables[g.GetVariableName()] = varPort
|
|
||||||
}
|
|
||||||
|
|
||||||
if !g.SetOutPort(1, varPort) {
|
|
||||||
return -1, fmt.Errorf("set out port failed,node name %s", g.nodeName)
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package stack
|
|
||||||
|
|
||||||
// Stack 是一个通用类型的栈结构
|
|
||||||
type Stack[T any] struct {
|
|
||||||
items []*T
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push 将元素压入栈顶
|
|
||||||
func (s *Stack[T]) Push(item *T) {
|
|
||||||
s.items = append(s.items, item)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pop 弹出并返回栈顶元素,如果栈为空则返回错误
|
|
||||||
func (s *Stack[T]) Pop() *T {
|
|
||||||
if len(s.items) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
index := len(s.items) - 1
|
|
||||||
item := s.items[index]
|
|
||||||
s.items = s.items[:index]
|
|
||||||
return item
|
|
||||||
}
|
|
||||||
|
|
||||||
// Peek 返回栈顶元素但不移除,如果栈为空则返回错误
|
|
||||||
func (s *Stack[T]) Peek() *T {
|
|
||||||
if len(s.items) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return s.items[len(s.items)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsEmpty 检查栈是否为空
|
|
||||||
func (s *Stack[T]) IsEmpty() bool {
|
|
||||||
return len(s.items) == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Size 返回栈中元素的数量
|
|
||||||
func (s *Stack[T]) Size() int {
|
|
||||||
return len(s.items)
|
|
||||||
}
|
|
||||||
@@ -6,21 +6,21 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupTimer(timer ITimer) ITimer {
|
func SetupTimer(timer ITimer) ITimer{
|
||||||
if timer.IsOpen() == true {
|
if timer.IsOpen() == true {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
timer.Open(true)
|
timer.Open(true)
|
||||||
timerHeapLock.Lock() // 使用锁规避竞争条件
|
timerHeapLock.Lock() // 使用锁规避竞争条件
|
||||||
heap.Push(&timerHeap, timer)
|
heap.Push(&timerHeap,timer)
|
||||||
timerHeapLock.Unlock()
|
timerHeapLock.Unlock()
|
||||||
return timer
|
return timer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTimer(d time.Duration) *Timer {
|
func NewTimer(d time.Duration) *Timer{
|
||||||
c := make(chan ITimer, 1)
|
c := make(chan ITimer,1)
|
||||||
timer := newTimer(d, c, nil, "")
|
timer := newTimer(d,c,nil,"")
|
||||||
SetupTimer(timer)
|
SetupTimer(timer)
|
||||||
|
|
||||||
return timer
|
return timer
|
||||||
@@ -43,7 +43,7 @@ func (h *_TimerHeap) Less(i, j int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *_TimerHeap) Swap(i, j int) {
|
func (h *_TimerHeap) Swap(i, j int) {
|
||||||
h.timers[i], h.timers[j] = h.timers[j], h.timers[i]
|
h.timers[i],h.timers[j] = h.timers[j],h.timers[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *_TimerHeap) Push(x interface{}) {
|
func (h *_TimerHeap) Push(x interface{}) {
|
||||||
@@ -62,16 +62,16 @@ var (
|
|||||||
timeOffset time.Duration
|
timeOffset time.Duration
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartTimer(minTimerInterval time.Duration, maxTimerNum int) {
|
func StartTimer(minTimerInterval time.Duration,maxTimerNum int){
|
||||||
timerHeap.timers = make([]ITimer, 0, maxTimerNum)
|
timerHeap.timers = make([]ITimer,0,maxTimerNum)
|
||||||
heap.Init(&timerHeap) // 初始化定时器heap
|
heap.Init(&timerHeap) // 初始化定时器heap
|
||||||
|
|
||||||
go tickRoutine(minTimerInterval)
|
go tickRoutine(minTimerInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tickRoutine(minTimerInterval time.Duration) {
|
func tickRoutine(minTimerInterval time.Duration){
|
||||||
var bContinue bool
|
var bContinue bool
|
||||||
for {
|
for{
|
||||||
bContinue = tick()
|
bContinue = tick()
|
||||||
if bContinue == false {
|
if bContinue == false {
|
||||||
time.Sleep(minTimerInterval)
|
time.Sleep(minTimerInterval)
|
||||||
@@ -79,7 +79,7 @@ func tickRoutine(minTimerInterval time.Duration) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tick() bool {
|
func tick() bool{
|
||||||
now := Now()
|
now := Now()
|
||||||
timerHeapLock.Lock()
|
timerHeapLock.Lock()
|
||||||
if timerHeap.Len() <= 0 { // 没有任何定时器,立刻返回
|
if timerHeap.Len() <= 0 { // 没有任何定时器,立刻返回
|
||||||
@@ -100,7 +100,7 @@ func tick() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func Now() time.Time {
|
func Now() time.Time{
|
||||||
if timeOffset == 0 {
|
if timeOffset == 0 {
|
||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
@@ -108,12 +108,4 @@ func Now() time.Time {
|
|||||||
return time.Now().Add(timeOffset)
|
return time.Now().Add(timeOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTimeOffset 设置时间偏移量
|
|
||||||
func SetTimeOffset(offset time.Duration) {
|
|
||||||
timeOffset = offset
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddTimeOffset 添加时间偏移量
|
|
||||||
func AddTimeOffset(addOffset time.Duration) {
|
|
||||||
timeOffset += addOffset
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user