Beispiel #1
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()
}
Beispiel #2
0
func testDevLoop(t *testing.T, addr string) {
	s1, err := pair.NewSocket()
	if err != nil {
		t.Errorf("Failed to open S1: %v", err)
		return
	}
	defer s1.Close()
	s1.AddTransport(tcp.NewTransport())
	s1.AddTransport(inproc.NewTransport())
	s1.AddTransport(ipc.NewTransport())
	s1.AddTransport(tlstcp.NewTransport())
	s1.AddTransport(ws.NewTransport())
	s1.AddTransport(wss.NewTransport())

	options := make(map[string]interface{})
	if strings.HasPrefix(addr, "wss://") || strings.HasPrefix(addr, "tls+tcp://") {
		options[mangos.OptionTLSConfig] = srvCfg
	}

	if err := s1.ListenOptions(addr, options); err != nil {
		t.Errorf("Failed listening to %s: %v", addr, err)
		return
	}

	if err := mangos.Device(s1, s1); err != nil {
		t.Errorf("Device failed: %v", err)
		return
	}

	RunTests(t, addr, deviceCaseClient())
}
Beispiel #3
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))
	}
}
Beispiel #4
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())
			}
		}
	}
}
Beispiel #5
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())
		}
	}
}
Beispiel #6
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))
		}
	}
}
Beispiel #7
0
// AddTransports adds all known transports to the given socket.
func AddTransports(sock mangos.Socket) {
	sock.AddTransport(tcp.NewTransport())
	sock.AddTransport(inproc.NewTransport())
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tlstcp.NewTransport())
	sock.AddTransport(ws.NewTransport())
	sock.AddTransport(wss.NewTransport())
}
Beispiel #8
0
func testDevChain(t *testing.T, addr1 string, addr2 string, addr3 string) {
	// This tests using multiple devices across a few transports.
	// It looks like this:  addr1->addr2->addr3 <==> addr3->addr2->addr1
	var err error
	s := make([]mangos.Socket, 5)
	for i := 0; i < 5; i++ {
		if s[i], err = pair.NewSocket(); err != nil {
			t.Errorf("Failed to open S1_1: %v", err)
			return
		}
		defer s[i].Close()
		s[i].AddTransport(tcp.NewTransport())
		s[i].AddTransport(inproc.NewTransport())
		s[i].AddTransport(tlstcp.NewTransport())
		s[i].AddTransport(ipc.NewTransport())
		s[i].AddTransport(ws.NewTransport())
	}

	if err = s[0].Listen(addr1); err != nil {
		t.Errorf("s[0] Listen: %v", err)
		return
	}
	if err = s[1].Dial(addr2); err != nil {
		t.Errorf("s[1] Dial: %v", err)
		return
	}
	if err = s[2].Listen(addr2); err != nil {
		t.Errorf("s[2] Listen: %v", err)
		return
	}
	if err = s[3].Dial(addr3); err != nil {
		t.Errorf("s[3] Dial: %v", err)
		return
	}
	if err = s[4].Listen(addr3); err != nil {
		t.Errorf("s[4] Listen: %v", err)
		return
	}
	if err = mangos.Device(s[0], s[1]); err != nil {
		t.Errorf("s[0],s[1] Device: %v", err)
		return
	}
	if err = mangos.Device(s[2], s[3]); err != nil {
		t.Errorf("s[2],s[3] Device: %v", err)
		return
	}
	if err = mangos.Device(s[4], nil); err != nil {
		t.Errorf("s[4] Device: %v", err)
		return
	}
	RunTests(t, addr1, deviceCaseClient())
}
Beispiel #9
0
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")
}
Beispiel #10
0
func starTestNewClient(t *testing.T, addr string, id int) *starTester {
	var err error
	bt := &starTester{id: id, rdoneq: make(chan bool), sdoneq: make(chan bool)}

	if bt.sock, err = star.NewSocket(); err != nil {
		t.Errorf("Failed getting client %d socket: %v", id, err)
		return nil
	}
	bt.sock.AddTransport(tcp.NewTransport())
	if err = bt.sock.Dial(addr); err != nil {
		t.Errorf("Failed client %d dialing: %v", id, err)
		bt.sock.Close()
		return nil
	}
	return bt
}
Beispiel #11
0
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)
	}
}
Beispiel #12
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()
}
Beispiel #13
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)
	}
}
Beispiel #14
0
func clientWorker(url string, id int) {
	var sock mangos.Socket
	var m *mangos.Message
	var err error

	if sock, err = req.NewSocket(); err != nil {
		die("can't get new req socket: %s", err.Error())
	}

	// Leave this in Cooked mode!

	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())
	}

	// send an empty messsage
	m = mangos.NewMessage(1)
	if err = sock.SendMsg(m); err != nil {
		die("can't send request: %s", err.Error())
	}

	if m, err = sock.RecvMsg(); err != nil {
		die("can't recv reply: %s", err.Error())
	}
	sock.Close()

	if len(m.Body) != 4 {
		die("bad response len: %d", len(m.Body))
	}

	worker := binary.BigEndian.Uint32(m.Body[0:])

	L.Lock()
	fmt.Printf("Client: %4d   Server: %4d\n", id, worker)
	L.Unlock()
}
Beispiel #15
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()
}
Beispiel #16
0
func TestMaxRxTCP(t *testing.T) {
	testMaxRx(t, AddrTestTCP, tcp.NewTransport())
}
Beispiel #17
0
func TestPortHook(t *testing.T) {
	t.Logf("Testing Add Hook")

	srvtest := &hooktest{allow: true, t: t}
	clitest := &hooktest{allow: true, t: t}

	addr := AddrTestTCP

	srvtest.expect = []hookinfo{{
		action: mangos.PortActionAdd,
		addr:   addr,
		server: true,
		isopen: true,
	}, {
		action: mangos.PortActionRemove,
		addr:   addr,
		server: true,
		isopen: false,
	}}

	clitest.expect = []hookinfo{{
		action: mangos.PortActionAdd,
		addr:   addr,
		server: false,
		isopen: true,
	}, {
		action: mangos.PortActionRemove,
		addr:   addr,
		server: false,
		isopen: false,
	}}

	sockreq, err := req.NewSocket()
	if err != nil {
		t.Errorf("NewSocket failed: %v", err)
		return
	}
	defer sockreq.Close()
	sockreq.AddTransport(tcp.NewTransport())
	if sockreq.SetPortHook(clitest.Hook) != nil {
		t.Errorf("SetPortHook result not nil!")
		return
	}
	d, err := sockreq.NewDialer(addr, nil)
	if err != nil {
		t.Errorf("NewDialer failed: %v", err)
		return
	}

	sockrep, err := rep.NewSocket()
	if err != nil {
		t.Errorf("NewSocket failed: %v", err)
		return
	}
	defer sockrep.Close()
	sockrep.AddTransport(tcp.NewTransport())
	if sockrep.SetPortHook(srvtest.Hook) != nil {
		t.Errorf("SetPortHook result not nil!")
		return
	}
	l, err := sockrep.NewListener(addr, nil)
	if err != nil {
		t.Errorf("NewListener failed: %v", err)
		return
	}

	if err := l.Listen(); err != nil {
		t.Errorf("Listen failed: %v", err)
		return
	}

	if err := d.Dial(); err != nil {
		t.Errorf("Dial failed: %v", err)
		return
	}

	// wait a second for connection to establish
	// could also issue a req/rep...
	t.Logf("Waiting a bit...")
	time.Sleep(100 * time.Millisecond)

	d.Close()
	l.Close()

	// shut down the server
	sockrep.Close()
	sockreq.Close()

	time.Sleep(100 * time.Millisecond)

	clitest.Lock()
	defer clitest.Unlock()

	srvtest.Lock()
	defer srvtest.Unlock()

	for i, info := range clitest.expect {
		t.Logf("Exp C[%d]: %s", i, info.String())
	}
	for i, info := range clitest.calls {
		t.Logf("Got C[%d]: %s", i, info.String())
	}
	for i, info := range srvtest.expect {
		t.Logf("Exp S[%d]: %s", i, info.String())
	}
	for i, info := range srvtest.calls {
		t.Logf("Got S[%d]: %s", i, info.String())
	}

	if len(srvtest.calls) != len(srvtest.expect) {
		t.Errorf("Server got wrong # calls, %d != %d",
			len(srvtest.calls), len(srvtest.expect))
		return
	}
	for i := range srvtest.calls {
		if srvtest.calls[i].String() != srvtest.expect[i].String() {
			t.Errorf("Server hook %d wrong: %s != %s", i,
				srvtest.calls[i].String(),
				srvtest.expect[i].String())
		}
	}

	if len(clitest.calls) != len(clitest.expect) {
		t.Errorf("Client got wrong # calls, %d != %d",
			len(clitest.calls), len(clitest.expect))
		return
	}
	for i := range clitest.calls {
		if clitest.calls[i].String() != clitest.expect[i].String() {
			t.Errorf("Server hook %d wrong: %s != %s", i,
				clitest.calls[i].String(),
				clitest.expect[i].String())
		}
	}
}