github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/envelope_carrier.go (about)

     1  package kit
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/clubpay/ronykit/kit/utils"
     7  	"github.com/goccy/go-reflect"
     8  )
     9  
    10  type carrierKind int
    11  
    12  const (
    13  	incomingCarrier carrierKind = iota + 1
    14  	outgoingCarrier
    15  	eofCarrier
    16  )
    17  
    18  // envelopeCarrier is a serializable message which is used by Cluster component of the
    19  // EdgeServer to send information from one instance to another instance.
    20  type envelopeCarrier struct {
    21  	// SessionID is a unique identifier for each remote-execution session.
    22  	SessionID string `json:"id"`
    23  	// Kind identifies what type of the data this carrier has
    24  	Kind carrierKind `json:"kind"`
    25  	// OriginID the instance's id of the sender of this message
    26  	OriginID string `json:"originID"`
    27  	// TargetID the instance's id of the receiver of this message
    28  	TargetID string       `json:"targetID"`
    29  	Data     *carrierData `json:"data"`
    30  }
    31  
    32  func (ec *envelopeCarrier) FillWithContext(ctx *Context) *envelopeCarrier {
    33  	ec.Data = &carrierData{
    34  		EnvelopeID:  ctx.In().GetID(),
    35  		IsREST:      ctx.IsREST(),
    36  		MsgType:     reflect.TypeOf(ctx.In().GetMsg()).String(),
    37  		Msg:         marshalEnvelopeCarrier(ctx.In().GetMsg()),
    38  		ContractID:  ctx.ContractID(),
    39  		ServiceName: ctx.ServiceName(),
    40  		Route:       ctx.Route(),
    41  		ConnHdr:     map[string]string{},
    42  		Hdr:         map[string]string{},
    43  	}
    44  
    45  	if tp := ctx.sb.tp; tp != nil {
    46  		tp.Inject(ctx.ctx, ec.Data)
    47  	}
    48  
    49  	ctx.In().
    50  		WalkHdr(func(key, val string) bool {
    51  			ec.Data.Hdr[key] = val
    52  
    53  			return true
    54  		})
    55  
    56  	ctx.Conn().
    57  		Walk(func(key string, val string) bool {
    58  			ec.Data.ConnHdr[key] = val
    59  
    60  			return true
    61  		})
    62  
    63  	return ec
    64  }
    65  
    66  func (ec *envelopeCarrier) FillWithEnvelope(e *Envelope) *envelopeCarrier {
    67  	ec.Data = &carrierData{
    68  		EnvelopeID:  utils.B2S(e.id),
    69  		IsREST:      e.ctx.IsREST(),
    70  		MsgType:     reflect.TypeOf(e.GetMsg()).String(),
    71  		Msg:         marshalEnvelopeCarrier(e.GetMsg()),
    72  		ContractID:  e.ctx.ContractID(),
    73  		ServiceName: e.ctx.ServiceName(),
    74  		Route:       e.ctx.Route(),
    75  		ConnHdr:     map[string]string{},
    76  		Hdr:         map[string]string{},
    77  	}
    78  
    79  	e.WalkHdr(func(key string, val string) bool {
    80  		ec.Data.Hdr[key] = val
    81  
    82  		return true
    83  	})
    84  
    85  	e.conn.Walk(func(key string, val string) bool {
    86  		ec.Data.ConnHdr[key] = val
    87  
    88  		return true
    89  	})
    90  
    91  	return ec
    92  }
    93  
    94  type carrierData struct {
    95  	EnvelopeID  string            `json:"envelopID,omitempty"`
    96  	ConnHdr     map[string]string `json:"connHdr,omitempty"`
    97  	IsREST      bool              `json:"isREST,omitempty"`
    98  	Hdr         map[string]string `json:"hdr,omitempty"`
    99  	MsgType     string            `json:"msgType,omitempty"`
   100  	Msg         []byte            `json:"msg,omitempty"`
   101  	ContractID  string            `json:"cid,omitempty"`
   102  	ServiceName string            `json:"svc,omitempty"`
   103  	Route       string            `json:"route,omitempty"`
   104  }
   105  
   106  func (c carrierData) Get(key string) string {
   107  	return c.ConnHdr[key]
   108  }
   109  
   110  func (c carrierData) Set(key string, value string) {
   111  	c.ConnHdr[key] = value
   112  }
   113  
   114  func (ec *envelopeCarrier) ToJSON() []byte {
   115  	data, _ := json.Marshal(ec)
   116  
   117  	return data
   118  }
   119  
   120  func (ec *envelopeCarrier) FromJSON(data []byte) error {
   121  	err := json.Unmarshal(data, ec)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  func newEnvelopeCarrier(kind carrierKind, sessionID, originID, targetID string) *envelopeCarrier {
   130  	return &envelopeCarrier{
   131  		Kind:      kind,
   132  		SessionID: sessionID,
   133  		OriginID:  originID,
   134  		TargetID:  targetID,
   135  	}
   136  }
   137  
   138  func unmarshalEnvelopeCarrier(data []byte, m Message) {
   139  	err := json.Unmarshal(data, m)
   140  	if err != nil {
   141  		panic(err)
   142  	}
   143  }
   144  
   145  func marshalEnvelopeCarrier(m Message) []byte {
   146  	switch v := m.(type) {
   147  	case RawMessage:
   148  		return v
   149  	}
   150  	data, err := json.Marshal(m)
   151  	if err != nil {
   152  		panic(err)
   153  	}
   154  
   155  	return data
   156  }