forked from ueckoken/plarail2021-soft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement/external grpc handler (#251)
* implement statesync handler and add some manifests * add Dockerfile * fix * refactor kvs * add iroiro * fix * fix ci * fix tests * implement block handler * Commit from GitHub Actions (compile_PB) * fix ci * fix deadlock * add godoc * fix * fix ci Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
866ae16
commit e81f112
Showing
31 changed files
with
1,488 additions
and
991 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"ueckoken/plarail2022-external/internal" | ||
"ueckoken/plarail2022-external/pkg/envStore" | ||
"ueckoken/plarail2022-external/pkg/syncController" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const namespace = "plarailexternal" | ||
|
||
func main() { | ||
clientHandler2syncController := make(chan syncController.StationState, 16) | ||
syncController2clientHandler := make(chan syncController.StationState, 64) | ||
initEspStatus2syncController := make(chan syncController.StationState) | ||
|
||
envVal := envStore.GetEnv() | ||
|
||
clientConn := prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Name: "clients_connections_seconds", | ||
Help: "Number of connections handling websocket", | ||
}, | ||
[]string{}, | ||
) | ||
|
||
clientConnTotal := prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Name: "clients_connections_total", | ||
Help: "Total client connection", | ||
}, | ||
[]string{}, | ||
) | ||
|
||
controlCommandTotal := prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Name: "client_commands_total", | ||
Help: "Total client commands", | ||
}, | ||
[]string{}, | ||
) | ||
|
||
httpServer := internal.HTTPServer{ | ||
ClientHandler2syncController: clientHandler2syncController, | ||
SyncController2clientHandler: syncController2clientHandler, | ||
Environment: envVal, | ||
NumberOfClientConnection: clientConn, | ||
TotalClientConnection: clientConnTotal, | ||
TotalCLientCommands: controlCommandTotal, | ||
Clients: &internal.ClientsCollection{}, | ||
} | ||
syncController := syncController.SyncController{ | ||
ClientHandler2syncController: clientHandler2syncController, | ||
SyncController2clientHandler: syncController2clientHandler, | ||
Environment: envVal, | ||
InitServoRoute: initEspStatus2syncController, | ||
logger, err := zap.NewDevelopment() | ||
if err != nil { | ||
log.Fatalln("failed to initialize zap") | ||
} | ||
go httpServer.StartServer() | ||
syncController.StartSyncController() | ||
internal.Run(logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package internal | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"ueckoken/plarail2022-external/pkg/synccontroller" | ||
"ueckoken/plarail2022-external/spec" | ||
) | ||
|
||
// startBlockSync starts sync controller for block state. | ||
func startBlockSync(logger *zap.Logger, syncInput chan synccontroller.KV[spec.Blocks_BlockId, spec.NotifyStateRequest_State], syncOutput chan<- synccontroller.KV[spec.Blocks_BlockId, spec.NotifyStateRequest_State]) { | ||
s := synccontroller.NewSyncController(logger, syncInput, syncOutput) | ||
|
||
go s.Run() | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"ueckoken/plarail2022-external/pkg/envStore" | ||
"ueckoken/plarail2022-external/pkg/synccontroller" | ||
"ueckoken/plarail2022-external/spec" | ||
|
||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
// GrpcStateHandler is a handler for gRPC. | ||
type GrpcStateHandler struct { | ||
logger *zap.Logger | ||
env *envStore.Env | ||
spec.UnimplementedControlServer | ||
stateOutput chan<- synccontroller.KV[spec.Stations_StationId, spec.Command2InternalRequest_State] | ||
stateInput <-chan synccontroller.KV[spec.Stations_StationId, spec.Command2InternalRequest_State] | ||
} | ||
|
||
// NewGrpcHandler creates gRPC handler. | ||
func NewGrpcHandler(logger *zap.Logger, env *envStore.Env, stateOutput chan<- synccontroller.KV[spec.Stations_StationId, spec.Command2InternalRequest_State], stateInput <-chan synccontroller.KV[spec.Stations_StationId, spec.Command2InternalRequest_State]) *GrpcStateHandler { | ||
return &GrpcStateHandler{logger: logger, env: env, stateOutput: stateOutput, stateInput: stateInput} | ||
} | ||
|
||
// Command2Internal handles requests from ATS. | ||
func (g GrpcStateHandler) Command2Internal(_ context.Context, req *spec.RequestSync) (*spec.ResponseSync, error) { | ||
s := synccontroller.KV[spec.Stations_StationId, spec.Command2InternalRequest_State]{ | ||
Key: req.GetStation().GetStationId(), | ||
Value: spec.Command2InternalRequest_State(req.GetState()), | ||
} | ||
g.stateOutput <- s | ||
return &spec.ResponseSync{Response: spec.ResponseSync_SUCCESS}, nil | ||
} | ||
|
||
// / handleInput transmits changes received in channel to ATS. | ||
func (g GrpcStateHandler) handleInput(ctx context.Context) { | ||
con, err := grpc.DialContext(ctx, g.env.ClientSideServer.ATSAddress.String(), | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
) | ||
if err != nil { | ||
g.logger.Error("failed to connect ATS", zap.Error(err)) | ||
} | ||
defer con.Close() | ||
for d := range g.stateInput { | ||
client := spec.NewControlClient(con) | ||
req := &spec.RequestSync{ | ||
Station: &spec.Stations{StationId: d.Key}, | ||
State: spec.RequestSync_State(d.Value), | ||
} | ||
res, err := client.Command2Internal(ctx, req) | ||
if err != nil { | ||
g.logger.Error("failed to send data to ATS", zap.Any("payload", req), zap.Error(err)) | ||
} | ||
if res.GetResponse() != spec.ResponseSync_SUCCESS { | ||
g.logger.Error("ATS response seems to be unsuccessfull", zap.Any("payload", res.GetResponse())) | ||
} | ||
} | ||
} | ||
|
||
type GrpcBlockHandler struct { | ||
env *envStore.Env | ||
logger *zap.Logger | ||
spec.UnimplementedBlockStateSyncServer | ||
stateOutput chan<- synccontroller.KV[spec.Blocks_BlockId, spec.NotifyStateRequest_State] | ||
stateInput <-chan synccontroller.KV[spec.Blocks_BlockId, spec.NotifyStateRequest_State] | ||
} | ||
|
||
// NewGrpcBlockHandler creates gRPC handler. | ||
func NewGrpcBlockHandler(logger *zap.Logger, env *envStore.Env, stateOutput chan<- synccontroller.KV[spec.Blocks_BlockId, spec.NotifyStateRequest_State], stateInput <-chan synccontroller.KV[spec.Blocks_BlockId, spec.NotifyStateRequest_State]) *GrpcBlockHandler { | ||
return &GrpcBlockHandler{logger: logger, env: env, stateOutput: stateOutput, stateInput: stateInput} | ||
} | ||
|
||
// NotifyState handles requests from ATS. | ||
func (g GrpcBlockHandler) NotifyState(_ context.Context, req *spec.NotifyStateRequest) (*spec.NotifyStateResponse, error) { | ||
g.stateOutput <- synccontroller.KV[spec.Blocks_BlockId, spec.NotifyStateRequest_State]{Key: req.GetBlock().GetBlockId(), Value: req.GetState()} | ||
return &spec.NotifyStateResponse{Response: spec.NotifyStateResponse_SUCCESS}, nil | ||
} | ||
|
||
// handleInput transmits changes received in channel to ATS. | ||
func (g GrpcBlockHandler) handleInput(ctx context.Context) { | ||
con, err := grpc.DialContext(ctx, g.env.ClientSideServer.ATSAddress.String(), | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
) | ||
if err != nil { | ||
g.logger.Error("failed to connect ATS", zap.Error(err)) | ||
} | ||
defer con.Close() | ||
for d := range g.stateInput { | ||
client := spec.NewBlockStateSyncClient(con) | ||
req := &spec.NotifyStateRequest{ | ||
Block: &spec.Blocks{BlockId: d.Key}, | ||
State: d.Value, | ||
} | ||
res, err := client.NotifyState(ctx, req) | ||
if err != nil { | ||
g.logger.Error("failed to send data to ATS", zap.Any("payload", req), zap.Error(err)) | ||
} | ||
if res.GetResponse() != spec.NotifyStateResponse_SUCCESS { | ||
g.logger.Error("ATS response seems to be unsuccessfull", zap.Any("payload", res.GetResponse())) | ||
} | ||
} | ||
} | ||
|
||
// GRPCListenAndServe listens and serve. | ||
func GRPCListenAndServe(ctx context.Context, logger *zap.Logger, port uint, handler *GrpcStateHandler, blockhandler *GrpcBlockHandler) { | ||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | ||
if err != nil { | ||
logger.Panic("failed to listen", zap.Error(err)) | ||
} | ||
go handler.handleInput(ctx) | ||
go blockhandler.handleInput(ctx) | ||
s := grpc.NewServer() | ||
spec.RegisterControlServer(s, handler) | ||
spec.RegisterBlockStateSyncServer(s, blockhandler) | ||
if err := s.Serve(lis); err != nil { | ||
logger.Panic("failed to server", zap.Error(err)) | ||
} | ||
} |
Oops, something went wrong.