// Sends marshaled data through 0mq socket. func (g *Goodbye) Send(socket *zmq.Socket) (err error) { frame, err := g.Marshal() if err != nil { return err } socType, err := socket.GetType() if err != nil { return err } // If we're sending to a ROUTER, we send the routingId first if socType == zmq.ROUTER { _, err = socket.SendBytes(g.routingId, zmq.SNDMORE) if err != nil { return err } } // Now send the data frame _, err = socket.SendBytes(frame, 0) if err != nil { return err } return err }
func receieve(socketrep *zmq.Socket, s Server) { var d database d.Map = make(map[int]*mydetails) go d.statemachinecommit(s) for { fmt.Println("tcp://127.0.0.1:7001") msg, err := socketrep.RecvBytes(0) fmt.Println("recived") if err != nil { fmt.Println(err) } var req request json.Unmarshal(msg, &req) fmt.Println("parsing----------------------------------------------------------------------------------------") fmt.Println(s.leaderid) fmt.Println("parsing") fmt.Println(req) resp := d.parse(req, s) fmt.Println(resp) message, _ := json.Marshal(resp) socketrep.SendBytes(message, 0) } }
func StartZeromqProtoRepServer(port string) { var socket *zmq.Socket var err error if socket, err = zmq.NewSocket(zmq.REP); err != nil { log.Fatal(err) } if err = socket.Bind("tcp://0.0.0.0:" + port); err != nil { log.Fatal(err) } for { request, _ := socket.RecvBytes(0) var body pb.Request proto.Unmarshal(request, &body) response, _ := proto.Marshal(&pb.Response{ Method: body.Method, PayloadLength: int64(len(body.Payload)), }) socket.SendBytes(response, 0) } }
func StartZeromqProtobufTest(address string, clients int, requestsPerClient int, messageSize int, timer metrics.Timer, requestSize *int) func(wg *sync.WaitGroup) { return func(wg *sync.WaitGroup) { var socket *zmq.Socket var err error var request []byte if socket, err = zmq.NewSocket(zmq.REQ); err != nil { log.Fatal(err) } defer socket.Close() defer wg.Done() if err = socket.Connect("tcp://" + address); err != nil { log.Fatal(err) } if request, err = proto.Marshal(&pb.Request{ Method: "TEST", Payload: strings.Repeat("a", messageSize), }); err != nil { log.Fatal(err) } *requestSize = len(request) for i := 0; i < requestsPerClient; i++ { timer.Time(func() { socket.SendBytes(request, 0) socket.Recv(0) }) } } }
func sendError(socket *zmq.Socket, req *Request, err error) { // Response envelope resp := &Response{ Error: &Response_Error{}, } if req != nil { resp.UUID = req.UUID } // If error is a zrpc error if zrpcErr, ok := err.(zrpcError); ok { resp.StatusCode = proto.Uint32(uint32(zrpcErr.GetStatusCode())) resp.Error.Message = proto.String(zrpcErr.GetMessage()) } else { // Default to internal error resp.StatusCode = proto.Uint32(uint32(http.StatusInternalServerError)) resp.Error.Message = proto.String(err.Error()) } // Encode the response buf, protoErr := proto.Marshal(resp) if protoErr != nil { glog.Error(protoErr) return } // Send the response if _, err := socket.SendBytes(buf, 0); err != nil { glog.Error(err) } }
func send(socket *zmq.Socket, update keyUpdate) error { var err error if _, err = socket.Send(update.key, zmq.SNDMORE); err != nil { return err } if _, err = socket.SendBytes(update.payload, 0); err != nil { return err } return nil }
func send(requester *zmq.Socket, req *proto.Request) { data, err := protobuf.Marshal(req) panicOnErr(err) requester.SendBytes(data, 0) reply, err := requester.RecvBytes(0) panicOnErr(err) response := new(proto.Response) err = protobuf.Unmarshal(reply, response) panicOnErr(err) fmt.Println("Received ", response.String()) }
// Send sends marshaled data through 0mq socket. func (b *Binary) Send(socket *zmq.Socket) (err error) { frame, err := b.Marshal() if err != nil { return err } socType, err := socket.GetType() if err != nil { return err } // If we're sending to a ROUTER, we send the routingID first if socType == zmq.ROUTER { _, err = socket.SendBytes(b.routingID, zmq.SNDMORE) if err != nil { return err } } // Now send the data frame _, err = socket.SendBytes(frame, zmq.SNDMORE) if err != nil { return err } // Now send any frame fields, in order _, err = socket.SendBytes(b.Address, zmq.SNDMORE) _, err = socket.SendBytes(b.Content, 0) return err }
func AddToQueue(sender *zmq.Socket, data interface{}) { s, err := json.Marshal(data) if err != nil { glog.Error("failed to serialize object when adding to queue ", err, " data = ", data) return } // uncomment this line to see the whole json serialization of data - for debugging purpose glog.V(1).Info(string(s)) _, err = sender.SendBytes(s, zmq.DONTWAIT) if err != nil { glog.Error("Error sending to ZMQ :", err) } glog.V(1).Info("Add to queue done") }
// Sends marshaled data through 0mq socket. func (d *Deliver) Send(socket *zmq.Socket) (err error) { frame, err := d.Marshal() if err != nil { return err } socType, err := socket.GetType() if err != nil { return err } // If we're sending to a ROUTER, we send the routingId first if socType == zmq.ROUTER { _, err = socket.SendBytes(d.routingId, zmq.SNDMORE) if err != nil { return err } } // Now send the data frame _, err = socket.SendBytes(frame, zmq.SNDMORE) if err != nil { return err } // Now send any frame fields, in order _, err = socket.SendBytes(d.Content, 0) return err }
func (w *worker) socketLoop(s *zmq.Socket, send <-chan Data, recv chan<- Data, state zmq.State) { if recv != nil { defer close(recv) } var ( sendBuf Data sendPending bool recvBuf Data recvPending bool ) for { var ( err error sendActive <-chan Data recvActive chan<- Data ) if !sendPending { sendActive = send } if recvPending { recvActive = recv } select { case <-w.notifier: const fullState = zmq.POLLIN | zmq.POLLOUT if state&fullState != fullState { if state, err = s.GetEvents(); err != nil { handleGeneralError(err) return } } case <-w.closer: return case sendBuf, sendPending = <-sendActive: if !sendPending { send = nil } case recvActive <- recvBuf: recvPending = false recvBuf.Bytes = nil } for { loop := false if sendPending && state&zmq.POLLOUT != 0 { flags := zmq.DONTWAIT if sendBuf.More { flags |= zmq.SNDMORE } if _, err = s.SendBytes(sendBuf.Bytes, flags); err == nil { sendPending = false sendBuf.Bytes = nil loop = true } else if !handleIOError(err) { return } if state, err = s.GetEvents(); err != nil { handleGeneralError(err) return } } if !recvPending && state&zmq.POLLIN != 0 { if data, err := s.RecvBytes(zmq.DONTWAIT); err == nil { if more, err := s.GetRcvmore(); err == nil { recvBuf.Bytes = data recvBuf.More = more recvPending = true loop = true } else { handleGeneralError(err) return } } else if !handleIOError(err) { return } if state, err = s.GetEvents(); err != nil { handleGeneralError(err) return } } if !loop { break } } } }
// handleOutPort handles messages sent from this server func (s *serverImpl) handleOutPort() { // fmt.Println("handleOutPort: Waiting for message on outbox") // initial cache refresh s.setAddressOf(make(map[int]string)) s.refreshPeerCache(BROADCAST) peerSocketCache := make(map[int]*zmq.Socket) for { msg := <-s.outbox receivers := list.New() // BROADCAST packet or packet for new server if _, ok := s.address(msg.Pid); !ok { s.refreshPeerCache(msg.Pid) // packet is dropped silently if _, ok := s.address(msg.Pid); !ok && msg.Pid != BROADCAST { fmt.Println("Address of ", msg.Pid, " could not be found") continue } } if msg.Pid != BROADCAST { receivers.PushBack(msg.Pid) } else { s.RLock() for key, _ := range s.addressOf { if key != s.pid { receivers.PushBack(key) } } s.RUnlock() } // change msg.Pid to match this server // receiving server will then find correct Pid msg.Pid = s.Pid() // send message to receivers gobbedMessage := EnvelopeToBytes(msg) for pid := receivers.Front(); pid != nil; pid = pid.Next() { // cache connections to peers var requester *zmq.Socket var ok bool var err error pidInt := 0 if pidInt, ok = pid.Value.(int); ok { } if requester, ok = peerSocketCache[pidInt]; !ok { requester, err = zmq.NewSocket(zmq.PUSH) if err != nil { fmt.Println("Error in creating request socket. ", err.Error()) return } peerSocketCache[pidInt] = requester socketStr, _ := s.address(pidInt) requester.Connect("tcp://" + socketStr) defer requester.Close() } requester.SendBytes(gobbedMessage, 0) } } }
func pointToPointListener(socket *zmq.Socket, getResponse func(hash []byte) []byte) { for { req, _ := socket.RecvBytes(0) socket.SendBytes(getResponse(req), 0) } }
func main() { flag.Parse(nil) address_list := flag.Args if len(address_list) == 0 { fmt.Println("No Addresses submitted") fmt.Println(flag.Help()) return } var send, recv bool skip := false var socket *zmq.Socket switch *socket_type { case "PUSH": socket, _ = zmq.NewSocket(zmq.PUSH) send = true case "PULL": socket, _ = zmq.NewSocket(zmq.PULL) recv = true case "PUB": socket, _ = zmq.NewSocket(zmq.PUB) send = true case "SUB": socket, _ = zmq.NewSocket(zmq.SUB) recv = true if len(*subscriptions) == 0 { socket.SetSubscribe("") } for _, subscription := range *subscriptions { socket.SetSubscribe(subscription) } case "REQ": socket, _ = zmq.NewSocket(zmq.REQ) send = true recv = true case "REP": socket, _ = zmq.NewSocket(zmq.REP) send = true recv = true skip = true } defer socket.Close() // connect or bind if *mode { for _, address := range address_list { socket.Connect(address) } } else { for _, address := range address_list { socket.Bind(address) } } delim := byte('\n') if *null { fmt.Println("Setting delim to null") delim = byte(0x00) } reader := bufio.NewReader(os.Stdin) writer := bufio.NewWriter(os.Stdout) for i := 0; i < *number || *number == -1; i++ { if send && !skip { line, _ := reader.ReadBytes(delim) socket.SendBytes([]byte(line), 0) } if recv { data, _ := socket.RecvBytes(0) writer.Write(data) writer.Flush() } if skip { skip = false } } fmt.Println("finished", *number) }