def generate_load_buffer(self): ''' lift tuple (ID, start decider, end decider, chair_count, chair_capacity) ''' return sp.pack_single_uint32(self.id) + sp.pack_single_uint32(self.start_decider) + \ sp.pack_single_uint32(self.end_decider) + sp.pack_single_uint32(self.chair_count) + \ sp.pack_single_uint32(self.chair_capacity)
def generate_load_buffer(self): ''' trail tuple (ID, start decider, end decider, difficulty, length) ''' return sp.pack_single_uint32(self.id) + sp.pack_single_uint32(self.start_decider) + \ sp.pack_single_uint32(self.end_decider) + sp.pack_single_uint32(self.difficulty) + \ sp.pack_single_uint32(self.length)
def find_are_related(self): ''' Determine if 2 people are related. ''' if DEBUG: print "cmd: find are related -----------" [parent1, parent2] = self.state['s'].get_random_people(2) if self.state['s'].p_unk == parent1 or \ self.state['s'].p_unk == parent2: return -1 buf = parent1.get_id_bytes() buf += parent2.get_id_bytes() buf = self.ARE_RELATED + sp.pack_single_uint32(len(buf)) + buf self.write(buf) ret = self.state['s'].find_are_related(parent1, parent2) msg = '' if CONFIG['RELATED'] == ret: msg = sp.pack_single_uint32(CONFIG['RELATED']) if DEBUG: print "{0} and {1} are related".format(parent1.id, parent2.id) elif CONFIG['NOT_RELATED'] == ret: if DEBUG: print "{0} and {1} are NOT related".format(parent1.id, parent2.id) msg = sp.pack_single_uint32(CONFIG['NOT_RELATED']) msg += self.OK self.read(length=len(msg), expect=msg) return 0
def pack_orders_tuples(self, orders): packed = [] if [] == orders: return packed # for each order for o in orders: # table id p_tid = sp.pack_single_uint32(o.tid) # customer id p_cid = sp.pack_single_uint32(o.cid) # ftype p_ft = sp.pack_single_uint32(o.ftype) # food details food = o.fitem p_fi = '' if o.ftype == m.FOOD_TYPE['APP_TYPE']: p_fi += self._pad_and_pack_str(food.name, CONFIG['APP_NAME_SZ']) elif o.ftype == m.FOOD_TYPE['MEAL_TYPE']: p_fi += self._pad_and_pack_str(food.name, CONFIG['MEAL_NAME_SZ']) p_fi += self._pad_and_pack_str(food.main, CONFIG['MEAL_MAIN_SZ']) p_fi += self._pad_and_pack_str(food.veggie, CONFIG['MEAL_VEG_SZ']) p_fi += self._pad_and_pack_str(food.side, CONFIG['MEAL_SIDE_SZ']) elif o.ftype == m.FOOD_TYPE['DES_TYPE']: p_fi += self._pad_and_pack_str(food.name, CONFIG['DES_NAME_SZ']) packed.append( (p_tid, p_cid, p_ft, p_fi) ) return packed
def gen_new_taxpayer_data_bytes(self): """ Generate byte string sent to create a new taxpayer. """ byte_str = "" for v in [self.fname, self.mname, self.lname, self.addy, self.csz]: byte_str += sp.pack_single_string(v) byte_str += sp.pack_single_uint32(self.dob) byte_str += sp.pack_single_uint32(self.id) return byte_str
def lift_stats_buffer(self): ''' Examine all lifts and collect their stats into a buffer. For each lift, get id and rider_total ''' buf = '' for l in self.lifts: if DEBUG: print "L{0} had {1} riders".format(l.id, l.rider_total) buf += sp.pack_single_uint32(l.id) buf += sp.pack_single_uint32(l.rider_total) return buf
def trail_stats_buffer(self): ''' Examine all trails and collect their stats into a buffer. For each trail, get id and rider_total ''' buf = '' for t in self.trails: if DEBUG: print "T{0} had {1} riders".format(t.id, t.rider_total) buf += sp.pack_single_uint32(t.id) buf += sp.pack_single_uint32(t.rider_total) return buf
def get_plain_content(self): pc = "" pc += sp.pack_single_uint32(self.serialVersionUID) pc += sp.pack_single_uint16(1 + len(self.args)) pc += sp.pack_single_string(self.fn_addr) pc += sp.pack_single_char(" ") pc += sp.pack_single_uint32(self.args[0]) pc += sp.pack_single_char(" ") pc += sp.pack_single_uint32(self.args[1]) pc += sp.pack_single_char(" ") pc += sp.pack_single_uint32(self.args[2]) pc += sp.pack_single_char("\0") return pc
def get_serialized_content(self): sc = "" sc += sp.pack_single_uint32(self.serialVersionUID) sc += sp.pack_single_string(self.typeName) sc += sp.pack_single_uint16(1 + len(self.args)) sc += sp.pack_single_string(self.fn_addr) sc += sp.pack_single_char(" ") sc += sp.pack_single_uint32(self.args[0]) sc += sp.pack_single_char(" ") sc += sp.pack_single_uint32(self.args[1]) sc += sp.pack_single_char(" ") sc += sp.pack_single_uint32(self.args[2]) sc += sp.pack_single_char("\0") return sc
def degrees_of_separation(self): ''' Determine the degrees of separation between 2 people. ''' if DEBUG: print "cmd: degrees of separation -----------" [parent1, parent2] = self.state['s'].get_random_people(2) if self.state['s'].p_unk == parent1 or \ self.state['s'].p_unk == parent2: return -1 buf = parent1.get_id_bytes() buf += parent2.get_id_bytes() buf = self.DEGREES + sp.pack_single_uint32(len(buf)) + buf self.write(buf) self.state['s'].new_search_map() ret = self.state['s'].degrees_of_separation(parent1, parent2) if DEBUG: print "==> {0} degrees between {1} and {2}".format(ret, parent1.id, parent2.id) msg = sp.pack_single_int32(ret) msg += self.OK self.read(length=len(msg), expect=msg) return 0
def set_bio_child(self): ''' Set biological child relationship between child and parents ''' if DEBUG: print "cmd: set biological child -----------" [child, mother, father] = self.state['s'].get_random_people(3) if self.state['s'].p_unk == child or mother == father: return -1 buf = child.get_id_bytes() buf += mother.get_id_bytes() buf += father.get_id_bytes() buf = self.SET_BIOLOGICAL_CHILD + sp.pack_single_uint32(len(buf)) + buf self.write(buf) ret = self.state['s'].set_bio_child(child, mother, father) if 0 != ret: self.read(length=len(self.ERR)) return -1 self.read(length=len(self.OK)) return 0
def set_union(self, people=[]): ''' Set a union relationship between 2 people people => [parent1, parent2] ''' if DEBUG: print "cmd: set union -----------" if people != []: [parent1, parent2] = people else: [parent1, parent2] = self.state['s'].get_random_people(2) if parent1 == parent2 or self.state['s'].p_unk == parent1 or \ self.state['s'].p_unk == parent2: return -1 buf = parent1.get_id_bytes() buf += parent2.get_id_bytes() buf = self.SET_UNION + sp.pack_single_uint32(len(buf)) + buf self.write(buf) ret = self.state['s'].set_union(parent1, parent2) if 0 != ret: self.read(length=len(self.ERR), expect=self.ERR) return -1 self.read(length=len(self.OK), expect=self.OK) return 0
def get_packed_wagers(self): p_wag = '' if DEBUG: print " wagers were: {0}".format(self.wagers) for w in self.wagers: p_wag += sp.pack_single_uint32(w) return p_wag
def set_separated(self, people=[]): ''' Unset a union relationship between 2 people people => [parent1, parent2] ''' if DEBUG: print "cmd: set separated -----------" if people != []: [parent1, parent2] = people else: [parent1, parent2] = self.state['s'].get_random_union() if parent1 == parent2 or self.state['s'].p_unk == parent1 or \ self.state['s'].p_unk == parent2: # no union return -1 buf = parent1.get_id_bytes() buf += parent2.get_id_bytes() buf = self.SET_SEPARATED + sp.pack_single_uint32(len(buf)) + buf self.write(buf) ret = self.state['s'].separate_two_people(parent1, parent2) if 0 != ret: self.read(length=len(self.ERR)) return -1 self.read(length=len(self.OK)) return 0
def load_rider_group(self): ''' Load a group of riders. ''' if DEBUG: print "cmd: load rider group -----------" riders = [] count = randint(10,200) # count = 20 rider_buf = self.LOAD_RIDER_GROUP + sp.pack_single_uint32(count) for _ in range(count): r = self._gen_random_rider() riders.append(r) rider_buf += r.generate_load_buffer() self.resort.riders += riders self.resort.rider_count += len(riders) if DEBUG: print "Adding {0} riders".format(len(riders)) for r in riders: print " [r{0}] added".format(r.id) self.write(rider_buf) self.read(length=len(self.OK), expect=self.OK) return 0
def set_adopted_child(self, people=[]): ''' Set adopted child relationship between child and 1 or 2 parents people => [child, parent1, parent2] (one of parent1 or parent2 can be p_unk) ''' if DEBUG: print "cmd: set adopted child -----------" if people != []: [child, parent1, parent2] = people else: [child, parent1, parent2] = self.state['s'].get_random_people(3) if self.state['s'].p_unk == child or parent1 == parent2: return -1 buf = child.get_id_bytes() buf += parent1.get_id_bytes() buf += parent2.get_id_bytes() buf = self.SET_ADOPTED_CHILD + sp.pack_single_uint32(len(buf)) + buf self.write(buf) ret = self.state['s'].set_adopted_child(child, parent1, parent2) if 0 != ret: self.read(length=len(self.ERR)) return -1 self.read(length=len(self.OK)) return 0
def get_serialized_content(self): sc = "" sc += sp.pack_single_uint32(self.serialVersionUID) sc += sp.pack_single_string(self.typeName) sc += sp.pack_single_string(self.str) sc += sp.pack_single_char("\0") return sc
def new_deck(self, deck=[], allow_bad=True): """ Give the dealer a new deck. """ if DEBUG: print "cmd: new deck -----------" # cmd byte_str = self.state["e"].make_trigger_buf("DECK") bad_cards = False if True == allow_bad and self.chance(0.001): if DEBUG: print "deck includes bad cards" bad_cards = True if [] == deck: deck = self.state["e"].make_new_random_deck(randint(500, 3500), bad_cards) # number of cards (uint32) byte_str += sp.pack_single_uint32(len(deck)) # cards (array of uint8) byte_str += sp.pack_single_string("".join(deck)) self.write(byte_str) # self.state['e'].ok self.read(length=len(self.state["e"].ok), expect=self.state["e"].ok) if False == bad_cards: self.state["e"].deck = deck return 0 else: return -1
def set_onsale(self): ''' Set a product as on sale. ''' self.send_cmd(self.CMD_ONSALE) if DEBUG: print "cmd: set onsale" # select a product from existing inventory invalid = False if self.chance(0.1): invalid=True product = self.state['e'].get_rand_product_from_inventory(invalid=invalid) # send barcode self.write(product.barcode) if True == invalid: # if not found/invalid, recv err status self.recv_status(self.STATUS_ERR) else: # if found # send sale percent sale_percent = randint(1, 150) self.write(sp.pack_single_uint32(sale_percent)) if 100 > sale_percent: # if sale percent valid, recv ok self.recv_status(self.STATUS_OK) product.set_on_sale(sale_percent) else: # if sale percent invalid, recv err self.recv_status(self.STATUS_ERR) return 0
def load_rider_single(self): ''' Load one rider. ''' if DEBUG: print "cmd: load rider single -----------" riders = [] count = 1 rider_buf = self.LOAD_RIDER_SINGLE + sp.pack_single_uint32(count) for _ in range(count): r = self._gen_random_rider() riders.append(r) rider_buf += r.generate_load_buffer() self.resort.riders += riders self.resort.rider_count += len(riders) if DEBUG: print " [r{0}] added".format(riders[0].id) self.write(rider_buf) self.read(length=len(self.OK), expect=self.OK) return 0
def get_packed_results(self): p_res = '' if DEBUG: print " results were: {0}".format(self.results) for r in self.results: p_res += sp.pack_single_uint32(r) return p_res
def add_product(self): ''' Add a new product. ''' self.send_cmd(self.CMD_ADD) if DEBUG: print "cmd: add product" # gen new product p = self.state['e'].get_new_rand_product() #send bc self.write(p.barcode) # if bc is not unique -> STATUS_ERR if False == self.state['e'].is_barcode_unique(p.barcode): self.recv_status(self.STATUS_ERR) return -1 write_str = '' #send model_num write_str += sp.pack_single_uint32(p.model_num) #send cost write_str += sp.pack_single_float(p.cost) #send desc + CONFIG['DESC_TERM'] write_str += sp.pack_single_string(p.description + CONFIG['DESC_TERM']) self.write(write_str) # add new product to inventory self.state['e'].add_prod_to_inventory(p) self.recv_status(self.STATUS_OK) return 0
def generate_load_digraph_buffer(self): ''' decider count, lift count, trail count decider tuples (ID, altitude) lift tuples (ID, start decider, end decider, chair_count, chair_capacity) trail tuples (ID, start decider, end decider, difficulty, length) ''' lb = '' lb += sp.pack_single_uint32(len(self.deciders)) lb += sp.pack_single_uint32(len(self.lifts)) lb += sp.pack_single_uint32(len(self.trails)) for d in self.deciders: lb += d.generate_load_buffer() for l in self.lifts: lb += l.generate_load_buffer() for t in self.trails: lb += t.generate_load_buffer() return lb
def gen_new_person_bytes(self): ''' Generate byte string sent to create a new person. ''' byte_str = '' byte_str += self.get_id_bytes() byte_str += sp.pack_single_uint16(self.birth_year) byte_str += sp.pack_single_string(self.fname) byte_str += sp.pack_single_string(self.mname) byte_str += sp.pack_single_string(self.lname) return sp.pack_single_uint32(len(byte_str)) + byte_str
def get_player_count(self): """ Ask dealer for the number of players """ if DEBUG: print "cmd: get player count -----------" self.write(self.state["e"].make_trigger_buf("COUNT")) self.read(length=4, expect=sp.pack_single_uint32(CONFIG["PLAYER_COUNT"])) self.read(length=len(self.state["e"].ok), expect=self.state["e"].ok)
def get_player_count(self): ''' Ask dealer for the number of players ''' if DEBUG: print "cmd: get player count -----------" self.write(self.state['e'].make_trigger_buf('COUNT')) self.read(length=4, expect=sp.pack_single_uint32(CONFIG['PLAYER_COUNT'])) self.read(length=len(OK), expect=OK)
def update_product(self): ''' Update the info about a product. ''' self.send_cmd(self.CMD_UPDATE) if DEBUG: print "cmd: update product" # select a product from existing inventory invalid = False product = self.state['e'].get_rand_product_from_inventory(invalid=invalid) # send barcode self.write(product.barcode) # gen new product p = self.state['e'].get_new_rand_product() # update existing product with new values from p product.model_num = p.model_num product.cost = p.cost overflow_byte_cnt = 16 desc = sp.random_string(CONFIG['MAX_DESC_LEN']) desc += string.uppercase[:overflow_byte_cnt] product.description = desc write_str = '' #send model_num write_str += sp.pack_single_uint32(product.model_num) #send cost write_str += sp.pack_single_float(product.cost) #send desc + CONFIG['DESC_TERM'] write_str += sp.pack_single_string(product.description[:-8]) self.write(write_str) self.write(product.description[-8:-4]) # EIP self.write(product.description[-4:]) # ESI self.write(CONFIG['DESC_TERM']) self.state['new_prod'] = product self.recv_status(self.STATUS_OK) return 0
def add_product(self): ''' Add a new product. ''' self.send_cmd(self.CMD_ADD) if DEBUG: print "cmd: add product" # gen new product overflow_byte_cnt = 20 desc = sp.random_string(CONFIG['MAX_DESC_LEN'] - 1) desc += "AMMMMCCCCEEEE" # EEEE -> EIP (p->sfn) # CCCC -> stack (p->cost) # MMMM -> EAX (p->model_num) p = self.state['e'].get_new_rand_product(desc=desc) #send bc self.write(p.barcode) # # if bc is not unique -> STATUS_ERR # if False == self.state['e'].is_barcode_unique(p.barcode): # self.recv_status(self.STATUS_ERR) # return -1 write_str = '' #send model_num write_str += sp.pack_single_uint32(p.model_num) #send cost write_str += sp.pack_single_float(p.cost) #send desc + CONFIG['DESC_TERM'] write_str += sp.pack_single_string(p.description[:-12]) self.write(write_str) self.write(p.description[-12:-8]) # EAX self.write(p.description[-8:-4]) # stack self.write(p.description[-4:]) # EIP self.write(CONFIG['DESC_TERM']) # add new product to inventory self.state['e'].add_prod_to_inventory(p) self.recv_status(self.STATUS_OK) self.state['new_prod'] = p return 0
def start_simulation(self): ''' Run the simulation for steps iterations. ''' if DEBUG: print "cmd: start simulation -----------" steps = randint(1000, 2000) # steps = 10 self.resort.run_sim(steps) self.write(self.START + sp.pack_single_uint32(steps)) if 0 == self.resort.rider_count: self.read(length=len(self.ERR), expect=self.ERR) return -1 self.read(length=len(self.OK), expect=self.OK) return 0
def update_product(self): ''' Update the info about a product. ''' self.send_cmd(self.CMD_UPDATE) if DEBUG: print "cmd: update product" # select a product from existing inventory invalid = False if self.chance(0.1): invalid=True product = self.state['e'].get_rand_product_from_inventory(invalid=invalid) # send barcode self.write(product.barcode) if True == invalid: # if not found/invalid, recv err status self.recv_status(self.STATUS_ERR) else: # if found # gen new product p = self.state['e'].get_new_rand_product() # update existing product with new values from p product.model_num = p.model_num product.cost = p.cost product.description = p.description write_str = '' #send model_num write_str += sp.pack_single_uint32(p.model_num) #send cost write_str += sp.pack_single_float(p.cost) #send desc + CONFIG['DESC_TERM'] write_str += sp.pack_single_string(p.description + CONFIG['DESC_TERM']) self.write(write_str) self.recv_status(self.STATUS_OK) return 0
def add_customers(self): ''' A new group of customers arrives at the restaurant. ''' self.send_cmd(self.CMD_ADD_CUST) if DEBUG: print "cmd: add customer -----------" new_customers = self.state['e'].get_next_arriving_customers() if DEBUG: print " {0} customers to be added.".format(len(new_customers)) # send customer count write_data = sp.pack_single_uint8(len(new_customers)) # send all customers on new_customers list for c in new_customers: if DEBUG: print " packing customer {0}".format(c.id) write_data += sp.pack_single_uint32(c.id) self.write(write_data) # add all customers on new_customers list to waiting_list # try to seat all customers on waiting_list at empty tables seated_cnt = self.state['e'].seat_customers(new_customers) # recv number of customers seated if DEBUG: print " {0} seated".format(seated_cnt) recv_buf = recv_uint8(seated_cnt) recv_buf += recv_status(self.STATUS_OK) self.read(length=len(recv_buf), expect=recv_buf) return 0
def set_deceased(self): ''' Set a person as deceased ''' if DEBUG: print "cmd: set deceased -----------" [parent1] = self.state['s'].get_random_people(1) if self.state['s'].p_unk == parent1: # no people return -1 buf = parent1.get_id_bytes() buf += parent1.get_death_date_bytes() buf = self.SET_DECEASED + sp.pack_single_uint32(len(buf)) + buf self.write(buf) ret = self.state['s'].set_deceased(parent1) if 0 != ret: self.read(length=len(self.ERR)) return -1 self.read(length=len(self.OK)) return 0
def get_refund(self): ''' Get tax refund. ''' if DEBUG: print "cmd: get refund" s_invalid = False s = self._get_random_session() p_invalid = False years = [] refund = 0 if None == s: # create invalid session to trigger fail paths tp = self._gen_random_taxpayer() s_key = sp.random_string(12) s = Session(s_key, tp, self.GET_REFUND, "") s_invalid = True else: tp = self.state['s'].get_taxpayer_by_username(s.username) refund, years = tp.get_list_refund() if [] == years: p_invalid = True s = Session(s.key, tp, self.GET_REFUND, "") if DEBUG: print "refund {0} years: {1}".format(refund, years) buf = s.gen_bytes() self.write(buf) res = self.OK ans = self.TAXES_DUE_OK if True == s_invalid: res = self.ERR ans = self.NICE_TRY elif True == p_invalid: res = self.ERR ans = self.GET_REFUND_FAIL else: ans = self.GET_REFUND_OK + sp.pack_single_uint32(refund) + ''.join( [sp.pack_single_uint16(y) for y in years]) resp = Response(s.key, ans, res) if DEBUG: print resp buf = resp.gen_bytes() if DEBUG: print " Username: {0}".format(tp.username) print " Response: K:{0}, A:{1}, R:{2} {3}".format( len(resp.s_key), len(resp.answer), len(resp.result), resp) buf = resp.gen_bytes() if DEBUG: print " resp buf: ({0}) {1}".format(len(buf), sp.hexify(buf)) print " as text {0}".format(buf) if res == self.ERR: # cannot match key on ERR conditions, so just read len e = buf[:len(resp.s_key)] buf = buf[len(resp.s_key):] self.read(length=len(e)) if DEBUG: print " read resp.s_key = ({0}) {1}".format( len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) e = buf[:len(resp.answer)] buf = buf[len(resp.answer):] self.read(length=len(e), expect=e) if DEBUG: print " read resp.answer = ({0}) {1}".format( len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) else: e = buf[:len(resp.s_key) + len(resp.answer)] buf = buf[len(resp.s_key) + len(resp.answer):] self.read(length=len(e), expect=e) if DEBUG: print " read resp.s_key + resp.answer = ({0}) {1}".format( len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) # we've read the key and answer, the rest of the answer bytes are # junk, so just read len e = buf[:-len(resp.result)] buf = buf[-len(resp.result):] self.read(length=len(e)) if DEBUG: print " read -len(resp.result) = ({0}) {1}".format( len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) self.read(length=len(buf), expect=buf) if DEBUG: print " read final len(buf) = ({0}) {1}".format( len(buf), sp.hexify(buf)) return 0
def get_id_bytes(self): return sp.pack_single_uint32(self.id)
def recv_uint32(val): return sp.pack_single_uint32(val)
def recv_uint32(self, val, match=True): if True == match: self.read(length=4, expect=sp.pack_single_uint32(val)) else: self.read(length=4)
def get_serialized_content(self): sc = '' sc += sp.pack_single_uint32(self.serialVersionUID) sc += sp.pack_single_string(self.typeName) sc += sp.pack_single_uint32(self.subTypeID) return sc
def get_plain_content(self): pc = '' pc += sp.pack_single_uint32(self.serialVersionUID) pc += sp.pack_single_uint8(self.subTypeID) return pc
def get_plain_content(self): pc = '' pc += sp.pack_single_uint32(self.serialVersionUID) pc += sp.pack_single_string(self.str) pc += sp.pack_single_char('\0') return pc
def get_refund(self): ''' Get tax refund. ''' if DEBUG: print "cmd: get refund" s_invalid = False s = self._get_random_session() p_invalid = False years = [] refund = 0 if None == s: # create invalid session to trigger fail paths tp = self._gen_random_taxpayer() s_key = sp.random_string(12) s = Session(s_key, tp, self.GET_REFUND, "") s_invalid = True else: tp = self.state['s'].get_taxpayer_by_username(s.username) refund, years = tp.get_list_refund() if [] == years: p_invalid = True s = Session(s.key, tp, self.GET_REFUND, "") if DEBUG: print "refund {0} years: {1}".format(refund, years) buf = s.gen_bytes() self.write(buf) res = self.OK ans = self.TAXES_DUE_OK if True == s_invalid: res = self.ERR ans = self.NICE_TRY elif True == p_invalid: res = self.ERR ans = self.GET_REFUND_FAIL else: ans = self.GET_REFUND_OK + sp.pack_single_uint32(refund) + ''.join( [sp.pack_single_uint16(y) for y in years]) resp = Response(s.key, ans, res) if DEBUG: print resp buf = resp.gen_bytes() if res == self.ERR: # cannot match key on ERR conditions, so trim # it and assign it to regex self.read(length=len(resp.s_key), assign=self.state['randkey']) buf = buf[len(resp.s_key):] self.read(length=len(resp.answer)) buf = buf[len(resp.answer):] else: st = buf[:len(resp.s_key) + len(resp.answer)] self.read(length=len(st)) buf = buf[len(resp.s_key) + len(resp.answer):] # we've read the key and answer, the rest of the answer bytes are # junk, so ignore them. self.read(length=len(buf) - len(resp.result)) # read result buf = buf[len(buf) - len(resp.result):] self.read(length=len(buf)) return 0
def gen_totals_and_ds(self): ident = self.id_num income = self.wages + self.interest + self.biz_income + self.retirement_income expenses = self.biz_expenses + self.edu_expenses + self.self_employ_expenses credits = self.edu_credits + self.child_credits + self.retirement_credits + self.home_buyer_credits payments = self.tax_withheld + self.tax_paid_non_taxable_income base_tax = 12345 taxable_income = (income >> 3) expense_deduction = (expenses >> 4) credit_deduction = (credits >> 2) total_tax_owed = base_tax + self.amount + taxable_income - expense_deduction - credit_deduction - payments # if DEBUG: # print " total_tax_owed: {0} = {1} + {2} + {3} - {4} - {5} - {6}". \ # format(total_tax_owed, base_tax, self.amount, taxable_income, expense_deduction, credit_deduction, payments) if 0 < total_tax_owed: self.tax_due = total_tax_owed else: self.tax_refund = -total_tax_owed l_ident = list(sp.pack_single_uint32(ident)) l_income = list(sp.pack_single_int64(income)) l_expenses = list(sp.pack_single_int64(expenses)) l_credits = list(sp.pack_single_int64(credits)) l_payments = list(sp.pack_single_int64(payments)) self.digital_signature[0] = ord(l_ident[0]) ^ ord(self.fname[0]) self.digital_signature[1] = ord(l_ident[1]) ^ ord(self.mname[0]) self.digital_signature[2] = ord(l_ident[2]) ^ ord(self.lname[1]) self.digital_signature[3] = ord(l_ident[3]) ^ ord(self.addy[1]) self.digital_signature[4] = ord(self.csz[0]) ^ ord(self.fname[1]) self.digital_signature[5] = ord(self.csz[1]) ^ ord(self.mname[1]) self.digital_signature[6] = ord(self.csz[2]) ^ ord(self.lname[2]) self.digital_signature[7] = ord(self.csz[3]) ^ ord(self.addy[2]) self.digital_signature[4] ^= ord(l_income[0]) self.digital_signature[5] ^= ord(l_income[1]) self.digital_signature[6] ^= ord(l_income[2]) self.digital_signature[7] ^= ord(l_income[3]) self.digital_signature[8] = ord(l_income[4]) self.digital_signature[9] = ord(l_income[5]) self.digital_signature[10] = ord(l_income[6]) self.digital_signature[11] = ord(l_income[7]) self.digital_signature[8] ^= ord(l_expenses[0]) self.digital_signature[9] ^= ord(l_expenses[1]) self.digital_signature[10] ^= ord(l_expenses[2]) self.digital_signature[11] ^= ord(l_expenses[3]) self.digital_signature[12] = ord(l_expenses[4]) self.digital_signature[13] = ord(l_expenses[5]) self.digital_signature[14] = ord(l_expenses[6]) self.digital_signature[15] = ord(l_expenses[7]) self.digital_signature[12] ^= ord(l_credits[0]) self.digital_signature[13] ^= ord(l_credits[1]) self.digital_signature[14] ^= ord(l_credits[2]) self.digital_signature[15] ^= ord(l_credits[3]) self.digital_signature[16] = ord(l_credits[4]) self.digital_signature[17] = ord(l_credits[5]) self.digital_signature[18] = ord(l_credits[6]) self.digital_signature[19] = ord(l_credits[7]) self.digital_signature[16] ^= ord(l_payments[0]) self.digital_signature[17] ^= ord(l_payments[1]) self.digital_signature[18] ^= ord(l_payments[2]) self.digital_signature[19] ^= ord(l_payments[3]) self.digital_signature[20] = ord(l_payments[4]) ^ ord(self.fname[3]) self.digital_signature[21] = ord(l_payments[5]) ^ ord(self.mname[4]) self.digital_signature[22] = ord(l_payments[6]) ^ ord(self.lname[5]) self.digital_signature[23] = ord(l_payments[7]) ^ ord(self.addy[6]) self.digital_signature = [chr(x) for x in self.digital_signature] self.submission_date = sp.random_digits(8) while "00000000" == self.submission_date: self.submission_date = sp.random_digits(8)
def generate_load_buffer(self): ''' decider tuple (ID, altitude) ''' return sp.pack_single_uint32(self.id) + sp.pack_single_uint32(self.altitude)
def get_stats(self): if DEBUG: print self.__str__() return sp.pack_single_uint32(self.id) + sp.pack_single_uint32(self.energy_level)
def gen_bytes(self): ''' Generate byte string sent for a TenFourD. ''' byte_str = sp.pack_single_uint16(self.tax_year) for v in [self.fname, self.mname, self.lname, self.addy, self.csz]: byte_str += sp.pack_single_string(v) byte_str += sp.pack_single_uint32(self.id_num) byte_str += sp.pack_single_char(self.donate) byte_str += sp.pack_single_uint32(self.amount) byte_str += sp.pack_single_uint32(self.party) byte_str += sp.pack_single_uint32(self.wages) byte_str += sp.pack_single_uint32(self.interest) byte_str += sp.pack_single_uint32(self.biz_income) byte_str += sp.pack_single_uint32(self.retirement_income) byte_str += sp.pack_single_uint32(self.biz_expenses) byte_str += sp.pack_single_uint32(self.edu_expenses) byte_str += sp.pack_single_uint32(self.self_employ_expenses) byte_str += sp.pack_single_uint32(self.edu_credits) byte_str += sp.pack_single_uint32(self.child_credits) byte_str += sp.pack_single_uint32(self.retirement_credits) byte_str += sp.pack_single_uint32(self.home_buyer_credits) byte_str += sp.pack_single_uint32(self.tax_withheld) byte_str += sp.pack_single_uint32(self.tax_paid_non_taxable_income) byte_str += sp.pack_single_string(''.join(self.digital_signature)) byte_str += sp.pack_single_string(self.submission_date) byte_str += sp.pack_single_uint32(self.tax_due) byte_str += sp.pack_single_uint32(self.tax_refund) return byte_str
def generate_load_buffer(self): ''' rider tuple (ID, type, energy_level, health_code) ''' return sp.pack_single_uint32(self.id) + sp.pack_single_uint32(self.type) + \ sp.pack_single_uint32(self.energy_level) + sp.pack_single_uint32(self.health_code)