Beispiel #1
0
func view_path(w http.ResponseWriter, r *http.Request, dbname, srcnode, dstnode string) {
	db, err1 := database.ConnectToDatabase(dbname, db_network, db_address)
	if err1 != nil {
		t_error.Execute(w, err1)
		return
	}
	labels, err6 := db.GetLabels()
	if err6 != nil {
		t_error.Execute(w, err6)
		return
	}
	srcid, err4 := strconv.Atoi(srcnode)
	if err4 != nil {
		t_error.Execute(w, err4)
		return
	}
	dstid, err5 := strconv.Atoi(dstnode)
	if err5 != nil {
		t_error.Execute(w, err5)
		return
	}
	path, err2 := db.GetPath(labels[srcid], labels[dstid])
	if err2 != nil {
		t_error.Execute(w, err2)
		return
	}
	parts := strings.Split(path, " ")
	if len(parts) < 3 {
		t_error.Execute(w, "No apparent path exists")
		return
	}
	pathpart := parts[:len(parts)-2]
	revlabels := utils.GetRevLabels(labels)
	idpath := make([]int, len(pathpart))
	for idx, part := range pathpart {
		idpath[idx] = revlabels[part]
	}
	sidpath := make([]string, len(idpath))
	for idx, id := range idpath {
		sidpath[idx] = fmt.Sprint(id)
	}
	cost, _ := strconv.Atoi(parts[len(parts)-1])
	err3 := t_path.Execute(w, tinPath{Dbname: dbname, Rawpath: path, Path: pathpart, Netpath: strings.Join(sidpath, "/"), Fullpath: strings.Join(pathpart, " -> "), Cost: cost})
	if err3 != nil {
		t_error.Execute(w, err3)
	}
}
Beispiel #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()
		}
	}
}