Guest User

Untitled

a guest
Mar 6th, 2014
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4. "encoding/gob"
  5. "fmt"
  6. "net"
  7. "time"
  8. "os"
  9. "sync"
  10. )
  11.  
  12. const (
  13. dataStr string = "This is my string. There are many like it. But this one is mine."
  14. rounds int = 1
  15. )
  16.  
  17. type tester struct {
  18. T time.Time
  19. X net.IP
  20. D []byte
  21. M map[string]([]byte)
  22. }
  23.  
  24. type testa struct {
  25. X, Y, Z uint64
  26. Msg []byte
  27. }
  28.  
  29. func main() {
  30. var wg sync.WaitGroup
  31. lst, err := net.Listen("unix", "/tmp/comms")
  32. if err != nil {
  33. fmt.Printf("listen error: %v\n", err)
  34. os.Exit(-1)
  35. }
  36. defer lst.Close()
  37. conn, err := net.Dial("unix", "/tmp/comms")
  38. if err != nil {
  39. fmt.Printf("connect error: %v\n", err)
  40. os.Exit(-1)
  41. }
  42. defer conn.Close()
  43.  
  44. wg.Add(2)
  45. go reader(lst, &wg)
  46. go writer(conn, &wg)
  47. wg.Wait()
  48. }
  49.  
  50.  
  51. func writer(conn net.Conn, wg *sync.WaitGroup) {
  52. var err error
  53. var i int
  54. genc := gob.NewEncoder(conn)
  55. ip := net.ParseIP("BE::EF")
  56. for i = 0; i < rounds; i++ {
  57. err = genc.Encode(tester{time.Now(), ip, []byte(dataStr), nil})
  58. if err != nil {
  59. fmt.Printf("Encode error: %v\n", err)
  60. os.Exit(-1)
  61. }
  62. }
  63. err = genc.Encode(tester{time.Now(), nil, nil, nil})
  64. if err != nil {
  65. fmt.Printf("Failed to encode the nil message\n")
  66. os.Exit(-1)
  67. }
  68. fmt.Printf("Threw nil entry after %d rounds\n", i)
  69. err = genc.Encode(testa{1, 2, 3, []byte("HERE IS YOUR MESSAGE")})
  70. if err != nil {
  71. fmt.Printf("Failed to encode testa message\n")
  72. os.Exit(-1)
  73. }
  74. wg.Done()
  75. }
  76.  
  77. func reader(lst net.Listener, wg *sync.WaitGroup) {
  78. var t tester
  79. var ta testa
  80.  
  81. conn, err := lst.Accept()
  82. if err != nil {
  83. fmt.Printf("Accept error: %v\n", err)
  84. os.Exit(-1)
  85. }
  86. defer conn.Close()
  87. gdec := gob.NewDecoder(conn)
  88.  
  89. for i := 0; ; i++{
  90. err := gdec.Decode(&t)
  91. if err != nil {
  92. fmt.Printf("Decode error on #%d: %v\n", i, err)
  93. os.Exit(-1)
  94. }
  95. if t.D == nil || len(t.D) == 0 {
  96. fmt.Printf("Got the nil message\n")
  97. break
  98. }
  99. }
  100. err = gdec.Decode(&ta)
  101. if err != nil {
  102. fmt.Printf("She is dead jim...\n")
  103. os.Exit(-1)
  104. }
  105. if string(ta.Msg) != "HERE IS YOUR MESSAGE" {
  106. fmt.Printf("Failed to get the message out\n")
  107. os.Exit(-1)
  108. }
  109. wg.Done()
  110. }
RAW Paste Data