You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.8 KiB

  1. package pb
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. "net"
  7. "strconv"
  8. "strings"
  9. )
  10. type ServerAddress string
  11. type ServerAddresses string
  12. func NewServerAddress(host string, port int, grpcPort int) ServerAddress {
  13. if grpcPort == 0 || grpcPort == port+10000 {
  14. return ServerAddress(util.JoinHostPort(host, port))
  15. }
  16. return ServerAddress(util.JoinHostPort(host, port) + "." + strconv.Itoa(grpcPort))
  17. }
  18. func NewServerAddressWithGrpcPort(address string, grpcPort int) ServerAddress {
  19. if grpcPort == 0 {
  20. return ServerAddress(address)
  21. }
  22. _, port, _ := hostAndPort(address)
  23. if uint64(grpcPort) == port+10000 {
  24. return ServerAddress(address)
  25. }
  26. return ServerAddress(address + "." + strconv.Itoa(grpcPort))
  27. }
  28. func NewServerAddressFromDataNode(dn *master_pb.DataNodeInfo) ServerAddress {
  29. return NewServerAddressWithGrpcPort(dn.Id, int(dn.GrpcPort))
  30. }
  31. func NewServerAddressFromLocation(dn *master_pb.Location) ServerAddress {
  32. return NewServerAddressWithGrpcPort(dn.Url, int(dn.GrpcPort))
  33. }
  34. func (sa ServerAddress) String() string {
  35. return sa.ToHttpAddress()
  36. }
  37. func (sa ServerAddress) ToHttpAddress() string {
  38. portsSepIndex := strings.LastIndex(string(sa), ":")
  39. if portsSepIndex < 0 {
  40. return string(sa)
  41. }
  42. if portsSepIndex+1 >= len(sa) {
  43. return string(sa)
  44. }
  45. ports := string(sa[portsSepIndex+1:])
  46. sepIndex := strings.LastIndex(string(ports), ".")
  47. if sepIndex >= 0 {
  48. host := string(sa[0:portsSepIndex])
  49. return net.JoinHostPort(host, ports[0:sepIndex])
  50. }
  51. return string(sa)
  52. }
  53. func (sa ServerAddress) ToGrpcAddress() string {
  54. portsSepIndex := strings.LastIndex(string(sa), ":")
  55. if portsSepIndex < 0 {
  56. return string(sa)
  57. }
  58. if portsSepIndex+1 >= len(sa) {
  59. return string(sa)
  60. }
  61. ports := string(sa[portsSepIndex+1:])
  62. sepIndex := strings.LastIndex(ports, ".")
  63. if sepIndex >= 0 {
  64. host := string(sa[0:portsSepIndex])
  65. return net.JoinHostPort(host, ports[sepIndex+1:])
  66. }
  67. return ServerToGrpcAddress(string(sa))
  68. }
  69. func (sa ServerAddresses) ToAddresses() (addresses []ServerAddress) {
  70. parts := strings.Split(string(sa), ",")
  71. for _, address := range parts {
  72. if address != "" {
  73. addresses = append(addresses, ServerAddress(address))
  74. }
  75. }
  76. return
  77. }
  78. func (sa ServerAddresses) ToAddressMap() (addresses map[string]ServerAddress) {
  79. addresses = make(map[string]ServerAddress)
  80. for _, address := range sa.ToAddresses() {
  81. addresses[string(address)] = address
  82. }
  83. return
  84. }
  85. func (sa ServerAddresses) ToAddressStrings() (addresses []string) {
  86. parts := strings.Split(string(sa), ",")
  87. for _, address := range parts {
  88. addresses = append(addresses, address)
  89. }
  90. return
  91. }
  92. func ToAddressStrings(addresses []ServerAddress) []string {
  93. var strings []string
  94. for _, addr := range addresses {
  95. strings = append(strings, string(addr))
  96. }
  97. return strings
  98. }
  99. func ToAddressStringsFromMap(addresses map[string]ServerAddress) []string {
  100. var strings []string
  101. for _, addr := range addresses {
  102. strings = append(strings, string(addr))
  103. }
  104. return strings
  105. }
  106. func FromAddressStrings(strings []string) []ServerAddress {
  107. var addresses []ServerAddress
  108. for _, addr := range strings {
  109. addresses = append(addresses, ServerAddress(addr))
  110. }
  111. return addresses
  112. }
  113. func ParseUrl(input string) (address ServerAddress, path string, err error) {
  114. if !strings.HasPrefix(input, "http://") {
  115. return "", "", fmt.Errorf("url %s needs prefix 'http://'", input)
  116. }
  117. input = input[7:]
  118. pathSeparatorIndex := strings.Index(input, "/")
  119. hostAndPorts := input
  120. if pathSeparatorIndex > 0 {
  121. path = input[pathSeparatorIndex:]
  122. hostAndPorts = input[0:pathSeparatorIndex]
  123. }
  124. commaSeparatorIndex := strings.Index(input, ":")
  125. if commaSeparatorIndex < 0 {
  126. err = fmt.Errorf("port should be specified in %s", input)
  127. return
  128. }
  129. address = ServerAddress(hostAndPorts)
  130. return
  131. }