Example #1
0
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))
	}
}
Example #2
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 {
		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))
		}
	}
}
Example #3
0
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()
}
Example #4
0
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)
	}
}
Example #5
0
func sendRecvLoop(sock mangos.Socket) {
	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 {
			recvLoop(sock)
			return
		}

		now := time.Now()

		// maximum wait time is upper bound of recvTimeout and
		// sendInterval

		if recvTimeout < 0 || recvTimeout > sendInterval {
			sock.SetOption(mangos.OptionRecvDeadline,
				time.Second*time.Duration(sendInterval))
		}
		msg, err = sock.RecvMsg()
		switch err {
		case mangos.ErrRecvTimeout:
		case nil:
			printMsg(msg)
			msg.Free()
		default:
			fatalf("RecvMsg failed: %v", err)
		}
		time.Sleep((time.Second * time.Duration(sendInterval)) -
			time.Now().Sub(now))
	}
}