added /_ctl/client-conns/id/nocies

This commit is contained in:
hyung-hwan 2025-02-18 01:17:51 +09:00
parent a0efb55c3e
commit 81f7bb0c0d
3 changed files with 76 additions and 0 deletions

View File

@ -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(&noti)
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

View File

@ -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

View File

@ -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())