Ejemplo n.º 1
0
func newEvtChannel(addr string) (mangos.Socket, error) {
	socket, err := sub.NewSocket()
	if err != nil {
		return nil, err
	}

	if err := socket.SetOption(mangos.OptionRecvDeadline, time.Second*120); err != nil {
		socket.Close()
		return nil, err
	}

	//socket.AddTransport(ipc.NewTransport())
	socket.AddTransport(tcp.NewTransport())
	if err := socket.Dial(addr); err != nil {
		socket.Close()
		return nil, err
	}

	err = socket.SetOption(mangos.OptionSubscribe, []byte(""))
	if err != nil {
		return nil, err
	}

	return socket, nil
}
Ejemplo n.º 2
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))
	}
}
Ejemplo n.º 3
0
func (st *subTest) Init(t *testing.T, addr string) bool {
	var err error
	if st.Sock, err = sub.NewSocket(); err != nil {
		st.Errorf("NewSocket(): %v", err)
		return false
	}
	rv := st.T.Init(t, addr)
	// We have to subscribe to the START message!
	if !rv {
		return false
	}
	err = st.Sock.SetOption(mangos.OptionSubscribe, []byte("START"))
	if err != nil {
		st.Errorf("Failed subscription to START: %v", err)
		return false
	}

	err = st.Sock.SetOption(mangos.OptionSubscribe, []byte("END"))
	if err != nil {
		st.Errorf("Failed to subscribe to END: %v", err)
		return false
	}

	err = st.Sock.SetOption(mangos.OptionSubscribe, []byte("/rain"))
	if err != nil {
		st.Errorf("Failed subscribing to the rain: %v", err)
		return false
	}

	return true
}
Ejemplo n.º 4
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
}
Ejemplo n.º 5
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
}