-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.py
224 lines (170 loc) · 7.46 KB
/
producer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import sys
import pyndn
import logging
import datetime
import sys, os, os.path
from mydata import MyData, ChunkInfo, log
from mydata import DEFAULT_BLOCK_SIZE, DEFAULT_RUN_TIMEOUT
from mydata import ADAPTIVE_MOD_FLAG
import signal
PAPER="optimal-chunk-size"
ITEM = "producer"
class Producer(pyndn.Closure):
def __init__(self, name, mydata, content_path=None, **kwargs):
self.name = pyndn.Name(name) #not include segment number and version number
self.handle = pyndn.NDN()
self.exclusions = pyndn.ExclusionFilter()
self.mod = kwargs.get("mod", "non-adatpive")
self.default_block_size = kwargs.get("default_block_size", DEFAULT_BLOCK_SIZE)
self.mydata = mydata
s = content_path
if s == None:
s = os.path.basename(name)
if os.path.exists(s):
log.info("file %s will be published with name %s " %(s, name))
else:
log.error("%s is not exist under %s directory" %(s, "current" if content_path==None else "the"))
exit(0)
self.file_in = open(s, "rb")
self.file_size = os.path.getsize(s)
def start(self):
self.handle.setInterestFilter(self.name, self)
self.handle.run(-1)
def prepare(self, upcallInfo):
ist = upcallInfo.Interest
ist_name = ist.name
mod = "non-adaptive"
expected_block_size = self.default_block_size
chunkid = int(ist_name[-1])
begin_byte = chunkid * self.default_block_size
for i in range(len(ist_name)):
sub = ist_name[i]
if sub == ADAPTIVE_MOD_FLAG:
if self.mod != "adaptive":
log.error("Interest ask for adptive mod while server does not support")
#return None
mod = "adaptive"
assert i+1 < len(ist_name), "bad name %s" %(ist_name)
if self.mod == "adaptive":
expected_block_size = int(ist_name[i+1])
begin_byte = chunkid
break
if begin_byte > self.file_size:
log.warn("already reach the final block: begin_byte: %s, file_size: %s" %(begin_byte, self.file_size))
exit(1)
self.file_in.seek(begin_byte)
data = self.file_in.read(expected_block_size)
log.debug("Interest: %s, mod: %s, expected_block_size: %s, begin_byte: %s, chunk_size: %s" \
%(ist.name, mod, expected_block_size, begin_byte, len(data)))
final_block_id = None
if self.file_in.tell() == self.file_size:
final_block_id = chunkid
log.debug("data is %s"%(data))
# create a new data packet
co = pyndn.ContentObject()
# since they want us to use versions and segments append those to our name
#co.name = self.name.appendVersion().appendSegment(0)
co.name = ist_name
# place the content
co.content = data
si = co.signedInfo
key = self.handle.getDefaultKey()
# key used to sign data (required by ndnx)
si.publisherPublicKeyDigest = key.publicKeyID
# how to obtain the key (required by ndn); here we attach the
# key to the data (not too secure), we could also provide name
# of the key under which it is stored in DER format
si.freshnessSeconds = 0
si.keyLocator = pyndn.KeyLocator(key)
# data type (not needed, since DATA is the default)
si.type = pyndn.CONTENT_DATA
# number of the last segment (0 - i.e. this is the only
# segment)
if final_block_id != None:
si.finalBlockID = pyndn.Name.num2seg(final_block_id)
# signing the packet
co.sign(key)
chunkinfo = ChunkInfo(chunkid)
chunkinfo.begin_byte = begin_byte
chunkinfo.content_size = len(data)
chunkinfo.expected_block_size = expected_block_size
chunkinfo.retxN = 1
self.mydata.add_chunk_info(chunkinfo)
return co
def upcall(self, kind, upcallInfo):
if kind != pyndn.UPCALL_INTEREST:
log.warn("get kind: %s" %str(kind))
return pyndn.RESULT_OK
co = self.prepare(upcallInfo)
self.handle.put(co)
log.info("content: %s" %co)
return pyndn.RESULT_INTEREST_CONSUMED
def stop(self):
#self.mydata.list_info()
self.handle.setRunTimeout(DEFAULT_RUN_TIMEOUT/100)
return
mydata = MyData()
def signal_handler(signal, frame):
print 'You pressed Ctrl+C!'
mydata.list_info()
sys.exit(0)
def usage():
s = '''
python consumer <NAME> [INTEREST_SCHEMA:-i|s|e] [MOD:-a|-na] [DEFAULT_BLOCK_SIZE: -b DEFAULT_BLOCK_SIZE]
e.g.: python consumer.py /swordguy/chunksize/a -i -na -b 4096
------------------------------------------------------------
Usage:
-h: show help information
Name: make sure NAME is routable to the producer
Interest Schema: default -i
-i: use id directly in the Interest, e.g.: <name>/0, <name>/0
-e: use exclusion in the Interest, e.g.: <name> with exclusion <0,1,2>
-s: use segment in the Interest, e.g.: <name>/%FD00
Mod: default -na
-a: adapt expected optimal block size according to loss rate
-na: non-adaptive, use static block size
DEFAULT_BLOCK_SIZE: default 4096
-b DEFAULT_BLOCK_SIZE: set the default block size to DEFAULT_BLOCK_SIZE
-----------------------------------------------------------------
'''
print s
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1].startswith("-"):
usage()
exit(0)
signal.signal(signal.SIGINT, signal_handler)
name = sys.argv[1]
content_path = None
begin_index = 2
if len(sys.argv) >=3 and (not sys.argv[2].startswith("-")):
content_path = sys.argv[2]
begin_index += 1
interest_schema = "id"
mod = "adaptive"
default_block_size = DEFAULT_BLOCK_SIZE
for i in range(begin_index, len(sys.argv)):
av = sys.argv[i]
if av == "-e":#use exclusion
interest_schema = "exclusion"
elif av == "-s": #use segment
interest_schema = "segment"
elif av == "-i":#use id directly in the name
interest_schema = "id"
elif av == "-a": #adaptive mode, which adapt chunk size according to the packet loss rate
mod = "adaptive"
elif av == "-na": #adaptive mode, which adapt chunk size according to the packet loss rate
mod = "non-adaptive"
elif av == "-b": #block size
assert i <= len(sys.argv), "-b should be followed by chunk size"
i += 1
default_block_size = int(sys.argv[i])
elif av == "-h" or av == "--help":
usage()
#exit(0)
#global mydata
#mydata = MyData()
consumer = Producer(name=name, mydata=mydata, content_path=content_path,\
mod=mod, default_block_size=default_block_size)
consumer.start()
mydata.list_info(enable_chunks=True)
log.info("program %s:%s end" %(PAPER, ITEM))