github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/testEnv/pb/service/service.go (about)

     1  package service
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ronaksoft/rony"
     7  	"github.com/ronaksoft/rony/edge"
     8  	"github.com/ronaksoft/rony/errors"
     9  	"github.com/ronaksoft/rony/tools"
    10  )
    11  
    12  /*
    13     Creation Time: 2020 - Mar - 06
    14     Created by:  (ehsan)
    15     Maintainers:
    16        1.  Ehsan N. Moosa (E2)
    17     Auditor: Ehsan N. Moosa (E2)
    18     Copyright Ronak Software Group 2020
    19  */
    20  
    21  //go:generate protoc -I=. -I=../../../.. --go_out=. service.proto
    22  //go:generate protoc -I=. -I=../../../.. --gorony_out=. service.proto
    23  //go:generate protoc -I=. -I=../../../.. --gorony_out=rony_opt=open_api:. service.proto
    24  
    25  // Sample implements ISample interface.
    26  type Sample struct {
    27  	ServerID string
    28  }
    29  
    30  func (h *Sample) Set(ctx *edge.RequestCtx, req *SetRequest, res *SetResponse) *rony.Error {
    31  	return nil
    32  }
    33  
    34  func (h *Sample) Get(ctx *edge.RequestCtx, req *GetRequest, res *GetResponse) *rony.Error {
    35  	return nil
    36  }
    37  
    38  func (h *Sample) EchoInternal(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error {
    39  	panic("implement me")
    40  }
    41  
    42  func (h *Sample) Echo(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error {
    43  	res.ServerID = h.ServerID
    44  	res.Timestamp = req.Timestamp
    45  	res.Int = req.Int
    46  	res.Responder = h.ServerID
    47  	res.SomeData = append(res.SomeData, req.SomeData...)
    48  
    49  	return nil
    50  }
    51  
    52  func (h *Sample) EchoDelay(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error {
    53  	res.ServerID = h.ServerID
    54  	res.Timestamp = req.Timestamp
    55  	res.Int = req.Int
    56  	res.Responder = h.ServerID
    57  	res.SomeData = append(res.SomeData, req.SomeData...)
    58  
    59  	time.Sleep(time.Second * 1)
    60  
    61  	return nil
    62  }
    63  
    64  func (h *Sample) EchoTunnel(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error {
    65  	res.ServerID = h.ServerID
    66  	res.Timestamp = req.Timestamp
    67  	res.Int = req.Int
    68  	res.Responder = h.ServerID
    69  
    70  	return nil
    71  }
    72  
    73  var EchoRest = edge.NewRestProxy(
    74  	func(conn rony.RestConn, ctx *edge.DispatchCtx) error {
    75  		req := &EchoRequest{}
    76  		err := req.UnmarshalJSON(conn.Body())
    77  		if err != nil {
    78  			return err
    79  		}
    80  		ctx.Fill(conn.ConnID(), C_SampleEcho, req)
    81  
    82  		return nil
    83  	},
    84  	func(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) {
    85  		if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) {
    86  			switch envelope.Constructor {
    87  			case C_EchoResponse:
    88  				x := &EchoResponse{}
    89  				_ = x.Unmarshal(envelope.Message)
    90  				var b []byte
    91  				b, err = x.MarshalJSON()
    92  				if err != nil {
    93  					return
    94  				}
    95  				err = conn.WriteBinary(ctx.StreamID(), b)
    96  			case rony.C_Error:
    97  				x := &rony.Error{}
    98  				_ = x.Unmarshal(envelope.Message)
    99  				err = x
   100  			default:
   101  				err = errors.ErrUnexpectedResponse
   102  			}
   103  		}) {
   104  			err = errors.ErrInternalServer
   105  		}
   106  
   107  		return
   108  	},
   109  )
   110  
   111  var EchoRestBinding = edge.NewRestProxy(
   112  	func(conn rony.RestConn, ctx *edge.DispatchCtx) error {
   113  		req := &EchoRequest{}
   114  		req.Int = tools.StrToInt64(tools.GetString(conn.Get("value"), "0"))
   115  		req.Timestamp = tools.StrToInt64(tools.GetString(conn.Get("ts"), "0"))
   116  		ctx.Fill(conn.ConnID(), C_SampleEcho, req)
   117  
   118  		return nil
   119  	},
   120  	func(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) {
   121  		if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) {
   122  			switch envelope.Constructor {
   123  			case C_EchoResponse:
   124  				x := &EchoResponse{}
   125  				_ = x.Unmarshal(envelope.Message)
   126  				var b []byte
   127  				b, err = x.MarshalJSON()
   128  				if err != nil {
   129  					return
   130  				}
   131  				err = conn.WriteBinary(ctx.StreamID(), b)
   132  
   133  				return
   134  			case rony.C_Error:
   135  				x := &rony.Error{}
   136  				_ = x.Unmarshal(envelope.Message)
   137  				err = x
   138  			}
   139  			err = errors.ErrUnexpectedResponse
   140  		}) {
   141  			err = errors.ErrInternalServer
   142  		}
   143  
   144  		return
   145  	},
   146  )