From 280b1c6c83b676ef4e592f4ecf60cb5b54b6a753 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Tue, 16 Jun 2020 10:40:34 +0200 Subject: Optimize socketclient adapter and add various code improvements This commit includes: Features - optimized [socketclient](adapter/socketclient) adapter and add method to set client name - added list of compatible messages to `CompatibilityError` Fixes - `MsgCodec` will recover panic occurring during a message decoding - calling `Unsubscibe` will close the notification channel Other - improved log messages to provide more relevant info Examples - added more code samples of working with unions in [union example](examples/union-example) - added profiling mode to [perf bench](examples/perf-bench) example - improved [simple client](examples/simple-client) example to work properly even with multiple runs Dependencies - updated `github.com/sirupsen/logrus` dep to `v1.6.0` - updated `github.com/lunixbochs/struc` dep to `v0.0.0-20200521075829-a4cb8d33dbbe` Change-Id: I136a3968ccf9e93760d7ee2b9902fc7e6390a09d Signed-off-by: Ondrej Fabry --- examples/union-example/union_example.go | 101 +++++++++++++++++--------------- 1 file changed, 55 insertions(+), 46 deletions(-) (limited to 'examples/union-example/union_example.go') diff --git a/examples/union-example/union_example.go b/examples/union-example/union_example.go index 92c3ec2..9993ee1 100644 --- a/examples/union-example/union_example.go +++ b/examples/union-example/union_example.go @@ -16,73 +16,82 @@ package main import ( - "bytes" "fmt" "log" "net" + "reflect" + "git.fd.io/govpp.git/codec" "git.fd.io/govpp.git/examples/binapi/ip" "git.fd.io/govpp.git/examples/binapi/ip_types" - - "github.com/lunixbochs/struc" ) +func init() { + log.SetFlags(0) +} + func main() { + constructExample() + encodingExample() - usageExample() + + // convert IP from string form into Address type containing union + convertIP("10.10.1.1") + convertIP("ff80::1") } -func encodingExample() { - // create union with IPv4 address - var unionIP4 ip.AddressUnion - unionIP4.SetIP4(ip.IP4Address{192, 168, 1, 10}) - - // use it in the Address type - addr := &ip.Address{ - Af: ip_types.ADDRESS_IP4, - Un: ip_types.AddressUnionIP4(ip.IP4Address{192, 168, 1, 10}), - } - log.Printf("encoding union IPv4: %v", addr.Un.GetIP4()) +func constructExample() { + var union ip_types.AddressUnion - // encode the address with union - data := encode(addr) - // decode the address with union - addr2 := decode(data) + // create AddressUnion with AdressUnionXXX constructors + union = ip_types.AddressUnionIP4(ip.IP4Address{192, 168, 1, 10}) + union = ip_types.AddressUnionIP6(ip.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}) - log.Printf("decoded union IPv4: %v", addr2.Un.GetIP4()) + // set AddressUnion with SetXXX methods + union.SetIP4(ip.IP4Address{192, 168, 1, 10}) + union.SetIP6(ip.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}) } -func encode(addr *ip.Address) []byte { - log.Printf("encoding address: %#v", addr) - buf := new(bytes.Buffer) - if err := struc.Pack(buf, addr); err != nil { - panic(err) +func encodingExample() { + var c codec.MsgCodec + + // encode this message + var msg = ip.IPPuntRedirect{ + Punt: ip.PuntRedirect{ + Nh: ip_types.Address{ + Af: ip_types.ADDRESS_IP4, + Un: ip_types.AddressUnionIP4(ip.IP4Address{192, 168, 1, 10}), + }, + }, + IsAdd: true, } - return buf.Bytes() -} + log.Printf("encoding message: %+v", msg) -func decode(data []byte) *ip.Address { - addr := new(ip.Address) - buf := bytes.NewReader(data) - if err := struc.Unpack(buf, addr); err != nil { - panic(err) + b, err := c.EncodeMsg(&msg, 1) + if err != nil { + log.Fatal(err) } - log.Printf("decoded address: %#v", addr) - return addr -} -func usageExample() { - var convAddr = func(ip string) { - addr, err := ipToAddress(ip) - if err != nil { - log.Printf("converting ip %q failed: %v", ip, err) - } - fmt.Printf("% 0X\n", addr) + // decode into this message + var msg2 ip.IPPuntRedirect + if err := c.DecodeMsg(b, &msg2); err != nil { + log.Fatal(err) } + log.Printf("decoded message: %+v", msg2) - convAddr("10.10.10.10") - convAddr("::1") - convAddr("") + // compare the messages + if !reflect.DeepEqual(msg, msg2) { + log.Fatal("messages are not equal") + } +} + +func convertIP(ip string) { + addr, err := ipToAddress(ip) + if err != nil { + log.Printf("error converting IP: %v", err) + return + } + fmt.Printf("converted IP %q to: %+v\n", ip, addr) } func ipToAddress(ipstr string) (addr ip.Address, err error) { @@ -98,7 +107,7 @@ func ipToAddress(ipstr string) (addr ip.Address, err error) { } else { addr.Af = ip_types.ADDRESS_IP4 var ip4addr ip.IP4Address - copy(ip4addr[:], ip4) + copy(ip4addr[:], ip4.To4()) addr.Un.SetIP4(ip4addr) } return -- cgit 1.2.3-korg