コード例 #1
0
ファイル: server.py プロジェクト: fiona-tina/ECE568
def connect_world_command(world_id):
    conn, cursor = connect_db()

    query = f"""
        SELECT id, location_x, location_y
        FROM amazon_frontend_warehouse
    """
    cursor.execute(query)
    rows = cursor.fetchall()
    cursor.close()

    initWarehouses = []
    for row in rows:
        initWarehouse = world.AInitWarehouse()
        initWarehouse.id = row[0]
        initWarehouse.x = row[1]
        initWarehouse.y = row[2]
        initWarehouses.append(initWarehouse)

    command = world.AConnect()
    command.worldid = world_id
    command.isAmazon = True
    for initWarehouse in initWarehouses:
        command.initwh.add().CopyFrom(initWarehouse)

    return command
コード例 #2
0
ファイル: message_handlers.py プロジェクト: fiona-tina/ECE568
def connect_world(initWarehouses):
    world_socket = None
    world_id = None

    # // AMAZON CONNECT
    while True:
        world_socket = get_open_socket(WORLD_ADDRESS)
        createWorld = world.AConnect()
        createWorld.isAmazon = True
        send_request(createWorld, world_socket)
        res = recv_response(world.AConnected, world_socket)
        world_id = res.worldid
        world_socket.close()

        world_socket = get_open_socket(WORLD_ADDRESS)
        connectWorld = createWorld
        connectWorld.worldid = world_id
        for initWarehouse in initWarehouses:
            connectWorld.initwh.add().CopyFrom(initWarehouse)

        send_request(connectWorld,
                     world_socket)  # connect with init_warehouses
        res = recv_response(world.AConnected, world_socket)
        if res.result == 'connected!':
            break

    return world_socket, world_id
コード例 #3
0
def init_operation(world_sock, amazon_db):
    cm = world_amazon_pb2.AConnect()
    cm.worldid = 1
    cm.isAmazon = True

    # create warehouse
    iw = cm.initwh.add()
    iw.id = 1
    iw.x = 2
    iw.y = 4

    send_message(world_sock, cm)
    recv_Aconnected = get_message(world_sock, world_amazon_pb2.AConnected)
    '''
    # buy initial items to the warehouse
    cm = world_amazon_pb2.ACommands()
    buy = cm.buy.add()
    buy.whnum = 1
    buy.seqnum = relate_world.seqnum
    product1 = buy.things.add()
    product1.id = 1
    product1.description = "Apple"
    product1.count = 20
    product2 = buy.things.add()
    product2.id = 2
    product2.description = "Ball"
    product2.count = 10
    product3 = buy.things.add()
    product3.id = 3
    product3.description = "Candy"
    product3.count = 5

    send_message(world_sock, cm)

    relate_world.seqnum += 1
    recv_AResponse = get_message(world_sock, world_amazon_pb2.AResponses)

    # send Amazon side's ack
    cm2 = world_amazon_pb2.ACommands()
    if len(recv_AResponse.arrived) != 0:
        for arrived in recv_AResponse.arrived:
            cm2.acks.append(arrived.seqnum)
    send_message(world_sock, cm2)
    '''

    c = amazon_db.cursor()
    c.execute(
        'TRUNCATE TABLE MAIN_WAREHOUSE, MAIN_FILL, MAIN_ITEM, MAIN_ORDER;')
    c.execute(
        "INSERT INTO MAIN_WAREHOUSE (id, x, y) SELECT 1, 2, 4 WHERE NOT EXISTS (SELECT id FROM MAIN_WAREHOUSE WHERE id = 1);"
    )
    #c.execute("INSERT INTO MAIN_ITEM (id, name, quantity) SELECT 1, 'Apple', 20 WHERE NOT EXISTS (SELECT id FROM MAIN_ITEM WHERE id = 1);")
    #c.execute("INSERT INTO MAIN_ITEM (id, name, quantity) SELECT 2, 'Ball', 10 WHERE NOT EXISTS (SELECT id FROM MAIN_ITEM WHERE id = 2);")
    #c.execute("INSERT INTO MAIN_ITEM (id, name, quantity) SELECT 3, 'Candy', 5 WHERE NOT EXISTS (SELECT id FROM MAIN_ITEM WHERE id = 3);")
    amazon_db.commit()
    c.close()
コード例 #4
0
def send_Aconnect(sock):

    cm=world_amazon_pb2.AConnect()
    cm.worldid=10
    cm.isAmazon=True
    '''
    iw=cm.initwh.add()
    iw.id=1
    iw.x=223
    iw.y=134
    '''
   
    send_message(sock,cm)
コード例 #5
0
ファイル: send_recv_world.py プロジェクト: js896/ECE568
def connect_world():
    """
    Wrapp up init info to world simulator
    """
    cmd = world_amazon_pb2.AConnect()
    wh = world_amazon_pb2.AInitWarehouse()
    wh.id = 1
    wh.x = 1
    wh.y = 1
    cmd.isAmazon = True
    cmd.worldid = 1
    cmd.initwh.append(wh)
    return cmd
コード例 #6
0
ファイル: server.py プロジェクト: yuangungun5/Mini-Amazon
def SocketServer(conn, ups_sock):
    #receive ups connect
    ua_connect = readClientRequest(ups_sock)
    ua_c = wa.UA_Connect()
    ua_c.ParseFromString(ua_connect)
    print(ua_c.worldid)

    first = wa.AConnect()
    first.worldid = ua_c.worldid
    first.isAmazon = True
    print(first)

    CreateTable("WH", conn)
    cursor = conn.cursor()
    InitWH(conn)
    InitStock(conn)

    sql = '''SELECT * FROM website_warehouse;'''
    cursor.execute(sql)
    records = cursor.fetchall()
    for row in records:
        initWH = first.initwh.add()
        initWH.id = row[0]
        initWH.x = row[1]
        initWH.y = row[2]
        print(f"{row[0]}  {row[1]}  {row[2]}")

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(worldAddress)

    sendMessage(s, first)
    print("send connect and init")

    whole_message = readClientRequest(s)
    m = wa.AConnected()
    m.ParseFromString(whole_message)
    print(m)

    try:
        t1 = threading.Thread(target=connectToWeb, args=())
        t2 = threading.Thread(target=clientHandler, args=(conn, s, ups_sock))
        t3 = threading.Thread(target=receiveFromUps, args=(ups_sock, ))
        t1.start()
        t2.start()
        t3.start()
        t1.join()
        t2.join()
        t3.join()
    finally:
        print('close')
        s.close()
コード例 #7
0
def connect_world_id(socket, id, wh_info):
    worldConnection = world_amazon_pb2.AConnect()
    if id:
        worldConnection.worldid = int(id)
    worldConnection.isAmazon = True
    for wh in wh_info:
        worldConnection.initwh.add(id=wh['id'], x=wh['x'], y=wh['y'])
    print(worldConnection)
    my_send(socket, worldConnection)

    command = world_amazon_pb2.AConnected()
    command.ParseFromString(my_recv(socket))
    print(command)
    return command.result
コード例 #8
0
def init_warehouses(world_sock, world_id, warehouses):
    AConnect_command = wapb2.AConnect()
    AConnect_command.worldid = world_id
    AConnect_command.isAmazon = True

    for w in warehouses:
        wh_proto = AConnect_command.initwh.add()
        wh_proto.id = w['id']
        wh_proto.x = w['x']
        wh_proto.y = w['y']

    send_pbuf(world_sock, AConnect_command)
    # recv response from world
    AConnected_response = wapb2.AConnected()
    AConnected_response.ParseFromString(read_serialized_pbuf(world_sock))
    print(AConnected_response.result)
コード例 #9
0
 def create_connection_msg(self, warehouse_data, worldid):
     # CONNECT 
     connect = wapb.AConnect()
     if worldid is not None:
         connect.worldid = worldid
     connect.isAmazon = True
     for data in warehouse_data:
         warehouse = wapb.AInitWarehouse()
         warehouse.id = data['warehouse_id']
         warehouse.x, warehouse.y = data['x'], data['y']
         connect.initwh.append(warehouse)
     assert len(connect.initwh) == 1
     # connect.worldid = 2   
     data = connect.SerializeToString()
     connect_send = data
     # print("Message to send is: {}".format(connect_send))
     return connect_send
コード例 #10
0
def connect_world(socket):
    connect_msg = world_amazon.AConnect()
    connect_msg.worldid = WORLD_ID
    connect_msg.isAmazon = True

    #init warehouse
    world_msg = connect_msg.initwh.add()
    world_msg.x = 100
    world_msg.y = 100
    try:
        cur = conn.cursor()
        cur.execute("INSERT INTO amazonweb_warehouse (x_location, y_location) VALUES (%s, %s)",("100", "100"))
        conn.commit()
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    world_msg.id = create_warehouse()
    
    send_msg(connect_msg,socket)
    response = recv_msg(world_amazon.AConnected, socket)
    if response.result == "connected!":
        print("Amazon Connect to world:" + str(response.worldid) + " succeed!")
    else:
        print("Amazon Connect to world:" + str(response.worldid) + " fails!")
コード例 #11
0
    cursor.execute(sql)
    conn.commit()
    sql = '''DELETE from amazon_web_order;'''
    cursor.execute(sql)
    conn.commit()
    sql = '''DELETE from amazon_web_product'''
    cursor.execute(sql)
    conn.commit()

    # Receive world ID and connect to UPS
    # TODO
    socket_UPS, wid = tools.recv_worldid()
    print('receive UPS socket ^_^, worldid = (%d)' % (wid))

    # Connect to the world (Warehouse)
    cmd = world_amazon_pb2.AConnect()
    cmd.worldid = int(wid)  # wid from above, TODO
    cmd.isAmazon = True
    warehouse = cmd.initwh.add()
    warehouse.id = 1
    warehouse.x = 10
    warehouse.y = 10
    socket_world = tools.connectWorld(cmd)
    print('receive world socket, connect to world ^_^')
    #cur = conn.cursor()

    # create warehouse
    sql = '''INSERT INTO amazon_web_warehouse (whid, x, y) VALUES (%s, %s, %s)'''
    cursor.execute(sql, (warehouse.id, warehouse.x, warehouse.y))
    conn.commit()
コード例 #12
0
def world_connect(world_addr, world_port, world_id,warehouses):
    AConnect_command = wapb2.AConnect()
    AConnect_command.worldid = world_id
    AConnect_command.isAmazon = True
コード例 #13
0
=======
=======
>>>>>>> 356bd9c2d5fbe0693d181b6fe514bbdbe6abaf76:backend/unused/world_connection.py
    for w in warehouses:
        wh_proto = AConnect_command.initwh.add()
        wh_proto.id = w['id']
        wh_proto.x = w['x']
        wh_proto.y = w['y']
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
<<<<<<< HEAD:backend/world_connection.py
>>>>>>> fd6c319ed98facfed1fb9babc45e30b93d76d289
    s.connect((world_addr, world_port))
    print('connect to world')
    # constrcut AConnect command
<<<<<<< HEAD
    AConnect_command = wapb2.AConnect()
    AConnect_command.worldid = world_id
    AConnect_command.isAmazon = True
=======


>>>>>>> fd6c319ed98facfed1fb9babc45e30b93d76d289
    # send AConnect to world
=======

    s.connect((world_addr, world_port))
    print('connect to world')
    # constrcut AConnect command
>>>>>>> 356bd9c2d5fbe0693d181b6fe514bbdbe6abaf76:backend/unused/world_connection.py
    send(s, AConnect_command , 'send Aconnect to world')
    # recv response from world