def connect_to_world(self, addr=worldaddr): self.worldsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.worldsocket.connect(addr) request = amazon_pb2.AConnect() request.worldid = self.worldid wh = request.initwh.add() wh.id = 1 wh.x = 1 wh.y = 1 request.isAmazon = True print(request) send_request(self.worldsocket, request) while True: whole_message = recv_response(self.worldsocket) response = amazon_pb2.AConnected() try: response.ParseFromString(whole_message) except: continue print(response) print("World id is %d" % response.worldid) break
listen_socket_web.bind((HOST, PORT)) listen_socket_web.listen(200) client_connection, client_address = listen_socket_web.accept() request = client_connection.recv(10400) print(request.decode('utf-8')) client_connection.close() xml_request = request.decode('utf-8') Handler = web_requestHandler() parseString(xml_request, Handler) #Receive the command used to connect the world and create our warehouses if Handler.commandtype == "createWarehouse": s_command = amazon_pb2.AConnect() s_command.worldid = int(wid) s_command.isAmazon = True warehouse_num = uni_int(Handler.address_X) for i in range(1, uni_int(Handler.address_X) + 1): warehouse = s_command.initwh.add() warehouse.id = i warehouse.x = i warehouse.y = i socketonly = connectWorld(s_command) #handle the communication with amazonWeb, World and UPS in different thread threadamazon = threading.Thread(target=amazonWeb, args=( listen_socket_web, conn,
def Connect(_worldid): connect = amazon_pb2.AConnect() connect.worldid = _worldid return connect
mutex_django.acquire(1) msg_queue.put(command_msg) mutex_django.release() if __name__ == "__main__": # Create sockets: django, warehouse, UPS socket_dj_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket_wh_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket_ups_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Build up the connection socket_wh_client.connect((WH_HOST, WH_PORT)) # Connect socket_ups_client.connect((UPS_HOST, UPS_PORT)) # Connect to warehouse world connect_msg = amazon_pb2.AConnect() connect_msg.worldid = 1007 send_msg(socket_wh_client, connect_msg) Recv_Connected(recv_msg_4B(socket_wh_client)) # Send Default simulated speed speed = amazon_pb2.ACommands() speed.simspeed = 10000 send_msg(socket_wh_client, speed) Recv_Responses(recv_msg_4B(socket_wh_client), msg_queue, ups_queue, mutex_django, mutex_ups) # Bind port for django server socket try: socket_dj_server.bind((SELF_HOST, SELF_PORT)) except socket.error as msg:
def AConnect(self): msg = amazon_pb2.AConnect() msg.worldid = 1000 self.send(msg) self.recv()
def init(request): # populate database for both sim and c++/py # this part is to make the api idempotent (at most once) if Product.objects.all().exists() or Warehouse.objects.all().exists(): return render(request, 'store/cart.html', {"info": "Already inited, not doing anything."}) sim_connect = amazon_pb2.AConnect() sim_connect.worldid = WORLD sim_connect_bytes = sim_connect.SerializeToString() try: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.settimeout(SOCKET_TIMEOUT) bytes_size = len(sim_connect_bytes) print("size is {0}".format(bytes_size)) s.connect((SIM_HOST, SIM_PORT)) varintEncoder(s.send, bytes_size) s.sendall(sim_connect_bytes) a = s.recv(1024) # handle this warehouses = [] for w in range(N_WAREHOUSES): w_local = Warehouse(x_coord=0, y_coord=0, truck_id=-1) w_local.save() warehouses.append(w_local) command_pb = amazon_pb2.ACommands() command_pb.simspeed = 40000000 command_pb.disconnect = True prod_map = {} for name, tup in PRODUCTS.items(): count, price, description = tup p_local = Product(name=name, description=description, rating=0, num_ratings=0) p_local.save() prod_map[name] = p_local for w in warehouses: purchase_pb = command_pb.buy.add() # purchase_pb = amazon_pb2.APurchaseMore() purchase_pb.whnum = w.id for name, tup in PRODUCTS.items(): count, price, description = tup i_local = Inventory(product=prod_map[name], count=count, price=price, warehouse=w) i_local.save() thing = purchase_pb.things.add() thing.id = prod_map[name].id thing.description = description thing.count = count command_pb_bytes = command_pb.SerializeToString() print(command_pb_bytes) bytes_size = len(command_pb_bytes) print("size is {0}".format(bytes_size)) varintEncoder(s.send, bytes_size) s.sendall(command_pb_bytes) a = s.recv(1024) # handle this print(a) s.close() except socket.timeout: return render( request, 'store/cart.html', {"message": "backend error: socket timed out/conn refused."}) except ConnectionRefusedError: return render( request, 'store/cart.html', {"message": "backend error: socket timed out/conn refused."}) print("done") return render(request, 'store/cart.html', {"info": "init succeeded."})