72 lines
1.3 KiB
Go
72 lines
1.3 KiB
Go
package uniss
|
|
|
|
import (
|
|
"dashboard/logger"
|
|
"dashboard/models"
|
|
"dashboard/pkg/rpcsup"
|
|
"fmt"
|
|
"net/rpc"
|
|
"sync"
|
|
)
|
|
|
|
type rpcClients struct {
|
|
*logger.Logger
|
|
mu sync.RWMutex
|
|
rpcMapClients map[string]*rpc.Client
|
|
port int
|
|
}
|
|
|
|
type rpcIdAddres struct {
|
|
id string
|
|
host string
|
|
port int
|
|
}
|
|
|
|
func newRpcClients(log *logger.Logger, mport int) *rpcClients {
|
|
res := new(rpcClients)
|
|
|
|
res.Logger = log
|
|
res.port = mport
|
|
|
|
res.rpcMapClients = make(map[string]*rpc.Client)
|
|
|
|
return res
|
|
}
|
|
|
|
func (r *rpcClients) flushRpcClients(ids []*rpcIdAddres) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
|
|
for _, cli := range r.rpcMapClients {
|
|
if cli != nil {
|
|
cli.Close()
|
|
}
|
|
}
|
|
|
|
for _, idAddr := range ids {
|
|
address := fmt.Sprintf("%s:%d", idAddr.host, r.port)
|
|
|
|
cli, err := rpcsup.JsonClient(r.Logger, address)
|
|
if err != nil {
|
|
r.Sugar().Errorf("%s create rpc client error: %s", idAddr.id, address)
|
|
continue
|
|
}
|
|
r.rpcMapClients[idAddr.id] = cli
|
|
}
|
|
}
|
|
|
|
func (r *rpcClients) getConfig(id string, req models.UnisRpcRequest) (*models.UnisRpcResponse, error) {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
|
|
out := &models.UnisRpcResponse{}
|
|
|
|
if client := r.rpcMapClients[id]; client != nil {
|
|
if err := client.Call(models.UnisStationConfig.Methord(), req, out); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return out, nil
|
|
}
|