Advertisement
cooperlees

Untitled

Oct 4th, 2019
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.62 KB | None | 0 0
  1. // Sample go to hit the driver-if for a Link Assoc
  2. // TG sends Thrift messages over zmq sockets ...
  3.  
  4. package main
  5.  
  6. import (
  7.     "context"
  8.     "fmt"
  9.     "os"
  10.     ctrl "controller"
  11.     drv "drivermessage"
  12.  
  13.     thrift "git.apache.org/thrift.git/lib/go/thrift"
  14.     zmq "github.com/pebbe/zmq4"
  15. )
  16.  
  17.  
  18. func deserializeDriverThrift([]byte data) (drv.ttypes.DriverDevAllocRes, error) {
  19.     dr_response = drv.ttypes.DriverDevAllocRes()
  20.     return dr_response, nil
  21. }
  22.  
  23.  
  24. func serializeDriverThrift(driverMesasge drv.ttypes.DriverMessage) ([]byte, error) {
  25.     var messageBytes []byte
  26.  
  27.     buffer := thrift.NewMemoryBufferLen(1024)
  28.     proto := thrift.NewCompactProtocol(buffer)
  29.     serializer := &thrift.TSerializer{
  30.         Transport: buffer,
  31.         Protocol:  proto,
  32.     }
  33.     defer serializer.Transport.Close()
  34.  
  35.     err := serializer.Read(&messageBytes, driverMesasge)
  36.     return messageBytes, err
  37. }
  38.  
  39.  
  40. func main() {
  41.     connect_str := "tcp://localhost:18990"
  42.     requester, _ := zmq.NewSocket(zmq.REQ)
  43.     defer requester.Close()
  44.     requester.Connect(connect_str)
  45.     fmt.Println("Connected to ", connect_str)
  46.  
  47.     dr_msg := drv.ttypes.DriverMessage()
  48.     dr_msg.radioMac = "69:69:69:69:69:69"
  49.     dr_msg.value = drv.ttypes.DriverDevAllocReq(dr_msg.radioMac)
  50.  
  51.     dr_mgs_bytes, err = serializeDriverThrift(dr_msg)
  52.     if err != nil:
  53.         fmt.Println("Error with serialzing dr_msg")
  54.         os.Exit(1)
  55.  
  56.     msg = ctrl.ttypes.Message(ctrl.ttypes.MessageType.DR_DEV_ALLOC_REQ,
  57.         dr_mgs_bytes)
  58.  
  59.     fmt.Println("Sending ", msg)
  60.     requester.Send(msg, 0)
  61.  
  62.     // Wait for reply - need to
  63.     reply, _ := requester.Recv(0)
  64.     fmt.Println("Received ", reply)
  65.  
  66.     // TODO: deserialize with Thrift
  67.     deserializeDriverThrift(reply)
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement