Beispiel #1
0
func server(url string, nworkers int) {
	var sock mangos.Socket
	var err error
	var wg sync.WaitGroup

	rand.Seed(time.Now().UnixNano())

	if sock, err = rep.NewSocket(); err != nil {
		die("can't get new rep socket: %s", err)
	}
	if err = sock.SetOption(mangos.OptionRaw, true); err != nil {
		die("can't set raw mode: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on rep socket: %s", err.Error())
	}
	wg.Add(nworkers)
	fmt.Printf("Starting %d workers\n", nworkers)
	for id := 0; id < nworkers; id++ {
		go func(id int) {
			defer wg.Done()
			serverWorker(sock, id)
		}(id)
	}
	wg.Wait()
}
func StartNanomsgProtoServer(port string) {
	var server mangos.Socket
	var err error

	if server, err = rep.NewSocket(); err != nil {
		log.Fatal(err)
	}

	server.AddTransport(tcp.NewTransport())
	server.Listen("tcp://0.0.0.0:" + port)

	for {
		msg, _ := server.Recv()

		var body pb.Request

		proto.Unmarshal(msg, &body)

		data, _ := proto.Marshal(&pb.Response{
			Method:        body.Method,
			PayloadLength: int64(len(body.Payload)),
		})

		server.Send(data)
	}
}
Beispiel #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
}
func StartNanomsgJsonServer(port string) {
	var server mangos.Socket
	var err error

	if server, err = rep.NewSocket(); err != nil {
		log.Fatal(err)
	}

	server.AddTransport(tcp.NewTransport())
	server.Listen("tcp://0.0.0.0:" + port)

	for {
		msg, _ := server.Recv()

		var body Request
		var response []byte

		json.Unmarshal(msg, &body)

		if response, err = json.Marshal(Response{Method: body.Method, PayloadLength: len(body.Payload)}); err != nil {
			log.Fatal(err)
		}

		server.Send(response)
	}
}
Beispiel #5
0
func server(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = surveyor.NewSocket(); err != nil {
		die("can't get new surveyor socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on surveyor socket: %s", err.Error())
	}
	err = sock.SetOption(mangos.OptionSurveyTime, time.Second*2)
	if err != nil {
		die("SetOption(): %s", err.Error())
	}
	for {
		fmt.Println("SERVER: SENDING DATE SURVEY REQUEST")
		if err = sock.Send([]byte("DATE")); err != nil {
			die("Failed sending survey: %s", err.Error())
		}
		for {
			if msg, err = sock.Recv(); err != nil {
				break
			}
			fmt.Printf("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n",
				string(msg))
		}
	}
}
Beispiel #6
0
func node0(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = rep.NewSocket(); err != nil {
		die("can't get new rep socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on rep socket: %s", err.Error())
	}
	for {
		// Could also use sock.RecvMsg to get header
		msg, err = sock.Recv()
		if string(msg) == "DATE" { // no need to terminate
			fmt.Println("NODE0: RECEIVED DATE REQUEST")
			d := date()
			fmt.Printf("NODE0: SENDING DATE %s\n", d)
			err = sock.Send([]byte(d))
			if err != nil {
				die("can't send reply: %s", err.Error())
			}
		}
	}
}
Beispiel #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()
}
Beispiel #8
0
func node0(url string) {
	var sock mangos.Socket
	var err error
	if sock, err = pair.NewSocket(); err != nil {
		die("can't get new pair socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on pair socket: %s", err.Error())
	}
	send_recv(sock, "node0")
}
Beispiel #9
0
func Server(urlServer string, urlPubSub string, opt Options) (*DiscoveryServer, error) {
	var sock mangos.Socket
	var err error
	var publisher *Publisher

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

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

	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())

	err = sock.Listen(urlServer)
	if err != nil {
		return nil, err
	}
	err = sock.SetOption(mangos.OptionSurveyTime, opt.SurveyTime)
	if err != nil {
		return nil, err
	}
	err = sock.SetOption(mangos.OptionRecvDeadline, opt.RecvDeadline)
	if err != nil {
		return nil, err
	}

	pubCtx, pubCancel := context.WithCancel(ctx)
	publisher, err = NewPublisher(pubCtx, urlPubSub)
	if err != nil {
		pubCancel()
		return nil, err
	}
	services := NewServices(publisher)

	server := &DiscoveryServer{
		services: services,

		urlServer: urlServer,
		urlPubSub: urlPubSub,
		opt:       opt,

		ctx:    ctx,
		cancel: cancel,
		sock:   sock,
	}

	go server.run()
	return server, nil
}
Beispiel #10
0
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)
	}
}
Beispiel #11
0
func main() {
	how_rand := flag.Bool("rand", false, "")
	flag.Parse()
	if flag.NArg() < 1 {
		log.Fatal("Missing arguments: at least one endpoint to bind to")
	}

	var err error
	var out mangos.Socket
	if out, err = push.NewSocket(); err != nil {
		log.Fatal("Nanomsg socket creation failure: ", err)
	} else {
		defer out.Close()
		out.AddTransport(ipc.NewTransport())
		out.AddTransport(tcp.NewTransport())
		out.SetOption(mangos.OptionWriteQLen, 16)
		out.SetOption(mangos.OptionRetryTime, 0)
		for i := 0; i < flag.NArg(); i++ {
			if err := out.Listen(flag.Arg(i)); err != nil {
				log.Fatal("Nanomsg listen() error: ", err)
			}
		}
	}

	switch {
	case *how_rand:
		Gen(out, func(tab []string) string {
			if len(tab) <= 0 {
				return ""
			}
			return tab[rand.Intn(len(tab))]
		})
	default:
		i := 0
		Gen(out, func(tab []string) string {
			if len(tab) <= 0 {
				return ""
			}
			i = (i + 1) % len(tab)
			return tab[i]
		})
	}
}
Beispiel #12
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)
	}
}
func (receiver *SPReceiver) Receive(address string) error {
	var sock mangos.Socket
	var err error

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

	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err := sock.Listen(address); err != nil {
		return err
	}

	for {
		if _, err := sock.Recv(); err != nil {
			return err
		}
	}

	return nil
}
Beispiel #14
0
func node(args []string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	var x int

	if sock, err = bus.NewSocket(); err != nil {
		die("bus.NewSocket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(args[2]); err != nil {
		die("sock.Listen: %s", err.Error())
	}

	// wait for everyone to start listening
	time.Sleep(time.Second)
	for x = 3; x < len(args); x++ {
		if err = sock.Dial(args[x]); err != nil {
			die("socket.Dial: %s", err.Error())
		}
	}

	// wait for everyone to join
	time.Sleep(time.Second)

	fmt.Printf("%s: SENDING '%s' ONTO BUS\n", args[1], args[1])
	if err = sock.Send([]byte(args[1])); err != nil {
		die("sock.Send: %s", err.Error())
	}
	for {
		if msg, err = sock.Recv(); err != nil {
			die("sock.Recv: %s", err.Error())
		}
		fmt.Printf("%s: RECEIVED \"%s\" FROM BUS\n", args[1],
			string(msg))

	}
}
func (m *Master) queryWorkers(c chan<- float64) {
	var (
		err  error
		sock mangos.Socket
	)

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

	sock.AddTransport(tcp.NewTransport())

	// Begin listening on the URL
	if err = sock.Listen(m.url.String()); err != nil {
		utils.Die("Can't listen on surveyor socket: %s", err.Error())
	}

	// Set "deadline" for the survey and a timeout for receiving responses
	if err = sock.SetOption(mangos.OptionSurveyTime, m.surveyDeadline); err != nil {
		utils.Die("SetOption(mangos.OptionSurveyTime): %s", err.Error())
	}
	if err = sock.SetOption(mangos.OptionRecvDeadline, m.surveyDeadline+(1*time.Second)); err != nil {
		utils.Die("SetOption(mangos.OptionRecvDeadline): %s", err.Error())
	}

	for {
		fmt.Println("Sending master request")
		if err = sock.Send([]byte("CPU")); err != nil {
			utils.Die("Failed sending survey: %s", err.Error())
		}

		loadAvgs := []float64{}
		for {
			var msg []byte
			if msg, err = sock.Recv(); err != nil {
				break
			}
			parts := strings.Split(string(msg), ",")
			if len(parts) != 2 {
				continue
			}

			ip := parts[0]
			loadAvgString := parts[1]

			// Find the corresponding droplet
			var worker *Worker
			for _, w := range m.workers {
				if w.privateAddr == ip {
					worker = w
					break
				}
			}
			if worker == nil {
				fmt.Printf("Message received from unknown worker '%s'. Skipping...\n", ip)
				continue
			}

			var loadAvg float64
			if loadAvg, err = strconv.ParseFloat(string(loadAvgString), 64); err != nil {
				utils.Die("ParseFloat(): %s", err.Error())
			}

			// Set their load average and append this worker's load average to the list
			worker.loadAvg = loadAvg
			loadAvgs = append(loadAvgs, loadAvg)
		}

		// Compute the average loadAvg
		var loadAvg float64
		for _, avg := range loadAvgs {
			loadAvg += avg
		}
		loadAvg /= float64(len(loadAvgs))

		// Send the load averages
		if !math.IsNaN(loadAvg) {
			c <- loadAvg
		}

		// Wait
		time.Sleep(m.queryInterval)
	}
}