示例#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
}
示例#2
0
func (publ *Publisher) run() {
	sock, err := pub.NewSocket()
	if err != nil {
		log.Fatalln("pub.NewSocket error:", err)
	}
	sock.AddTransport(inproc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	defer sock.Close()
	if publ.Connect {
		err = sock.Dial(publ.Endpoint)
	} else {
		err = sock.Listen(publ.Endpoint)
	}
	if err != nil {
		log.Fatalln("sock connect failed:", err)
	}

	// sending on a socket straight away silently fails, so wait 20ms. ugh.
	time.Sleep(time.Millisecond * 20)

	for ev := range publ.Channel {
		// format: topic\0data
		data := fmt.Sprintf("%s\000%s", ev.Topic, ev.String())
		err := sock.Send([]byte(data))
		if err != nil {
			log.Fatalln("Failed to Send message:", err)
		}
	}
}
示例#3
0
func NewPublisher(ctx context.Context, url string) (*Publisher, error) {
	var sock mangos.Socket
	var err error

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

	err = sock.Listen(url)
	if err != nil {
		return nil, err
	}

	publiser := &Publisher{
		ctx:  ctx,
		url:  url,
		sock: sock,

		publishCh: make(chan []string),
	}

	go publiser.run()
	return publiser, nil
}
示例#4
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()
}
示例#5
0
func (pt *pubTest) Init(t *testing.T, addr string) bool {
	pt.pubidx = 0
	var err error
	if pt.Sock, err = pub.NewSocket(); err != nil {
		pt.Errorf("NewSocket(): %v", err)
		return false
	}
	return pt.T.Init(t, addr)
}
示例#6
0
func newEvtPublisher(addr string) (mangos.Socket, error) {
	log.Println("alauncher: creating event publisher...")
	socket, err := pub.NewSocket()
	if err != nil {
		return nil, err
	}

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

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

	return socket, nil
}
示例#7
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)
	}
}