File was renamed from mc/micronode.go |
| | |
| | | |
| | | import ( |
| | | "basic.com/valib/bhomebus.git" |
| | | "bhomeclient" |
| | | "context" |
| | | "encoding/json" |
| | | "errors" |
| | |
| | | |
| | | type MicroNode struct { |
| | | ctx context.Context |
| | | handle *bhomeclient.BHBus |
| | | reg *bhomeclient.RegisterInfo |
| | | procInfo *bhomeclient.ProcInfo |
| | | handle *BHBus |
| | | reg *RegisterInfo |
| | | procInfo *ProcInfo |
| | | handlers map[string]MicroFunc |
| | | serverId string |
| | | fnLog func(...interface{}) |
| | | |
| | | SubChM map[string]chan *bhomeclient.MsgInfo //以订阅的主题为key |
| | | SubChM map[string]chan *MsgInfo //以订阅的主题为key |
| | | } |
| | | |
| | | func NewMicroNode(ctx context.Context,q chan os.Signal, serverId string, reg *bhomeclient.RegisterInfo, procInfo *bhomeclient.ProcInfo,fnLog func(v ...interface{})) (*MicroNode, error){ |
| | | conf := bhomeclient.NewConfig(bhomeclient.KEY_REGISTER,512,5,10,10,100, fnLog) |
| | | handle, err := bhomeclient.Register(ctx, q, conf, reg) |
| | | func NewMicroNode(ctx context.Context,q chan os.Signal, serverId string, reg *RegisterInfo, procInfo *ProcInfo,fnLog func(v ...interface{})) (*MicroNode, error){ |
| | | conf := NewConfig(KEY_REGISTER,512,5,10,10,100, fnLog) |
| | | handle, err := Register(ctx, q, conf, reg) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | |
| | | reg: reg, |
| | | procInfo: procInfo, |
| | | fnLog: fnLog, |
| | | SubChM: make(map[string]chan *bhomeclient.MsgInfo), |
| | | SubChM: make(map[string]chan *MsgInfo), |
| | | } |
| | | for _,subTopic := range reg.SubTopic { |
| | | mn.SubChM[subTopic] = make(chan *bhomeclient.MsgInfo, 512) |
| | | mn.SubChM[subTopic] = make(chan *MsgInfo, 512) |
| | | } |
| | | |
| | | return mn, nil |
| | |
| | | } |
| | | } |
| | | |
| | | func (ms *MicroNode) UpdateNodeTopics(ts []bhomeclient.NodeInfo) { |
| | | func (ms *MicroNode) UpdateNodeTopics(ts []NodeInfo) { |
| | | ms.handle.UpdateNodeTopics(ts) |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | func (ms *MicroNode) startHeartbeat() { |
| | | hbi := &bhomeclient.HeartBeatInfo{ |
| | | hbi := &HeartBeatInfo{ |
| | | HealthLevel: "health", |
| | | Fps: 12, |
| | | WarnInfo: "warn", |
| | |
| | | ms.printLog("1:", time.Since(t)) |
| | | t = time.Now() |
| | | rb, _ := json.Marshal(request) |
| | | msgR := &bhomeclient.MsgInfo { |
| | | msgR := &MsgInfo { |
| | | Topic: request.Path, |
| | | Body: rb, |
| | | } |
| | |
| | | |
| | | func (ms *MicroNode) RequestTopic(serverId string, request Request) (*Reply,error) { |
| | | rb, _ := json.Marshal(request) |
| | | msgR := &bhomeclient.MsgInfo{ |
| | | msgR := &MsgInfo{ |
| | | Topic: request.Path, |
| | | Body: rb, |
| | | } |
| | |
| | | return netNodes |
| | | } |
| | | |
| | | func (ms *MicroNode) serve(msgR *bhomeclient.MsgInfo, p int) { |
| | | func (ms *MicroNode) serve(msgR *MsgInfo, p int) { |
| | | var reqBody Request |
| | | err := json.Unmarshal(msgR.Body, &reqBody) |
| | | if err != nil { |
| | |
| | | if err != nil { |
| | | ms.printLog("marshal *ri err:", err) |
| | | } |
| | | rMsg := bhomeclient.MsgInfo{ |
| | | rMsg := MsgInfo{ |
| | | Body: rd, |
| | | } |
| | | ms.handle.Reply(p, rMsg) |
| | |
| | | } |
| | | |
| | | func (ms *MicroNode) PublishNet(nodes []bhomebus.NetNode, topic string,msg []byte) error { |
| | | pi := &bhomeclient.MsgInfo{ |
| | | pi := &MsgInfo{ |
| | | Topic: topic, |
| | | Body: msg, |
| | | } |