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
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()
def connectWorld(socket, worldid): msgUW = wu.UConnect() msgUW.isAmazon = False msgUW.worldid = worldid sendMsg(socket, msgUW) msg = recvMsg(socket, "UConnected") if msg.result == "connected!": print("Connect to World %d!" % msg.worldid) else: print("Connection to World %d fails, %s" % (msg.worldid, msg.result)) return msg
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
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)
def get_world_id(Cw, world, Cdb): UCon_msg = wupb.UConnect() UCon_msg.isAmazon = False if world > 0: UCon_msg.worldid = world else: for i in range(5): t = UCon_msg.trucks.add() t.id = i t.x = 10 t.y = 10 insert_truck(Cdb, i, 'idle') send_msg(Cw, UCon_msg) res = world_ups_recv(Cw, True) print('World connection status: ', res.result) return res.worldid
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
def createWorld(socket, db): msgUW = wu.UConnect() msgUW.isAmazon = False for i in range(50): truck = msgUW.trucks.add() truck.id = i truck.x = 100 truck.y = 100 addTruck(db, i) sendMsg(socket, msgUW) msg = recvMsg(socket, "UConnected") if msg.result == "connected!": print("New World %d is created!" % msg.worldid) else: print("Fail to create a new World, %s" % msg.result) return msg
def GetWorld(wSock, world, db): print('getting world id') connMsg = wupb.UConnect() connMsg.isAmazon = False if world == 0: num_truck = 100 # store trucks in database while put it in connMsg for i in range(num_truck): t = connMsg.trucks.add() t.id = i t.x = 99 t.y = 99 Inserttruck(db, i, 'idle') if world > 0: connMsg.worldid = world Send(wSock, connMsg) res = URecv(wSock, True) print('World connection status: ', res.result) return res.worldid
def connect_to_world(): connect_msg = world_ups_pb2.UConnect() connect_msg.isAmazon = False connect_msg.trucks = init_truck() return connect_msg
def uconnect_msg(): connect_msg = world_ups_pb2.UConnect() connect_msg.isAmazon = False for i in range(1, 1 + TRUCK_NUM): connect_msg.trucks.add(id=i, x=i, y=i) return connect_msg