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 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 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 recv_float(self, val, match=True): if True == match: self.read(length=4, expect=sp.pack_single_float(val)) else: self.read(length=4)