Skip to content
Snippets Groups Projects
gRPCStatusView.go 1.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • Malte Bauch's avatar
    Malte Bauch committed
    package views
    
    import (
    	"code.fbi.h-da.de/cocsn/gosdn/cmd/gosdn-tview/app"
    	"github.com/rivo/tview"
    	"google.golang.org/grpc"
    	"time"
    )
    
    
    //GRPCStatusView is an application view to display the current status of
    //the gRPC server (e.g. connected, unavailable,...)
    
    Malte Bauch's avatar
    Malte Bauch committed
    type GRPCStatusView struct {
    	gRPCStatusView *tview.TextView
    }
    
    
    //NewGRPCStatusView creates a new GRPCStatusView
    
    Malte Bauch's avatar
    Malte Bauch committed
    func NewGRPCStatusView(app *app.App, conn *grpc.ClientConn) *GRPCStatusView {
    	sv := &GRPCStatusView{
    		gRPCStatusView: tview.NewTextView(),
    	}
    
    	sv.gRPCStatusView.
    
    Malte Bauch's avatar
    Malte Bauch committed
    		SetDynamicColors(true).
    		SetTextAlign(tview.AlignCenter).
    
    Malte Bauch's avatar
    Malte Bauch committed
    		SetRegions(true).
    		SetBorder(true).
    		SetTitle("gRPC")
    
    
    	//TODO: gRPCs Health Check looks like a way better alternative here!
    	//--> no app.Draw() required anymore
    
    Malte Bauch's avatar
    Malte Bauch committed
    	sv.gRPCStatusView.SetChangedFunc(func() {
    		app.Draw()
    	})
    
    
    Malte Bauch's avatar
    Malte Bauch committed
    	go gRPCTicker(sv, conn)
    
    Malte Bauch's avatar
    Malte Bauch committed
    
    	return sv
    }
    
    
    //GetContent returns the tview.Primitive belonging to the gRPCStatusView
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (sv *GRPCStatusView) GetContent() tview.Primitive {
    	return sv.gRPCStatusView
    }
    
    
    //SetContent sets new string content for the gRPCStatusView
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (sv *GRPCStatusView) SetContent(s string) {
    	sv.gRPCStatusView.Clear()
    	sv.gRPCStatusView.SetText(s)
    }
    
    
    Malte Bauch's avatar
    Malte Bauch committed
    func gRPCTicker(sv *GRPCStatusView, conn *grpc.ClientConn) {
    	//TODO: refactor -> get rid of hardcoded values
    
    Malte Bauch's avatar
    Malte Bauch committed
    	ticker := time.NewTicker(5 * time.Second)
    	for range ticker.C {
    
    Malte Bauch's avatar
    Malte Bauch committed
    		if str := conn.GetState().String(); str == "READY" || str == "IDLE" {
    			sv.SetContent("[green]" + "connected")
    		} else {
    			sv.SetContent("[red]" + "disconnected")
    		}
    
    Malte Bauch's avatar
    Malte Bauch committed
    	}
    }