Example #1
0
func main() {

	var filename string
	var address string
	var name string
	var nodeLabels []string
	var topology graph.Graph
	var fileErr error

	var input string
	fmt.Print("[create|connect]> ")
	fmt.Scanln(&input)

	if input == "store" {

		fmt.Print("enter topology filename > ")
		fmt.Scanln(&filename)
		fmt.Print("enter topology name > ")
		fmt.Scanln(&name)
		fmt.Print("enter address and port of database > ")
		fmt.Scanln(&address)

		if address == "" {
			fmt.Println("- no address specified, using default database")
			address = "128.153.144.171:6379"
		}

		topology, fileErr = utils.ReadFileToGraph(filename)

		if fileErr != nil {
			panic(fileErr)
		}
		nodeLabels = utils.GetLabelList(topology)
		numberOfNodes := len(nodeLabels)
		labelMap := utils.GetLabelMap(topology)

		rdb, err := database.NewRoutingDatabase(name, "tcp", address, labelMap)
		fmt.Println("Connecting to data base")

		if err != nil {
			panic(err)
		}
		fmt.Println("Connected")

		fmt.Println("Setting paths")

		var src, dest string

		for i := 0; i < numberOfNodes; i++ {
			for j := 0; j < numberOfNodes; j++ {
				if i != j {
					src = nodeLabels[i]
					dest = nodeLabels[j]
					paths, distance, _ := graph.Dijkstra(topology, src, dest)
					path := strings.Join(paths[1:], " ")
					rdb.SetPath(src, dest, fmt.Sprintf("%s %s | %d", src, path, int(distance[dest])))
				}
			}
		}

		fmt.Println("Paths set, storing paths")
		rdb.StorePathsInDB()
		fmt.Println("Paths stored in data base")
		rdb.Disconnect()

	} else if input == "grab" {
		fmt.Print("enter topology name > ")
		fmt.Scanln(&name)
		fmt.Print("enter address and port of database > ")
		fmt.Scanln(&address)

		if address == "" {
			fmt.Println("- no address specified, using default database")
			address = "128.153.144.171:6379"
		}
		rdb, err := database.NewRoutingDatabaseFromDB(name, "tcp", address)
		if err != nil {
			panic(err)
		}
		fmt.Println("Connected")
		var src, dest string

		for {
			fmt.Print("Enter a first node > ")
			fmt.Scanln(&input)
			src = input
			fmt.Print("Enter a second node > ")
			fmt.Scanln(&input)
			dest = input

			path, DBerr := rdb.GetPathFromDB(src, dest)
			if DBerr != nil {
				panic(DBerr)
			}

			fmt.Println("The shortest path is: ", path)
		}
		rdb.Disconnect()
	} else {
		fmt.Println("invalid input program terminated")
	}

}
Example #2
0
func main() {

	var ipAddress, recordName, filename, input string
	var db database.RoutingDatabase
	var DBerr error

	color.Set(color.FgHiGreen, color.Bold, color.BlinkSlow)
	fmt.Println("\nWelcome to the NRA System, written in golang\n")
	color.Unset()

	fmt.Println("Enter IP address and port of the database server you'd like to use")
	fmt.Println("If none is entered, the default server will be used (testing only)\n")
	color.Unset()

	color.Set(color.FgWhite)
	fmt.Print("IP address and port >> ")
	color.Unset()

	fmt.Scanln(&ipAddress)

	if ipAddress == "" {
		color.Set(color.FgHiBlue)
		fmt.Println("No ip address selected, using default database")
		color.Unset()
		ipAddress = "128.153.144.171:6379"
	}

	color.Set(color.FgWhite)
	fmt.Print("Enter the name of the database record you wish to use >> ")
	color.Unset()
	fmt.Scanln(&recordName)

	exists, DBerr := database.TopologyExists(recordName, "tcp", ipAddress)

	if DBerr != nil {
		panic(DBerr)
	}

	if exists {
		color.Set(color.FgHiBlue)
		fmt.Println("This record exists!\n")
		color.Unset()

		db, DBerr = database.ConnectToDatabase(recordName, "tcp", ipAddress)

		topo, TPerr := db.GetTopology()

		//fmt.Printf("CLI: gb.GetTopo[%v]: %v\n", TPerr, topo)

		if DBerr != nil {
			panic(DBerr)
		}
		queryPaths(db)

	} else {
		color.Set(color.FgHiBlue)
		fmt.Println("This record does not exist.  Please check your spelling")
		color.Set(color.FgWhite)
		fmt.Print("Would you like to create a record by this name? [Y/N] >> ")
		color.Unset()
		fmt.Scanln(&input)

		if input == "Y" || input == "y" {
			color.Set(color.FgWhite)
			fmt.Print("Give me the name of a topology file >> ")
			color.Unset()

			fmt.Scanln(&filename)
			g, labels, Uerror := utils.ReadFileToGraph(filename)

			if Uerror != nil {
				panic(Uerror)
			}

			revLabels := utils.GetRevLabels(labels)
			paths := path.DijkstraAllPaths(g)
			pathMap := algorithms.ConvertAllPaths(g, paths)

			realPathMap := make(map[int]map[int]string)

			for k := range pathMap {
				smallMap := pathMap[k]
				newMap := make(map[int]string)
				for k2 := range smallMap {
					stringForPath := smallMap[k2].PathString(labels)
					newMap[k2] = stringForPath
				}
				realPathMap[k] = newMap
			}

			//fmt.Printf("CLI: pathMap: %v\n", pathMap)
			//fmt.Printf("CLI: realPathMap: %v\n", realPathMap)

			db, DBerr = database.NewRoutingDatabase(recordName, "tcp", ipAddress, revLabels, realPathMap, utils.GetNeighborMap(g))

			topo, TPerr := db.GetTopology()

			//fmt.Printf("CLI: db.GetTopo[%v]: %v\n", TPerr, topo)

			if DBerr != nil {
				panic(DBerr)
			}

			color.Set(color.FgHiGreen, color.Bold)
			fmt.Println("Database created correctly!")
			color.Unset()

			queryPaths(db)

		} else {
			color.Set(color.FgHiGreen, color.Bold)
			fmt.Println("Thank you for using golang NRA!")
			color.Unset()
		}
	}
}