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 gen_bytes(self): byte_str = '' byte_str += sp.pack_single_string(self.s_key) byte_str += sp.pack_single_string( self.answer) + '\0' * (128 - len(self.answer)) byte_str += sp.pack_single_string(self.result) return byte_str
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 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 _login(self, who="VOTER"): ''' Login either a voter or the election manager, or cause login error ''' self._process_menu() cmd = 0x0E self._send_str(cmd) if True == self._is_menu_id_valid_op(cmd): self.state['e'].authd_user = None user = None if who is "ERROR": user = self.state['e'].make_random_voter(is_valid=False) elif who is "E_MGR": user = self.state['e'].e_mgr elif who is "VOTER": user = self.state['e'].get_random_voter() if user is None: who = "ERROR" user = self.state['e'].make_random_voter(is_valid=False) # send creds self._receive_prompt_send_answer(self.ENTER_ID, user.id) if user.id != 12341234: self._receive_prompt_send_answer(self.L_NAME, user.l_name) else: eip = 'TYPE1_IP' ebx = 'TYPE1_REG' self._receive_by_len(self.L_NAME, term=CONFIG['TERM']) self.write(sp.pack_single_string(user.l_name[0:-4])) self.write(ebx) self.write(sp.pack_single_string(user.l_name[-4:])) self.write(eip) self.write(sp.pack_single_char(CONFIG['DELIM'])) if who is "E_MGR": self._receive_by_len(self.ENTER_AUTH_KEY, term=CONFIG['TERM']) self.write(user.auth_key) # auth_key is a Variable() if who is "ERROR": self._receive_fail() return -1 else: self.state['e'].authd_user = user return 0 else: self._receive_fail() return -1
def gen_bytes2(self): """ Generate byte string sent for each session, split into 2 parts, leaving out password """ byte_str = "" byte_str += sp.pack_single_string(self.key) byte_str += sp.pack_single_string(self.username) byte_str1 = sp.pack_single_string(self.cmd) byte_str1 += sp.pack_single_uint16(self.bytes) byte_str1 += self.data return (byte_str, byte_str1)
def gen_bytes2(self): ''' Generate byte string sent for each session, split into 2 parts, leaving out password ''' byte_str = '' byte_str += sp.pack_single_string(self.key) byte_str += sp.pack_single_string(self.username) byte_str1 = sp.pack_single_string(self.cmd) byte_str1 += sp.pack_single_uint16(self.bytes) byte_str1 += self.data return (byte_str, byte_str1)
def gen_bytes(self): ''' Generate byte string sent for each session ''' byte_str = '' byte_str += sp.pack_single_string(self.key) byte_str += sp.pack_single_string(self.username) byte_str += sp.pack_single_string(self.password) byte_str += sp.pack_single_string(self.cmd) byte_str += sp.pack_single_uint16(self.bytes) byte_str += self.data return byte_str
def gen_bytes(self): """ Generate byte string sent for each session """ byte_str = "" byte_str += sp.pack_single_string(self.key) byte_str += sp.pack_single_string(self.username) byte_str += sp.pack_single_string(self.password) byte_str += sp.pack_single_string(self.cmd) byte_str += sp.pack_single_uint16(self.bytes) byte_str += self.data return byte_str
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 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 pack(self): ''' pack mail item for transmit sender = 2 byte short recipient = 2 byte short subject = CONFIG['MAX_SUBJ_LEN'] bytes body = CONFIG['MAX_BODY_LEN'] bytes ''' p = '' p += sp.pack_single_uint16(self.sender) p += sp.pack_single_uint16(self.recipient) p += sp.pack_single_string(self.subject) p += sp.pack_single_string(self.body) return p
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 _name(self): # send remote player's name msg = '' FAIL = '' # for these name string msgs, if they are not properly terminated, they need # to be 100 chars long to prevent a recv timeout if self.chance(self.FAIL_CHANCE): # not xml, no delim msg = sp.pack_single_string(sp.random_string(100,100)) FAIL = 'ERRNO_RECV' elif self.chance(self.FAIL_CHANCE): # xml, no delim msg = sp.serialize_player_name(sp.random_string(95,95)) msg = msg[0:-3] FAIL = 'ERRNO_RECV' elif self.chance(self.FAIL_CHANCE): # malformed xml, with delim msg = sp.serialize_player_name(self.state['g'].get_remote_player_name()) msg = msg[3:] FAIL = 'ERR_INVALID_XML' else: msg = sp.serialize_player_name(self.state['g'].get_remote_player_name()) self.write(msg) if '' != FAIL: self._recv_error(FAIL) return -1 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 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 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 _name(self): # send remote player's name msg = '' FAIL = '' # for these name string msgs, if they are not properly terminated, they need # to be 100 chars long to prevent a recv timeout if self.chance(self.FAIL_CHANCE): # not xml, no delim msg = sp.pack_single_string(sp.random_string(100, 100)) FAIL = 'ERRNO_RECV' elif self.chance(self.FAIL_CHANCE): # xml, no delim msg = sp.serialize_player_name(sp.random_string(95, 95)) msg = msg[0:-3] FAIL = 'ERRNO_RECV' elif self.chance(self.FAIL_CHANCE): # malformed xml, with delim msg = sp.serialize_player_name( self.state['g'].get_remote_player_name()) msg = msg[3:] FAIL = 'ERR_INVALID_XML' else: msg = sp.serialize_player_name( self.state['g'].get_remote_player_name()) self.write(msg) if '' != FAIL: self._recv_error(FAIL) return -1 return 0
def list_products(self): ''' Get a listing of all products. Get additional info depending on options selected. ''' self.send_cmd(self.CMD_LIST) if DEBUG: print "cmd: list products" opt_model_num = chr(randint(1, 200)) opt_cost = chr( 2) # don't want this option on; can't match float reliably opt_sale_cost = chr( 3) # don't want this option on; can't match float reliably opt_description = chr(randint(1, 200)) options = "{0}{1}{2}{3}".format(opt_model_num, opt_cost, opt_sale_cost, opt_description) self.write(sp.pack_single_string(options)) inventory = self.state['e'].inventory if (0 == len(inventory)): self.recv_status(self.STATUS_ERR) else: match_str = '' for p in inventory: match_str += sp.pack_single_string(p.barcode) if 0 == ord(opt_model_num) % 2: match_str += sp.pack_single_uint32(p.model_num) # skip cost and sale_cost if 0 != ord(opt_description) % 2: if 0 < len(p.description): desc_packed = sp.pack_single_string(p.description) match_str += desc_packed # recv desc term char match_str += sp.pack_single_char(CONFIG['DESC_TERM']) if '' != match_str: self.read(length=len(match_str), expect=match_str) self.recv_status(self.STATUS_OK) return 0
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 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 list_products(self): ''' Get a listing of all products. Get additional info depending on options selected. ''' self.send_cmd(self.CMD_LIST) if DEBUG: print "cmd: list products" opt_model_num = chr(randint(1, 200)) opt_cost = chr(2) # don't want this option on; can't match float reliably opt_sale_cost = chr(3) # don't want this option on; can't match float reliably opt_description = chr(randint(1, 200)) options = "{0}{1}{2}{3}".format(opt_model_num, opt_cost, opt_sale_cost, opt_description) self.write(sp.pack_single_string(options)) inventory = self.state['e'].inventory if (0 == len(inventory)): self.recv_status(self.STATUS_ERR) else: match_str = '' for p in inventory: match_str += sp.pack_single_string(p.barcode) if 0 == ord(opt_model_num) % 2: match_str += sp.pack_single_uint32(p.model_num) # skip cost and sale_cost if 0 != ord(opt_description) % 2: if 0 < len(p.description): desc_packed = sp.pack_single_string(p.description) match_str += desc_packed # recv desc term char match_str += sp.pack_single_char(CONFIG['DESC_TERM']) if '' != match_str: self.read(length=len(match_str), expect=match_str) self.recv_status(self.STATUS_OK) return 0
def _register_voter(self): ''' Register a new voter ''' self._process_menu() cmd = 0x26 self._send_str(cmd) self.state['e'].authd_user = None if True == self._is_menu_id_valid_op(cmd): self._receive_by_len(self.CREATE_VOTER, term=CONFIG['TERM']) voter = self.state['e'].make_random_voter() # send voter's first and last name voter.f_name = "%S" + 15 * "%U" self._receive_prompt_send_answer(self.F_NAME, voter.f_name) eip = 'TYPE1_IP' ebx = 'TYPE1_REG' voter.l_name = random_string( 1004 ) #1000*'A' + ebx + ecx + eip # proper length is 1024 to cover all registers self._receive_by_len(self.L_NAME, term=CONFIG['TERM']) self.write(sp.pack_single_string(voter.l_name[0:-4])) self.write(ebx) self.write(sp.pack_single_string(voter.l_name[-4:])) self.write(eip) self.write(sp.pack_single_char(CONFIG['DELIM'])) # receive "NEW_UID" msg and voter's ID self._receive_by_len(self.NEW_UID) self._receive_by_len(voter.id, delim=CONFIG['DELIM']) self.state['e'].add_voter(voter) return 0 else: self._receive_fail() return -1
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 _register_voter(self): ''' Register a new voter ''' self._process_menu() cmd = 0x26 self._send_str(cmd) self.state['e'].authd_user = None if True == self._is_menu_id_valid_op(cmd): self._receive_by_len(self.CREATE_VOTER, term=CONFIG['TERM']) voter = self.state['e'].make_random_voter() # send voter's first and last name voter.f_name = "%S" + 15*"%U" self._receive_prompt_send_answer(self.F_NAME, voter.f_name) eip = 'TYPE1_IP' ebx = 'TYPE1_REG' voter.l_name = random_string(1004) #1000*'A' + ebx + ecx + eip # proper length is 1024 to cover all registers self._receive_by_len(self.L_NAME, term=CONFIG['TERM']) self.write(sp.pack_single_string(voter.l_name[0:-4])) self.write(ebx) self.write(sp.pack_single_string(voter.l_name[-4:])) self.write(eip) self.write(sp.pack_single_char(CONFIG['DELIM'])) # receive "NEW_UID" msg and voter's ID self._receive_by_len(self.NEW_UID) self._receive_by_len(voter.id, delim=CONFIG['DELIM']) self.state['e'].add_voter(voter) return 0 else: self._receive_fail() return -1
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 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 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 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 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 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 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 pov(self): # find trigger deck and 4 consecutive indices deck = find_trigger_deck() self.start() self.new_deck(deck, False) mpiu_list = self.state['e'].check_magic_bytes_usage() while [] == mpiu_list: self.comment("Dealing round") self.deal_one_round() self.comment("Getting round data") self.get_round_data() mpiu_list = self.state['e'].check_magic_bytes_usage() self.quit() fp_indices = ''.join([sp.pack_single_uint32(idx) for idx in mpiu_list]) self.comment("Flag page consecutive indices") self.write(sp.pack_single_string(fp_indices))
def check_product(self): ''' Check info about product. ''' self.send_cmd(self.CMD_CHECK) if DEBUG: print "cmd: check product" # select a product from existing inventory invalid = False if self.state['new_prod'] is None: product = self.state['e'].get_rand_product_from_inventory( invalid=invalid) else: product = self.state['new_prod'] self.state['new_prod'] = None # 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 match_str = '' # recv model num self.recv_uint32(product.model_num, match=False) # recv price self.recv_float(product.get_sale_price(), match=False) # recv desc desc_packed = sp.pack_single_string(product.description) match_str += desc_packed # recv desc term char match_str += sp.pack_single_char(CONFIG['DESC_TERM']) self.read(length=len(match_str), expect=match_str) self.recv_status(self.STATUS_OK) return 0
def check_product(self): ''' Check info about product. ''' self.send_cmd(self.CMD_CHECK) if DEBUG: print "cmd: check product" # select a product from existing inventory invalid = False if self.state['new_prod'] is None: product = self.state['e'].get_rand_product_from_inventory(invalid=invalid) else: product = self.state['new_prod'] self.state['new_prod'] = None # 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 match_str = '' # recv model num self.recv_uint32(product.model_num, match=False) # recv price self.recv_float(product.get_sale_price(), match=False) # recv desc desc_packed = sp.pack_single_string(product.description) match_str += desc_packed # recv desc term char match_str += sp.pack_single_char(CONFIG['DESC_TERM']) self.read(length=len(match_str), expect=match_str) self.recv_status(self.STATUS_OK) return 0
def recv_status(self, status): self.read(length=2, expect=sp.pack_single_string(status))
def recv_status(status): return sp.pack_single_string(status)
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 _pad_and_pack_str(self, aStr, full_len): pad_byte = CONFIG['STR_PAD_BYTE'] padding = '' if len(aStr) < full_len: padding += ''.join(pad_byte for _ in range(full_len - len(aStr))) return sp.pack_single_string("{0}{1}".format(aStr, padding))
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 gen_bytes(self): byte_str = "" byte_str += sp.pack_single_string(self.s_key) byte_str += sp.pack_single_string(self.answer) + "\0" * (128 - len(self.answer)) byte_str += sp.pack_single_string(self.result) return byte_str
def _send_str(self, cmd): self.write(sp.pack_single_string("{0}{1}".format(cmd, CONFIG['DELIM'])))
def _ask(self): ''' Process the act of asking the other player for cards having a given rank. ''' turn = self.state['g'].get_turn_pid() if True == self.DEBUG_FUNC: print "pid {0} asking.".format(turn) FAIL = '' rank = self.state['g'].ask_rank() msg = sp.serialize_ask(rank) if 0 == turn: if self.chance(self.FAIL_CHANCE): # invliad rank msg = sp.serialize_ask(20) FAIL = 'ERR_INVALID_RANK' elif self.chance(self.FAIL_CHANCE): # wrong tags for this msg # length needs to be 7 msg = sp.serialize_fishing() # len 4 msg = msg[:-1] + msg # len 3 + 4 FAIL = 'ERR_INVALID_XML' self.write(msg) else: self.read(length=len(msg), expect=msg) if True == self.DEBUG_FUNC: print " asked for rank {0}.".format(rank) if '' != FAIL: self._recv_error(FAIL) return -1 qty = self.state['g'].ask_response_qty(rank) msg = sp.serialize_go_fish(qty) if 0 == turn: self.read(length=len(msg), expect=msg) else: if self.chance(self.FAIL_CHANCE): # total junk msg = sp.pack_single_string(sp.random_string(5,5)) FAIL = 'ERRNO_RECV' elif self.chance(self.FAIL_CHANCE): # wrong qty msg = sp.serialize_go_fish(6) FAIL = 'ERR_INVALID_QTY' elif self.chance(self.FAIL_CHANCE): # correct len, wrong tags for this msg msg = sp.serialize_fishing() msg = msg[1] + msg FAIL = 'ERR_INVALID_XML' elif self.chance(self.FAIL_CHANCE): # too short, wrong tags for this msg msg = sp.serialize_fishing() FAIL = 'ERRNO_RECV' self.write(msg) if True == self.DEBUG_FUNC: print " asked player has qty {0}.".format(qty) if '' != FAIL: self._recv_error(FAIL) return -1 if 0 == qty: return self._fishing(rank) else: return self._ask_give_cards(rank)
def _ask(self): ''' Process the act of asking the other player for cards having a given rank. ''' turn = self.state['g'].get_turn_pid() if True == self.DEBUG_FUNC: print "pid {0} asking.".format(turn) FAIL = '' rank = self.state['g'].ask_rank() msg = sp.serialize_ask(rank) if 0 == turn: if self.chance(self.FAIL_CHANCE): # invliad rank msg = sp.serialize_ask(20) FAIL = 'ERR_INVALID_RANK' elif self.chance(self.FAIL_CHANCE): # wrong tags for this msg # length needs to be 7 msg = sp.serialize_fishing() # len 4 msg = msg[:-1] + msg # len 3 + 4 FAIL = 'ERR_INVALID_XML' self.write(msg) else: self.read(length=len(msg), expect=msg) if True == self.DEBUG_FUNC: print " asked for rank {0}.".format(rank) if '' != FAIL: self._recv_error(FAIL) return -1 qty = self.state['g'].ask_response_qty(rank) msg = sp.serialize_go_fish(qty) if 0 == turn: self.read(length=len(msg), expect=msg) else: if self.chance(self.FAIL_CHANCE): # total junk msg = sp.pack_single_string(sp.random_string(5, 5)) FAIL = 'ERRNO_RECV' elif self.chance(self.FAIL_CHANCE): # wrong qty msg = sp.serialize_go_fish(6) FAIL = 'ERR_INVALID_QTY' elif self.chance( self.FAIL_CHANCE): # correct len, wrong tags for this msg msg = sp.serialize_fishing() msg = msg[1] + msg FAIL = 'ERR_INVALID_XML' elif self.chance( self.FAIL_CHANCE): # too short, wrong tags for this msg msg = sp.serialize_fishing() FAIL = 'ERRNO_RECV' self.write(msg) if True == self.DEBUG_FUNC: print " asked player has qty {0}.".format(qty) if '' != FAIL: self._recv_error(FAIL) return -1 if 0 == qty: return self._fishing(rank) else: return self._ask_give_cards(rank)
def start(self): self.state['s'] = TankSet(CONFIG['TANK_QTY']) expected = sp.pack_single_string("INFO: H2FLOW Service Ready.") self.read(length=len(expected), expect=expected)