Exemplo n.º 1
0
def PacketDrop(wSock, aSock, db):
    print('check seqnum table every 30s, resend all those request/ACK')

    while True:
        time.sleep(30)
        print('start to check ack')
        with db_lock:
            cur = db.cursor()
            sql = 'SELECT * FROM usend'
            cur.execute(sql)
            row = cur.fetchone()

        ToWorld = wupb.UCommands()
        ToAmazon = uapb.UCommunicate()
        ToWorld_empty = True
        ToAmazon_empty = True

        while row:
            with db_lock:
                # TOCHECK: distinguish what type of Msg, add to ToWorld or ToAmazon
                if row[2] == 'UGoPickup':
                    print('resend UGoPickup')
                    tp = ToWorld.pickups.add()
                    ToWorld_empty = False
                elif row[2] == 'UGoDeliver':
                    print('resend UGoDeliver')
                    tp = ToWorld.deliveries.add()
                    ToWorld_empty = False
                elif row[2] == 'UQuery':
                    print('resend UQuery')
                    tp = ToWorld.queries.add()
                    ToWorld_empty = False
                elif row[2] == 'UOrderPlaced':
                    print('resend UOrderPlaced')
                    tp = ToAmazon.uorderplaced.add()
                    ToAmazon_empty = False
                elif row[2] == 'UArrivedAtWarehouse':
                    print('resend UArrivedAtwarehouse')
                    tp = ToAmazon.uarrived.add()
                    ToAmazon_empty = False
                elif row[2] == 'UPackageDelivered':
                    print('resend UPackageDelivered')
                    tp = ToAmazon.udelivered.add()
                    ToAmazon_empty = False

                tp.ParseFromString(row[1])
                row = cur.fetchone()

        if not ToWorld_empty:
            Send(wSock, ToWorld)
        else:
            print('world empty')
        if not ToAmazon_empty:
            Send(aSock, ToAmazon)
        else:
            print('amazon empty')
def completions_handler(completions, world_id, amazon_fd, world_fd):
    try:
        db_conn = psycopg2.connect(
            "dbname='postgres' user='******' password='******'"
            "host='" + db_host + "' port='" + db_port + "'")
        db_cur = db_conn.cursor()
    except psycopg2.OperationalError as error:
        print(error)
    UCommu = ups_amazon_pb2.UCommunicate()
    #mutex.acquire()
    for completion in completions:
        if completion.status == "ARRIVE WAREHOUSE":
            UArrivedAtWarehouse = UCommu.uarrived.add()
            UArrivedAtWarehouse.truckid = completion.truckid
            UArrivedAtWarehouse.seqnum = get_seqnum()
            location_x = completion.x
            location_y = completion.y
            truckid = completion.truckid
            update_truck_completion(location_x, location_y, truckid, world_id)
            try:
                db_cur.execute("select packageid from package where "
                               "worldid = '" + str(world_id) +
                               "' and truckid = '" + str(truckid) +
                               "' and status = 'E'")
                rows = db_cur.fetchall()
            except psycopg2.OperationalError as error:
                print(error)
            for row in rows:
                db_cur.execute("update package set status = 'W' where "
                               "packageid = '" + str(row[0]) +
                               "' and worldid = '" + str(world_id) + "'")
                db_conn.commit()
        if completion.status == "IDLE":
            location_x = completion.x
            location_y = completion.y
            truckid = completion.truckid
            db_cur.execute("update truck set status = 'I', "
                           "location_x = '" + str(location_x) + "'," +
                           "location_y = '" + str(location_y) +
                           "'where truckid = '" + str(truckid) +
                           "' and worldid = '" + str(world_id) + "'")
            db_conn.commit()
    seqnum = get_seqnum()
    UCommu.acks.append(seqnum)
    json_msg = pb2json(UCommu)
    db_cur.execute(
        """INSERT INTO amazon_ack (seqnum,message) VALUES(%s,%s);""",
        (seqnum, json.dumps(json_msg)))
    db_conn.commit()
    send_to_amazon(UCommu, amazon_fd)
    #     if UPackageDelivered.seqnum in amazon_ack_list:
    #         break
    db_conn.commit()
    #mutex.release()
    return
def orderplaced_handler(orderplaced_handle_list, world_id, amazon_fd, world_fd,
                        truck_list):
    UCommands = world_ups_pb2.UCommands()
    UCommu = ups_amazon_pb2.UCommunicate()
    #mutex.acquire()
    package_db_handle(orderplaced_handle_list, world_id, amazon_fd, world_fd,
                      truck_list)
    for i in range(0, len(orderplaced_handle_list)):
        UGoPickup = UCommands.pickups.add()
        UGoPickup.truckid = int(truck_list[i])
        UGoPickup.whid = orderplaced_handle_list[i].whid
        UGoPickup.seqnum = get_seqnum()
        #edited
        UOrderPlaced = UCommu.uorderplaced.add()
        UOrderPlaced.packageid = orderplaced_handle_list[i].packageid
        UOrderPlaced.truckid = int(truck_list[i])
        UOrderPlaced.seqnum = get_seqnum()
        update_truck_orderplaced(UOrderPlaced.packageid, world_id, amazon_fd,
                                 world_fd, UOrderPlaced.truckid)
        tmp2 = """update truck set status = 'E' where truckid =  %s and worldid =%s"""
        db_conn = psycopg2.connect(
            "dbname='postgres' user='******' password='******'"
            "host='" + db_host + "' port='" + db_port + "'")
        db_cur = db_conn.cursor()
        db_cur.execute(tmp2, (str(UGoPickup.truckid), str(world_id)))
        db_conn.commit()
    seqnum1 = get_seqnum()
    UCommu.acks.append(seqnum1)
    json_msg = pb2json(UCommu)
    db_cur.execute(
        """INSERT INTO amazon_ack (seqnum,message) VALUES(%s,%s);""",
        (seqnum1, json.dumps(json_msg)))
    db_conn.commit()
    send_to_amazon(UCommu, amazon_fd)
    #    if UOrderPlaced.seqnum in amazon_ack_list:
    #        break
    #while True:
    seqnum2 = get_seqnum()
    UCommands.acks.append(seqnum2)
    json_msg2 = pb2json(UCommands)
    db_cur.execute("""INSERT INTO world_ack (seqnum,message) VALUES(%s,%s);""",
                   (seqnum2, json.dumps(json_msg2)))
    db_conn.commit()
    send_to_world(UCommands, world_fd)
    #    if  UGoPickup.seqnum in world_ack_list:
    #        break
    #mutex.release()
    return
Exemplo n.º 4
0
def send_unack_msg_to_amazon(amazon_fd):
    db_conn = psycopg2.connect(
        "dbname='postgres' user='******' password = '******'"
        "host='" + db_host + "' port='" + db_port + "'")
    db_cur = db_conn.cursor()
    while True:
        """
        get all message that haven't receive ack
        """
        db_cur.execute("""select message from  amazon_ack""")
        """send them all again"""
        msgs_json = db_cur.fetchall()
        for msg_json in msgs_json:
            """restore it back to Message and send again"""
            msg = ups_amazon_pb2.UCommunicate()
            msg = json2pb(msg, msg_json, useFieldNumber=False)
            _EncodeVarint(amazon_fd.send, len(msg.SerializeToString()), None)
            amazon_fd.sendall(msg.SerializeToString())
        sleep(60)
def delivered_handler(deliveries, world_id, amazon_fd, world_fd):
    UCommu = ups_amazon_pb2.UCommunicate()
    db_conn = psycopg2.connect(
        "dbname='postgres' user='******' password='******'"
        "host='" + db_host + "' port='" + db_port + "'")
    db_cur = db_conn.cursor()
    #mutex.acquire()
    for deliver in deliveries:
        UPackageDelivered = UCommu.udelivered.add()
        UPackageDelivered.packageid = deliver.packageid
        UPackageDelivered.seqnum = get_seqnum()
        #TODO: add lock
        db_cur.execute("select location_x, location_y from package"
                       " where worldid = '" + str(world_id) +
                       "' and packageid = '" + str(deliver.packageid) +
                       "' and status = 'O'")
        row = db_cur.fetchone()
        package_x = int(row[0])
        package_y = int(row[1])
        truck_update_deliver(deliver.truckid, world_id,
                             UPackageDelivered.packageid)
        db_cur.execute("update truck set location_x = '" + str(package_x) +
                       "', location_y = ' " + str(package_y) +
                       "'where truckid = '" + str(deliver.truckid) +
                       "' and worldid = '" + str(world_id) + "'")
        db_conn.commit()
    seqnum = get_seqnum()
    UCommu.acks.append(seqnum)
    json_msg = pb2json(UCommu)
    db_cur.execute(
        """INSERT INTO amazon_ack (seqnum,message) VALUES(%s,%s);""",
        (seqnum, json.dumps(json_msg)))
    db_conn.commit()
    send_email(world_id, amazon_fd, world_fd, UPackageDelivered.packageid)
    send_to_amazon(UCommu, amazon_fd)
    #    if UPackageDelivered.seqnum in amazon_ack_list:
    #        break
    db_conn.commit()
    #mutex.release()
    return
Exemplo n.º 6
0
def send_ack_to_amazon(ack, amazon_fd):
    #print("sending ack to AMAZON")
    UCommu = ups_amazon_pb2.UCommunicate()
    UCommu.acks.append(ack)
    send_to_amazon(UCommu, amazon_fd)
Exemplo n.º 7
0
def ProcessARes(msg, aSock, wSock, conn):
    print('process aresponse')
    print(msg)
    global SeqNum
    toWorldEmpty = True
    toAmazonEmpty = True

    # Check the acks field, delete those in UPS seqnum table
    for ack in msg.acks:
        Deleteusend(conn, ack)

    # Prepare UCommand & UCommunicate
    ToWorld = wupb.UCommands()
    ToAmazon = uapb.UCommunicate()

    # Check for AOrderPlaced
    for aor in msg.aorderplaced:
        print('process AOrderPlace')

        # Select one truck from truck table
        truck = Findidle(conn)
        if truck == -1:
            truck = Findtruck(conn)
            if truck == -1:
                print('no truck available, ignore the order')
                continue

        # Store in amazon seqnum table
        exists = Arecvseq(conn, aor.seqnum)
        if not exists:
            Insertarecv(conn, aor.seqnum)
        else:
            print('process before')
            continue

        toWorldEmpty = False
        toAmazonEmpty = False

        # Add ACK
        ToAmazon.acks.append(aor.seqnum)

        # Add package in package table
        items = ''
        amount = 0
        for thing in aor.things:
            if not items:
                items = thing.name
            else:
                items = items + ',' + thing.name
            amount += thing.count
        Insertpackage(conn, aor.packageid, aor.x, aor.y, 'created', items,
                      amount, truck, aor.UPSuserid)

        # Add UGoPickup to ToWorld
        pick = ToWorld.pickups.add()
        pick.truckid = truck
        pick.whid = aor.whid
        with seq_lock:
            pick.seqnum = SeqNum
            SeqNum += 1

        # Add UOrderplaced to toAmazon
        order = ToAmazon.uorderplaced.add()
        order.packageid = aor.packageid
        order.truckid = truck
        with seq_lock:
            order.seqnum = SeqNum
            SeqNum += 1

        # Add send message to usend table
        Insertusend(conn, pick.seqnum, pick.SerializeToString(), 'UGoPickup')
        Insertusend(conn, order.seqnum, order.SerializeToString(),
                    'UOrderPlaced')

        # Update status of package and truck
        Truckstatus(conn, truck, 'to warehouse')
        Packagestatus(conn, aor.packageid, 'truck en-route to warehouse')

    # Check for ALoadingFinished
    for alod in msg.aloaded:
        toWorldEmpty = False
        toAmazonEmpty = False
        print('process ALoadingFinished')

        # Store in amazon seqnum table
        exists = Arecvseq(conn, alod.seqnum)
        if not exists:
            Insertarecv(conn, alod.seqnum)
        else:
            print('process before')
            continue

        # Add ACK
        ToAmazon.acks.append(alod.seqnum)

        # Update status of package & update the truck amount
        Packagestatus(conn, alod.packageid, 'out for delivery')
        Truckamount(conn, alod.truckid, True)
        Truckstatus(conn, alod.truckid, 'delivering')

        # Add UGoDeliver to ToWorld
        xy = Packageaddress(conn, alod.packageid)
        deliver = ToWorld.deliveries.add()
        deliver.truckid = alod.truckid
        package = deliver.packages.add()
        package.packageid = alod.packageid
        package.x = xy[0]
        package.y = xy[1]
        with seq_lock:
            deliver.seqnum = SeqNum
            SeqNum += 1

        # Add send message to usend table
        Insertusend(conn, deliver.seqnum, deliver.SerializeToString(),
                    'UGoDeliver')

    # Send ACK to World & Amazon
    if not toAmazonEmpty:
        Send(aSock, ToAmazon)
    if not toWorldEmpty:
        Send(wSock, ToWorld)
Exemplo n.º 8
0
def ProcessURes(msg, wSock, aSock, db):
    print('Process UResponse...')
    print(msg)
    global SeqNum
    toAmazonEmpty = True
    toWorldEmpty = True

    # Prepare UCommand & UCommunicate
    ToWorld = wupb.UCommands()
    ToAmazon = uapb.UCommunicate()

    # check for UFinished
    print('Looking into UFinished msg')
    for ufin in msg.completions:
        toWorldEmpty = False

        # Add ACK
        ToWorld.acks.append(ufin.seqnum)

        # Check ufinished status, notify amazon
        # first check whether this ufin has been processed before
        entry = Wrecvseq(db, ufin.seqnum)
        if entry:
            continue

        # record the seqnum from World
        Insertwrecv(db, ufin.seqnum)

        if ufin.status == 'ARRIVE WAREHOUSE':
            toAmazonEmpty = False
            print('notify amazon')
            arrive = ToAmazon.uarrived.add()
            arrive.truckid = ufin.truckid
            with seq_lock:
                arrive.seqnum = SeqNum
                SeqNum += 1
            Insertusend(db, arrive.seqnum, arrive.SerializeToString(),
                        'UArrivedAtWarehouse')
            Truckstatus(db, ufin.truckid, 'loading')

        elif ufin.status == 'IDLE':
            print('dont need notify, notify in delivered')
            # update truck status in database to idle
            Truckstatus(db, ufin.truckid, 'idle')

    # check for UDeliveryMade
    print('Looking into UDeliveryMade Msg...')
    for udel in msg.delivered:
        toWorldEmpty = False
        toAmazonEmpty = False

        # Add ACK
        ToWorld.acks.append(udel.seqnum)

        #  first check whether this udel has been processed before
        if Checkdelivered(db, udel.packageid):
            continue

        # send out notification email
        SendEmail(db, udel.packageid)

        # record the seqnum from World
        Insertwrecv(db, udel.seqnum)

        # update delivery status for package in db, send to amazon
        delivered = ToAmazon.udelivered.add()
        delivered.packageid = udel.packageid
        Packagestatus(db, udel.packageid, 'delivered')
        Truckamount(db, udel.truckid, False)

        # record this MSG in database
        with seq_lock:
            delivered.seqnum = SeqNum
            Insertusend(db, SeqNum, delivered.SerializeToString(),
                        'UPackageDelivered')
            SeqNum += 1

    # add ack for truckstatus, if any
    for utruck in msg.truckstatus:
        toWorldEmpty = False
        # Add ACK
        ToWorld.acks.append(utruck.seqnum)
        UpdatePackagePos(db, utruck.truckid, utruck.x, utruck.y)

    # Check the acks field, delete those in UPS seqnum table
    for ACK in msg.acks:
        Deleteusend(db, ACK)

    # Send ACK to World & updates to Amazon
    if not toWorldEmpty:
        Send(wSock, ToWorld)
    if not toAmazonEmpty:
        Send(aSock, ToAmazon)