예제 #1
0
func main() {
	flag.Parse()

	sourceFile := func() *os.File {
		if *input == "-" {
			return os.Stdin
		} else {
			s, err := os.Open(*input)
			if err != nil {
				log.Fatalf("Failed to open input file (%s): %s", *input, err.Error())
			}
			return s
		}
		return os.Stdin // not really reached, go 1.1 requires this. sad.
	}()

	source, err := ook.OpenFile(sourceFile)
	if err != nil {
		log.Fatalf("Unable to open %s: %s", *input, err.Error())
	}
	defer source.Close()

	for {
		burst, err := source.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatalf("Error reading burst from input: %s", err.Error())
		}
		if *verbose {
			log.Printf("read burst: %d pulses, offset=%dHz", len(burst.Pulses), burst.Pulses[0].Frequency)
		}
		ook.Quantify(burst)
	}
}
예제 #2
0
func main() {
	flag.Parse()

	terminate := make(chan os.Signal)
	signal.Notify(terminate, syscall.SIGINT)

	udpAddr, err := net.ResolveUDPAddr("udp", *address)
	if err != nil {
		log.Fatalf("Unable to resolve multicasting address (%s): %s", *address, err.Error())
	}
	if *verbose {
		log.Printf("Multicasting to %s", udpAddr.String())
	}

	iface, err := net.InterfaceByName(*ifaceName)
	if err != nil {
		log.Fatalf("Unknown interface (%s): %s", *ifaceName, err.Error())
	}

	ifaceAddr := interfaceIP4(iface)
	if ifaceAddr == nil {
		log.Fatalf("Could not find an IPv4 address on the interface")
	}

	localAddr := net.UDPAddr{IP: ifaceAddr, Port: 0}

	udpConn, err := net.DialUDP("udp4", &localAddr, udpAddr)
	if err != nil {
		log.Fatalf("Failed to dial UDP: %s", err.Error())
	}

	source := func() *os.File {
		if *input == "-" {
			return os.Stdin
		} else {
			s, err := os.Open(*input)
			if err != nil {
				log.Fatalf("Failed to open input file (%s): %s", *input, err.Error())
			}
			return s
		}
	}()

	reader, err := ook.OpenFile(source)
	if err != nil {
		log.Fatalf("Failed to open input source: %s", err.Error())
	}

	for {
		burst, err := reader.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatalf("Error reading burst: %s", err.Error())
		}
		log.Printf("Read %d pulses", len(burst.Pulses))

		bytes, err := burst.Encode()
		if err != nil {
			log.Fatalf("Failed to encode burst: %s", err.Error())
		}
		log.Printf("Encodes to %d bytes", len(bytes))

		c, err := udpConn.Write(bytes)
		if err != nil {
			log.Fatalf("Failed to write burst (%s -> %s): %s", localAddr.String(), udpAddr.String(), err.Error())
		}
		if c != len(bytes) {
			log.Fatalf("Truncated burst %d != %d", c, len(bytes))
		}
	}
}