renamed parse_duration_string to ParseDurationString

added DurationToSecString()
This commit is contained in:
hyung-hwan 2025-01-19 01:56:31 +09:00
parent 0809f9bedc
commit cfc0db4b54
3 changed files with 20 additions and 12 deletions

View File

@ -183,7 +183,7 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.ServerPeerNet,
ServerPeerOption: r.ServerPeerOption.String(),
Lifetime: fmt.Sprintf("%.09f", r.Lifetime.Seconds()),
Lifetime: DurationToSecString(r.Lifetime),
LifetimeStart: r.LifetimeStart.Unix(),
})
}
@ -296,7 +296,7 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.ServerPeerNet,
ServerPeerOption: r.ServerPeerOption.String(),
Lifetime: fmt.Sprintf("%.09f", r.Lifetime.Seconds()),
Lifetime: DurationToSecString(r.Lifetime),
LifetimeStart: r.LifetimeStart.Unix(),
})
}
@ -374,7 +374,7 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.ServerPeerNet,
ServerPeerOption: r.ServerPeerOption.String(),
Lifetime: fmt.Sprintf("%.09f", r.Lifetime.Seconds()),
Lifetime: DurationToSecString(r.Lifetime),
LifetimeStart: r.LifetimeStart.Unix(),
})
}
@ -402,14 +402,14 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
goto oops
}
server_peer_option = string_to_route_option(jcr.ServerPeerOption)
server_peer_option = StringToRouteOption(jcr.ServerPeerOption)
if server_peer_option == RouteOption(ROUTE_OPTION_UNSPEC) {
status_code = WriteEmptyRespHeader(w, http.StatusBadRequest)
err = fmt.Errorf("wrong server-peer-option value - %s", server_peer_option)
goto oops
}
lifetime, err = parse_duration_string(jcr.Lifetime)
lifetime, err = ParseDurationString(jcr.Lifetime)
if err != nil {
status_code = WriteEmptyRespHeader(w, http.StatusBadRequest)
err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error())
@ -509,7 +509,9 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.ServerPeerNet,
ServerPeerOption: r.ServerPeerOption.String(),
Lifetime: r.Lifetime.String(),
Lifetime: DurationToSecString(r.Lifetime),
LifetimeStart: r.LifetimeStart.Unix(),
})
if err != nil { goto oops }
@ -523,7 +525,7 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
goto oops
}
lifetime, err = parse_duration_string(jcr.Lifetime)
lifetime, err = ParseDurationString(jcr.Lifetime)
if err != nil {
status_code = WriteEmptyRespHeader(w, http.StatusBadRequest)
err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error())
@ -614,7 +616,8 @@ func (ctl *client_ctl_client_conns_id_routes_spsp) ServeHTTP(w http.ResponseWrit
ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.ServerPeerNet,
ServerPeerOption: r.ServerPeerOption.String(),
Lifetime: r.Lifetime.String(),
Lifetime: DurationToSecString(r.Lifetime),
LifetimeStart: r.LifetimeStart.Unix(),
})
if err != nil { goto oops }
@ -628,7 +631,7 @@ func (ctl *client_ctl_client_conns_id_routes_spsp) ServeHTTP(w http.ResponseWrit
goto oops
}
lifetime, err = parse_duration_string(jcr.Lifetime)
lifetime, err = ParseDurationString(jcr.Lifetime)
if err != nil {
status_code = WriteEmptyRespHeader(w, http.StatusBadRequest)
err = fmt.Errorf("wrong lifetime value %s - %s", jcr.Lifetime, err.Error())

View File

@ -188,7 +188,7 @@ func NewClientRoute(cts *ClientConn, id RouteId, client_peer_addr string, client
r.PeerAddr = client_peer_addr // client-side peer
r.PeerName = client_peer_name
// if the client_peer_addr is a domain name, it can't tell between tcp4 and tcp6
r.PeerOption = string_to_route_option(TcpAddrStrClass(client_peer_addr))
r.PeerOption = StringToRouteOption(TcpAddrStrClass(client_peer_addr))
r.ServerPeerAddr = server_peer_svc_addr
r.ServerPeerNet = server_peer_svc_net // permitted network for server-side peer

View File

@ -1,5 +1,6 @@
package hodu
import "fmt"
import "net"
import "net/http"
import "net/netip"
@ -91,7 +92,7 @@ func word_to_route_option(word string) RouteOption {
return RouteOption(ROUTE_OPTION_UNSPEC)
}
func string_to_route_option(desc string) RouteOption {
func StringToRouteOption(desc string) RouteOption {
var fld string
var option RouteOption
var p RouteOption
@ -155,7 +156,7 @@ func get_last_rune_of_non_empty_string(s string) rune {
return tmp[len(tmp) - 1]
}
func parse_duration_string(dur string) (time.Duration, error) {
func ParseDurationString(dur string) (time.Duration, error) {
// i want the input to be in seconds with resolution of 9 digits after
// the decimal point. For example, 0.05 to mean 500ms.
// however, i don't care if a unit is part of the input.
@ -168,6 +169,10 @@ func parse_duration_string(dur string) (time.Duration, error) {
return time.ParseDuration(tmp)
}
func DurationToSecString(d time.Duration) string {
return fmt.Sprintf("%.09f", d.Seconds())
}
func WriteJsonRespHeader(w http.ResponseWriter, status_code int) int {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status_code)