Exemplo n.º 1
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
}
Exemplo n.º 2
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 * 30); 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
}
Exemplo n.º 3
0
func NewSubscriber(ctx context.Context, url string) (*Subscriber, error) {
	var sock mangos.Socket
	var err error

	sock, err = sub.NewSocket()
	if err != nil {
		return nil, err
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())

	err = sock.Dial(url)
	if err != nil {
		return nil, err
	}
	// subscribes to everything
	err = sock.SetOption(mangos.OptionSubscribe, []byte(""))
	if err != nil {
		return nil, err
	}

	subscriber := &Subscriber{
		url:     url,
		ctx:     ctx,
		sock:    sock,
		changes: make(chan []string, 8),
	}

	go subscriber.run()
	return subscriber, nil
}
Exemplo n.º 4
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
}
Exemplo n.º 5
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))
	}
}
Exemplo n.º 6
0
func run(endpoint string, filter string, connect bool, ch chan *pubsub.Event) {
	sock, err := sub.NewSocket()
	if err != nil {
		log.Fatalln("sub.NewSocket error:", err)
	}
	sock.AddTransport(inproc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	defer sock.Close()
	if connect {
		err = sock.Dial(endpoint)
	} else {
		err = sock.Listen(endpoint)
	}
	if err != nil {
		log.Fatalln("sock connect failed:", err)
	}
	err = sock.SetOption(mangos.OptionSubscribe, []byte(filter))
	if err != nil {
		log.Fatalln("sock SetOption failed:", err)
	}

	for {
		msg, e := sock.Recv()
		if e != nil {
			log.Fatal("error:", e)
		}

		event := ParseMsg(string(msg))
		if event != nil {
			ch <- event
		}
	}
}
Exemplo n.º 7
0
func main() {
	flag.Parse()

	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("tcp://127.0.0.1:55555"); err != nil {
		die("can't listen on pub socket: %s", err.Error())
	}

	//sub
	var subscriber mangos.Socket

	if subscriber, err = sub.NewSocket(); err != nil {
		die("can't get new sub socket: %s", err.Error())
	}
	subscriber.AddTransport(ipc.NewTransport())
	subscriber.AddTransport(tcp.NewTransport())
	if err = subscriber.Dial("tcp://127.0.0.1:55555"); err != nil {
		die("can't dial on sub socket: %s", err.Error())
	}
	// Empty byte array effectively subscribes to everything
	err = subscriber.SetOption(mangos.OptionSubscribe, []byte(""))

	start := time.Now()

	//var msg []byte
	for i := 1; i <= *numbPtr; i++ {
		//conn.Publish("test", []byte(randSeq(320)))
		sock.Send([]byte(randSeq(320)))

		if _, err = subscriber.Recv(); err != nil {
			die("Cannot recv: %s", err.Error())
		}

		//log.Println(string(msg))
		/*
			runtime.Gosched()
			atomic.AddUint64(&ops, 1)
			if ops == uint64(*numbPtr) {
				elapsed := time.Since(start)
				log.Printf("Time took %s", elapsed)
			}
		*/
	}

	elapsed := time.Since(start)
	log.Printf("Time took %s", elapsed)

	defer sock.Close()

	fmt.Scanln()
}
Exemplo n.º 8
0
func (c *Context) Run(name string) {
	push, _ := push.NewSocket()
	push.AddTransport(tcp.NewTransport())
	push.Dial("tcp://127.0.0.1:5006")

	pull, _ := sub.NewSocket()
	pull.AddTransport(tcp.NewTransport())
	pull.Dial("tcp://127.0.0.1:5005")
	pull.SetOption(mangos.OptionSubscribe, []byte(""))

	for {
		var protocol [4][]byte
		bytes, _ := pull.Recv()
		msgpack.Unmarshal(bytes, &protocol)

		if string(protocol[1]) != "*" && string(protocol[1]) != name {
			continue
		}

		if string(protocol[2]) == "message" {
			var message [5][]byte
			msgpack.Unmarshal(protocol[3], &message)

			for _, f := range c.messages {
				result := f(Message{
					string(message[0]),
					string(message[1]),
					string(message[2]),
					string(message[3]),
					string(message[4]),
				})

				if result != "" {
					packed, _ := msgpack.Marshal([5]string{
						string(message[0]),
						string(message[2]),
						string(message[1]),
						result,
						string(message[4]),
					})

					output, _ := msgpack.Marshal([4]string{
						name,
						"protocol." + string(message[0]),
						"message",
						string(packed),
					})

					push.Send(output)
				}
			}
		}

		if string(protocol[2]) == "command" {
			var message [2]interface{}
			msgpack.Unmarshal(protocol[3], &message)

			var embed [5][]byte
			msgpack.Unmarshal([]byte(message[0].(string)), &embed)

			orig, _ := message[0].(string)
			line, _ := message[1].([]interface{})[0].(string)
			command := strings.SplitN(line, " ", 2)
			f := c.commands[strings.TrimPrefix(command[0], ".")]

			result := f(Command{
				strings.TrimPrefix(command[0], "."),
				command[1],
				Message{
					string(embed[0]),
					string(embed[1]),
					string(embed[2]),
					string(embed[3]),
					string(embed[4]),
				},
			})

			if result != "" {
				new_messages := message[1].([]interface{})
				new_messages = append([]interface{}{result}, new_messages[1:]...)

				packed, _ := msgpack.Marshal([2]interface{}{
					orig,
					new_messages,
				})

				output, _ := msgpack.Marshal([4]string{
					name,
					string(protocol[0]),
					"response",
					string(packed),
				})

				push.Send(output)
			}
		}
	}
}