func bind(socket *zmq.Socket, transport string, address string, port int) { endpoint := fmt.Sprintf("%s://%s:%d", transport, address, port) err := socket.Bind(endpoint) if err != nil { die("failed binding %s: %s", endpoint, err.Error()) } }
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) } }
// SendReply will send the reply back to the connected reply proxy func SendReply(r *http.Response, body string, repsock *zmq.Socket) { rep := &messages.Reply{StatusCode: r.StatusCode, Body: body, Headers: r.Header} reps := rep.String() fmt.Printf("[PRIVATE] sending %d bytes or \n----\n%s\n-----\n", len(reps), reps) repsock.Send(reps, 0) fmt.Printf("[PRIVATE] sent %d bytes\n", len(reps)) }
func r(replier *zmq.Socket) { for { got, _ := replier.Recv(0) log.Println("Received", got) replier.Send(`{"jsonrpc":"2.0","id":"youpi","result":{"name":"test","hostname":"localtest","protocol":"test","port":0}}`, 0) } }
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 New(id int, f string) Cluster { var myid int var peer [MAX_SERVER]int var cluster [MAX_SERVER]*zmq.Socket var mycluster *zmq.Socket var no_of_p int file, _ := ioutil.ReadFile(f) var jsontype jsonobject json.Unmarshal(file, &jsontype) myid = id no_of_p = jsontype.Object.Buffer_size for i := 0; i < jsontype.Object.Buffer_size; i++ { if jsontype.Object.Peers[i].Id != myid { peer[i] = jsontype.Object.Peers[i].Id cluster[i], _ = zmq.NewSocket(zmq.PUSH) cluster[i].Connect("tcp://" + jsontype.Object.Peers[i].Host) } else { mycluster, _ = zmq.NewSocket(zmq.PULL) mycluster.SetIdentity(string(id)) a := strings.Split(jsontype.Object.Peers[i].Host, ":") mycluster.Bind("tcp://*:" + a[1]) } } return Cluster{myid: id, peer: peer, no_of_peer: no_of_p, my_cluster: mycluster, cluster: cluster, inbox: make(chan *Envelope, BUFFER_LENGTH_IN), outbox: make(chan *Envelope, BUFFER_LENGTH_OUT)} }
// This is simple send function here i communicate with raft using simple request-response socket and ask to raft their status // like phase of raft and we can also close raft server using this function. // Here msg :- 1 means msg for ask phase of server and msg:- 0 means close the raft server func send(msg string, requester_C *zmq.Socket) string { // Decoding of phse receive by type 0 msg is 0 :- Follower, 1:- Candidate, 2:- Leader // By default phase :- "3" means no response from server and server is close requester_C.Send(msg, 0) phase := "3" if msg == "0" { return "0" } phase, _ = requester_C.Recv(0) type1 := s.Split(phase, " ")[0] value := s.Split(phase, " ")[1] if type1 == "-1" { if value == "0" { phase = "Follower" } else if value == "1" { phase = "Candidate" } else if value == "2" { phase = "Leader" } } else if type1 == "-2" { leaderId, _ = strconv.Atoi(value) } else if type1 == "0" { leaderId, _ = strconv.Atoi(s.Split(phase, " ")[2]) msg_index, _ := strconv.Atoi(value) send(message[msg_index], requester[leaderId-1]) return "0" } else { } return phase }
func RecMsgFromPeers(s *zmq.Socket, ser Servernode) { var e Envelope for { msg, err := s.RecvMessage(0) a1 := strings.Split(msg[0], "&{") a2 := strings.Split(a1[1], " ") pid, _ := strconv.Atoi(a2[0]) mid, _ := strconv.Atoi(a2[1]) msgid := int64(mid) text := "" for b := 2; b < len(a2); b++ { text = text + " " + a2[b] } text1 := text[1:] msgtext := strings.Split(text1, "}")[0] e.Pid = pid e.MsgId = msgid e.Msg = msgtext go QueueInbox(ser, e, ser.Inbox()) if err != nil { break } s.SendMessage(msg) } }
func (c *WorkerConnection) run(socket *zmq.Socket) { defer socket.Close() socket.SetRcvtimeo(c.activeTimeout) for { select { case <-c.quit: close(c.wait) return case request := <-c.requestChannel: if _, err := socket.SendMessage(request); err != nil { log.Println("Failed to send request:", err) } case request := <-c.connectionChan: go c.handleConnectionRequest(socket, request) default: message, err := socket.RecvMessageBytes(0) if err != nil { // Needed to yield to goroutines when GOMAXPROCS is 1. // Note: The 1.3 preemptive scheduler doesn't seem to work here, // so this is still required. runtime.Gosched() break } socket.SetRcvtimeo(c.activeTimeout) go c.handleResponse(message) } } }
// 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 (k *kernelRunner) readMessage(socket *zmq.Socket) (*message, []string, error) { parts, err := socket.RecvMessage(0) if err != nil { return nil, nil, err } return deserializeMessage(parts, []byte(k.connInfo.Key)) }
/**************************************************************** Receiving Message from all Servers ****************************************************************/ func receiveMessage(recSockets *zmq.Socket, conn *Connection) { for { select { // Stopping Go Routine case _, _ = <-conn.stopGoRoutine: return default: recSockets.SetRcvtimeo(500 * time.Millisecond) msg, err := recSockets.Recv(0) if err == nil { msg_array := strings.Split(msg, ":") msg_seq := msg_array[0] isLeader := msg_array[1] leaderId, _ := strconv.Atoi(msg_array[2]) count, _ := strconv.Atoi(isLeader) // Added the LeaderCount if the response belongs to corresponding request if strconv.Itoa(conn.attempt) == msg_seq { conn.recvCount += 1 conn.leaders += count if leaderId != 0 { conn.leaderId = leaderId } } } } } }
// Receives marshaled data from 0mq socket. func recv(socket *zmq.Socket, flag zmq.Flag) (t Transit, err error) { // Read all frames frames, err := socket.RecvMessageBytes(flag) if err != nil { return nil, err } sType, err := socket.GetType() if err != nil { return nil, err } var routingId []byte // If message came from a router socket, first frame is routingId if sType == zmq.ROUTER { if len(frames) <= 1 { return nil, errors.New("no routingId") } routingId = frames[0] frames = frames[1:] } t, err = Unmarshal(frames...) if err != nil { return nil, err } if sType == zmq.ROUTER { t.SetRoutingId(routingId) } return t, err }
/************************************************************ Creates Sockets for Sending and Receiving purpose *************************************************************/ func createSendAndReceivingSocket(configFile string, selfPort int) ([]*zmq.Socket, *zmq.Socket) { file, e := ioutil.ReadFile("./" + configFile) if e != nil { fmt.Printf("Raft Test File error: %v\n", e) os.Exit(1) } var jsontype jsonobject json.Unmarshal(file, &jsontype) elementCount := len(jsontype.Server) sendConnections := make([]*zmq.Socket, elementCount) tempId := 0 for _, value := range jsontype.Server { sendConnections[tempId], _ = zmq.NewSocket(zmq.PUSH) sendConnections[tempId].Connect("tcp://localhost:" + strconv.Itoa(value.Port_Num+1)) tempId++ } var receiveConnection *zmq.Socket receiveConnection, _ = zmq.NewSocket(zmq.PULL) receiveConnection.Bind("tcp://*:" + strconv.Itoa(selfPort)) return sendConnections, receiveConnection }
// NewEventSubSocketHandler returns a function that handles event notifications func NewEventSubSocketHandler(eventSubSocket *zmq4.Socket) func(zmq4.State) error { var nodeIDMap map[string]uint32 var err error if nodeIDMap, err = centraldb.GetNodeIDMap(); err != nil { fog.Critical("centraldb.GetNodeIDMap() failed %s", err) } return func(_ zmq4.State) error { var err error var ok bool var webWriterStart msg.WebWriterStart var timestamp time.Time var sourceNodeID uint32 marshalledMessage, err := eventSubSocket.RecvMessage(0) if err != nil { return fmt.Errorf("RecvMessage %s", err) } // the 0th part should be the topic, we skip that err = json.Unmarshal([]byte(marshalledMessage[1]), &webWriterStart) if err != nil { return fmt.Errorf("Unmarshal %s", err) } if webWriterStart.MessageType != "web-writer-start" { return fmt.Errorf("unknown message type '%s'", webWriterStart.MessageType) } timestamp, err = tools.ParseTimestampRepr(webWriterStart.TimestampRepr) if err != nil { return fmt.Errorf("unable to parse %s %s", webWriterStart.TimestampRepr, err) } sourceNodeID, ok = nodeIDMap[webWriterStart.SourceNodeName] if !ok { return fmt.Errorf("unknown source_node_name %s", webWriterStart.SourceNodeName) } fog.Debug("cancel-segments-from-node %s", webWriterStart.SourceNodeName) // cancel all all segment rows // * from a specifiic source node // * are in active status // * with a timestamp earlier than the specified time. // This is triggered by a web server restart stmt := nodedb.Stmts["cancel-segments-from-node"] if _, err = stmt.Exec(sourceNodeID, timestamp); err != nil { return fmt.Errorf("cancel-segments-from-node %s", err) } return nil } }
func openDoor(sp gpio.DirectPinDriver, publisher *zmq.Socket) { sp.DigitalWrite(1) publisher.SendMessage("door.state.unlock", "Door Unlocked") gobot.After(5*time.Second, func() { sp.DigitalWrite(0) publisher.SendMessage("door.state.lock", "Door Locked") }) }
// Echo service func echo(socket *zmq.Socket) (err error) { msg, err := socket.RecvMessage(0) if err != nil { return } _, err = socket.SendMessage(msg) return }
// ZmqSendMulti Sends a slice of strings as a multi-part message func ZmqSendMulti(s *zmq.Socket, msg []string) { lastIdx := len(msg) - 1 for idx, part := range msg { if idx == lastIdx { s.Send(part, 0) } else { s.Send(part, zmq.SNDMORE) } } }
// ZmqRecvMulti Receives a multi-part message and return it as a slice of strings func ZmqRecvMulti(s *zmq.Socket) []string { var msg []string more := true for more { part, _ := s.Recv(0) msg = append(msg, part) more, _ = s.GetRcvmore() } return msg }
func (k *kernelRunner) sendMessage(msg *message, ids []string, socket *zmq.Socket) error { parts, err := serializeMessage(msg, ids, []byte(k.connInfo.Key)) if err != nil { return fmt.Errorf("serializing message: %v", err) } if _, err := socket.SendMessage(parts); err != nil { return fmt.Errorf("sending message: %v", err) } return nil }
func applyZMQConfig(socket *zmq.Socket, configs []ZMQConfig) { for _, config := range configs { switch config.Type { case "bind": socket.Bind(config.Uri) case "connect": socket.Connect(config.Uri) } } }
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) }) } } }
/****************************************************************************** * 概述: 订阅退订 * 函数名: dealCmd * 返回值: * 参数列表: 参数名 参数类型 取值范围 描述 * *******************************************************************************/ func (this *ZmqSocket) dealCmd(cmdSocket *zmq4.Socket) (int, error) { log4.Debug("start deal cmd...") for { //半包处理 cmdsStr, err0 := cmdSocket.Recv(zmq4.DONTWAIT) if err0 != nil { errno1 := zmq4.AsErrno(err0) switch errno1 { case zmq4.Errno(syscall.EAGAIN): return 0, nil case zmq4.Errno(syscall.EINTR): continue default: log4.Debug("zmq req Get err %v, %d!", errno1, errno1) } } if len(cmdsStr) == 0 { log4.Debug("deal cmd return") return 0, nil } ss := strings.Split(cmdsStr, " ") log4.Debug("recv cmd %s", cmdsStr) for i := 0; i < len(ss); i++ { if len(ss[i]) == 0 { continue } if ss[i] == "stop" { // log4.Debug("recv cmd will stop %s %s", this.mreqUrl, this.msubUrl) cmdSocket.Send("0", 0) return 1, nil } if !this.mChoose { // log4.Debug("recv cmd ,but notChoose so return") return 0, nil } if ss[i][0] == 'r' { if err := this.msubSocket.SetSubscribe(ss[i][1:]); err != nil { log4.Error("SetSubscribe(%s) falied, %s", ss[i][1:], err.Error()) return 0, err } log4.Debug("setSubscribe ok %s", ss[i][1:]) continue } if ss[i][0] == 'u' { if err := this.msubSocket.SetUnsubscribe(ss[i][1:]); err != nil { log4.Error("SetUnSubscribe(%s) falied, %s", ss[i][1:], err.Error()) return 0, err } log4.Debug("setUnSubscribe ok %s", ss[i][1:]) continue } } } return 0, nil }
func sendToClient(message string, header []string, title string, more zmq.Flag, frontend *zmq.Socket) { //fmt.Println("\t", title) for key := range header { //fmt.Printf("\tSending to client: %s\n", header[key]) frontend.Send(header[key], zmq.SNDMORE) } //fmt.Printf("\tSending to client: %s\n", message) frontend.Send(message, more) //fmt.Println("\tDone") }
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 }
// main entry point for data writer func main() { var err error var writerSocket *zmq4.Socket var eventSubSocket *zmq4.Socket fog.Info("program starts") if writerSocket, err = createWriterSocket(); err != nil { fog.Critical("createWriterSocket %s", err) } defer writerSocket.Close() fog.Info("binding writer socket to %s", dataWriterAddress) if err = writerSocket.Bind(dataWriterAddress); err != nil { fog.Critical("Bind(%s) %s", dataWriterAddress, err) } if eventSubSocket, err = createEventSubSocket(); err != nil { fog.Critical("createEventSubSocket %s", err) } defer eventSubSocket.Close() fog.Info("connecting event sub socket to %s", eventAggregatorPubAddress) if err = eventSubSocket.Connect(eventAggregatorPubAddress); err != nil { fog.Critical("Connect(%s) %s", eventAggregatorPubAddress, err) } messageChan := NewMessageHandler() reactor := zmq4.NewReactor() reactor.AddChannel(tools.NewSignalWatcher(), 1, tools.SigtermHandler) reactor.AddSocket(writerSocket, zmq4.POLLIN, NewWriterSocketHandler(writerSocket, messageChan)) reactor.AddSocket(eventSubSocket, zmq4.POLLIN, NewEventSubSocketHandler(eventSubSocket)) fog.Debug("starting reactor.Run") reactor.SetVerbose(true) err = reactor.Run(reactorPollingInterval) if err == tools.SigtermError { fog.Info("program terminates normally due to SIGTERM") } else if errno, ok := err.(syscall.Errno); ok { // we can get 'interrupted system call' if we get SIGTERM while // a socket is waiting on a read. That's not too bad. if errno == syscall.EINTR { fog.Warn("reactor.Run returns '%s' assuming SIGTERM", errno) } else { fog.Error("reactor.Run returns %T '%s'", errno, errno) } } else { fog.Error("reactor.Run returns %T %s", err, err) } }
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()) }
// Application wants to speak to us, see if it's possible func (bstar *Bstar) voter_ready(socket *zmq.Socket) error { // If server can accept input now, call appl handler bstar.event = client_REQUEST err := bstar.execute_fsm() if err == nil { bstar.voter_fn(socket) } else { // Destroy waiting message, no-one to read it socket.RecvMessage(0) } return nil }
// args: // - addr: multicast group/address to listen to // - port: port number; addr:port builds the mcast socket // - iface: name of network interface to listen to // - d_dl_sock: // - stopch: func ListenUDPMcast(addr, port, iface string, d_dl_sock *zmq.Socket, stopch chan bool) { eth, err := net.InterfaceByName(iface) if err != nil { fmt.Println("Error interface:", err.Error()) os.Exit(1) } group := net.ParseIP(addr) if group == nil { fmt.Println("Error: invalid group address:", addr) os.Exit(1) } // listen to all udp packets on mcast port c, err := net.ListenPacket("udp4", "0.0.0.0:"+port) if err != nil { fmt.Println("Error listening for mcast:", err.Error()) os.Exit(1) } // close the listener when the application closes defer c.Close() // join mcast group p := ipv4.NewPacketConn(c) if err := p.JoinGroup(eth, &net.UDPAddr{IP: group}); err != nil { fmt.Println("Error joining:", err.Error()) os.Exit(1) } fmt.Println("Listening on " + addr + ":" + port) // enable transmissons of control message if err := p.SetControlMessage(ipv4.FlagDst, true); err != nil { fmt.Println("Error control message", err.Error()) } c1 := devreader.MakeChannel(UMSocket{p, group}) LOOP: for { select { case v1 := <-c1: fmt.Println("received UDP multicast") // forward to coord node d_dl_sock.Send(string(v1), 0) case <-stopch: break LOOP } } }
func initSecurity(private_key_path string, server_key_path string, sock *zmq.Socket) { zmq.AuthStart() private_key, public_key, err := keyloader.InitKeys(private_key_path) condlog.Fatal(err, fmt.Sprintf("Unable to read key pair for private key '%v'", private_key_path)) zmq.AuthCurveAdd("scrabble", public_key) err = keyloader.CheckPermissions(server_key_path) condlog.Fatal(err, "Untrustworthy key file") server_key_buf, err := ioutil.ReadFile(server_key_path) condlog.Fatal(err, fmt.Sprintf("Unable to load public server key '%v'", server_key_path)) server_key := string(server_key_buf) sock.ClientAuthCurve(server_key, public_key, private_key) }