implemented some more control channel handler in server.go

This commit is contained in:
hyung-hwan 2024-12-08 23:16:43 +09:00
parent 3c9b65476a
commit be0d31b06c
5 changed files with 194 additions and 17 deletions

View File

@ -397,7 +397,6 @@ done:
oops: oops:
c.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error()) c.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error())
return return
} }
// ------------------------------------ // ------------------------------------
@ -412,6 +411,7 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
var route_nid uint64 var route_nid uint64
var je *json.Encoder var je *json.Encoder
var cts *ClientConn var cts *ClientConn
var r *ClientRoute
defer func() { defer func() {
var err interface{} = recover() var err interface{} = recover()
@ -444,24 +444,27 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
goto done goto done
} }
r = cts.FindClientRouteById(uint32(route_nid))
if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: "non-existent route id - " + conn_id}); err != nil { goto oops }
goto done
}
switch req.Method { switch req.Method {
case http.MethodGet: case http.MethodGet:
var r *ClientRoute status_code = http.StatusOK; w.WriteHeader(status_code)
r = cts.FindClientRouteById(uint32(route_nid))
if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: "non-existent route id - " + conn_id}); err != nil { goto oops }
goto done
}
err = je.Encode(json_out_client_route{ err = je.Encode(json_out_client_route{
Id: r.id, Id: r.id,
ClientPeerAddr: r.peer_addr, ClientPeerAddr: r.peer_addr,
ServerPeerListenAddr: r.server_peer_listen_addr.String(), ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.server_peer_net,
ServerPeerProto: r.server_peer_proto,
}) })
if err != nil { goto oops } if err != nil { goto oops }
case http.MethodDelete: case http.MethodDelete:
cts.ReqStop() r.ReqStop()
status_code = http.StatusNoContent; w.WriteHeader(status_code) status_code = http.StatusNoContent; w.WriteHeader(status_code)
default: default:

View File

@ -17,11 +17,11 @@ const (
) )
type Logger interface { type Logger interface {
Write (id string, level LogLevel, fmtstr string, args ...interface{}) Write(id string, level LogLevel, fmtstr string, args ...interface{})
} }
type Service interface { type Service interface {
RunTask (wg *sync.WaitGroup) // blocking. run the actual task loop. it must call wg.Done() upon exit from itself. RunTask(wg *sync.WaitGroup) // blocking. run the actual task loop. it must call wg.Done() upon exit from itself.
StartService(data interface{}) // non-blocking. spin up a service. it may be invokded multiple times for multiple instances StartService(data interface{}) // non-blocking. spin up a service. it may be invokded multiple times for multiple instances
StopServices() // non-blocking. send stop request to all services spun up StopServices() // non-blocking. send stop request to all services spun up
WaitForTermination() // blocking. must wait until all services are stopped WaitForTermination() // blocking. must wait until all services are stopped

View File

@ -2,7 +2,11 @@ syntax = "proto3";
option go_package = "./hodu"; option go_package = "./hodu";
//package hodu; // no idea if it's still important... // this one affects the full name of the the actual calls.
// /<package-name>.<service-name>/<call-name>
// i want to keep the package line commented out such that
// the full name is /<service-name>/<call-name> (e.g. /Hodu/GetSeed)
//package hodu;
service Hodu { service Hodu {
rpc GetSeed (Seed) returns (Seed) {} rpc GetSeed (Seed) returns (Seed) {}

View File

@ -202,7 +202,7 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
} }
done: done:
s.log.Write("", LOG_DEBUG, "[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.String(), status_code) // TODO: time taken s.log.Write("", LOG_DEBUG, "[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.String(), status_code)
return return
oops: oops:
@ -213,13 +213,157 @@ oops:
// ------------------------------------ // ------------------------------------
func (ctl *server_ctl_server_conns_id_routes) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (ctl *server_ctl_server_conns_id_routes) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// TODO var s *Server
var status_code int
var err error
var conn_id string
var conn_nid uint64
var je *json.Encoder
var cts *ServerConn
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.s.log, req, err) }
}()
s = ctl.s
je = json.NewEncoder(w)
conn_id = req.PathValue("conn_id")
conn_nid, err = strconv.ParseUint(conn_id, 10, 32)
if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops }
goto done
}
cts = s.FindServerConnById(uint32(conn_nid))
if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops }
goto done
}
switch req.Method {
case http.MethodGet:
var r *ServerRoute
var jsp []json_out_server_route
jsp = make([]json_out_server_route, 0)
cts.route_mtx.Lock()
for _, r = range cts.route_map {
jsp = append(jsp, json_out_server_route{
Id: r.id,
ClientPeerAddr: r.ptc_addr,
ServerPeerListenAddr: r.svc_addr.String(),
ServerPeerNet: r.svc_permitted_net.String(),
ServerPeerProto: r.svc_proto,
})
}
cts.route_mtx.Unlock()
status_code = http.StatusOK; w.WriteHeader(status_code)
if err = je.Encode(jsp); err != nil { goto oops }
case http.MethodDelete:
//cts.RemoveAllServerRoutes()
cts.ReqStopAllServerRoutes()
status_code = http.StatusNoContent; w.WriteHeader(status_code)
default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code)
}
done:
s.log.Write("", LOG_DEBUG, "[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.String(), status_code) // TODO: time taken
return
oops:
s.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error())
return
} }
// ------------------------------------ // ------------------------------------
func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// TODO var s *Server
var status_code int
var err error
var conn_id string
var conn_nid uint64
var route_id string
var route_nid uint64
var je *json.Encoder
var cts *ServerConn
var r *ServerRoute
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.s.log, req, err) }
}()
s = ctl.s
je = json.NewEncoder(w)
conn_id = req.PathValue("conn_id")
route_id = req.PathValue("route_id")
conn_nid, err = strconv.ParseUint(conn_id, 10, 32)
if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: "wrong connection id - " + conn_id}); err != nil { goto oops }
goto done
}
route_nid, err = strconv.ParseUint(route_id, 10, 32)
if err != nil {
status_code = http.StatusBadRequest; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: "wrong route id - " + route_id}); err != nil { goto oops }
goto done
}
cts = s.FindServerConnById(uint32(conn_nid))
if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops }
goto done
}
r = cts.FindServerRouteById(uint32(route_nid))
if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: "non-existent route id - " + conn_id}); err != nil { goto oops }
goto done
}
switch req.Method {
case http.MethodGet:
status_code = http.StatusOK; w.WriteHeader(status_code)
err = je.Encode(json_out_client_route{
Id: r.id,
ClientPeerAddr: r.ptc_addr,
ServerPeerListenAddr: r.svc_addr.String(),
ServerPeerNet: r.svc_permitted_net.String(),
ServerPeerProto: r.svc_proto,
})
if err != nil { goto oops }
case http.MethodDelete:
r.ReqStop()
status_code = http.StatusNoContent; w.WriteHeader(status_code)
default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code)
}
done:
// TODO: need to handle x-forwarded-for and other stuff? this is not a real web service, though
s.log.Write("", LOG_DEBUG, "[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.String(), status_code) // TODO: time taken
return
oops:
s.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error())
return
} }
// ------------------------------------ // ------------------------------------

View File

@ -401,6 +401,32 @@ func (cts *ServerConn) RemoveServerRouteById(route_id uint32) (*ServerRoute, err
return r, nil return r, nil
} }
func (cts *ServerConn) FindServerRouteById(route_id uint32) *ServerRoute {
var r *ServerRoute
var ok bool
cts.route_mtx.Lock()
r, ok = cts.route_map[route_id]
if !ok {
cts.route_mtx.Unlock()
return nil
}
cts.route_mtx.Unlock()
return r
}
func (cts *ServerConn) ReqStopAllServerRoutes() {
var r *ServerRoute
cts.route_mtx.Lock()
defer cts.route_mtx.Unlock()
for _, r = range cts.route_map {
r.ReqStop()
}
}
func (cts *ServerConn) ReportEvent(route_id uint32, pts_id uint32, event_type PACKET_KIND, event_data interface{}) error { func (cts *ServerConn) ReportEvent(route_id uint32, pts_id uint32, event_type PACKET_KIND, event_data interface{}) error {
var r *ServerRoute var r *ServerRoute
var ok bool var ok bool
@ -992,7 +1018,7 @@ func (s *Server) RunCtlTask(wg *sync.WaitGroup) {
go func(i int, cs *http.Server) { go func(i int, cs *http.Server) {
var l net.Listener var l net.Listener
s.log.Write ("", LOG_INFO, "Control channel[%d] started on %s", i, s.ctl_addr[i]) s.log.Write("", LOG_INFO, "Control channel[%d] started on %s", i, s.ctl_addr[i])
// defeat hard-coded "tcp" in ListenAndServe() and ListenAndServeTLS() // defeat hard-coded "tcp" in ListenAndServe() and ListenAndServeTLS()
// err = cs.ListenAndServe() // err = cs.ListenAndServe()
@ -1141,7 +1167,7 @@ func (s *Server) RemoveServerConnByAddr(addr net.Addr) error {
return nil return nil
} }
func (s* Server) FindServerConnById(id uint32) *ServerConn { func (s *Server) FindServerConnById(id uint32) *ServerConn {
var cts *ServerConn var cts *ServerConn
var ok bool var ok bool