blob: fb2d25300296a2d39263c561f613731a87ea3ff0 [file] [log] [blame]
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07001## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2#
3# Copyright (c) 2011-2013, Regents of the University of California
4# Alexander Afanasyev
5#
6# GNU 3.0 license, See the LICENSE file for more information
7#
8# Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9#
10
11#
12# Based on PyCCN code, copyrighted and licensed as follows
13#
14# Copyright (c) 2011-2013, Regents of the University of California
15# BSD license, See the COPYING file for more information
16# Written by: Derek Kulinski <takeda@takeda.tk>
17# Jeff Burke <jburke@ucla.edu>
18#
19
20import math
21import ndn
22
23class Wrapper(object):
24 def __init__(self, name, key):
25 self.name = name
26 self.key = key
27
28 kl = ndn.KeyLocator(key)
29 self.signed_info = ndn.SignedInfo(key_locator = kl, key_digest = key.publicKeyID)
30
31 def __call__(self, chunk, segment, segments):
32 name = self.name + ndn.Name.num2seg(segment)
33 self.signed_info.finalBlockID = ndn.Name.num2seg(segments - 1)
34
35 co = ndn.ContentObject(name = name, content = chunk, signed_info = self.signed_info)
36 co.sign(self.key)
37
38 return co
39
40def segmenter(data, wrapper = None, chunk_size = 4096):
41 segment = 0
42 segments = math.ceil(len(data) / float(chunk_size))
43
44 while segment < segments:
45 start = segment * chunk_size
46 end = min(start + chunk_size, len(data))
47 chunk = data[start : end]
48
49 if wrapper is not None:
50 chunk = wrapper(chunk, segment, segments)
51
52 yield chunk
53
54 segment += 1
55