Esempio n. 1
0
def GS2CChat(sock_fd, msg):
    packer = C_net.CPacker()
    packer.pack_int(0x01)
    packer.pack_char(1)
    packer.pack_short(2)
    packer.pack_int(10)
    packer.pack_log(10000)
    packer.pack_string(msg)
    network.send_msg(sock_fd, packer.get_data())
    print("GS2CChat = {}".format(msg))
Esempio n. 2
0
def threaded_client(conn, player):
    global game
    # send clinet player
    msg = game.players[player]
    send_msg(conn, msg)
    # devider
    conn.recv(1)
    # send overall game
    msg = game
    send_msg(conn, msg)

    reply = ""
    while True:
        try:
            data = recv(conn)

            if not data:
                print("Disconnected")
                break
            else:
                if data != "get" and data != "r":
                    game.play(data)
                elif data == "r":
                    game.game_status = 'r'
                    for dead in game.just_died:
                        game.board[dead.row][dead.col] = None

                reply = game

            if game.animation and player == game.animation.id:
                animation = game.animation

                if animation.start < 30:
                    animation.start += 1
                else:
                    animation.start = 1
                    if animation.winner:
                        game.board[animation.winner.row][
                            animation.winner.col] = None
                        if (animation.winner.row,
                                animation.winner.col) in game.revealed_pieces:
                            index = game.revealed_pieces.index(
                                (animation.winner.row, animation.winner.col))
                            game.revealed_pieces[index] = (
                                game.revealed_pieces[index][0] +
                                animation.direction[0],
                                game.revealed_pieces[index][1] +
                                animation.direction[1])
                        animation.winner.row += animation.direction[0]
                        animation.winner.col += animation.direction[1]
                        game.board[animation.winner.row][
                            animation.winner.col] = animation.winner
                        if animation.winner.row == animation.end[
                                0] and animation.winner.col == animation.end[1]:
                            game.board[animation.just_died[0].row][
                                animation.just_died[0].col] = None
                            game.board[animation.end[0]][
                                animation.end[1]] = animation.winner
                            game.animation = None
                            game.revealed_pieces = [(animation.end[0],
                                                     animation.end[1])]

                    else:
                        for dead in animation.just_died:
                            if dead.row != animation.end[
                                    0] or dead.col != animation.end[1]:
                                if (dead.row,
                                        dead.col) in game.revealed_pieces:
                                    index = game.revealed_pieces.index(
                                        (dead.row, dead.col))
                                    game.revealed_pieces[index] = (
                                        game.revealed_pieces[index][0] +
                                        animation.direction[0],
                                        game.revealed_pieces[index][1] +
                                        animation.direction[1])
                                game.board[dead.row][dead.col] = None
                                dead.row += animation.direction[0]
                                dead.col += animation.direction[1]
                                game.board[dead.row][dead.col] = dead

                        if animation.just_died[0].row == animation.just_died[
                                1].row and animation.just_died[
                                    0].col == animation.just_died[1].col:
                            game.board[animation.just_died[0].row][
                                animation.just_died[0].col] = None
                            game.animation = None

                #print("Received: ", data)
                #print("Sending : ", reply)
            #print(reply)
            #print(pickle.loads(reply))
            send_msg(conn, reply)
        except Exception as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            print(exc_type, fname, exc_tb.tb_lineno, str(e))
            break

    print("Lost connection")
    conn.close()
Esempio n. 3
0
def processer(stop_signal):
    """ Processer thread """
    global chain, transaction_queue, proposed_block, send_msg, set_delay, schedule_proposer, schedule_interrupt
    print("processing")
    while True:
        while task_queue.empty():
            if stop_signal():
                break
        if not task_queue.empty():
            task = task_queue.get()
            # print('task:', str(task)[:60])

        if task['type'] == 'console':
            if len(task['args']) == 0:
                pass

            elif task['args'][0] == 'propose' or task['args'][0] == 'p':
                propose()

            elif task['args'][0] == 'moneyTransfer' or task['args'][0] == 't':
                sender = int(task['args'][1])
                receiver = int(task['args'][2])
                amount = float(task['args'][3])
                trans = Transaction(sender, receiver, amount)
                if sender == receiver:
                    print('Sender and receiver cannot be the same')
                    continue
                # compute current balance after transactions in queue
                current_balance = apply_transactions(copy(chain.balance),
                                                     transaction_queue)
                if apply_transactions(current_balance, [trans]):
                    print("Valid transaction.")
                    transaction_queue.append(trans)
                    schedule_proposer()
                else:
                    print('Insufficient balance.')

            elif task['args'][0] == 'printBlockchain' or task['args'][
                    0] == 'pc':
                print(chain)
            elif task['args'][0] == 'printBalance' or task['args'][0] == 'pb':
                print(chain.balance)
            elif task['args'][0] == 'printQueue' or task['args'][0] == 'pq':
                print(transaction_queue)
            elif task['args'][0] == 'printPaxos' or task['args'][0] == 'pp':
                paxos.print()

            elif task['args'][0] == 'update':  # debug
                send_msg(int(task['args'][1]), {
                    'type': 'chain-reply',
                    'chain': chain
                })

            elif task['args'][0] == 'failLink':
                link_off(int(task['args'][1]))
            elif task['args'][0] == 'fixLink':
                link_on(int(task['args'][1]))
            elif task['args'][0] == 'failProcess':
                return
            elif task['args'][0] == 'delay':
                set_delay(float(task['args'][1]))
            elif task['args'][0] == 'save':
                save()
            elif task['args'][0] == 'load':
                load()
                print(paxos)
                print(transaction_queue)

        elif task['type'] == 'msg-prepare':
            # print('msg-prepare')
            paxos.recv_prepare(task)

        elif task['type'] == 'msg-promise':
            # print('msg-promise')
            paxos.recv_promise(task)

        elif task['type'] == 'msg-accept':
            # print('msg-accept')
            paxos.recv_accept(task)

        elif task['type'] == 'msg-accepted':
            # print('msg-accepted')
            paxos.recv_accepted(task)

        elif task['type'] == 'msg-decision':
            # print('msg-decision')
            paxos.recv_decision(task)

        elif task['type'] == 'chain-request':
            # print('chain-request')
            send_msg(task['from'], {'type': 'chain-reply', 'chain': chain})

        elif task['type'] == 'chain-reply':
            # print('chain-reply')
            received_chain = task['chain']
            if received_chain.depth > chain.depth:
                # reset chain, paxos, queue
                chain = received_chain
                schedule_interrupt = True
                paxos.update_depth(chain.depth)
                transaction_queue = []
                proposed_block = None
                print(
                    'Chain updated. All transactions in queue are discarded.')

        save()  #save after every task
Esempio n. 4
0
def on_offer(self, pid):
    global chain
    send_msg(pid, {'type': 'chain-reply', 'chain': chain})
Esempio n. 5
0
def on_inconsistent_depth(self, pid):
    # print('on_inconsistent_depth')
    send_msg(pid, {'type': 'chain-request', 'from': my_pid})
Esempio n. 6
0
    def run(self):

        global start_time
        global stoped_clock

        while True:

            msg = network.recv_msg(self.socket)

            if msg is not None:

                (header, payload) = pickle.loads(msg)

                if header == 'start':

                    print('start')

                    self.barrier.wait(
                    )  # wait for all nodes to send start message

                    self.model_lock.acquire()
                    if start_time == 0:
                        start_time = time.time()
                    self.model_lock.release()

                    p = pickle.dumps(self.init_weights, -1)
                    self.barrier.wait(
                    )  # wait until all workers are ready to receive the initial weights

                    network.send_msg(self.socket,
                                     p)  # send initial weights to the worker

                elif header == 'train':  # received new gradients

                    grads_and_vars = payload

                    if self.sync_mode == 'sync':
                        self.model_lock.acquire()
                        self.optimizer.apply_gradients(
                            zip(grads_and_vars,
                                self.model.trainable_variables))
                        self.model_lock.release()

                        self.barrier.wait()  # wait for all workers
                        new_weights = self.model.get_weights()

                    else:  # no need to wait for workers here
                        self.model_lock.acquire()
                        self.optimizer.apply_gradients(
                            zip(grads_and_vars,
                                self.model.trainable_variables))
                        new_weights = self.model.get_weights()
                        self.model_lock.release()

                    p = pickle.dumps(new_weights, -1)
                    network.send_msg(self.socket, p)

                    pass

                elif header == 'end':

                    print('end')

                    self.barrier.wait()  # wait for all workers to finish

                    self.model_lock.acquire()
                    if not stoped_clock:
                        elapsed_time = time.time() - start_time
                        stoped_clock = True
                        print('elapsed time = ' + str(elapsed_time))

                        if self.sup_socket is not None:
                            p = pickle.dumps(('done', elapsed_time), -1)
                            msg = struct.pack('>I', len(p)) + p
                            self.sup_socket.sendall(msg)

                    self.model_lock.release()
                    break

                else:
                    print('Received unknown message')

            else:
                print('Connection closed')
                self.socket.close()

        pass
Esempio n. 7
0
def train(batch_size,
          model_fn=None,
          dataset_fn=None,
          num_epochs=1,
          index=0,
          n_nodes=0,
          cpu_affinity=False,
          server_address='127.0.0.1'):

    if cpu_affinity:
        p = psutil.Process()
        p.cpu_affinity([index + 1])  # cpu 0 is parameter server

    batch_size = int(batch_size)
    sock = None

    is_connected = False
    while not is_connected:  # connect to the parameter server
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect((server_address, 6000))
            is_connected = True
        except ConnectionRefusedError:
            time.sleep(1)

    dataset = dataset_fn(batch_size, index=index)  # get dataset
    model = model_fn()  # get model

    p = pickle.dumps(('start', 'payload'), -1)
    network.send_msg(sock, p)  # send initial message to parameter server

    p = network.recv_msg(sock)  # wait and receive initial weights
    init_weights = pickle.loads(p)
    model.set_weights(init_weights)  # set initial weights

    iteration = 0

    for epoch in range(num_epochs):

        for x, y in dataset:

            x = tf.cast(x, tf.float32)
            y = tf.cast(y, 'int32')

            loss_value, grads_and_vars = grad(model, x,
                                              y)  # calculate gradients

            p = pickle.dumps(('train', grads_and_vars), -1)
            network.send_msg(sock, p)  # send new gradients to parameter server

            p = network.recv_msg(
                sock)  # wait and receive new weights from parameter server
            weights = pickle.loads(p)
            model.set_weights(weights)  # load new weights to the model

            if iteration % 50 == 0:
                print(iteration)

            iteration += 1

    p = pickle.dumps(('end', 'end'), -1)
    network.send_msg(sock, p)
    sock.close()
Esempio n. 8
0
def threaded_client(conn, this_player_id):
    """
    This function serves and handles one client
    """
    global game

    # recieves player request to join game
    this_player = None
    while True:
        # recieves player team & role
        try:
            this_player_team, this_player_role = recv(conn)
        except Exception as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            print(exc_type, fname, exc_tb.tb_lineno, str(e))
            break

        # checks if role is not taken
        for player in game.players:
            if player.online and player.role == this_player_role and player.team == this_player_team:
                send_msg(conn, "role taken")
                break

        # if role is accepted
        else:
            send_msg(conn, "role accepted")
            this_player = game.add_new_player(this_player_team, this_player_role)
            break

    # receives and handles requests and notifications from user
    current_player_state = this_player.get_state(game)
    while this_player:
        try:
            if this_player.submarine.surfacing and not game.is_stopped:
                if time.time() > this_player.submarine.surfacing + this_player.submarine.surface_duration:
                    this_player.submarine.surfacing = False
                    this_player.submarine.surface_duration = SURFACE_DURATION

            data = recv(conn, blocking=False)

            if data:

                if data == "get": # client init
                    current_player_state = this_player.get_state(game)

                elif "clicked" in data: # user acted and the client will send what he clicked on
                    target_clicked = recv(conn)
                    this_player.clicked(game, target_clicked)
                    current_player_state = this_player.get_state(game)

                # captain stuff
                elif data == "captain stop":
                    if not game.power_in_action:
                        game.power_in_action = Power(this_player)
                        game.is_stopped = True
                        for submarine in game.submarines:
                            if submarine.surfacing:
                                submarine.surface_duration = submarine.surface_duration - (time.time() - submarine.surfacing)
                    current_player_state = this_player.get_state(game)

                elif data == "captain resume":
                    if game.power_in_action:
                        game.power_in_action.resume(game)
                    current_player_state = this_player.get_state(game)

                elif data == "captain sonar answer":
                    answer_data = recv(conn)
                    if game.power_in_action and game.power_in_action.action_type == ActionType.SONAR:
                        game.power_in_action.answer_accepted(game, answer_data)
                    current_player_state = this_player.get_state(game)

                elif "captain submitted" in data:
                    action_type_submitted = int(data.split(' ')[-1])
                    this_player.start_power(game, action_type_submitted)

                    current_player_state = this_player.get_state(game)

                send_msg(conn, tuple(current_player_state))

            # update client for the new state if needed
            if this_player.get_state(game) != current_player_state:
                send_msg(conn, "sending game state")
                current_player_state = this_player.get_state(game)
                send_msg(conn, tuple(current_player_state))

        except Exception as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            print(exc_type, fname, exc_tb.tb_lineno, str(e))
            break

    print("Lost connection")
    conn.close()
    this_player.disconnected()