def run_one_hop(pkt_num): pkt_size = T ff_order = 8 input_data = '' for x in xrange(pkt_num): input_data = input_data + pkt_size * chr(random.randint(0, 255)) # define network's source and destination src = Encoder(M, pkt_num, pkt_size, input_data) dst = Decoder(M, pkt_num, pkt_size) start = time.clock() output_data, c1 = _run_one_hop(src, dst) end = time.clock() # print "output_data's length: ", len(output_data) for i in xrange(pkt_num * pkt_size): if input_data[i] != output_data[i]: print "#"*30 + " memory coding error " + "#"*30 break return end - start, c1.num_sent, c1.num_received, dst.num_inact
def run_two_hop(pkt_num): pkt_size = T ff_order = 8 input_data = '' for x in xrange(pkt_num): input_data = input_data + pkt_size * chr(random.randint(0, 255)) # define network's source and destination src = Encoder(M, pkt_num, pkt_size, input_data) dst = Decoder(M, pkt_num, pkt_size) recoder = Recoder(M, pkt_size) start = time.clock() output_data, c1, c2 = run_network(mode, src, dst, recoder) end = time.clock() #print "output_data's length: ", len(output_data) # for i in xrange(pkt_num * pkt_size): # if input_data[i] != output_data[i]: # print "#"*30 + " memory coding error " + "#"*30 # break return end - start, ((c1.num_sent, c1.num_received), (c2.num_sent, c2.num_received), dst.num_inact)
def test_bats_s(from_file, to_file): def a(): print 'a()' loss_rate = 0.1 r = 0 e = Encoder.fromfile(from_file) d = Decoder.tofile(to_file, os.path.getsize(from_file), a, hashFile(from_file)) while not d.complete(): s = e.genPacket() if s: if random() >= loss_rate: d.receivePacket(s) r += 1 print 'received:%d,decoded:%d'%(r, d.getDecoded())
def test_bats_s(from_file, to_file): def a(): print 'a()' loss_rate = 0.1 r = 0 e = Encoder.fromfile(from_file) d = Decoder.tofile(to_file, os.path.getsize(from_file), a, hashFile(from_file)) while not d.complete(): s = e.genPacket() if s: if random() >= loss_rate: d.receivePacket(s) r += 1 print 'received:%d,decoded:%d' % (r, d.getDecoded())
def test_bats(from_file, to_file): def a(): print 'a()' r = 0 e = Encoder.fromfile(from_file) for i in xrange(10001): e.genPacket() #f = open("pp", "r+b") d = Decoder.tofile(to_file, os.path.getsize(from_file), a, hashFile(from_file)) j = '' ib = 0 h = {} hm = 0 hid = {} hidm = 0 while not d.complete(): s = e.genPacket() #s = f.read(1042) if s == '': continue if s not in h: h[s] = 1 hm = 1 if 1 > hm else hm else: h[s] += 1 hm = h[s] if h[s] > hm else hm i = (ord(s[0]) + ord(s[1]) * 256) if i not in hid: hid[i] = 1 hidm = 1 if 1 > hidm else hidm else: hid[i] += 1 hidm = hid[i] if hid[i] > hidm else hidm if s: j = base64.b64decode(json.loads(json.dumps(base64.b64encode(s)))) d.receivePacket(j) r += 1 ib = i if i > ib else ib print ib, i, hm, hidm print type(j) print 'received:%d,decoded:%d' % (r, d.getDecoded())
def test_bats(from_file, to_file): def a(): print 'a()' r = 0 e = Encoder.fromfile(from_file) for i in xrange(10001): e.genPacket() #f = open("pp", "r+b") d = Decoder.tofile(to_file, os.path.getsize(from_file), a, hashFile(from_file)) j = '' ib = 0 h = {} hm = 0 hid = {} hidm = 0 while not d.complete(): s = e.genPacket() #s = f.read(1042) if s == '': continue if s not in h: h[s] = 1 hm = 1 if 1>hm else hm else: h[s] += 1 hm = h[s] if h[s]>hm else hm i = (ord(s[0])+ord(s[1])*256) if i not in hid: hid[i] = 1 hidm = 1 if 1>hidm else hidm else: hid[i] += 1 hidm = hid[i] if hid[i]>hidm else hidm if s: j = base64.b64decode(json.loads(json.dumps(base64.b64encode(s)))) d.receivePacket(j) r += 1 ib = i if i>ib else ib print ib, i, hm, hidm print type(j) print 'received:%d,decoded:%d'%(r, d.getDecoded())
def main(): global g_decoding_finised enc = Encoder.fromfile(sys.argv[1]) # Create recoder instance rec = Recoder(16, PACKET_SIZE) # channel loss rate r = 0.05 buf = [] while not g_decoding_finised: send = enc.genPacket() # Generate a packet # new batch if send == '': if len(buf) > 0: tmp = ''.join(buf) for i in xrange(0, 16): if random() >= r: resend = rec.genPacket(tmp, len(buf)) dec.receivePacket(resend) del buf[0:len(buf)] # old batch else: #simulate channel if random() >= r: buf.append(send) dec.waitDecodingThread() ret = subprocess.call(['diff', sys.argv[1], 'output']) sys.exit(ret)
import os from random import random import bats from bats import Encoder, Decoder, Recoder PACKET_SIZE = bats.PACKET_SIZE if __name__ == "__main__": import sys if len(sys.argv) == 1: input_file = 'input' else: input_file = sys.argv[1] enc = Encoder.fromfile(input_file) # Create decoder instance given destination filename and filesize. dec = Decoder.tofile('output', os.path.getsize(input_file)) # Create recoder instance rec = Recoder(16, PACKET_SIZE) Rcnt = 1 Scnt = 1 # channel loss rate r = 0.05 import time t0 = time.time()
import subprocess def callback(): #ret = subprocess.call(['diff', sys.argv[1], 'output']) #sys.exit(ret) pass def cal_hash(f, block_size): while True: data = f.read(block_size) if not data: break hashlib.sha256().update(data) return hashlib.sha256().hexdigest() enc = Encoder.fromfile(sys.argv[1]) myfile = open(sys.argv[1], 'rb') dec = NIODecoder.tofile('output', os.path.getsize(sys.argv[1]), callback, cal_hash(myfile, 512*1024)) myfile.close() # Create recoder instance rec = Recoder(16, 1024) Rcnt = 1 Scnt = 1 # channel loss rate r = 0.05
def callback(): #ret = subprocess.call(['diff', sys.argv[1], 'output']) #sys.exit(ret) pass def cal_hash(f, block_size): while True: data = f.read(block_size) if not data: break hashlib.sha256().update(data) return hashlib.sha256().hexdigest() enc = Encoder.fromfile(sys.argv[1]) myfile = open(sys.argv[1], 'rb') dec = NIODecoder.tofile('output', os.path.getsize(sys.argv[1]), callback, cal_hash(myfile, 512 * 1024)) myfile.close() # Create recoder instance rec = Recoder(16, 1024) Rcnt = 1 Scnt = 1 # channel loss rate r = 0.05