Example #1
0
File: meshweb.go Project: hwhw/mesh
func main() {
	flag.Parse()

	var err error
	db, err = nodedb.New(
		*gluonPurgePtr,
		*batAdvVisPurgePtr,
		*storePtr,
		*logPtr)

	if err != nil {
		log.Fatalf("Error opening database: %v", err)
	}

	if *importNodesPtr != "" {
		if err := db.ImportNodesFile(*importNodesPtr, false); err != nil {
			log.Printf("Error while importing nodes from %v: %v, continuing", *importNodesPtr, err)
		}
	}

	if *importNodesPersistentPtr != "" {
		if err := db.ImportNodesFile(*importNodesPersistentPtr, false); err != nil {
			log.Printf("Error while importing nodes from %v: %v, continuing", *importNodesPersistentPtr, err)
		}
	}

	client := alfred.NewClient(*clientNetworkPtr, *clientAddressPtr, nil)
	db.StartUpdater(client, *updateWaitPtr, *retryWaitPtr)
	db.StartPurger(*gluonPurgeIntPtr, *batAdvVisPurgeIntPtr)
	db.StartLogger(*nodeOfflineDuration)

	if *httpPtr == "" {
		log.Printf("no HTTP server, just updating")
		select {}
	} else {
		log.Printf("starting HTTP server")
		webservice.Run(db, *httpPtr, *httpAdminPtr, *httpdStaticPtr, *nodeOfflineDuration)
	}
}
Example #2
0
func main() {
	flag.Parse()
	client := alfred.NewClient(*network, *address, nil)
	var reterr error
	switch flag.Arg(0) {
	case "set":
		id, err := strconv.Atoi(flag.Arg(1))
		if err != nil || id < 1 || id > 255 {
			failure("error: invalid type %v\n", flag.Arg(1))
			goto failure
		}
		buf, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			failure("error: reading data from stdin, %v\n", err)
		}
		reterr = client.PushData(uint8(id), compress(buf))
	case "get":
		id, err := strconv.Atoi(flag.Arg(1))
		if err != nil || id < 1 || id > 255 {
			failure("error: invalid type %v\n", flag.Arg(1))
			goto failure
		}
		reterr = client.Request(uint8(id), func(d alfred.Data) error {
			fmt.Printf("{%s, \"", d.Source)
			buf := uncompress(d.Data)
			for _, c := range buf {
				switch {
				case c == 0x5C: // backslash
					os.Stdout.Write([]byte{c, c})
				case c == 0x22: // parenthesis
					os.Stdout.Write([]byte{0x5C, c})
				case 0x20 <= c && c <= 0x7E:
					os.Stdout.Write([]byte{c})
				default:
					fmt.Printf("\\x%02x", c)
				}
			}
			fmt.Print("\"},\n")
			return nil
		})
	case "mode":
		mode, err := strconv.Atoi(flag.Arg(1))
		if err != nil || mode < 0 || mode > 2 {
			failure("error: invalid mode %v\n", flag.Arg(1))
			goto failure
		}
		reterr = client.ModeSwitch(uint8(mode))
	case "interfaces":
		ifaces := []byte(flag.Arg(1))
		if len(ifaces) < 1 {
			failure("you need to specify interface(s)")
		}
		reterr = client.ChangeInterface(ifaces)
	default:
		usage()
	}
	if reterr != nil {
		failure("error: %v\n", reterr)
	}
	return
failure:
	os.Exit(-1)
}