Ejemplo 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
}
Ejemplo n.º 2
0
func client(url string, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte

	if sock, err = respondent.NewSocket(); err != nil {
		die("can't get new respondent socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on respondent socket: %s", err.Error())
	}
	for {
		if msg, err = sock.Recv(); err != nil {
			die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("CLIENT(%s): RECEIVED \"%s\" SURVEY REQUEST\n",
			name, string(msg))

		d := date()
		fmt.Printf("CLIENT(%s): SENDING DATE SURVEY RESPONSE\n", name)
		if err = sock.Send([]byte(d)); err != nil {
			die("Cannot send: %s", err.Error())
		}
	}
}
Ejemplo n.º 3
0
func (rt *responderTest) Init(t *testing.T, addr string) bool {
	var err error
	if rt.Sock, err = respondent.NewSocket(); err != nil {
		rt.Errorf("NewSocket(): %v", err)
		return false
	}
	return rt.T.Init(t, addr)
}
Ejemplo n.º 4
0
func startNode(masterHost, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	masterUrl := url.URL{Scheme: "tcp", Host: masterHost}
	ip := getPrivateIP()

	// Try to get new "respondent" socket
	if sock, err = respondent.NewSocket(); err != nil {
		utils.Die("Can't get new respondent socket: %s", err.Error())
	}
	defer sock.Close()

	sock.AddTransport(tcp.NewTransport())

	// Connect to master
	if err = sock.Dial(masterUrl.String()); err != nil {
		utils.Die("Can't dial on respondent socket: %s", err.Error())
	}

	// Wait for a survey request and send responses
	for {
		if msg, err = sock.Recv(); err != nil {
			utils.Die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("Client(%s): Received \"%s\" survey request\n", name, string(msg))

		var loadAvg *load.LoadAvgStat
		if loadAvg, err = load.LoadAvg(); err != nil {
			utils.Die("Cannot get load average: %s", err.Error())
		}

		var cpuInfo []cpu.CPUInfoStat
		if cpuInfo, err = cpu.CPUInfo(); err != nil {
			utils.Die("Cannot get CPU info: %s", err.Error())
		}
		fmt.Printf("CPU INFO len: %d\n", len(cpuInfo))

		// Get the normalized CPU load
		avg := loadAvg.Load1
		cores := int32(0)
		for _, info := range cpuInfo {
			fmt.Printf("Inner Cores: %d\n", info.Cores)
			cores += info.Cores
		}
		fmt.Printf("Load avg: %f\n", avg)
		fmt.Printf("Cores: %d\n", cores)
		avg = avg / float64(cores)

		fmt.Printf("Client(%s): Sending survey response\n", name)
		if err = sock.Send([]byte(fmt.Sprintf("%s,%f", ip, avg))); err != nil {
			utils.Die("Cannot send: %s", err.Error())
		}
	}
}
Ejemplo n.º 5
0
func ClientWithSub(urlServer string, urlPubSub string, service string) (*DiscoveryClient, error) {
	var sock mangos.Socket
	var err error
	var subscriber *Subscriber

	ctx, cancel := context.WithCancel(context.Background())

	if urlPubSub != "" {
		subCtx, _ := context.WithCancel(ctx)
		subscriber, err = NewSubscriber(subCtx, urlPubSub)
		if err != nil {
			return nil, err
		}
	}

	sock, err = respondent.NewSocket()
	if err != nil {
		return nil, err
	}

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

	client := &DiscoveryClient{
		urlServer:  urlServer,
		urlPubSub:  urlPubSub,
		service:    service,
		ctx:        ctx,
		cancel:     cancel,
		sock:       sock,
		subscriber: subscriber,
	}

	go client.run()
	return client, nil
}