github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/gateway/dummy/conn.go (about) 1 package dummyGateway 2 3 import ( 4 "mime/multipart" 5 "sync" 6 7 "github.com/ronaksoft/rony/errors" 8 ) 9 10 /* 11 Creation Time: 2020 - Oct - 02 12 Created by: (ehsan) 13 Maintainers: 14 1. Ehsan N. Moosa (E2) 15 Auditor: Ehsan N. Moosa (E2) 16 Copyright Ronak Software Group 2020 17 */ 18 19 type Conn struct { 20 id uint64 21 clientIP string 22 persistent bool 23 mtx sync.Mutex 24 kv map[string]interface{} 25 onMessage func(connID uint64, streamID int64, data []byte, hdr map[string]string) 26 27 // extra data for REST 28 status int 29 httpHdr map[string]string 30 method string 31 path string 32 body []byte 33 } 34 35 func NewConn(id uint64) *Conn { 36 return &Conn{ 37 id: id, 38 kv: map[string]interface{}{}, 39 httpHdr: map[string]string{}, 40 persistent: false, 41 } 42 } 43 44 func (c *Conn) WithHandler(onMessage func(connID uint64, streamID int64, data []byte, hdr map[string]string)) *Conn { 45 c.onMessage = onMessage 46 47 return c 48 } 49 50 func (c *Conn) ReadHeader(key string) string { 51 return c.httpHdr[key] 52 } 53 54 func (c *Conn) Redirect(statusCode int, newHostPort string) { 55 // TODO:: implement it 56 } 57 58 func (c *Conn) RedirectURL(statusCode int, url string) { 59 // TODO:: implement it 60 } 61 62 func (c *Conn) WriteStatus(status int) { 63 c.status = status 64 } 65 66 func (c *Conn) WriteHeader(key, value string) { 67 c.mtx.Lock() 68 defer c.mtx.Unlock() 69 c.httpHdr[key] = value 70 } 71 72 func (c *Conn) MultiPart() (*multipart.Form, error) { 73 return nil, errors.New(errors.NotImplemented, "Multipart") 74 } 75 76 func (c *Conn) Method() string { 77 return c.method 78 } 79 80 func (c *Conn) Path() string { 81 return c.path 82 } 83 84 func (c *Conn) Body() []byte { 85 return c.body 86 } 87 88 func (c *Conn) Get(key string) interface{} { 89 c.mtx.Lock() 90 defer c.mtx.Unlock() 91 92 return c.kv[key] 93 } 94 95 func (c *Conn) Set(key string, val interface{}) { 96 c.mtx.Lock() 97 defer c.mtx.Unlock() 98 c.kv[key] = val 99 } 100 101 func (c *Conn) Walk(f func(k string, v interface{}) bool) { 102 c.mtx.Lock() 103 defer c.mtx.Unlock() 104 for k, v := range c.kv { 105 if !f(k, v) { 106 return 107 } 108 } 109 } 110 111 func (c *Conn) ConnID() uint64 { 112 return c.id 113 } 114 115 func (c *Conn) ClientIP() string { 116 return c.clientIP 117 } 118 119 func (c *Conn) WriteBinary(streamID int64, data []byte) error { 120 c.onMessage(c.id, streamID, data, c.httpHdr) 121 122 return nil 123 } 124 125 func (c *Conn) Persistent() bool { 126 return c.persistent 127 } 128 129 func (c *Conn) SetPersistent(b bool) *Conn { 130 c.persistent = b 131 132 return c 133 }