github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/core/node_base.go (about)

     1  package core
     2  
     3  import (
     4  	"github.com/15mga/kiwi"
     5  	"github.com/15mga/kiwi/util"
     6  )
     7  
     8  func newNodeBase() nodeBase {
     9  	return nodeBase{}
    10  }
    11  
    12  type nodeBase struct {
    13  }
    14  
    15  func (n *nodeBase) Init() *util.Err {
    16  	return nil
    17  }
    18  
    19  func (n *nodeBase) Connect(ip string, port int, svc kiwi.TSvc, nodeId int64, ver string, head util.M) {
    20  }
    21  
    22  func (n *nodeBase) Disconnect(svc kiwi.TSvc, id int64) {
    23  }
    24  
    25  func (n *nodeBase) Push(pus kiwi.ISndPush) {
    26  	panic("implement me")
    27  }
    28  
    29  func (n *nodeBase) PushNode(nodeId int64, pus kiwi.ISndPush) {
    30  	panic("implement me")
    31  }
    32  
    33  func (n *nodeBase) Request(req kiwi.ISndRequest) {
    34  	panic("implement me")
    35  }
    36  
    37  func (n *nodeBase) RequestNode(nodeId int64, req kiwi.ISndRequest) {
    38  	panic("implement me")
    39  }
    40  
    41  func (n *nodeBase) Notify(ntf kiwi.ISndNotice) {
    42  	panic("implement me")
    43  }
    44  
    45  func (n *nodeBase) ReceiveWatchNotice(nodeId int64, codes []kiwi.TCode) {
    46  	panic("implement me")
    47  }
    48  
    49  func (n *nodeBase) SendToNode(nodeId int64, bytes []byte, fnErr util.FnErr) {
    50  	//TODO implement me
    51  	panic("implement me")
    52  }
    53  
    54  func (n *nodeBase) receive(agent kiwi.IAgent, bytes []byte) {
    55  	switch bytes[0] {
    56  	case HdPush:
    57  		n.onPush(agent, bytes)
    58  	case HdRequest:
    59  		n.onRequest(agent, bytes)
    60  	case HdOk:
    61  		n.onResponseOk(agent, bytes)
    62  	case HdFail:
    63  		n.onResponseFail(agent, bytes)
    64  	case HdHeartbeat:
    65  		n.onHeartbeat(agent, bytes)
    66  	case HdNotify:
    67  		n.onNotify(agent, bytes)
    68  	case HdWatch:
    69  		n.onWatchNotify(agent, bytes)
    70  	default:
    71  		kiwi.Error2(util.EcNotExist, util.M{
    72  			"head": bytes[0],
    73  		})
    74  	}
    75  }
    76  
    77  func (n *nodeBase) onHeartbeat(agent kiwi.IAgent, bytes []byte) {
    78  
    79  }
    80  
    81  func (n *nodeBase) onPush(agent kiwi.IAgent, bytes []byte) {
    82  	pkt := NewRcvPusPkt()
    83  	err := kiwi.Packer().UnpackPush(bytes, pkt)
    84  	if err != nil {
    85  		if agent != nil {
    86  			err.AddParam("addr", agent.Addr())
    87  		}
    88  		kiwi.Error(err)
    89  		return
    90  	}
    91  	kiwi.Router().OnPush(pkt)
    92  }
    93  
    94  func (n *nodeBase) onRequest(agent kiwi.IAgent, bytes []byte) {
    95  	pkt := NewRcvReqPkt()
    96  	err := kiwi.Packer().UnpackRequest(bytes, pkt)
    97  	if err != nil {
    98  		if agent != nil {
    99  			err.AddParam("addr", agent.Addr())
   100  		}
   101  		kiwi.Error(err)
   102  		return
   103  	}
   104  	kiwi.Router().OnRequest(pkt)
   105  }
   106  
   107  func (n *nodeBase) onResponseOk(agent kiwi.IAgent, bytes []byte) {
   108  	head := make(util.M)
   109  	tid, payload, err := kiwi.Packer().UnpackResponseOk(bytes, head)
   110  	if err != nil {
   111  		if agent != nil {
   112  			err.AddParam("addr", agent.Addr())
   113  		}
   114  		kiwi.Error(err)
   115  		return
   116  	}
   117  	kiwi.Router().OnResponseOkBytes(tid, head, payload)
   118  }
   119  
   120  func (n *nodeBase) onResponseFail(agent kiwi.IAgent, bytes []byte) {
   121  	head := make(util.M)
   122  	tid, code, err := kiwi.Packer().UnpackResponseFail(bytes, head)
   123  	if err != nil {
   124  		if agent != nil {
   125  			err.AddParam("addr", agent.Addr())
   126  		}
   127  		kiwi.TE(tid, err)
   128  		return
   129  	}
   130  	kiwi.Router().OnResponseFail(tid, head, code)
   131  }
   132  
   133  func (n *nodeBase) onNotify(agent kiwi.IAgent, bytes []byte) {
   134  	pkt := NewRcvNtfPkt()
   135  	err := kiwi.Packer().UnpackNotify(bytes, pkt)
   136  	if err != nil {
   137  		if agent != nil {
   138  			err.AddParam("addr", agent.Addr())
   139  		}
   140  		kiwi.Error(err)
   141  		return
   142  	}
   143  	kiwi.Router().OnNotice(pkt)
   144  }
   145  
   146  func (n *nodeBase) onWatchNotify(agent kiwi.IAgent, bytes []byte) {
   147  	nodeId, codes, err := kiwi.Packer().UnpackWatchNotify(bytes)
   148  	if err != nil {
   149  		if agent != nil {
   150  			err.AddParam("addr", agent.Addr())
   151  		}
   152  		kiwi.Error(err)
   153  		return
   154  	}
   155  	kiwi.Node().ReceiveWatchNotice(nodeId, codes)
   156  }