Esempio n. 1
0
  public boolean go(String encFlag, String ccnName, String tapName, String readName) {
    CCNNetworkManager manager = null;
    try {
      if (encFlag.equals("0")) {
        SystemConfiguration.setDefaultEncoding(TextXMLCodec.codecName());
      } else {
        SystemConfiguration.setDefaultEncoding(BinaryXMLCodec.codecName());
      }
      File theFile = new File(readName);
      if (!theFile.exists()) {
        System.out.println("No such file: " + readName);
        usage();
        return false;
      }

      // Get writing handle
      CCNHandle handle = CCNHandle.open();
      manager = handle.getNetworkManager();
      // Set up tap so packets get written to file
      manager.setTap(tapName);

      ContentName name = ContentName.fromURI(ccnName);

      // Register standing interest so our put's will flow
      // This must be through separate handle instance so it
      // appears that there is an interest from a separate app
      // because interest from the same app as the writer will
      // not consume the data and therefore will block
      CCNHandle reader = CCNHandle.open();
      reader.expressInterest(new Interest(ccnName), this);

      // Remove automatic verification at this level. Can put it back in at
      // the tap level. CCNWriter is a segmenting writer, it makes no real
      // sense for it to return "a" ContentObject to verify.

      // Dump the file in small packets
      InputStream is = new FileInputStream(theFile);
      byte[] bytes = new byte[CHUNK_SIZE];
      int i = 0;
      CCNWriter writer = new CCNWriter(name, handle);
      writer.disableFlowControl();
      while (is.read(bytes) >= 0) {
        writer.put(ContentName.fromNative(name, new Integer(i++).toString()), bytes);
      }

      return true;

    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      if (null != manager) {
        // Need to call shutdown directly on manager at this point
        manager.shutdown();
      }
    }
  }