예제 #1
0
func StartNanomsgJsonTest(address string, clients int, requestsPerClient int, messageSize int, timer metrics.Timer, requestSize *int) func(wg *sync.WaitGroup) {
	return func(wg *sync.WaitGroup) {
		var socket mangos.Socket
		var err error

		if socket, err = req.NewSocket(); err != nil {
			log.Fatal(err)
		}

		defer socket.Close()
		defer wg.Done()

		socket.AddTransport(tcp.NewTransport())

		if err = socket.Dial("tcp://" + address); err != nil {
			log.Fatal(err)
		}

		request, _ := json.Marshal(Request{Method: "TEST", Payload: strings.Repeat("a", messageSize)})
		*requestSize = len(request)

		for i := 0; i < requestsPerClient; i++ {
			timer.Time(func() {
				if err = socket.Send(request); err != nil {
					log.Fatal(err)
				}

				socket.Recv()
			})
		}
	}
}
예제 #2
0
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)
	}
}
예제 #3
0
파일: client.go 프로젝트: bunin/Flotilla
func sendRequest(s mangos.Socket, request request) (*response, error) {
	fmt.Println("Sending a request")

	requestJSON, err := json.Marshal(request)
	if err != nil {
		// This is not recoverable.
		panic(err)
	}

	fmt.Printf("sendRequest: %s\n", requestJSON)

	if err := s.Send(requestJSON); err != nil {
		return nil, err
	}

	rep, err := s.Recv()
	if err != nil {
		return nil, err
	}

	var resp response
	if err := json.Unmarshal(rep, &resp); err != nil {
		return nil, err
	}

	return &resp, nil
}
예제 #4
0
func send(s mangos.Socket, obj interface{}) error {
	data, err := json.Marshal(obj)
	if err != nil {
		return stackerr.Wrap(err)
	}
	return stackerr.Wrap(s.Send(data))
}
예제 #5
0
파일: survey.go 프로젝트: iwarsong/bearded
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())
		}
	}
}
예제 #6
0
파일: survey.go 프로젝트: iwarsong/bearded
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))
		}
	}
}
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)
	}
}
예제 #8
0
파일: reqrep.go 프로젝트: iwarsong/bearded
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())
			}
		}
	}
}
예제 #9
0
파일: gen.go 프로젝트: jfsmig/lbtk
func output(in chan string, out mangos.Socket) {
	if out == nil {
		panic("Invalid socket")
	}
	for item := range in {
		out.Send([]byte(item))
	}
}
예제 #10
0
func publishEvt(channel mangos.Socket, evt string) error {
	if err := channel.Send([]byte(evt)); err != nil {
		log.Printf("fail to publish '%v' event:%v\n", evt, err)
		return err
	}

	return nil
}
예제 #11
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()
}
예제 #12
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())
		}
	}
}
예제 #13
0
func simpleSend(t *testing.T, tx mangos.Socket, wg *sync.WaitGroup) {
	var buf [256]byte
	i := 0
	for i = 0; i < 10000; i++ {
		l := rand.Intn(255) + 1
		buf[0] = uint8(l)
		if e := tx.Send(buf[:l]); e != nil {
			t.Fatalf("Send: %v", e)
			break
		}
	}
	t.Logf("Sent %d Msgs", i)
	if e := tx.Close(); e != nil {
		t.Fatalf("Tx Close: %v", e)
	}
	wg.Done()
}
예제 #14
0
func node1(url string, msg string) {
	var sock mangos.Socket
	var err error

	if sock, err = push.NewSocket(); err != nil {
		die("can't get new push socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on push socket: %s", err.Error())
	}
	fmt.Printf("NODE1: SENDING \"%s\"\n", msg)
	if err = sock.Send([]byte(msg)); err != nil {
		die("can't send message on push socket: %s", err.Error())
	}
	sock.Close()
}
예제 #15
0
파일: pubsub.go 프로젝트: iwarsong/bearded
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)
	}
}
예제 #16
0
파일: bus.go 프로젝트: iwarsong/bearded
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))

	}
}
예제 #17
0
func runCmdServer(channel mangos.Socket, done <-chan struct{}) (<-chan string, error) {
	cmdChan := make(chan string)
	go func() {
		for {
			// Could also use sock.RecvMsg to get header
			log.Println("alauncher: cmd server - waiting for a command...")
			select {
			case <-done:
				log.Println("alauncher: cmd server - done...")
				return
			default:
				if rawCmd, err := channel.Recv(); err != nil {
					switch err {
					case mangos.ErrRecvTimeout:
						log.Println("alauncher: cmd server - timeout... ok")
					default:
						log.Println("alauncher: cmd server - error =>", err)
					}
				} else {
					cmd := string(rawCmd)
					log.Println("alauncher: cmd server - got a command =>", cmd)
					cmdChan <- cmd
					//for now just ack the command and process the command asynchronously
					//NOTE:
					//must reply before receiving the next message
					//otherwise nanomsg/mangos will be confused :-)
					monitorFinishReply := "ok"
					err = channel.Send([]byte(monitorFinishReply))
					if err != nil {
						log.Println("alauncher: cmd server - fail to send monitor.finish reply =>", err)
					}
				}
			}
		}
	}()

	return cmdChan, nil
}
예제 #18
0
파일: reqrep.go 프로젝트: iwarsong/bearded
func node1(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte

	if sock, err = req.NewSocket(); err != nil {
		die("can't get new req socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on req socket: %s", err.Error())
	}
	fmt.Printf("NODE1: SENDING DATE REQUEST %s\n", "DATE")
	if err = sock.Send([]byte("DATE")); err != nil {
		die("can't send message on push socket: %s", err.Error())
	}
	if msg, err = sock.Recv(); err != nil {
		die("can't receive date: %s", err.Error())
	}
	fmt.Printf("NODE1: RECEIVED DATE %s\n", string(msg))
	sock.Close()
}
예제 #19
0
func sendCmd(channel mangos.Socket, cmd string) (string, error) {
	sendTimeouts := 0
	recvTimeouts := 0

	log.Printf("sendCmd(%s)\n", cmd)
	for {
		if err := channel.Send([]byte(cmd)); err != nil {
			switch err {
			case mangos.ErrSendTimeout:
				log.Println("sendCmd(): send timeout...")
				sendTimeouts++
				if sendTimeouts > 3 {
					return "", err
				}
			default:
				return "", err
			}
		}

		response, err := channel.Recv()
		if err != nil {
			switch err {
			case mangos.ErrRecvTimeout:
				log.Println("sendCmd(): receive timeout...")
				recvTimeouts++
				if recvTimeouts > 3 {
					return "", err
				}
			default:
				return "", err
			}
		}

		return string(response), nil
	}
}
예제 #20
0
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)
	}
}
예제 #21
0
파일: pair.go 프로젝트: iwarsong/bearded
func send_name(sock mangos.Socket, name string) {
	fmt.Printf("%s: SENDING \"%s\"\n", name, name)
	if err := sock.Send([]byte(name)); err != nil {
		die("failed sending: %s", err)
	}
}