Skip to content

Commit

Permalink
CLI: Send/wait for TriggerAction group
Browse files Browse the repository at this point in the history
  • Loading branch information
mame82 committed Oct 26, 2018
1 parent cdf8a0d commit d50c854
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 305 deletions.
97 changes: 97 additions & 0 deletions cli_client/cmd_trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cli_client

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
)



const (
templateFlagTriggerGroupName = "group-name"
templateFlagTriggerGroupValue = "group-value"
)

var (
// deploy
tmpTriggerGroupName = ""
tmpTriggerGroupValue = int32(0)
)

func TriggerCheckFlags(cmd *cobra.Command) {
valDefined,nameDefined := false,false
cmd.Flags().Visit(func(flag *pflag.Flag) {
if flag.Name == templateFlagTriggerGroupName {
nameDefined = true
}
if flag.Name == templateFlagTriggerGroupValue {
valDefined = true
}
})

check := true
if !nameDefined {
fmt.Printf("The '%s' flag has to be set\n", templateFlagTriggerGroupName)
check = false
}
if !valDefined {
fmt.Printf("The '%s' flag has to be set\n", templateFlagTriggerGroupValue)
check = false
}

if !check {
os.Exit(-1)
}
}

func init() {
cmdTrigger := &cobra.Command{
Use: "trigger",
Short: "Fire a group send action or wait for a group receive trigger",
}

cmdTriggerSend := &cobra.Command{
Use: "send",
Short: "Fire a group send action",
Run: func(cmd *cobra.Command, args []string) {
TriggerCheckFlags(cmd)
fmt.Printf("Sending value %d to group '%s'...", tmpTriggerGroupValue, tmpTriggerGroupName)
err := ClientTriggerGroupSend(StrRemoteHost,StrRemotePort,tmpTriggerGroupName,tmpTriggerGroupValue)
if err != nil {
fmt.Println("error: ", err)
os.Exit(-1)
}
fmt.Println("success")
},

}

cmdTriggerWait := &cobra.Command{
Use: "wait",
Short: "wait for a group receive trigger",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
TriggerCheckFlags(cmd)
fmt.Printf("Waiting for value %d on group '%s'...", tmpTriggerGroupValue, tmpTriggerGroupName)
err := ClientTriggerGroupWait(StrRemoteHost,StrRemotePort,tmpTriggerGroupName,tmpTriggerGroupValue)
if err != nil {
fmt.Println("error: ", err)
os.Exit(-1)
}
fmt.Println("received")
},
}



rootCmd.AddCommand(cmdTrigger)
cmdTrigger.AddCommand(cmdTriggerSend, cmdTriggerWait)

cmdTriggerSend.Flags().StringVarP(&tmpTriggerGroupName, templateFlagTriggerGroupName, "n", "","Name of the group to send to")
cmdTriggerSend.Flags().Int32VarP(&tmpTriggerGroupValue, templateFlagTriggerGroupValue, "v", 0,"The value to send")

cmdTriggerWait.Flags().StringVarP(&tmpTriggerGroupName, templateFlagTriggerGroupName, "n", "","Name of the group to listen")
cmdTriggerWait.Flags().Int32VarP(&tmpTriggerGroupValue, templateFlagTriggerGroupValue, "v", 0,"The value to wait for")
}
29 changes: 29 additions & 0 deletions cli_client/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ func ClientConnectServer(rpcHost string, rpcPort string) (
return
}

func ClientTriggerGroupWait(host string, port string, groupname string, value int32) (err error) {
address := host + ":" + port
connection, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil { return errors.New(fmt.Sprintf("Could not connect to P4wnP1 RPC server: %v", err)) }
defer connection.Close()

rpcClient := pb.NewP4WNP1Client(connection)
ctx := context.Background()

_,err = rpcClient.WaitTriggerGroupReceive(ctx, &pb.TriggerGroupReceive{GroupName: groupname, Value: value})

return
}
func ClientTriggerGroupSend(host string, port string, groupname string, value int32) (err error) {
address := host + ":" + port
connection, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil { return errors.New(fmt.Sprintf("Could not connect to P4wnP1 RPC server: %v", err)) }
defer connection.Close()

rpcClient := pb.NewP4WNP1Client(connection)
ctx := context.Background()
ctx,cancel := context.WithTimeout(ctx,TIMEOUT_SHORT)
defer cancel()

_,err = rpcClient.FireActionGroupSend(ctx, &pb.ActionGroupSend{GroupName: groupname, Value: value})

return
}

func ClientCreateTempDir(host string, port string, dir string, prefix string) (resultPath string, err error) {
return clientCreateTempDirOfFile(host,port,dir,prefix,true)
}
Expand Down
21 changes: 21 additions & 0 deletions proto/gopherjs/grpc.pb.gopherjs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d50c854

Please sign in to comment.