def ua_validated(db, world_socket, user, ups_acks, world_acks, email_socket, email_sender): # Check if result # If True, purchase pkg_id = user.shipId if user.result: pkg_info = q_pkg_id(db, pkg_id) # IF pkg is related to ups account, then turn to validate item_str = pkg_info[4] whnum = find_near_wh(db, pkg_info[2], pkg_info[3]) add_wh_info(db, pkg_id, whnum) # update_pkg_whinfo() ''' 1. Split item_str into purchase list (utils) 2. Assign warehouse info 3. Send world_buy ''' purchase_list = getListfromStr(item_str) update_pkg_status(db, 1, (pkg_id,)) world_buy(world_socket, whnum, purchase_list, world_acks) else: # If False, update status to cancel update_pkg_status(db, 9, (user.shipId,)) receiver = q_email_by_sid(db, pkg_id) pkg_status = 'CANCELLED since validation failed.' send_email(receiver, pkg_id, pkg_status, email_socket, email_sender)
def au_deliver(db, ups_socket, loaded, ups_acks): ship_list = [loaded.shipid] # shipinfo for sid in ship_list: deliver = UA_pb2.AtoUCommand() seqnum = next(gen) deliver.loadReq.add(seqNum=seqnum, shipId=ship_list, truckId=q_pkg_id(db, sid)[1]) send_ups(ups_socket, deliver, seqnum, ups_acks) update_pkg_status(db, 7, sid)
def au_pickup(db, ups_socket, shipId, ups_acks): seqnum = next(gen) au_command = UA_pb2.AtoUCommand() # find wh/x/y/buystr from db pickup = au_command.pikReq.add() pkg = q_pkg_id(db, shipId) print(pkg) pickup.seqNum = seqnum pickup.warehouseId = pkg[8] newship = pickup.shipment.add() newship.shipId = shipId if pkg[6] is not None: newship.UPSaccount = pkg[6] # parse purchase_list buy = getListfromStr(pkg[4]) for item in buy: newship.products.add(description = item['description'], count = item['count']) newship.destination_x = pkg[2] newship.destination_y = pkg[3] # send to ups send_ups(ups_socket, au_command, seqnum, ups_acks)
def listen_django(D_HOST, D_PORT, world_socket, ups_socket, db, world_acks, ups_acks, email_socket, email_sender): ########## Just for testing: Insert a new package # cursor = db.cursor() # cursor.execute('INSERT INTO "frontEndServer_package" (item_str, addr_x, addr_y, status) VALUES (%s, %s, %s, %s) RETURNING id', ("1,Apple,2;2,Banana,3;3,Milk,4", 4, 2, 0)) # pkg_id = cursor.fetchone()[0] # db.commit() # print("pkg_id: ") # print(pkg_id) # pkg_info = q_pkg_id(db, pkg_id) # if pkg_info[7] is not None: # au_validate(ups_socket, pkg_info[7]) # return # item_str = pkg_info[4] # whnum = find_near_wh(db, pkg_info[2], pkg_info[3]) # add_wh_info(db, pkg_id, whnum) # purchase_list = getListfromStr(item_str) # update_pkg_status(db, 1, (pkg_id,)) # world_buy(world_socket, whnum, purchase_list, world_acks) ########## listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Setting this socket option avoids the error: Address already in use # https://realpython.com/python-sockets/ listen_socket.bind((D_HOST, D_PORT)) listen_socket.listen() print("============ Start to listen Django ===========") while True: django_s, addr = listen_socket.accept() print("================ Accept Django ================") ''' 1. Recieve pkg_id 2. Select * from t where pkg_id (execTable) 2.1 Check status == Created 2.2 Get item_str ''' data = django_s.recv(65535) d1 = data.decode('utf-8') print("---------------- Message from Django ----------") print(d1) print("-----------------------------------------------") pkg_id = int(d1) receiver = q_email_by_sid(db, pkg_id) pkg_status = 'CREATED.' send_email(receiver, pkg_id, pkg_status, email_socket, email_sender) pkg_info = q_pkg_id(db, pkg_id) # IF pkg is related to ups account, then turn to validate if pkg_info[6] is not None: au_validate(ups_socket, ups_acks, pkg_info[6], pkg_info[0]) continue item_str = pkg_info[4] whnum = find_near_wh(db, pkg_info[2], pkg_info[3]) add_wh_info(db, pkg_id, whnum) # update_pkg_whinfo() ''' 1. Split item_str into purchase list (utils) 2. Assign warehouse info 3. Send world_buy ''' purchase_list = getListfromStr(item_str) update_pkg_status(db, 1, (pkg_id, )) world_buy(world_socket, whnum, purchase_list, world_acks)
def listen_world(world_socket, ups_socket, db, world_acks, world_seqs, ups_acks, ups_seqs): global buyseq_shipid print("========== Start to listen the world ==========\n") while True: try: response = recv_world(world_socket) except Exception as e: print("-------------- World Recv Failure ------------\n") break if response is not None: print("---------------- Message from world ----------") print(response) print("-----------------------------------------------") if response.finished: print("End world.") for _ in response.acks: world_acks.add(_) print(world_acks) for come_error in response.error: print("come_error") for come_arrived in response.arrived: # repeated APurchaseMore arrived = 1; # ack back to world ack_back_world(world_socket, come_arrived.seqnum) # avoid handle same message if come_arrived.seqnum in world_seqs: continue world_seqs.add(come_arrived.seqnum) print("Receive arrived") ''' 1. Update pkg status to PURCHASING 1.1 q_pkg_by_item ''' pkg_id = q_pkg_by_item(db, parse_list_to_str(come_arrived.things)) update_pkg_status(db, 2, (pkg_id, )) ''' 2. Send world APack 3. Update status to PACKING ''' threading.Thread(target=world_pack, args=(db, world_socket, world_acks, come_arrived.whnum, come_arrived.things, pkg_id)).start() update_pkg_status(db, 3, (pkg_id, )) for come_ready in response.ready: # repeated APacked ready = 2; ack_back_world(world_socket, come_ready.seqnum) if come_ready.seqnum in world_seqs: continue world_seqs.add(come_ready.seqnum) # If pkg is cancelled, dont send pick up. if q_pkg_id(db, come_ready.shipid)[5] == 9: continue # update status to packed update_pkg_status(db, 4, (come_ready.shipid, )) # tell ups to pickup print("tell ups to pickup") threading.Thread(target=au_pickup, args=(db, ups_socket, come_ready.shipid, ups_acks)).start() # au_pickup(db, ups_socket, come_ready.shipid, ups_acks) for come_loaded in response.loaded: # repeated ALoaded loaded = 3; ack_back_world(world_socket, come_loaded.seqnum) if come_loaded.seqnum in world_seqs: continue world_seqs.add(come_loaded.seqnum) # update status to loaded update_pkg_status(db, 6, (come_loaded.shipid, )) # tell ups to deliver print("tell ups to deliver") threading.Thread(target=au_deliver, args=(db, ups_socket, come_loaded, ups_acks)).start()