From f350afa0fec28f64b0a6597e09522af0b3abddea Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 4 May 2016 18:59:11 +1200 Subject: [PATCH] split off HubAddress struct --- HubAddress.go | 35 +++++++++++++++++++++++++++++++++++ libnmdc.go | 30 ------------------------------ 2 files changed, 35 insertions(+), 30 deletions(-) create mode 100644 HubAddress.go diff --git a/HubAddress.go b/HubAddress.go new file mode 100644 index 0000000..b282f43 --- /dev/null +++ b/HubAddress.go @@ -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 +} diff --git a/libnmdc.go b/libnmdc.go index 36b194c..74f2527 100644 --- a/libnmdc.go +++ b/libnmdc.go @@ -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