本文整理汇总了Golang中github.com/pebbe/zmq4.Socket.RecvMessage方法的典型用法代码示例。如果您正苦于以下问题:Golang Socket.RecvMessage方法的具体用法?Golang Socket.RecvMessage怎么用?Golang Socket.RecvMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pebbe/zmq4.Socket
的用法示例。
在下文中一共展示了Socket.RecvMessage方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RecMsgFromPeers
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)
}
}
示例2: readMessage
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))
}
示例3: snapshots
func snapshots(socket *zmq.Socket, srv *clonesrv_t) (err error) {
msg, err := socket.RecvMessage(0)
if err != nil {
return
}
identity := msg[0]
// Request is in second frame of message
request := msg[1]
if request != "ICANHAZ?" {
err = errors.New("E: bad request, aborting")
return
}
subtree := msg[2]
// Send state socket to client
for _, kvmsg := range srv.kvmap {
if key, _ := kvmsg.GetKey(); strings.HasPrefix(key, subtree) {
socket.Send(identity, zmq.SNDMORE)
kvmsg.Send(socket)
}
}
// Now send END message with sequence number
log.Println("I: sending shapshot =", srv.sequence)
socket.Send(identity, zmq.SNDMORE)
kvmsg := kvmsg.NewKvmsg(srv.sequence)
kvmsg.SetKey("KTHXBAI")
kvmsg.SetBody(subtree)
kvmsg.Send(socket)
return
}
示例4: NewEventSubSocketHandler
// 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
}
}
示例5: echo
// Echo service
func echo(socket *zmq.Socket) (err error) {
msg, err := socket.RecvMessage(0)
if err != nil {
return
}
_, err = socket.SendMessage(msg)
return
}
示例6: voter_ready
// 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
}
示例7: RecvKvmsg
// The RecvKvmsg function reads a key-value message from socket, and returns a new
// Kvmsg instance.
func RecvKvmsg(socket *zmq.Socket) (kvmsg *Kvmsg, err error) {
kvmsg = &Kvmsg{
present: make([]bool, kvmsg_FRAMES),
frame: make([]string, kvmsg_FRAMES),
}
msg, err := socket.RecvMessage(0)
if err != nil {
return
}
//fmt.Printf("Recv from %s: %q\n", socket, msg)
for i := 0; i < kvmsg_FRAMES && i < len(msg); i++ {
kvmsg.frame[i] = msg[i]
kvmsg.present[i] = true
}
return
}
示例8: RequestAndReply
/* {{{ func RequestAndReply(soc *zmq.Socket, msg interface{}) (reply []string, err error) {
* 支持超时时间,但要注意如果一个socket失败之后,因为req严格同步,连接池模式下这个socket最好销毁
*/
func RequestAndReply(soc *zmq.Socket, timeout time.Duration, msg ...interface{}) (reply []string, err error) {
poller := zmq.NewPoller()
poller.Add(soc, zmq.POLLIN)
if _, err := soc.SendMessage(msg...); err != nil {
return nil, err
}
if sockets, err := poller.Poll(timeout); err != nil {
return nil, err
} else if len(sockets) == 1 {
return soc.RecvMessage(zmq.DONTWAIT)
} else {
return nil, fmt.Errorf("time out!")
}
return
}
示例9: TestMultipleContexts
func TestMultipleContexts(t *testing.T) {
chQuit := make(chan interface{})
chErr := make(chan error, 2)
needQuit := false
var sock1, sock2, serv1, serv2 *zmq.Socket
var serv_ctx1, serv_ctx2, ctx1, ctx2 *zmq.Context
var err error
defer func() {
if needQuit {
chQuit <- true
chQuit <- true
<-chErr
<-chErr
}
for _, s := range []*zmq.Socket{sock1, sock2, serv1, serv2} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
for _, c := range []*zmq.Context{serv_ctx1, serv_ctx2, ctx1, ctx2} {
if c != nil {
c.Term()
}
}
}()
addr1 := "tcp://127.0.0.1:9997"
addr2 := "tcp://127.0.0.1:9998"
serv_ctx1, err = zmq.NewContext()
if err != nil {
t.Fatal("NewContext:", err)
}
serv1, err = serv_ctx1.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = serv1.Bind(addr1)
if err != nil {
t.Fatal("Bind:", err)
}
serv_ctx2, err = zmq.NewContext()
if err != nil {
t.Fatal("NewContext:", err)
}
serv2, err = serv_ctx2.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = serv2.Bind(addr2)
if err != nil {
t.Fatal("Bind:", err)
}
new_service := func(sock *zmq.Socket, addr string) {
socket_handler := func(state zmq.State) error {
msg, err := sock.RecvMessage(0)
if err != nil {
return err
}
_, err = sock.SendMessage(addr, msg)
return err
}
quit_handler := func(interface{}) error {
return errors.New("quit")
}
reactor := zmq.NewReactor()
reactor.AddSocket(sock, zmq.POLLIN, socket_handler)
reactor.AddChannel(chQuit, 1, quit_handler)
err = reactor.Run(100 * time.Millisecond)
chErr <- err
}
go new_service(serv1, addr1)
go new_service(serv2, addr2)
needQuit = true
time.Sleep(time.Second)
// default context
sock1, err = zmq.NewSocket(zmq.REQ)
if err != nil {
t.Fatal("NewSocket:", err)
}
sock2, err = zmq.NewSocket(zmq.REQ)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sock1.Connect(addr1)
if err != nil {
t.Fatal("sock1.Connect:", err)
}
err = sock2.Connect(addr2)
if err != nil {
//.........这里部分代码省略.........
示例10: TestSecurityPlain
func TestSecurityPlain(t *testing.T) {
time.Sleep(100 * time.Millisecond)
var handler, server, client *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{handler} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
handler, err := zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = handler.Bind("inproc://zeromq.zap.01")
if err != nil {
t.Fatal("handler.Bind:", err)
}
doHandler := func(state zmq.State) error {
msg, err := handler.RecvMessage(0)
if err != nil {
return err // Terminating
}
version := msg[0]
sequence := msg[1]
// domain := msg[2]
// address := msg[3]
identity := msg[4]
mechanism := msg[5]
username := msg[6]
password := msg[7]
if version != "1.0" {
return errors.New("version != 1.0")
}
if mechanism != "PLAIN" {
return errors.New("mechanism != PLAIN")
}
if identity != "IDENT" {
return errors.New("identity != IDENT")
}
if username == "admin" && password == "password" {
handler.SendMessage(version, sequence, "200", "OK", "anonymous", "")
} else {
handler.SendMessage(version, sequence, "400", "Invalid username or password", "", "")
}
return nil
}
doQuit := func(i interface{}) error {
err := handler.Close()
if err != nil {
t.Error("handler.Close:", err)
}
return errors.New("Quit")
}
quit := make(chan interface{})
reactor := zmq.NewReactor()
reactor.AddSocket(handler, zmq.POLLIN, doHandler)
reactor.AddChannel(quit, 0, doQuit)
go func() {
reactor.Run(100 * time.Millisecond)
quit <- true
}()
defer func() {
quit <- true
<-quit
close(quit)
}()
// Server socket will accept connections
server, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket", err)
}
err = server.SetIdentity("IDENT")
if err != nil {
t.Fatal("server.SetIdentity:", err)
}
err = server.SetPlainServer(1)
if err != nil {
t.Fatal("server.SetPlainServer(1):", err)
}
err = server.Bind("tcp://127.0.0.1:9998")
if err != nil {
t.Fatal("server.Bind")
}
// Check PLAIN security with correct username/password
client, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
//.........这里部分代码省略.........
示例11: TestSecurityNull
func TestSecurityNull(t *testing.T) {
time.Sleep(100 * time.Millisecond)
var handler, server, client *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{handler} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
handler, err := zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = handler.Bind("inproc://zeromq.zap.01")
if err != nil {
t.Fatal("handler.Bind:", err)
}
doHandler := func(state zmq.State) error {
msg, err := handler.RecvMessage(0)
if err != nil {
return err // Terminating
}
version := msg[0]
sequence := msg[1]
domain := msg[2]
// address := msg[3]
// identity := msg[4]
mechanism := msg[5]
if version != "1.0" {
return errors.New("version != 1.0")
}
if mechanism != "NULL" {
return errors.New("mechanism != NULL")
}
if domain == "TEST" {
handler.SendMessage(version, sequence, "200", "OK", "anonymous", "")
} else {
handler.SendMessage(version, sequence, "400", "BAD DOMAIN", "", "")
}
return nil
}
doQuit := func(i interface{}) error {
err := handler.Close()
handler = nil
if err != nil {
t.Error("handler.Close:", err)
}
return errors.New("Quit")
}
quit := make(chan interface{})
reactor := zmq.NewReactor()
reactor.AddSocket(handler, zmq.POLLIN, doHandler)
reactor.AddChannel(quit, 0, doQuit)
go func() {
reactor.Run(100 * time.Millisecond)
quit <- true
}()
defer func() {
quit <- true
<-quit
close(quit)
}()
// We bounce between a binding server and a connecting client
server, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
client, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
// We first test client/server with no ZAP domain
// Libzmq does not call our ZAP handler, the connect must succeed
err = server.Bind("tcp://127.0.0.1:9683")
if err != nil {
t.Fatal("server.Bind:", err)
}
err = client.Connect("tcp://127.0.0.1:9683")
if err != nil {
t.Fatal("client.Connect:", err)
}
msg, err := bounce(server, client)
if err != nil {
t.Error(msg, err)
}
server.Unbind("tcp://127.0.0.1:9683")
client.Disconnect("tcp://127.0.0.1:9683")
//.........这里部分代码省略.........
示例12: TestSecurityCurve
func TestSecurityCurve(t *testing.T) {
time.Sleep(100 * time.Millisecond)
var handler, server, client *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{handler} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
if _, minor, _ := zmq.Version(); minor >= 1 && !zmq.HasCurve() {
t.Skip("Curve not available")
}
// Generate new keypairs for this test
client_public, client_secret, err := zmq.NewCurveKeypair()
if err != nil {
t.Fatal("NewCurveKeypair:", err)
}
server_public, server_secret, err := zmq.NewCurveKeypair()
if err != nil {
t.Fatal("NewCurveKeypair:", err)
}
handler, err = zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = handler.Bind("inproc://zeromq.zap.01")
if err != nil {
t.Fatal("handler.Bind:", err)
}
doHandler := func(state zmq.State) error {
msg, err := handler.RecvMessage(0)
if err != nil {
return err // Terminating
}
version := msg[0]
sequence := msg[1]
// domain := msg[2]
// address := msg[3]
identity := msg[4]
mechanism := msg[5]
client_key := msg[6]
client_key_text := zmq.Z85encode(client_key)
if version != "1.0" {
return errors.New("version != 1.0")
}
if mechanism != "CURVE" {
return errors.New("mechanism != CURVE")
}
if identity != "IDENT" {
return errors.New("identity != IDENT")
}
if client_key_text == client_public {
handler.SendMessage(version, sequence, "200", "OK", "anonymous", "")
} else {
handler.SendMessage(version, sequence, "400", "Invalid client public key", "", "")
}
return nil
}
doQuit := func(i interface{}) error {
err := handler.Close()
handler = nil
if err != nil {
t.Error("handler.Close:", err)
}
return errors.New("Quit")
}
quit := make(chan interface{})
reactor := zmq.NewReactor()
reactor.AddSocket(handler, zmq.POLLIN, doHandler)
reactor.AddChannel(quit, 0, doQuit)
go func() {
reactor.Run(100 * time.Millisecond)
quit <- true
}()
defer func() {
quit <- true
<-quit
close(quit)
}()
// Server socket will accept connections
server, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = server.SetCurveServer(1)
if err != nil {
t.Fatal("server.SetCurveServer(1):", err)
//.........这里部分代码省略.........
示例13: TestRouterElement
func TestRouterElement(t *testing.T) {
var c *zmq.Context
var m *Mirror
var sa, sb *zmq.Socket
var err error
var addra string
var addrb string
defer func() {
if sa != nil {
sa.Close()
}
if sb != nil {
sb.Close()
}
if m != nil {
m.Close()
}
if c != nil {
c.Term()
}
}()
if c, err = zmq.NewContext(); err != nil {
t.Fatalf("Failed to create ZMQ context: %v", err)
}
if m, err = NewMirror(t, c, 1); err != nil {
t.Fatalf("Failed to create a new mirror: %v", err)
}
if sa, addra, err = svcrouter.NewHalfPair(c, true); err != nil {
t.Fatalf("Failed to create half pair A: %v", err)
}
if sb, addrb, err = svcrouter.NewHalfPair(c, true); err != nil {
t.Fatalf("Failed to create half pair B: %v", err)
}
if err = m.AddPeer(svcrouter.PeerDefinition{
Name: "ab",
ZmqType: zmq.PAIR,
Address: addra,
Bind: false,
PeerImpl: &MirrorPeerImpl{},
}); err != nil {
t.Fatalf("Could not add peer A: %v", err)
}
if err = m.AddPeer(svcrouter.PeerDefinition{
Name: "ba",
ZmqType: zmq.PAIR,
Address: addrb,
Bind: false,
PeerImpl: &MirrorPeerImpl{},
}); err != nil {
t.Fatalf("Could not add peer B: %v", err)
}
svcrouter.Barrier()
num := 100
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < num; i++ {
if _, err := sb.RecvMessage(0); err != nil {
t.Fatalf("sb receive error: %v", err)
}
}
}()
msg := [][]byte{[]byte("Hello"), []byte("World")}
for i := 0; i < num; i++ {
if _, err := sa.SendMessage(msg); err != nil {
t.Fatalf("sa send error: %v", err)
}
}
wg.Wait()
}
示例14: NewWriterSocketHandler
// NewWriterSocketHandler returns a function suitable for use as a handler
// by zmq.Reactor
func NewWriterSocketHandler(writerSocket *zmq4.Socket,
messageChan chan<- types.Message) func(zmq4.State) error {
// these messages get only and ack, not a reply
var ackOnlyMessages = map[string]ackOnlyMessageHandler{
"ping": handlePing,
"resilient-server-handshake": handleHandshake,
"resilient-server-signoff": handleSignoff}
return func(_ zmq4.State) error {
var err error
var ok bool
var rawMessage []string
var message types.Message
if rawMessage, err = writerSocket.RecvMessage(0); err != nil {
return fmt.Errorf("RecvMessage %s", err)
}
message.Marshalled = rawMessage[0]
message.Data = []byte(strings.Join(rawMessage[1:], ""))
if message.Type, err = msg.GetMessageType(message.Marshalled); err != nil {
return fmt.Errorf("GetMessageType %s", err)
}
if message.ID, err = msg.GetMessageID(message.Marshalled); err != nil {
return fmt.Errorf("GetMessageID %s", err)
}
reply := map[string]interface{}{
"message-type": "resilient-server-ack",
"message-id": message.ID,
"incoming-type": message.Type,
"accepted": true}
marshalledReply, err := json.Marshal(reply)
if err != nil {
return fmt.Errorf("Marshal %s", err)
}
_, err = writerSocket.SendMessage([]string{string(marshalledReply)})
if err != nil {
return fmt.Errorf("SendMessage %s", err)
}
/* ---------------------------------------------------------------
** 2014-05-21 dougfort:
** The python version of ResiliantServer maintains a dict of
** "client-address" for sending replies, but IMO we don't need
** that here because every message contains "client-address"
** so we can decouple the reply.
** -------------------------------------------------------------*/
handler, ok := ackOnlyMessages[message.Type]
if ok {
handler(message)
return nil
}
fog.Debug("writer-socket-handler received %s %s",
message.Type, message.ID)
messageChan <- message
return nil
}
}