Skip to content

Commit

Permalink
DevInfo now includes name
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyaxod committed Feb 29, 2024
1 parent 972b71f commit 0d8967a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func runServe(ccmd *cobra.Command, args []string) {
pro := protocol.NewProtocolRW(context.TODO(), c, c, nil)
dest := modules.NewToProtocol(uint64(serve_size), 777, pro)

dest.SendDevInfo()
dest.SendDevInfo("data")

go pro.Handle()

Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/migrator/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestMigratorSimplePipe(t *testing.T) {
go destFrom.HandleWriteAt()
go destFrom.HandleDevInfo()

destination.SendDevInfo()
destination.SendDevInfo("test")

conf := NewMigratorConfig().WithBlockSize(blockSize)
conf.LockerHandler = sourceStorage.Lock
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/modules/to_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func (i *ToProtocol) SendEvent(e protocol.EventType) error {
return protocol.DecodeEventResponse(r)
}

func (i *ToProtocol) SendDevInfo() error {
func (i *ToProtocol) SendDevInfo(name string) error {
di := &protocol.DevInfo{
Size: i.size,
Name: name,
}
b := protocol.EncodeDevInfo(di)
_, err := i.protocol.SendPacket(i.dev, protocol.ID_PICK_ANY, b)
Expand Down
14 changes: 11 additions & 3 deletions pkg/storage/protocol/dev_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,27 @@ import (

type DevInfo struct {
Size uint64
Name string
}

func EncodeDevInfo(di *DevInfo) []byte {
buff := make([]byte, 1+8)
buff := make([]byte, 1+8+2+len(di.Name))
buff[0] = COMMAND_DEV_INFO
binary.LittleEndian.PutUint64(buff[1:], di.Size)
binary.LittleEndian.PutUint16(buff[9:], uint16(len(di.Name)))
copy(buff[11:], []byte(di.Name))
return buff
}

func DecodeDevInfo(buff []byte) (*DevInfo, error) {
if buff == nil || len(buff) < 9 || buff[0] != COMMAND_DEV_INFO {
if buff == nil || len(buff) < 11 || buff[0] != COMMAND_DEV_INFO {
return nil, errors.New("Invalid packet")
}
size := binary.LittleEndian.Uint64(buff[1:])
return &DevInfo{Size: size}, nil
l := binary.LittleEndian.Uint16(buff[9:])
if int(l)+11 > len(buff) {
return nil, errors.New("Invalid packet")
}
name := string(buff[11 : 11+l])
return &DevInfo{Size: size, Name: name}, nil
}
3 changes: 2 additions & 1 deletion pkg/storage/protocol/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ func TestDirtyList(t *testing.T) {
}

func TestDevInfo(t *testing.T) {
b := EncodeDevInfo(&DevInfo{Size: 12345})
b := EncodeDevInfo(&DevInfo{Size: 12345, Name: "hello"})

di, err := DecodeDevInfo(b)
assert.NoError(t, err)
assert.Equal(t, uint64(12345), di.Size)
assert.Equal(t, "hello", di.Name)

// Make sure we can't decode silly things
_, err = DecodeDevInfo(nil)
Expand Down
10 changes: 5 additions & 5 deletions testing/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestProtocolWriteAt(t *testing.T) {
go destFromProtocol.HandleWriteAt()

// Send devInfo
sourceToProtocol.SendDevInfo()
sourceToProtocol.SendDevInfo("test")

buff := make([]byte, 4096)
rand.Read(buff)
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestProtocolReadAt(t *testing.T) {
go destFromProtocol.HandleReadAt()
go destFromProtocol.HandleWriteAt()

sourceToProtocol.SendDevInfo()
sourceToProtocol.SendDevInfo("test")

// Now check it was written to the source
buff2 := make([]byte, 4096)
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestProtocolRWWriteAt(t *testing.T) {
go destFromProtocol.HandleReadAt()
go destFromProtocol.HandleWriteAt()

sourceToProtocol.SendDevInfo()
sourceToProtocol.SendDevInfo("test")

// Should know the dev now...
assert.Equal(t, uint32(1), <-destDev)
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestProtocolRWReadAt(t *testing.T) {
go destFromProtocol.HandleReadAt()
go destFromProtocol.HandleWriteAt()

sourceToProtocol.SendDevInfo()
sourceToProtocol.SendDevInfo("test")

// Now check it was written to the source
buff2 := make([]byte, 4096)
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestProtocolEvents(t *testing.T) {
go destFromProtocol.HandleSend(context.TODO())

// Send devInfo
sourceToProtocol.SendDevInfo()
sourceToProtocol.SendDevInfo("test")

// Send some events and make sure they happen at the other end...

Expand Down

0 comments on commit 0d8967a

Please sign in to comment.