Ejemplo n.º 1
0
func main() {

	flag.Parse()

	// config
	configFile, err := os.Open(*configPath)
	if err != nil {
		log.Fatalln(err)
	}
	defer configFile.Close()

	err = json.NewDecoder(configFile).Decode(&config)
	if err != nil {
		log.Fatalln(err)
	}

	// connect to nfd
	conn, err := packet.Dial(config.NFD.Network, config.NFD.Address)
	if err != nil {
		log.Fatalln(err)
	}

	// create a new face
	recv := make(chan *ndn.Interest)
	face := ndn.NewFace(conn, recv)
	defer face.Close()

	// key
	pem, err := os.Open(config.PrivateKeyPath)
	if err != nil {
		log.Fatalln(err)
	}
	defer pem.Close()
	key, _ := ndn.DecodePrivateKey(pem)

	packet_size := 8192

	if config.PacketSize != 0 {
		packet_size = config.PacketSize
	}

	persist_db := "ndnfs.db"

	if len(config.ContentDB) != 0 {
		persist_db = config.ContentDB
	}

	// create an interest mux
	m := mux.New()
	// 7. logging
	m.Use(mux.Logger)
	// 6. versioning
	m.Use(mux.Versioner)
	// 5. before encrypting it, zip it
	m.Use(mux.Gzipper)
	// 4. before segmenting it, encrypt it
	m.Use(mux.Encryptor(key.(*ndn.RSAKey)))
	// 3. if the data packet is too large, segment it
	m.Use(mux.Segmentor(packet_size))
	// 2. reply the interest with the on-disk cache
	m.Use(persist.Cacher(persist_db))
	// 1. reply the interest with the in-memory cache
	m.Use(mux.Cacher)
	// 0. an interest packet comes
	m.Use(mux.Queuer)

	fmt.Println("")
	fmt.Println("Prefix = ", config.File.Prefix)
	fmt.Println("")

	files, err := filepath.Glob(config.File.Dir + "/*")
	if err != nil {
		log.Fatalln(err)
	} else {
		fmt.Println()
		fmt.Println("List of files")
		fmt.Println("-------------")
		for i := 0; i < len(files); i++ {

			if IsFile(files[i]) {
				_, filename := filepath.Split(files[i])
				fmt.Println("[", i, "] -", filename)
			}

		}

	}

	m.Handle(FileServer(config.File.Prefix, config.File.Dir))

	// pump the face's incoming interests into the mux
	m.Run(face, recv, key)
}
Ejemplo n.º 2
0
func main() {
	flag.Parse()

	var data []byte
	var retry int = 0

	// config
	configFile, err := os.Open(*configPath)
	if err != nil {
		log.Fatalln(err)
	}
	defer configFile.Close()

	err = json.NewDecoder(configFile).Decode(&config)
	if err != nil {
		log.Fatalln(err)
	}

	// connect to nfd
	conn, err := packet.Dial(config.NFD.Network, config.NFD.Address)
	if err != nil {
		log.Fatalln(err)
	}
	// start a new face but do not receive new interests
	face := ndn.NewFace(conn, nil)
	defer face.Close()

	// read producer key
	pem, err := os.Open(config.PrivateKeyPath)
	if err != nil {
		log.Fatalln(err)
	}
	defer pem.Close()
	key, _ := ndn.DecodePrivateKey(pem)

	// create a data fetcher
	f := mux.NewFetcher()
	// 0. a data packet comes
	// 1. verifiy checksum
	f.Use(mux.ChecksumVerifier)
	// 2. add the data to the in-memory cache
	f.Use(mux.Cacher)
	// 3. logging
	f.Use(mux.Logger)
	// see producer
	// 4. assemble segments if the content has multiple segments
	// 5. decrypt
	dec := mux.Decryptor(key.(*ndn.RSAKey))
	// 6. unzip

	fileSplit := strings.Split(*filePrefix, "/")
	fileName := fileSplit[len(fileSplit)-1]
	file, err := os.Create(fileName)
	if err != nil {
		log.Fatalln(err)
	}
	defer file.Close()

	fmt.Printf("\nFetching file %s from ndn:%s\n\n", fileName, *filePrefix)

	retry_limit := 10

	if config.RetryLimit != 0 {
		retry_limit = config.RetryLimit
	}

	for retry = 0; retry <= retry_limit; retry++ {
		data = f.Fetch(face, &ndn.Interest{Name: ndn.NewName(*filePrefix)}, mux.Assembler, dec, mux.Gunzipper)

		if data != nil {
			break
		} else {
			fmt.Println("Empty data!")
		}
	}

	if retry <= retry_limit {
		databytes, err := io.Copy(file, bytes.NewReader(data))
		if err != nil {
			log.Fatalln(err)
		}

		fmt.Printf("wrote %d bytes\n", databytes)
	} else {
		fmt.Printf("\nFailed to fetch %s file after %d times retry attempt.\n\n", fileName, retry)
		err := os.Remove(fileName)
		if err != nil {
			log.Fatalln(err)
		}
	}

}