当前位置: 首页>>代码示例>>Python>>正文


Python Network.broadcast_to_server方法代码示例

本文整理汇总了Python中network.Network.broadcast_to_server方法的典型用法代码示例。如果您正苦于以下问题:Python Network.broadcast_to_server方法的具体用法?Python Network.broadcast_to_server怎么用?Python Network.broadcast_to_server使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在network.Network的用法示例。


在下文中一共展示了Network.broadcast_to_server方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import broadcast_to_server [as 别名]
class Server:

    TIME_HEARTBEAT = 3

    '''
    @id: [0 .. num_nodes-1].
    @is_leader  is set to `True` when creating Server0, otherwise, to `False`.
    @is_replica is set to `True` if node_id is less than f+1
    @num_nodes  is used to identify the broadcast range.
    @uid        is used for printing the messages.
    @current_leader is updated when receiving heartbeat from the leader.
    Every server is a replica.
    '''
    def __init__(self, node_id, is_leader, num_nodes, num_clients, is_recover):
        self.node_id = node_id
        self.uid = "Server#" +  str(node_id)
        self.num_nodes = num_nodes
        self.num_clients = num_clients

        # network controller
        self.nt = Network(self.uid, num_nodes, num_clients)
        try:
            self.t_recv = Thread(target=self.receive)
            self.t_recv.start()
        except:
            print(self.uid, "error: unable to start new thread")

        # acceptor
        # acceptor must be started first
        self.acceptor = Acceptor(self.node_id, self.nt)

        # Leaders
        self.count_heartbeat = 4 # greater than 3
        self.is_leader       = is_leader
        self.leader_dead     = False
        if is_leader:
            time.sleep(2) # wait for other servers to start
            self.leader = Leader(node_id, self.num_nodes, self.nt)
            self.leader.init_scout()
            if TERM_LOG:
                print(self.uid, "is leader")
        self.current_leader = -1 # updated when receiving leader's heartbeat
                                 # remember to update replica.leader_id and leader

        # Replicas
        max_faulty = (num_nodes - 1) / 2 # f in the paper
        # if (node_id <= max_faulty):
        if node_id < num_nodes: # set all servers as replicas here
            # f+1  servers are replicas
            # 2f+1 (all)  servers are acceptors
            self.is_replica = False
            if is_recover:
                self.waitfor_replica = True
                self.replica_all_down = False
                self.broadcast_to_server(
                    str(("requestReplicaInfo", self.node_id)))
                threading.Timer(self.TIME_HEARTBEAT*2,
                                self.set_replica_all_down).start()
                while self.waitfor_replica and not self.replica_all_down:
                    pass
                if not self.replica_all_down:
                    self.replica = Replica(node_id, self.nt, self.recv_slot_num,
                                       self.recv_decisions)
                else:
                    self.replica = Replica(node_id, self.nt)
            else:
                self.replica = Replica(node_id, self.nt)
            self.is_replica = True
        else:
            self.is_replica = False

        # leader broadcasts heartbeat
        self.rev_heartbeat = True # whether receive heartbeat in current period
        if is_leader:
            self.broadcast_heartbeat()
        self.check_heartbeat()

        # notice Master that it starts
        self.nt.send_to_master(str(("serverStarted", self.node_id)))

    def set_replica_all_down(self):
        self.replica_all_down = True

    def broadcast_to_server(self, message):
        self.nt.broadcast_to_server(message)

    def broadcast_to_client(self, message):
        self.nt.broadcast_to_client(message)

    def send_to_server(self, dest_id, message):
        self.nt.send_to_server(dest_id, message)

    def send_to_client(self, dest_id, message):
        self.nt.send_to_client(dest_id, message)

    def receive(self):
        while 1:
            buf = self.nt.receive()
            if len(buf) > 0:
                # TODO: handle the received value
#.........这里部分代码省略.........
开发者ID:yuhc,项目名称:paxos-chatroom,代码行数:103,代码来源:node.py

示例2: __init__

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import broadcast_to_server [as 别名]
class Client:

    REQUEST_TIME = 3
    TIME_ALLCLEAR = 2

    """
    @uid is used for printing the messages.
    """

    def __init__(self, client_id, num_nodes):
        self.client_id = client_id
        self.command_id = 0
        self.num_nodes = num_nodes
        self.uid = "Client#" + str(client_id)
        self.chatlog = []
        self.queue = []  # all waiting messages
        self.history = set()  # all received messages
        self.leader_id = 0  # default leader is server#0
        self.counter = 0  # periodically resend messages

        # network controller
        self.nt = Network(self.uid, self.num_nodes)
        try:
            self.t_recv = Thread(target=self.receive)
            self.t_recv.start()
        except:
            print(self.uid, "error: unable to start new thread")

        # send requests periodically
        # self.period_request()

    def send(self, message):
        self.nt.broadcast_to_server(message)

    def send_request(self, triple):
        encode = str(("request", triple))
        # broadcast to all replicas or servers
        self.send(encode)

    def period_request(self):
        for tp in self.queue:
            self.send_request(tp)
        # threading.Timer(self.REQUEST_TIME, self.period_request).start()

    def monitor_queue(self):
        while self.queue:
            self.counter = self.counter + 1
            if self.counter == 5:
                if TERM_LOG:
                    print(self.uid, "sends initLeader")
                self.nt.broadcast_to_server(str(("initLeader", self.client_id)))  # reset current leader
            if self.counter >= 15:
                if TERM_LOG:
                    print(self.uid, "allClear timeout")
                self.counter = 0
                break
            time.sleep(self.TIME_ALLCLEAR)
        self.nt.send_to_master(str(("allCleared", self.client_id)))

    def receive(self):
        while 1:
            buf = self.nt.receive()
            if len(buf) > 0:
                # handle the received value
                if TERM_LOG:
                    print(self.uid, "handles", buf)
                buf = literal_eval(buf)
                # buf = buf.split()

                # send request to server (leader)
                # should we send requests to all replicas?
                if buf[0] == "sendMessage":
                    self.command_id = self.command_id + 1
                    triple = (self.client_id, self.command_id, buf[1])
                    self.queue.append(triple)
                    self.send_request(triple)

                # clear the message queue
                if buf[0] == "allClear":
                    self.counter = 0
                    threading.Timer(0, self.monitor_queue).start()

                # print chat log
                if buf[0] == "printChatLog":
                    log = ""
                    if TERM_LOG:
                        print(self.uid, "prints chat log")
                    for l in self.chatlog:
                        # TODO: may need index_number instead of slot_num
                        if TERM_LOG:
                            print(self.uid, ">>", l[1] - 1, l[0], l[2])
                        # else:
                        # print(l[1]-1, " ", l[0], ": ", l[2], sep='')
                        log = log + "{0} {1}: {2}\n".format(l[1] - 1, l[0], l[2])
                    self.nt.send_to_master(str(("chatLog", log.strip())))

                # receive response from leader, send ask to master
                # format: (response, client_id, cid, (index, chat))
                if buf[0] == "response":
                    if buf[1] == self.client_id:
#.........这里部分代码省略.........
开发者ID:yuhc,项目名称:paxos-chatroom,代码行数:103,代码来源:client.py

示例3: str

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import broadcast_to_server [as 别名]
            """ Print out the client specified by client_index's chat history
                in the format described on the handout """
            waitfor_chatlog = True
            nt.send_to_client(client_index, str(("printChatLog", 0)))
            # ensure the log has been printed
            while waitfor_chatlog:
                pass

        if line[0] == 'allClear':
            """ Ensure that this blocks until all messages that are going to
                come to consensus in PAXOS do, and that all clients have heard
                of them """
            waitfor_clear = set(range(num_clients))
            waitfor_leader_resp = True
            nt.broadcast_to_client(str(("allClear", 0)))
            nt.broadcast_to_server(str(("leaderAlive", 0)))
            while waitfor_clear or waitfor_leader_resp:
                if TERM_LOG:
                    time.sleep(CLEAR_TIME)
                    print(uid, "waits for allClear")
                pass

        if line[0] == 'crashServer':
            node_index = int(line[1])
            """ Immediately crash the server specified by node_index """
            if node_index in range(num_nodes):
                if nodes[node_index] != None:
                    os.kill(nodes[node_index], signal.SIGKILL)
                    nodes[node_index] = None
                else:
                    if TERM_LOG:
开发者ID:yuhc,项目名称:paxos-chatroom,代码行数:33,代码来源:Master.py


注:本文中的network.Network.broadcast_to_server方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。