updated code to use ClientRouteConfig when adding a new route in control channel

This commit is contained in:
hyung-hwan 2024-12-16 09:59:14 +09:00
parent a4ef87c031
commit 960520b905
3 changed files with 43 additions and 25 deletions

View File

@ -155,6 +155,7 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
jsp = append(jsp, json_out_client_route{
Id: r.id,
ClientPeerAddr: r.peer_addr,
ClientPeerName: r.peer_name,
ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.server_peer_net,
ServerPeerOption: r.server_peer_option.string(),
@ -273,6 +274,7 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
jsp = append(jsp, json_out_client_route{
Id: r.id,
ClientPeerAddr: r.peer_addr,
ClientPeerName: r.peer_name,
ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.server_peer_net,
ServerPeerOption: r.server_peer_option.string(),
@ -370,6 +372,7 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
case http.MethodPost:
var jcr json_in_client_route
var r *ClientRoute
var rc *ClientRouteConfig
var server_peer_option RouteOption
err = json.NewDecoder(req.Body).Decode(&jcr)
@ -391,7 +394,16 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
goto oops
}
r, err = cts.AddNewClientRoute(jcr.ClientPeerAddr, jcr.ClientPeerName, jcr.ServerPeerServiceAddr, jcr.ServerPeerServiceNet, server_peer_option)
rc = &ClientRouteConfig{
PeerAddr: jcr.ClientPeerAddr,
PeerName: jcr.ClientPeerName,
Option: server_peer_option,
ServiceAddr: jcr.ServerPeerServiceAddr,
ServiceNet: jcr.ServerPeerServiceNet,
}
//cts.AddClientRouteConfig(rc) // TODO: this is to remember... but how to delete it?
r, err = cts.AddNewClientRoute(rc)
if err != nil {
status_code = http.StatusInternalServerError; w.WriteHeader(status_code)
if err = je.Encode(json_errmsg{Text: err.Error()}); err != nil { goto oops }
@ -477,6 +489,7 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
err = je.Encode(json_out_client_route{
Id: r.id,
ClientPeerAddr: r.peer_addr,
ClientPeerName: r.peer_name,
ServerPeerListenAddr: r.server_peer_listen_addr.String(),
ServerPeerNet: r.server_peer_net,
ServerPeerOption: r.server_peer_option.string(),

View File

@ -602,14 +602,13 @@ func NewClientConn(c *Client, cfg *ClientConfig) *ClientConn {
return &cts
}
func (cts *ClientConn) AddNewClientRoute(addr string, name string, server_peer_svc_addr string, server_peer_svc_net string, option RouteOption) (*ClientRoute, error) {
func (cts *ClientConn) AddNewClientRoute(rc *ClientRouteConfig) (*ClientRoute, error) {
var r *ClientRoute
var start_id RouteId
cts.route_mtx.Lock()
//start_id = RouteId(rand.Uint64())
start_id = cts.route_next_id
cts.route_mtx.Lock()
for {
var ok bool
_, ok = cts.route_map[cts.route_next_id]
@ -621,13 +620,13 @@ func (cts *ClientConn) AddNewClientRoute(addr string, name string, server_peer_s
}
}
r = NewClientRoute(cts, cts.route_next_id, addr, name, server_peer_svc_addr, server_peer_svc_net, option)
r = NewClientRoute(cts, cts.route_next_id, rc.PeerAddr, rc.PeerName, rc.ServiceAddr, rc.ServiceNet, rc.Option)
cts.route_map[r.id] = r
cts.route_next_id++
cts.cli.stats.routes.Add(1)
cts.route_mtx.Unlock()
cts.cli.log.Write(cts.sid, LOG_INFO, "Added route(%d,%s)", r.id, addr)
cts.cli.log.Write(cts.sid, LOG_INFO, "Added route(%d,%d) %s", cts.id, r.id, r.peer_addr)
cts.route_wg.Add(1)
go r.RunTask(&cts.route_wg)
@ -718,12 +717,18 @@ func (cts *ClientConn) FindClientRouteById(route_id RouteId) *ClientRoute {
return r
}
func (cts *ClientConn) AddClientRouteConfig (route *ClientRouteConfig) {
cts.route_mtx.Lock()
cts.cfg.Routes = append(cts.cfg.Routes, *route)
cts.route_mtx.Unlock()
}
func (cts *ClientConn) AddClientRoutes(routes []ClientRouteConfig) error {
var v ClientRouteConfig
var err error
for _, v = range routes {
_, err = cts.AddNewClientRoute(v.PeerAddr, v.PeerName, v.ServiceAddr, "", v.Option)
_, err = cts.AddNewClientRoute(&v)
if err != nil {
return fmt.Errorf("unable to add client route for %s - %s", v, err.Error())
}

View File

@ -16,9 +16,9 @@ type app_logger_msg_t struct {
}
type AppLogger struct {
Id string
Out io.Writer
Mask hodu.LogMask
id string
out io.Writer
mask hodu.LogMask
file *os.File
file_name string // you can get the file name from file but this is to preserve the original.
@ -31,9 +31,9 @@ type AppLogger struct {
func NewAppLogger (id string, w io.Writer, mask hodu.LogMask) *AppLogger {
var l *AppLogger
l = &AppLogger{
Id: id,
Out: w,
Mask: mask,
id: id,
out: w,
mask: mask,
msg_chan: make(chan app_logger_msg_t, 256),
}
l.wg.Add(1)
@ -61,9 +61,9 @@ func NewAppLoggerToFile (id string, file_name string, max_size int64, rotate int
}
l = &AppLogger{
Id: id,
Out: f,
Mask: mask,
id: id,
out: f,
mask: mask,
file: f,
file_name: file_name,
file_max_size: max_size,
@ -94,8 +94,8 @@ main_loop:
select {
case msg = <-l.msg_chan:
if msg.code == 0 {
//l.Out.Write([]byte(msg))
io.WriteString(l.Out, msg.data)
//l.out.Write([]byte(msg))
io.WriteString(l.out, msg.data)
if l.file_max_size > 0 && l.file != nil {
var fi os.FileInfo
var err error
@ -116,12 +116,12 @@ main_loop:
func (l *AppLogger) Write(id string, level hodu.LogLevel, fmtstr string, args ...interface{}) {
if l.Mask & hodu.LogMask(level) == 0 { return }
if l.mask & hodu.LogMask(level) == 0 { return }
l.write(id, level, 1, fmtstr, args...)
}
func (l *AppLogger) WriteWithCallDepth(id string, level hodu.LogLevel, call_depth int, fmtstr string, args ...interface{}) {
if l.Mask & hodu.LogMask(level) == 0 { return }
if l.mask & hodu.LogMask(level) == 0 { return }
l.write(id, level, call_depth + 1, fmtstr, args...)
}
@ -136,7 +136,7 @@ func (l *AppLogger) write(id string, level hodu.LogLevel, call_depth int, fmtstr
var caller_ok bool
var sb strings.Builder
//if l.Mask & hodu.LogMask(level) == 0 { return }
//if l.mask & hodu.LogMask(level) == 0 { return }
now = time.Now()
@ -155,7 +155,7 @@ func (l *AppLogger) write(id string, level hodu.LogLevel, call_depth int, fmtstr
if caller_ok {
sb.WriteString(fmt.Sprintf("[%s:%d] ", filepath.Base(callerfile), caller_line))
}
sb.WriteString(l.Id)
sb.WriteString(l.id)
if id != "" {
sb.WriteString("(")
sb.WriteString(id)
@ -196,12 +196,12 @@ func (l *AppLogger) rotate() {
if err != nil {
l.file.Close()
l.file = nil
l.Out = os.Stderr
l.out = os.Stderr
// don't reset l.file_name. you can derive that there was an error
// if l.file_name is not blank, and if l.Out is os.Stderr,
// if l.file_name is not blank, and if l.out is os.Stderr,
} else {
l.file.Close()
l.file = f
l.Out = l.file
l.out = l.file
}
}