-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added new message types * Added storage.ListPath method * Added ListMessage processing * Added list command to client * Fixes & improvements * Changed chunk size * Incorrect message tyoe processing * Path -> Prefix in ListMessage * Added test for ListMessage encoding/decoding --------- Co-authored-by: Yury Frolov <[email protected]>
- Loading branch information
Showing
7 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package message | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
) | ||
|
||
type ListMessage struct { | ||
Prefix string | ||
} | ||
|
||
var _ ProtoMessage = &ListMessage{} | ||
|
||
func NewListMessage(name string) *ListMessage { | ||
return &ListMessage{ | ||
Prefix: name, | ||
} | ||
} | ||
|
||
func (c *ListMessage) Encode() []byte { | ||
bt := []byte{ | ||
byte(MessageTypeList), | ||
0, | ||
0, | ||
0, | ||
} | ||
|
||
bt = append(bt, []byte(c.Prefix)...) | ||
bt = append(bt, 0) | ||
ln := len(bt) + 8 | ||
|
||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, uint64(ln)) | ||
return append(bs, bt...) | ||
} | ||
|
||
func (c *ListMessage) Decode(body []byte) { | ||
c.Prefix = c.GetListName(body[4:]) | ||
} | ||
|
||
func (c *ListMessage) GetListName(b []byte) string { | ||
buff := bytes.NewBufferString("") | ||
|
||
for i := 0; i < len(b); i++ { | ||
if b[i] == 0 { | ||
break | ||
} | ||
buff.WriteByte(b[i]) | ||
} | ||
|
||
return buff.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package message | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
|
||
"github.com/yezzey-gp/yproxy/pkg/storage" | ||
) | ||
|
||
type ObjectMetaMessage struct { | ||
Content []*storage.S3ObjectMeta | ||
} | ||
|
||
var _ ProtoMessage = &ObjectMetaMessage{} | ||
|
||
func NewObjectMetaMessage(content []*storage.S3ObjectMeta) *ObjectMetaMessage { | ||
return &ObjectMetaMessage{ | ||
Content: content, | ||
} | ||
} | ||
|
||
func (c *ObjectMetaMessage) Encode() []byte { | ||
bt := []byte{ | ||
byte(MessageTypeObjectMeta), | ||
0, | ||
0, | ||
0, | ||
} | ||
|
||
for _, objMeta := range c.Content { | ||
bt = append(bt, []byte(objMeta.Path)...) | ||
bt = append(bt, 0) | ||
|
||
bn := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bn, uint64(objMeta.Size)) | ||
bt = append(bt, bn...) | ||
} | ||
|
||
ln := len(bt) + 8 | ||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, uint64(ln)) | ||
return append(bs, bt...) | ||
} | ||
|
||
func (c *ObjectMetaMessage) Decode(body []byte) { | ||
body = body[4:] | ||
c.Content = make([]*storage.S3ObjectMeta, 0) | ||
for len(body) > 0 { | ||
name, index := c.GetString(body) | ||
size := int64(binary.BigEndian.Uint64(body[index : index+8])) | ||
|
||
c.Content = append(c.Content, &storage.S3ObjectMeta{ | ||
Path: name, | ||
Size: size, | ||
}) | ||
body = body[index+8:] | ||
} | ||
} | ||
|
||
func (c *ObjectMetaMessage) GetString(b []byte) (string, int) { | ||
buff := bytes.NewBufferString("") | ||
|
||
i := 0 | ||
for ; i < len(b); i++ { | ||
if b[i] == 0 { | ||
break | ||
} | ||
buff.WriteByte(b[i]) | ||
} | ||
|
||
return buff.String(), i + 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters