github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/socketservice/testclient/testclient.go (about) 1 // Copyright 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package testclient implements a socketservice client meant for testing. 16 package main 17 18 import ( 19 "flag" 20 "sync" 21 22 log "github.com/golang/glog" 23 24 "github.com/google/fleetspeak/fleetspeak/src/client/channel" 25 "github.com/google/fleetspeak/fleetspeak/src/client/service" 26 "github.com/google/fleetspeak/fleetspeak/src/client/socketservice/client" 27 ) 28 29 var mode = flag.String("mode", "loopback", "Mode of operation.") 30 var socketPath = flag.String("socket_path", "", "Location of socket to contact.") 31 32 func main() { 33 flag.Parse() 34 35 // All of the modes require interaction with the socket. 36 if *socketPath == "" { 37 log.Exit("--socket_path is required") 38 } 39 40 switch *mode { 41 case "loopback": 42 loopback() 43 case "ackLoopback": 44 ackLoopback() 45 case "stutteringLoopback": 46 stutteringLoopback() 47 default: 48 log.Exitf("unknown mode: %s", *mode) 49 } 50 } 51 52 func openChannel() *channel.RelentlessChannel { 53 log.Infof("opening relentless channel to %s", *socketPath) 54 return client.OpenChannel(*socketPath, "0.5") 55 } 56 57 func loopback() { 58 log.Info("starting loopback") 59 60 ch := openChannel() 61 for m := range ch.In { 62 log.Infof("Looping message of type [%s]", m.MessageType) 63 m.MessageType = m.MessageType + "Response" 64 ch.Out <- service.AckMessage{M: m} 65 log.Infof("Message %x looped.", m.MessageId) 66 } 67 } 68 69 func ackLoopback() { 70 log.Info("starting acknowledging loopback") 71 72 ch := openChannel() 73 var w sync.WaitGroup 74 for m := range ch.In { 75 log.Infof("Looping message of type [%s]", m.MessageType) 76 m.MessageType = m.MessageType + "Response" 77 w.Add(1) 78 ch.Out <- service.AckMessage{M: m, Ack: w.Done} 79 log.Infof("Message %x looped.", m.MessageId) 80 w.Wait() 81 log.Infof("Message %x ack'd.", m.MessageId) 82 } 83 } 84 85 func stutteringLoopback() { 86 log.Info("starting stuttering loopback") 87 88 ch := openChannel() 89 var w sync.WaitGroup 90 for { 91 m, ok := <-ch.In 92 if !ok { 93 log.Fatal("RelentlessChannel unexpectedly closed.") 94 } 95 log.Infof("Looping message of type [%s]", m.MessageType) 96 m.MessageType = m.MessageType + "Response" 97 w.Add(1) 98 ch.Out <- service.AckMessage{M: m, Ack: w.Done} 99 log.Infof("Message %x looped.", m.MessageId) 100 w.Wait() 101 log.Infof("Message %x ack'd.", m.MessageId) 102 103 close(ch.Out) 104 105 ch = openChannel() 106 } 107 }