split off HubAddress struct

This commit is contained in:
mappu 2016-05-04 18:59:11 +12:00
parent dea4f996f9
commit f350afa0fe
2 changed files with 35 additions and 30 deletions

35
HubAddress.go Normal file
View File

@ -0,0 +1,35 @@
package libnmdc
import (
"net/url"
"strings"
)
type HubAddress string
func (this *HubAddress) parse() url.URL {
parsed, err := url.Parse(strings.ToLower(string(*this)))
if err != nil || len(parsed.Host) == 0 {
parsed = &url.URL{
Scheme: "nmdc",
Host: string(*this),
}
}
// Add default port if not specified
if !strings.ContainsRune(parsed.Host, ':') {
parsed.Host = parsed.Host + ":411"
}
return *parsed
}
func (this *HubAddress) IsSecure() bool {
parsed := this.parse()
return parsed.Scheme == "nmdcs" || parsed.Scheme == "dchubs"
}
func (this *HubAddress) GetHostOnly() string {
return this.parse().Host
}

View File

@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"net"
"net/url"
"regexp"
"strings"
"time"
@ -21,35 +20,6 @@ const (
type ConnectionState int
type HubEventType int
type HubAddress string
func (this *HubAddress) parse() url.URL {
parsed, err := url.Parse(strings.ToLower(string(*this)))
if err != nil || len(parsed.Host) == 0 {
parsed = &url.URL{
Scheme: "nmdc",
Host: string(*this),
}
}
// Add default port if not specified
if !strings.ContainsRune(parsed.Host, ':') {
parsed.Host = parsed.Host + ":411"
}
return *parsed
}
func (this *HubAddress) IsSecure() bool {
parsed := this.parse()
return parsed.Scheme == "nmdcs" || parsed.Scheme == "dchubs"
}
func (this *HubAddress) GetHostOnly() string {
return this.parse().Host
}
const (
CONNECTIONSTATE_DISCONNECTED = 1
CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress