func Dial(sock mangos.Socket, url string) error { if err := sock.Dial(url); err != nil { s := fmt.Sprintf("socket.Dial: %s", err) return errors.New(s) } return nil }
func recvName(sock mangos.Socket, name string) { var msg []byte var err error if msg, err = sock.Recv(); err == nil { fmt.Printf("%s: RECEIVED: \"%s\"\n", name, string(msg)) } }
func Send(sock mangos.Socket, msg []byte) error { if err := sock.Send(msg); err != nil { s := fmt.Sprintf("socket.Send: %s", err) return errors.New(s) } return nil }
func Listen(sock mangos.Socket, url string) error { if err := sock.Listen(url); err != nil { s := fmt.Sprintf("socket.Listen: %s", err) return errors.New(s) } return nil }
func send(socket mangos.Socket, message string) { log.Printf("Node %s sends %s\n", node, message) err := socket.Send([]byte(message)) if err != nil { log.Fatalf("Node %s failed to send '%s': %s\n", node, message, err.Error()) } }
func replyLoop(sock mangos.Socket) { if sendData == nil { fatalf("No data to send!") } for { msg, err := sock.RecvMsg() switch err { case mangos.ErrRecvTimeout: return case nil: default: fatalf("RecvMsg failed: %v", err) } printMsg(msg) msg.Free() msg = mangos.NewMessage(len(sendData)) msg.Body = append(msg.Body, sendData...) err = sock.SendMsg(msg) if err != nil { fatalf("SendMsg failed: %v", err) } } }
func (t *inprocTran) NewListener(addr string, sock mangos.Socket) (mangos.PipeListener, error) { if _, err := mangos.StripScheme(t, addr); err != nil { return nil, err } l := &listener{addr: addr, proto: sock.GetProtocol()} return l, nil }
func publishEvt(channel mangos.Socket, evt string) error { if err := channel.Send([]byte(evt)); err != nil { log.Debugf("fail to publish '%v' event:%v\n", evt, err) return err } return nil }
func Receive(sock mangos.Socket) (msg []byte, err error) { msg, err = sock.Recv() if err != nil { s := fmt.Sprintf("socket.Recv: %s", err) return nil, errors.New(s) } return msg, nil }
func sendRecv(sock mangos.Socket, name string) { for { sock.SetOption(mangos.OptionRecvDeadline, 100*time.Millisecond) recvName(sock, name) time.Sleep(time.Second) sendName(sock, name) } }
func getEvt(channel mangos.Socket) (string, error) { log.Debug("getEvt()") evt, err := channel.Recv() if err != nil { return "", err } return string(evt), nil }
func receive(socket mangos.Socket) string { bytes, err := socket.Recv() if err != nil { log.Fatalf("Node %s failed receiving a message: %s\n", node, err.Error()) } message := string(bytes) log.Printf("Node %s received %s\n", node, message) return message }
func client(url string, name string) { var sock mangos.Socket var err error var msg []byte if sock, err = respondent.NewSocket(); err != nil { die("can't get new respondent socket: %s", err.Error()) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Dial(url); err != nil { die("can't dial on respondent socket: %s", err.Error()) } for { if msg, err = sock.Recv(); err != nil { die("Cannot recv: %s", err.Error()) } fmt.Printf("CLIENT(%s): RECEIVED \"%s\" SURVEY REQUEST\n", name, string(msg)) d := date() fmt.Printf("CLIENT(%s): SENDING DATE SURVEY RESPONSE\n", name) if err = sock.Send([]byte(d)); err != nil { die("Cannot send: %s", err.Error()) } } }
func node0(url string) { var sock mangos.Socket var err error var msg []byte if sock, err = rep.NewSocket(); err != nil { die("can't get new rep socket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on rep socket: %s", err.Error()) } for { // Could also use sock.RecvMsg to get header msg, err = sock.Recv() if string(msg) == "DATE" { // no need to terminate fmt.Println("NODE0: RECEIVED DATE REQUEST") d := date() fmt.Printf("NODE0: SENDING DATE %s\n", d) err = sock.Send([]byte(d)) if err != nil { die("can't send reply: %s", err.Error()) } } } }
func client(url string, name string) { var sock mangos.Socket var err error var msg []byte if sock, err = sub.NewSocket(); err != nil { die("can't get new sub socket: %s", err.Error()) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Dial(url); err != nil { die("can't dial on sub socket: %s", err.Error()) } // Empty byte array effectively subscribes to everything err = sock.SetOption(mangos.OptionSubscribe, []byte("")) if err != nil { die("cannot subscribe: %s", err.Error()) } for { if msg, err = sock.Recv(); err != nil { die("Cannot recv: %s", err.Error()) } fmt.Printf("CLIENT(%s): RECEIVED %s\n", name, string(msg)) } }
func (t wsTran) NewListener(addr string, sock mangos.Socket) (mangos.PipeListener, error) { proto := sock.GetProtocol() l, e := t.listener(addr, proto) if e == nil { if v, e := sock.GetOption(mangos.OptionMaxRecvSize); e == nil { l.maxrx = v.(int) } l.mux.Handle(l.url.Path, l) } return l, e }
func node0(url string) { var sock mangos.Socket var err error if sock, err = pair.NewSocket(); err != nil { die("can't get new pair socket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on pair socket: %s", err.Error()) } sendRecv(sock, "node0") }
func (wsTran) NewDialer(addr string, sock mangos.Socket) (mangos.PipeDialer, error) { iswss := strings.HasPrefix(addr, "wss://") opts := make(map[string]interface{}) opts[mangos.OptionNoDelay] = true opts[mangos.OptionKeepAlive] = true proto := sock.GetProtocol() maxrx := 0 if v, e := sock.GetOption(mangos.OptionMaxRecvSize); e == nil { maxrx = v.(int) } return &dialer{addr: addr, proto: proto, iswss: iswss, opts: opts, maxrx: maxrx}, nil }
func recvLoop(sock mangos.Socket) { for { msg, err := sock.RecvMsg() switch err { case mangos.ErrProtoState: return case mangos.ErrRecvTimeout: return case nil: default: fatalf("RecvMsg failed: %v", err) } printMsg(msg) msg.Free() } }
func simpleSend(t *testing.T, tx mangos.Socket, wg *sync.WaitGroup) { var buf [256]byte i := 0 for i = 0; i < 10000; i++ { l := rand.Intn(255) + 1 buf[0] = uint8(l) if e := tx.Send(buf[:l]); e != nil { t.Fatalf("Send: %v", e) break } } t.Logf("Sent %d Msgs", i) if e := tx.Close(); e != nil { t.Fatalf("Tx Close: %v", e) } wg.Done() }
func server(url string, nworkers int) { var sock mangos.Socket var err error var wg sync.WaitGroup rand.Seed(time.Now().UnixNano()) if sock, err = rep.NewSocket(); err != nil { die("can't get new rep socket: %s", err) } if err = sock.SetOption(mangos.OptionRaw, true); err != nil { die("can't set raw mode: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on rep socket: %s", err.Error()) } wg.Add(nworkers) fmt.Printf("Starting %d workers\n", nworkers) for id := 0; id < nworkers; id++ { go func(id int) { defer wg.Done() serverWorker(sock, id) }(id) } wg.Wait() }
func runCmdServer(channel mangos.Socket, done <-chan struct{}) (<-chan messages.Message, error) { cmdChan := make(chan messages.Message) go func() { for { // Could also use sock.RecvMsg to get header log.Debug("sensor: cmd server - waiting for a command...") select { case <-done: log.Debug("sensor: cmd server - done...") return default: if rawCmd, err := channel.Recv(); err != nil { switch err { case mangos.ErrRecvTimeout: log.Debug("sensor: cmd server - timeout... ok") default: log.Debugln("sensor: cmd server - error =>", err) } } else { log.Debug("sensor: cmd server - got a command => ", string(rawCmd)) if cmd, err := messages.Decode(rawCmd); err != nil { log.Println(err) } else { cmdChan <- cmd } //for now just ack the command and process the command asynchronously //NOTE: //must reply before receiving the next message //otherwise nanomsg/mangos will be confused :-) monitorFinishReply := "ok" err = channel.Send([]byte(monitorFinishReply)) if err != nil { log.Warnln("sensor: cmd server - fail to send monitor.finish reply =>", err) } } } } }() return cmdChan, nil }
func sendCmd(channel mangos.Socket, cmd messages.Message) (string, error) { sendTimeouts := 0 recvTimeouts := 0 log.Debugf("sendCmd(%s)\n", cmd) for { sendData, err := messages.Encode(cmd) if err != nil { log.Info("sendCmd(): malformed cmd - ", err) return "", err } if err := channel.Send(sendData); err != nil { switch err { case mangos.ErrSendTimeout: log.Info("sendCmd(): send timeout...") sendTimeouts++ if sendTimeouts > 3 { return "", err } default: return "", err } } response, err := channel.Recv() if err != nil { switch err { case mangos.ErrRecvTimeout: log.Info("sendCmd(): receive timeout...") recvTimeouts++ if recvTimeouts > 3 { return "", err } default: return "", err } } return string(response), nil } }
func sendLoop(sock mangos.Socket) { if sendData == nil { fatalf("No data to send!") } for { msg := mangos.NewMessage(len(sendData)) msg.Body = append(msg.Body, sendData...) err := sock.SendMsg(msg) if err != nil { fatalf("SendMsg failed: %v", err) } if sendInterval >= 0 { time.Sleep(time.Duration(sendInterval) * time.Second) } else { break } } }
func node1(url string, msg string) { var sock mangos.Socket var err error if sock, err = push.NewSocket(); err != nil { die("can't get new push socket: %s", err.Error()) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Dial(url); err != nil { die("can't dial on push socket: %s", err.Error()) } fmt.Printf("NODE1: SENDING \"%s\"\n", msg) if err = sock.Send([]byte(msg)); err != nil { die("can't send message on push socket: %s", err.Error()) } sock.Close() }
func serverWorker(sock mangos.Socket, id int) { var err error delay := rand.Intn(int(time.Second)) for { var m *mangos.Message if m, err = sock.RecvMsg(); err != nil { return } m.Body = make([]byte, 4) time.Sleep(time.Duration(delay)) binary.BigEndian.PutUint32(m.Body[0:], uint32(id)) if err = sock.SendMsg(m); err != nil { return } } }
func sendRequest(s mangos.Socket, request request) (*response, error) { requestJSON, err := json.Marshal(request) if err != nil { // This is not recoverable. panic(err) } if err := s.Send(requestJSON); err != nil { return nil, err } rep, err := s.Recv() if err != nil { return nil, err } var resp response if err := json.Unmarshal(rep, &resp); err != nil { return nil, err } return &resp, nil }
func sendCmd(channel mangos.Socket, cmd string) (string, error) { sendTimeouts := 0 recvTimeouts := 0 log.Debugf("sendCmd(%s)\n", cmd) for { if err := channel.Send([]byte(cmd)); err != nil { switch err { case mangos.ErrSendTimeout: log.Info("sendCmd(): send timeout...") sendTimeouts++ if sendTimeouts > 3 { return "", err } default: return "", err } } response, err := channel.Recv() if err != nil { switch err { case mangos.ErrRecvTimeout: log.Info("sendCmd(): receive timeout...") recvTimeouts++ if recvTimeouts > 3 { return "", err } default: return "", err } } return string(response), nil } }
func simpleRecv(t *testing.T, rx mangos.Socket, wg *sync.WaitGroup) { i := 0 for i = 0; i < 10000; i++ { buf, e := rx.Recv() if e != nil { t.Fatalf("Recv: %v", e) break } if len(buf) < 1 { t.Fatalf("Recv: empty buf") break } if len(buf) != int(buf[0]) { t.Fatalf("Recv: length %d != expected %d", len(buf), buf[0]) break } } t.Logf("Recvd %d Msgs", i) if e := rx.Close(); e != nil { t.Fatalf("Rx Close: %v", e) } wg.Done() }
func node0(url string) { var sock mangos.Socket var err error var msg []byte if sock, err = pull.NewSocket(); err != nil { die("can't get new pull socket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on pull socket: %s", err.Error()) } for { // Could also use sock.RecvMsg to get header msg, err = sock.Recv() fmt.Printf("NODE0: RECEIVED \"%s\"\n", msg) } }