message.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package protocol
  5. import "fmt"
  6. type IndexMessage struct {
  7. Repository string // max:64
  8. Files []FileInfo
  9. }
  10. type FileInfo struct {
  11. Name string // max:8192
  12. Flags uint32
  13. Modified int64
  14. Version uint64
  15. LocalVersion uint64
  16. Blocks []BlockInfo
  17. }
  18. func (f FileInfo) String() string {
  19. return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%d, Size:%d, Blocks:%v}",
  20. f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.Blocks)
  21. }
  22. func (f FileInfo) Size() (bytes int64) {
  23. if IsDeleted(f.Flags) || IsDirectory(f.Flags) {
  24. return 128
  25. }
  26. for _, b := range f.Blocks {
  27. bytes += int64(b.Size)
  28. }
  29. return
  30. }
  31. func (f FileInfo) IsDeleted() bool {
  32. return IsDeleted(f.Flags)
  33. }
  34. // Used for unmarshalling a FileInfo structure but skipping the actual block list
  35. type FileInfoTruncated struct {
  36. Name string // max:8192
  37. Flags uint32
  38. Modified int64
  39. Version uint64
  40. LocalVersion uint64
  41. NumBlocks uint32
  42. }
  43. // Returns a statistical guess on the size, not the exact figure
  44. func (f FileInfoTruncated) Size() int64 {
  45. if IsDeleted(f.Flags) || IsDirectory(f.Flags) {
  46. return 128
  47. }
  48. if f.NumBlocks < 2 {
  49. return BlockSize / 2
  50. } else {
  51. return int64(f.NumBlocks-1)*BlockSize + BlockSize/2
  52. }
  53. }
  54. func (f FileInfoTruncated) IsDeleted() bool {
  55. return IsDeleted(f.Flags)
  56. }
  57. type FileIntf interface {
  58. Size() int64
  59. IsDeleted() bool
  60. }
  61. type BlockInfo struct {
  62. Offset int64 // noencode (cache only)
  63. Size uint32
  64. Hash []byte // max:64
  65. }
  66. func (b BlockInfo) String() string {
  67. return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
  68. }
  69. type RequestMessage struct {
  70. Repository string // max:64
  71. Name string // max:8192
  72. Offset uint64
  73. Size uint32
  74. }
  75. type ResponseMessage struct {
  76. Data []byte
  77. }
  78. type ClusterConfigMessage struct {
  79. ClientName string // max:64
  80. ClientVersion string // max:64
  81. Repositories []Repository // max:64
  82. Options []Option // max:64
  83. }
  84. func (o *ClusterConfigMessage) GetOption(key string) string {
  85. for _, option := range o.Options {
  86. if option.Key == key {
  87. return option.Value
  88. }
  89. }
  90. return ""
  91. }
  92. type Repository struct {
  93. ID string // max:64
  94. Nodes []Node // max:64
  95. }
  96. type Node struct {
  97. ID []byte // max:32
  98. Flags uint32
  99. MaxLocalVersion uint64
  100. }
  101. type Option struct {
  102. Key string // max:64
  103. Value string // max:1024
  104. }
  105. type CloseMessage struct {
  106. Reason string // max:1024
  107. }
  108. type EmptyMessage struct{}