Esempio n. 1
0
func main() {
	flag.Parse()
	alexander.ExportTypes(Message{})

	err := alexander.Start()
	defer alexander.Stop()
	if err != nil {
		log.Fatalf("Could not start alexander %s", err)
	}

	exitChan := make(chan bool)

	if *isProducer {
		go func() {
			for count := 0; count < 10; count++ {
				msg := &Message{Msg: *message, Origin: *nodeName, Sent: time.Now()}
				_, _, err := alexander.Do(msg, "PrintMe")
				if err != nil {
					log.Printf("Error processing task: %s", err)
				}
				time.Sleep(*frequency)
			}
			exitChan <- true
		}()
	}

	<-exitChan
}
Esempio n. 2
0
func main() {
	flag.Parse()

	// I had forgotten this initially and was baffled that the program runs on just one of my 4 CPUs.
	runtime.GOMAXPROCS(*maxprocs)

	if *distributed {
		alexander.Start()
		defer alexander.Stop()
	}

	if *generate {
		generateGraph(*graphFile, *numNodes)
	} else if *worker {
		for {
			time.Sleep(5 * time.Second)
		}
	} else if *graphFile != "" {
		graphData := loadGraph(*graphFile)
		pathsChan := make(chan NodePath)

		go Dijkstra(graphData, *distributed, pathsChan)

		_, err := os.Create(*pathsFile)
		if err != nil {
			log.Fatal(err)
		}

		outfile, err := os.OpenFile(*pathsFile, os.O_APPEND|os.O_WRONLY, 0600)
		if err != nil {
			log.Fatal(err)
		}
		defer outfile.Close()

		for idx := 0; idx < len(graphData); idx++ {
			p := <-pathsChan

			var pathTrail string
			for _, node := range p {
				pathTrail += fmt.Sprintf("%s[%d]->", node.Id, node.Distance)
			}
			pathTrail += "END"

			path := fmt.Sprintf("Path for %s: %s \n", p[0].Id, pathTrail)

			_, err = outfile.WriteString(path)
			if err != nil {
				log.Fatal(err)
			}
		}
	}
}