Ejemplo n.º 1
0
    def init(self, count):
        # Resend mechanism
        th_resend = threading.Thread(target=self.resend_data, args=())
        th_resend.setDaemon(True)
        th_resend.start()

        connect = world_ups_pb2.UConnect()
        connected = world_ups_pb2.UConnected()
        # self.database = database
        # Add truck to database with default settings
        for i in range(count):
            truck_id = create_truck()
            newtruck = connect.trucks.add()
            newtruck.id = truck_id
            newtruck.x = 1
            newtruck.y = 1
            # truck = Truck()
            # truck.save()
            # newtruck = connect.trucks.add()
            # newtruck.id = truck.truck_id
            # newtruck.x = truck.x
            # newtruck.y = truck.y
        connect.isAmazon = False
        self.send_data(connect)
        self.recv_data(connected)
        #self.file_handle.write(connected.result)
        self.world_id = connected.worldid
        th_handler = threading.Thread(target=self.handler, args=())
        th_handler.setDaemon(True)
        th_handler.start()
Ejemplo n.º 2
0
def recvMsg(socket, msgType):
    var_int_buff = []
    while True:
        buf = socket.recv(1)
        var_int_buff += buf
        msg_len, new_pos = _DecodeVarint32(var_int_buff, 0)
        if new_pos != 0:
            break
    whole_message = socket.recv(msg_len)

    if msgType == "UConnected":
        msg = wu.UConnected()
        msg.ParseFromString(whole_message)
    elif msgType == "UResponses":
        msg = wu.UResponses()
        msg.ParseFromString(whole_message)
    elif msgType == "AMessages":
        msg = ua.AMessages()
        msg.ParseFromString(whole_message)
    else:
        print("Receive an undefined message.")
        return
    print("------------")
    print("recv:", msg)
    print("------------")

    return msg
Ejemplo n.º 3
0
def init_world(world_fd, db_cur, db_conn):
    UConnect = world_ups_pb2.UConnect()
    UConnect.isAmazon = False
    #UConnect.worldid = 1
    for i in range(1, 3):
        car1 = UConnect.trucks.add()
        car1.id = i
        car1.x = i
        car1.y = i

    send_to_world(UConnect, world_fd)
    UConnected = world_ups_pb2.UConnected()
    UConnected = recv_from_world(UConnected, world_fd)
    world_id = UConnected.worldid

    for i in range(1, 3):
        db_cur.execute("insert into truck "
                       "(worldid, truckid, packageid, location_x,"
                       "location_y, status) values('" + str(world_id) +
                       "', '" + str(i) + "', " + '0' + ", '" + str(i) +
                       "', '" + str(i) + "', '" + 'I' + "')")
    print(UConnected)
    db_conn.commit()
    UComm = world_ups_pb2.UCommands()
    UComm.simspeed = 50000
    send_to_world(UComm, world_fd)
    return world_id
Ejemplo n.º 4
0
def URecv(sock, isUConnect=False):
    all_data = b''
    data = sock.recv(4)
    if not data:
        print('connection to world is closed')
    data_len, new_pos = _DecodeVarint32(data, 0)
    all_data += data[new_pos:]

    data_left = data_len - len(all_data)
    while True:
        data = sock.recv(data_left)
        all_data += data
        data_left -= len(data)

        if data_left <= 0:
            break

    if isUConnect:
        msg = wupb.UConnected()
        msg.ParseFromString(all_data)
        return msg

    msg = wupb.UResponses()
    msg.ParseFromString(all_data)
    return msg
Ejemplo n.º 5
0
def connect_world_simulator():
    # UConnect message to world simulator
    uconnect = world_ups_pb2.UConnect()
    uconnect.trucks.extend([truck1, truck2])
    uconnect.isAmazon = False
    conn_world_req = uconnect.SerializeToString()
    _EncodeVarint(conn_world.sendall, len(conn_world_req))
    conn_world.sendall(conn_world_req)

    # UConnected from world simulator
    uconnected = world_ups_pb2.UConnected()
    data = recv_world_id()
    uconnected.ParseFromString(data)
    print("world simulator status: ", uconnected.result)
    return uconnected.worldid
Ejemplo n.º 6
0
def connect_to_world_again(truck_number, world_id):
    global world_socket
    uconnect = world_ups_pb2.UConnect()
    uconnect.isAmazon = False
    uconnect.worldid = world_id

    #send UConnect to world
    send_msg(world_socket, uconnect.SerializeToString())
    print("[DEBUG] Sent UConnect")

    #receive UConnected from world
    received = receive_msg(world_socket)

    uconnected = world_ups_pb2.UConnected()
    uconnected.ParseFromString(received)

    print("[DEBUG] Received MSG: " + uconnected.result)
Ejemplo n.º 7
0
def connect_to_world(truck_number):
    global world_socket
    uconnect = world_ups_pb2.UConnect()
    uconnect.isAmazon = False
    '''
    ===============================================================
    TO DO (FINISHED & TESTED):
    
    1. Finish the database, initialize the truck table.
    2. Do not forget to drop the original table.

    '''

    # Connect to databse and initialize the table
    conn = connect_to_database()
    cur = conn.cursor()
    cur.execute("truncate table ups_package;")
    cur.execute("truncate table ups_truck;")

    for num in range(1, truck_number + 1):
        truck = uconnect.trucks.add()
        truck.id = num
        truck.x = 1
        truck.y = 1

        cur.execute("insert into ups_truck (truckid, truckstatus) values ('" +
                    str(num) + "', 'idle')")

    conn.commit()
    cur.close()
    conn.close()

    #send UConnect to world
    send_msg(world_socket, uconnect.SerializeToString())
    print("[DEBUG] Sent UConnect")

    #receive UConnected from world
    received = receive_msg(world_socket)

    uconnected = world_ups_pb2.UConnected()
    uconnected.ParseFromString(received)
    worldid = uconnected.worldid

    print("[DEBUG] Received MSG: " + uconnected.result)
    return worldid
Ejemplo n.º 8
0
def parse_connected_msg(connected_msg_str):
    msg = world_ups_pb2.UConnected()
    msg.ParseFromString(connected_msg_str)
    print("ups received UConnected: " + msg.result)
Ejemplo n.º 9
0
def parse_connected_msg(connected_msg_str):
    msg = world_ups_pb2.UConnected()
    msg.ParseFromString(connected_msg_str)
    print("world_th ups received UConnected: " + msg.result)
    print("world_th ups received worldid: " + str(msg.worldid))
    return msg.worldid
Ejemplo n.º 10
0
def parse_uconnected(msg_str):
    msg = world_ups_pb2.UConnected()
    msg.ParseFromString(msg_str)
    return msg
Ejemplo n.º 11
0
 def connect_to_world(self):
     send_msg_str_to_wld(uconnect_msg().SerializeToString(), self.skt)
     uconnected_msg = world_ups_pb2.UConnected()
     uconnected_msg.ParseFromString(recv_msg_str_from_wld(self.skt))
     self.out_q.put(uconnected_msg)