75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package uniss
|
|
|
|
import (
|
|
"dashboard/models"
|
|
"dashboard/pkg/rpcsup"
|
|
"dashboard/settings"
|
|
"fmt"
|
|
"net/rpc"
|
|
"sync"
|
|
)
|
|
|
|
// Called by someone else
|
|
type rpcClientStub struct {
|
|
mu sync.RWMutex
|
|
conf *settings.RpcConfig
|
|
rpcMapClients map[string]*rpc.Client
|
|
}
|
|
|
|
type rpcIdAddres struct {
|
|
id string
|
|
host string
|
|
index int
|
|
}
|
|
|
|
func newRpcClients(conf *settings.RpcConfig) *rpcClientStub {
|
|
res := new(rpcClientStub)
|
|
|
|
res.conf = conf
|
|
|
|
res.rpcMapClients = make(map[string]*rpc.Client)
|
|
|
|
return res
|
|
}
|
|
|
|
func (r *rpcClientStub) 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, idAddr.index+r.conf.BasePort)
|
|
|
|
cli, err := rpcsup.RpcClient(&rpcsup.RpcConfig{
|
|
Typec: r.conf.Type,
|
|
Network: r.conf.Network,
|
|
Address: address,
|
|
})
|
|
if err != nil {
|
|
log.Sugar().Errorf("%s create rpc client error: %s", idAddr.id, address)
|
|
continue
|
|
}
|
|
r.rpcMapClients[idAddr.id] = cli
|
|
}
|
|
}
|
|
|
|
func (r *rpcClientStub) 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
|
|
}
|