message_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package b1_test
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "src.eruta.nl/beoran/bdjncl/b1"
  7. )
  8. func ExampleMessage() {
  9. tm := b1.MakeMessage(123, 1, 2, "/jobs", []byte("body text"), "key1", "val1", "key2", "val2")
  10. ss := b1.MakeWire("Hello B1.")
  11. buf := bytes.Buffer{}
  12. err := binary.Write(&buf, binary.LittleEndian, ss)
  13. if err != nil {
  14. fmt.Printf("error: %s\n", err)
  15. }
  16. s2 := b1.Wire{}
  17. err = binary.Read(&buf, binary.LittleEndian, &s2)
  18. if err != nil {
  19. fmt.Printf("error: %s\n", err)
  20. }
  21. fmt.Println(s2.Len)
  22. fmt.Println(s2.String())
  23. buf = bytes.Buffer{}
  24. err = binary.Write(&buf, binary.LittleEndian, tm.MessageHeader)
  25. if err != nil {
  26. fmt.Printf("error: %s\n", err)
  27. }
  28. t2 := b1.MessageHeader{}
  29. err = binary.Read(&buf, binary.LittleEndian, &t2)
  30. if err != nil {
  31. fmt.Printf("error: %s\n", err)
  32. }
  33. fmt.Println(t2.Method.String())
  34. buf = bytes.Buffer{}
  35. tm.MarshalTo(&buf)
  36. m2 := b1.Message{}
  37. err = m2.UnmarshalFrom(&buf)
  38. if err != nil {
  39. fmt.Printf("error: %s\n", err)
  40. }
  41. fmt.Println(m2.Method)
  42. //output: 9
  43. //Hello B1.
  44. ///jobs
  45. ///jobs
  46. }