server_address.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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) ToAddressStrings() (addresses []string) {
  79. parts := strings.Split(string(sa), ",")
  80. for _, address := range parts {
  81. addresses = append(addresses, address)
  82. }
  83. return
  84. }
  85. func ToAddressStrings(addresses []ServerAddress) []string {
  86. var strings []string
  87. for _, addr := range addresses {
  88. strings = append(strings, string(addr))
  89. }
  90. return strings
  91. }
  92. func FromAddressStrings(strings []string) []ServerAddress {
  93. var addresses []ServerAddress
  94. for _, addr := range strings {
  95. addresses = append(addresses, ServerAddress(addr))
  96. }
  97. return addresses
  98. }
  99. func ParseUrl(input string) (address ServerAddress, path string, err error) {
  100. if !strings.HasPrefix(input, "http://") {
  101. return "", "", fmt.Errorf("url %s needs prefix 'http://'", input)
  102. }
  103. input = input[7:]
  104. pathSeparatorIndex := strings.Index(input, "/")
  105. hostAndPorts := input
  106. if pathSeparatorIndex > 0 {
  107. path = input[pathSeparatorIndex:]
  108. hostAndPorts = input[0:pathSeparatorIndex]
  109. }
  110. commaSeparatorIndex := strings.Index(input, ":")
  111. if commaSeparatorIndex < 0 {
  112. err = fmt.Errorf("port should be specified in %s", input)
  113. return
  114. }
  115. address = ServerAddress(hostAndPorts)
  116. return
  117. }