Example #1
0
File: bus.go Project: go-lsst/ncs
func newBusNode(addr string) (busNode, error) {
	var err error
	var node busNode

	sock, err := bus.NewSocket()
	if err != nil {
		return node, err
	}

	node.sock = sock
	node.sock.AddTransport(ipc.NewTransport())
	node.sock.AddTransport(tcp.NewTransport())

	err = node.sock.Listen("tcp://127.0.0.1:0")
	if err != nil {
		return node, err
	}

	err = node.sock.Dial(addr)
	if err != nil {
		return node, err
	}

	return node, err
}
Example #2
0
// NewSocket allocates a new Socket.  The Socket is the handle used to
// access the underlying library.
func NewSocket(d Domain, p Protocol) (*Socket, error) {

	var s Socket
	var err error

	s.proto = p
	s.dom = d

	switch p {
	case PUB:
		s.sock, err = pub.NewSocket()
	case SUB:
		s.sock, err = sub.NewSocket()
	case PUSH:
		s.sock, err = push.NewSocket()
	case PULL:
		s.sock, err = pull.NewSocket()
	case REQ:
		s.sock, err = req.NewSocket()
	case REP:
		s.sock, err = rep.NewSocket()
	case SURVEYOR:
		s.sock, err = surveyor.NewSocket()
	case RESPONDENT:
		s.sock, err = respondent.NewSocket()
	case PAIR:
		s.sock, err = pair.NewSocket()
	case BUS:
		s.sock, err = bus.NewSocket()
	default:
		err = mangos.ErrBadProto
	}

	if err != nil {
		return nil, err
	}

	switch d {
	case AF_SP:
	case AF_SP_RAW:
		err = s.sock.SetOption(mangos.OptionRaw, true)
	default:
		err = errBadDomain
	}
	if err != nil {
		s.sock.Close()
		return nil, err
	}

	// Compat mode sockets should timeout on send if we don't have any pipes
	if err = s.sock.SetOption(mangos.OptionWriteQLen, 0); err != nil {
		s.sock.Close()
		return nil, err
	}

	s.rto = -1
	s.sto = -1
	all.AddTransports(s.sock)
	return &s, nil
}
Example #3
0
func (bt *busTest) Init(t *testing.T, addr string) bool {
	var err error
	bt.resp = make(map[uint32]uint32)
	bt.start = make(map[uint32]bool)
	bt.send = 0
	bt.nstart = 0
	if bt.Sock, err = bus.NewSocket(); err != nil {
		bt.Errorf("NewSocket(): %v", err)
		return false
	}
	return bt.T.Init(t, addr)
}
Example #4
0
func newSocket(url, protocol string) (sock mangos.Socket, err error) {
	var str string

	switch protocol {
	case "bus":
		sock, err = bus.NewSocket()
		str = "bus.NewSocket: "
	case "pair":
		sock, err = pair.NewSocket()
		str = "pair.NewSocket: "
	case "push":
		sock, err = push.NewSocket()
		str = "push.NewSocket: "
	case "pull":
		sock, err = pull.NewSocket()
		str = "pull.NewSocket: "
	case "pub":
		sock, err = pub.NewSocket()
		str = "pub.NewSocket: "
	case "sub":
		sock, err = sub.NewSocket()
		str = "sub.NewSocket: "
	case "request":
		sock, err = req.NewSocket()
		str = "request.NewSocket: "
	case "reply":
		sock, err = rep.NewSocket()
		str = "reply.NewSocket: "
	case "surveyor":
		sock, err = surveyor.NewSocket()
		str = "surveyor.NewSocket: "
	case "respondent":
		sock, err = respondent.NewSocket()
		str = "respondent.NewSocket: "
	}

	if err != nil {
		s := fmt.Sprintf(str+" %s", err)
		return nil, errors.New(s)
	}

	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())

	return sock, nil
}
Example #5
0
File: bus.go Project: go-lsst/ncs
func (b *sysbus) init() error {
	sck, err := bus.NewSocket()
	if err != nil {
		b.Errorf("error creating a nanomsg socket: %v\n", err)
		return err
	}

	b.sck = sck
	b.sck.AddTransport(ipc.NewTransport())
	b.sck.AddTransport(tcp.NewTransport())

	err = b.sck.Listen(BusAddr)
	if err != nil {
		return err
	}

	return err
}
Example #6
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 #7
0
func testBusNonBlock(t *testing.T, addr string, tran mangos.Transport) {
	maxqlen := 2
	timeout := time.Second / 2

	rp, err := bus.NewSocket()
	if err != nil {
		t.Errorf("Failed to make PUB: %v", err)
		return
	}
	defer rp.Close()
	rp.AddTransport(tran)

	// Now try setting the option
	err = rp.SetOption(mangos.OptionWriteQLen, maxqlen)
	if err != nil {
		t.Errorf("Failed set WriteQLen: %v", err)
		return
	}
	// At this point, we can issue requests on rq, and read them from rp.
	if err = rp.SetOption(mangos.OptionSendDeadline, timeout); err != nil {
		t.Errorf("Failed set recv deadline")
		return
	}
	if err = rp.Listen(addr); err != nil {
		t.Errorf("Failed listen: %v", err)
		return
	}

	msg := []byte{'A', 'B', 'C'}

	for i := 0; i < maxqlen*2; i++ {
		t.Logf("Sending #%d", i)
		if err := rp.Send(msg); err != nil {
			t.Errorf("Failed to send: %v", err)
		}
	}
}