From 81f7bb0c0d5e38b9bd1de3edef2490e805e2299d Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 18 Feb 2025 01:17:51 +0900 Subject: [PATCH] added /_ctl/client-conns/id/nocies --- client-ctl.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ client.go | 18 ++++++++++++++++ server-ctl.go | 1 + 3 files changed, 76 insertions(+) diff --git a/client-ctl.go b/client-ctl.go index 0d213fe..fa3a810 100644 --- a/client-ctl.go +++ b/client-ctl.go @@ -125,6 +125,10 @@ type client_ctl_client_conns_id_routes_id_peers_id struct { client_ctl } +type client_ctl_client_conns_id_notices struct { + client_ctl +} + type client_ctl_stats struct { client_ctl } @@ -872,6 +876,59 @@ oops: // ------------------------------------ +func (ctl *client_ctl_client_conns_id_notices) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) { + var c *Client + var status_code int + var conn_id string + var cts *ClientConn + var je *json.Encoder + var err error + + c = ctl.c + je = json.NewEncoder(w) + + conn_id = req.PathValue("conn_id") + cts, err = c.FindClientConnByIdStr(conn_id) + if err != nil { + status_code = WriteJsonRespHeader(w, http.StatusNotFound) + je.Encode(JsonErrmsg{Text: err.Error()}) + goto oops + } + + switch req.Method { + case http.MethodPost: + var noti json_in_notice + + err = json.NewDecoder(req.Body).Decode(¬i) + if err != nil { + status_code = WriteEmptyRespHeader(w, http.StatusBadRequest) + goto oops + } + + // no check if noti.Text is empty as i want an empty message to be delivered too. + err = cts.psc.Send(MakeConnNoticePacket(noti.Text)) + if err != nil { + err = fmt.Errorf("failed to send conn_notice text to %s - %s", noti.Text, cts.remote_addr, err.Error()) + status_code = WriteJsonRespHeader(w, http.StatusInternalServerError) + je.Encode(JsonErrmsg{Text: err.Error()}) + goto oops + } + + status_code = WriteJsonRespHeader(w, http.StatusOK) + + default: + status_code = WriteEmptyRespHeader(w, http.StatusBadRequest) + } + +//done: + return status_code, nil + +oops: + return status_code, err +} + +// ------------------------------------ + func (ctl *client_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) { var c *Client var status_code int diff --git a/client.go b/client.go index bea0f8b..8387a17 100644 --- a/client.go +++ b/client.go @@ -7,9 +7,11 @@ import "fmt" import "log" import "net" import "net/http" +import "strconv" import "sync" import "sync/atomic" import "time" +import "unsafe" import "google.golang.org/grpc" import "google.golang.org/grpc/codes" @@ -1346,6 +1348,8 @@ func NewClient(ctx context.Context, name string, logger Logger, cfg *ClientConfi c.wrap_http_handler(&client_ctl_client_conns_id_routes_id_peers{client_ctl{c: &c, id: HS_ID_CTL}})) c.ctl_mux.Handle(c.ctl_prefix + "/_ctl/client-conns/{conn_id}/routes/{route_id}/peers/{peer_id}", c.wrap_http_handler(&client_ctl_client_conns_id_routes_id_peers_id{client_ctl{c: &c, id: HS_ID_CTL}})) + c.ctl_mux.Handle(c.ctl_prefix + "/_ctl/client-conns/{conn_id}/notices", + c.wrap_http_handler(&client_ctl_client_conns_id_notices{client_ctl{c: &c, id: HS_ID_CTL}})) c.ctl_mux.Handle(c.ctl_prefix + "/_ctl/stats", c.wrap_http_handler(&client_ctl_stats{client_ctl{c: &c, id: HS_ID_CTL}})) c.ctl_mux.Handle(c.ctl_prefix + "/_ctl/token", @@ -1533,6 +1537,20 @@ func (c *Client) FindClientRouteById(conn_id ConnId, route_id RouteId) *ClientRo return cts.FindClientRouteById(route_id) } +func (c *Client) FindClientConnByIdStr(conn_id string) (*ClientConn, error) { + var conn_nid uint64 + var cts *ClientConn + var err error + + conn_nid, err = strconv.ParseUint(conn_id, 10, int(unsafe.Sizeof(ConnId(0)) * 8)) + if err != nil { return nil, fmt.Errorf("invalid connection id %s - %s", conn_id, err.Error()); } + + cts = c.FindClientConnById(ConnId(conn_nid)) + if cts == nil { return nil, fmt.Errorf("non-existent connection id %d", conn_nid) } + + return cts, nil +} + func (c *Client) FindClientRouteByServerPeerSvcPortId(conn_id ConnId, port_id PortId) *ClientRoute { var cts *ClientConn var ok bool diff --git a/server-ctl.go b/server-ctl.go index b64e5d7..537c9a9 100644 --- a/server-ctl.go +++ b/server-ctl.go @@ -579,6 +579,7 @@ func (ctl *server_ctl_server_conns_id_notices) ServeHTTP(w http.ResponseWriter, goto oops } + // no check if noti.Text is empty as i want an empty message to be delivered too. err = cts.pss.Send(MakeConnNoticePacket(noti.Text)) if err != nil { err = fmt.Errorf("failed to send conn_notice text to %s - %s", noti.Text, cts.RemoteAddr, err.Error())