github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/pehrest/hash-listen-to-key.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package pehrest
     4  
     5  import (
     6  	"../achaemenid"
     7  	"../authorization"
     8  	er "../error"
     9  	"../ganjine"
    10  	lang "../language"
    11  	"../srpc"
    12  )
    13  
    14  // HashListenToKeyService store details about HashListenToKey service
    15  var HashListenToKeyService = achaemenid.Service{
    16  	ID:                115550110,
    17  	IssueDate:         1587282740,
    18  	ExpiryDate:        0,
    19  	ExpireInFavorOf:   "", // English name of favor service just to show off!
    20  	ExpireInFavorOfID: 0,
    21  	Status:            achaemenid.ServiceStatePreAlpha,
    22  
    23  	Authorization: authorization.Service{
    24  		CRUD:     authorization.CRUDRead,
    25  		UserType: authorization.UserTypeApp,
    26  	},
    27  
    28  	Name: map[lang.Language]string{
    29  		lang.LanguageEnglish: "Index Hash - Listen To Key",
    30  	},
    31  	Description: map[lang.Language]string{
    32  		lang.LanguageEnglish: `get records to given index hash when new record set!
    33  Request Must send to specific node that handle that hash index range!!
    34  This service has a lot of use cases like:
    35  - any geospatial usage e.g. tracking device or user, ...
    36  - following content author like telegram channels or instagram live video!`,
    37  	},
    38  	TAGS: []string{
    39  		"",
    40  	},
    41  
    42  	SRPCHandler: HashListenToKeySRPC,
    43  }
    44  
    45  // HashListenToKeySRPC is sRPC handler of HashListenToKey service.
    46  func HashListenToKeySRPC(st *achaemenid.Stream) {
    47  	if st.Connection.UserID != achaemenid.Server.AppID {
    48  		// TODO::: Attack??
    49  		st.Err = ganjine.ErrNotAuthorizeRequest
    50  		return
    51  	}
    52  
    53  	var req = &HashListenToKeyReq{}
    54  	req.SyllabDecoder(srpc.GetPayload(st.IncomePayload))
    55  
    56  	var res *HashListenToKeyRes
    57  	res, st.Err = HashListenToKey(req)
    58  	if st.Err != nil {
    59  		return
    60  	}
    61  
    62  	st.OutcomePayload = res.SyllabEncoder()
    63  }
    64  
    65  // HashListenToKeyReq is request structure of HashListenToKey()
    66  type HashListenToKeyReq struct {
    67  	IndexKey       [32]byte
    68  	ReceiveChannel chan []byte `syllab:"-"`
    69  }
    70  
    71  // HashListenToKeyRes is response structure of HashListenToKey()
    72  type HashListenToKeyRes struct {
    73  	Record []byte
    74  }
    75  
    76  // HashListenToKey get the recordID by index hash when new record set!
    77  func HashListenToKey(req *HashListenToKeyReq) (res *HashListenToKeyRes, err *er.Error) {
    78  	// TODO::: it can't be simple byte, maybe channel
    79  	return
    80  }
    81  
    82  /*
    83  	-- Syllab Encoder & Decoder --
    84  */
    85  
    86  // SyllabDecoder decode from buf to req
    87  // Due to this service just use internally, It skip check buf size syllab rule! Panic occur if bad request received!
    88  func (req *HashListenToKeyReq) SyllabDecoder(buf []byte) {
    89  	copy(req.IndexKey[:], buf[:])
    90  	return
    91  }
    92  
    93  // SyllabEncoder encode req to buf
    94  func (req *HashListenToKeyReq) SyllabEncoder() (buf []byte) {
    95  	buf = make([]byte, req.syllabLen()+4) // +4 for sRPC ID instead get offset argument
    96  	copy(buf[4:], req.IndexKey[:])
    97  	return
    98  }
    99  
   100  func (req *HashListenToKeyReq) syllabStackLen() (ln uint32) {
   101  	return 32
   102  }
   103  
   104  func (req *HashListenToKeyReq) syllabHeapLen() (ln uint32) {
   105  	return
   106  }
   107  
   108  func (req *HashListenToKeyReq) syllabLen() (ln uint64) {
   109  	return uint64(req.syllabStackLen() + req.syllabHeapLen())
   110  }
   111  
   112  // SyllabDecoder decode from buf to res
   113  // Due to this service just use internally, It skip check buf size syllab rule! Panic occur if bad request received!
   114  func (res *HashListenToKeyRes) SyllabDecoder(buf []byte) {
   115  	// Due to just have one field in res structure we break syllab rules and skip get address and size of res.Record from buf
   116  	res.Record = buf
   117  }
   118  
   119  // SyllabEncoder encode req to buf
   120  func (res *HashListenToKeyRes) SyllabEncoder() (buf []byte) {
   121  	buf = make([]byte, res.syllabLen()+4) // +4 for sRPC ID instead get offset argument
   122  	// Due to just have one field in res structure we break syllab rules and skip set address and size of res.Record in buf
   123  	// syllab.SetUInt32(buf, 4, res.syllabStackLen())
   124  	// syllab.SetUInt32(buf, 8, uint32(len(res.Record)))
   125  	copy(buf[4:], res.Record)
   126  	return
   127  }
   128  
   129  func (res *HashListenToKeyRes) syllabStackLen() (ln uint32) {
   130  	return 0
   131  }
   132  
   133  func (res *HashListenToKeyRes) syllabHeapLen() (ln uint32) {
   134  	ln = uint32(len(res.Record))
   135  	return
   136  }
   137  
   138  func (res *HashListenToKeyRes) syllabLen() (ln uint64) {
   139  	return uint64(res.syllabStackLen() + res.syllabHeapLen())
   140  }