コード例 #1
0
ファイル: compat.go プロジェクト: lucmichalski/mangos
// 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
ファイル: push_test.go プロジェクト: iwarsong/bearded
func (pt *PullTest) Init(t *testing.T, addr string) bool {
	var err error
	if pt.Sock, err = pull.NewSocket(); err != nil {
		pt.Errorf("NewSocket(): %v", err)
		return false
	}
	return pt.T.Init(t, addr)
}
コード例 #3
0
ファイル: pipeline.go プロジェクト: iwarsong/bearded
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)
	}
}
コード例 #4
0
ファイル: proxy-tcp.go プロジェクト: jfsmig/lbtk
func MakeFront(server *Server, concurrency int, config string) (*Front, error) {

	tokens := strings.Split(config, ",")
	if len(tokens) < 2 {
		return nil, BadFront
	}

	self := &Front{}

	// Prepare the front TCP socket
	if url, err := net.ResolveTCPAddr("tcp", tokens[0]); err != nil {
		return nil, err
	} else if ep, err := net.ListenTCP("tcp", url); err != nil {
		return nil, err
	} else {
		self.endpoint = ep
	}

	// Prepare the Nanomsg feeder
	if nn, err := pull.NewSocket(); err != nil {
		self.endpoint.Close()
		return nil, err
	} else {
		self.feeder = nn
		self.feeder.AddTransport(ipc.NewTransport())
		self.feeder.AddTransport(tcp.NewTransport())
		self.feeder.SetOption(mangos.OptionReadQLen, 16)
	}
	for i := 1; i < len(tokens); i++ {
		self.feeder.Dial(tokens[i])
	}

	// Eventually, finalize the structure
	self.server = server
	self.tokens = make(chan bool, concurrency)
	for ; concurrency > 0; concurrency-- {
		self.tokens <- true
	}

	return self, nil
}