/** * Prepare function prepare bundle to generate. It adds Primary Block Info at the start of * xmit_blocks and add endpoint eids at the start of dictionary. * * @param bundle Bundle to prepare * @param xmit_blocks Empty xmit_blocks of the blundle * @param source Source endpoint id * @param link Link of the bundle * @param list Owner type * @return Return success message on success */ public int prepare( final Bundle bundle, BlockInfoVec xmit_blocks, final BlockInfo source, final Link link, list_owner_t list) { // There shouldn't already be anything in the xmit_blocks assert (xmit_blocks.size() == 0) : TAG + ": prepare() there shouldn't be anything already in xmit_blocks"; // Add EIDs to start off the dictionary xmit_blocks.dict().add_eid(bundle.dest()); xmit_blocks.dict().add_eid(bundle.source()); xmit_blocks.dict().add_eid(bundle.replyto()); xmit_blocks.dict().add_eid(bundle.custodian()); // make sure to add the primary to the front xmit_blocks.add(0, new BlockInfo(this, source)); return BP_SUCCESS; }
/** * Generate primary block by encoding all the metadata of the primary block and copy to primary * block writeable buffer. * * @param bundle Bundle to generate * @param xmit_blocks xmit_blocks of the bundle * @param block Primary block of the bundle to generate and write to the writeable buffer */ public void generate_primary(final Bundle bundle, BlockInfoVec xmit_blocks, BlockInfo block) { // point at the local dictionary Dictionary dict = xmit_blocks.dict(); int primary_len = 0; // total length of the primary block PrimaryBlock primary = new PrimaryBlock(); primary_len = get_primary_len(bundle, dict, primary); block.set_contents(new SerializableByteBuffer(primary_len)); block.set_data_length((int) primary.block_length_value()); block.set_data_offset((int) (primary_len - primary.block_length_value())); /* * Advance buf and decrement len as we go through the process. */ IByteBuffer buf = block.writable_contents(); int len = primary_len; Log.d(TAG, String.format("generating primary: length %s", primary_len)); // Stick the version number in the first byte. buf.put((byte) BundleProtocol.CURRENT_VERSION); len -= 1; len -= write_sdnv(primary.processing_flags(), buf); len -= write_sdnv(primary.block_length(), buf); len -= write_sdnv(primary.dest_scheme_offset(), buf); len -= write_sdnv(primary.dest_ssp_offset(), buf); len -= write_sdnv(primary.source_scheme_offset(), buf); len -= write_sdnv(primary.source_ssp_offset(), buf); len -= write_sdnv(primary.replyto_scheme_offset(), buf); len -= write_sdnv(primary.replyto_ssp_offset(), buf); len -= write_sdnv(primary.custodian_scheme_offset(), buf); len -= write_sdnv(primary.custodian_ssp_offset(), buf); len -= write_sdnv(bundle.creation_ts().seconds(), buf); len -= write_sdnv(bundle.creation_ts().seqno(), buf); len -= write_sdnv(bundle.expiration(), buf); len -= write_sdnv(primary.dictionary_length(), buf); // Add the dictionary. Log.d(TAG, "Current Buf: " + buf.position()); Log.d(TAG, "Dict length: " + dict.dict_length()); Log.d(TAG, "Dict length: " + dict.dict_length()); buf.put(dict.dict()); // memcpy(buf, dict->dict(), dict->length()); // buf += dict->length(); len -= dict.dict_length(); Log.d(TAG, "Preparing len:" + len); /* * If the bundle is a fragment, stuff in SDNVs for the fragment * offset and original length. */ if (bundle.is_fragment()) { len -= write_sdnv(bundle.frag_offset(), buf); Log.d(TAG, "Preparing len:" + len); len -= write_sdnv(bundle.orig_length(), buf); Log.d(TAG, "Preparing len:" + len); } /* * Asuming that get_primary_len is written correctly, len should * now be zero since we initialized it to primary_len at the * beginning of the function. */ buf.position(0); assert (len == 0) : TAG + ": len not ==0"; Log.e(TAG, "Current Len: " + len); }