Example #1
0
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())
		}
	}
}
Example #2
0
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())
	}
}
Example #3
0
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
}
Example #4
0
func server(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = surveyor.NewSocket(); err != nil {
		die("can't get new surveyor socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on surveyor socket: %s", err.Error())
	}
	err = sock.SetOption(mangos.OptionSurveyTime, time.Second/2)
	if err != nil {
		die("SetOption(): %s", err.Error())
	}
	for {
		time.Sleep(time.Second)
		fmt.Println("SERVER: SENDING DATE SURVEY REQUEST")
		if err = sock.Send([]byte("DATE")); err != nil {
			die("Failed sending survey: %s", err.Error())
		}
		for {
			if msg, err = sock.Recv(); err != nil {
				break
			}
			fmt.Printf("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n",
				string(msg))
		}
		fmt.Println("SERVER: SURVEY OVER")
	}
}
Example #5
0
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())
			}
		}
	}
}
Example #6
0
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
}
Example #7
0
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()
}
Example #8
0
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()
}
Example #9
0
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
}
Example #10
0
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
	}
}
Example #11
0
func server(url string) {
	var sock mangos.Socket
	var err error
	if sock, err = pub.NewSocket(); err != nil {
		die("can't get new pub socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on pub socket: %s", err.Error())
	}
	for {
		// Could also use sock.RecvMsg to get header
		d := date()
		fmt.Printf("SERVER: PUBLISHING DATE %s\n", d)
		if err = sock.Send([]byte(d)); err != nil {
			die("Failed publishing: %s", err.Error())
		}
		time.Sleep(time.Second)
	}
}
Example #12
0
func node(args []string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	var x int

	if sock, err = bus.NewSocket(); err != nil {
		die("bus.NewSocket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(args[2]); err != nil {
		die("sock.Listen: %s", err.Error())
	}

	// wait for everyone to start listening
	time.Sleep(time.Second)
	for x = 3; x < len(args); x++ {
		if err = sock.Dial(args[x]); err != nil {
			die("socket.Dial: %s", err.Error())
		}
	}

	// wait for everyone to join
	time.Sleep(time.Second)

	fmt.Printf("%s: SENDING '%s' ONTO BUS\n", args[1], args[1])
	if err = sock.Send([]byte(args[1])); err != nil {
		die("sock.Send: %s", err.Error())
	}
	for {
		if msg, err = sock.Recv(); err != nil {
			die("sock.Recv: %s", err.Error())
		}
		fmt.Printf("%s: RECEIVED \"%s\" FROM BUS\n", args[1],
			string(msg))

	}
}
Example #13
0
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
}
Example #14
0
func node1(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte

	if sock, err = req.NewSocket(); err != nil {
		die("can't get new req socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on req socket: %s", err.Error())
	}
	fmt.Printf("NODE1: SENDING DATE REQUEST %s\n", "DATE")
	if err = sock.Send([]byte("DATE")); err != nil {
		die("can't send message on push socket: %s", err.Error())
	}
	if msg, err = sock.Recv(); err != nil {
		die("can't receive date: %s", err.Error())
	}
	fmt.Printf("NODE1: RECEIVED DATE %s\n", string(msg))
	sock.Close()
}
Example #15
0
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
	}
}
Example #16
0
func sendName(sock mangos.Socket, name string) {
	fmt.Printf("%s: SENDING \"%s\"\n", name, name)
	if err := sock.Send([]byte(name)); err != nil {
		die("failed sending: %s", err)
	}
}