server_address.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package pb
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  5. "github.com/seaweedfs/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. }