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


Python Network.send_to_master方法代码示例

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


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

示例1: __init__

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

    def __init__(self, node_id):
        self.node_id    = node_id
        self.uid        = "Client#" + str(node_id)

        self.version_vector = {} # session guarantees

        # create the network controller
        self.connected_server   = None # which server is connected
        self.nt                 = Network(self.uid)
        self.can_send_to_server = True
        try:
            self.t_recv = Thread(target=self.receive)
            self.t_recv.daemon = True
            self.t_recv.start()
        except:
            print(self.uid, "error: unable to start new thread")

    def receive(self):
        while 1:
            buf = self.nt.receive()
            if buf:
                if TERM_LOG:
                    print(self.uid, "handles:", str(buf))
                # TODO: parse buf

                if buf.mtype == "Put":
                    w = Write(self.node_id, None, "Put", None, 0,
                              (buf.content, self.version_vector))
                    m_put = Message(self.node_id, None, "Write", w)
                    c_can_send_to_server.acquire()
                    while True:
                        if self.can_send_to_server:
                            break
                        c_can_send_to_server.wait()
                    self.nt.send_to_node(self.connected_server, m_put)
                    c_can_send_to_server.release()

                elif buf.mtype == "Get":
                    m_get = Message(self.node_id, None, "Get", buf.content)
                    if TERM_LOG:
                        print(self.uid, "tries to acquire c_can_send_to_server in receive.Get")
                    c_can_send_to_server.acquire()
                    if TERM_LOG:
                        print(self.uid, "acquires c_can_send_to_server in receive.Get")
                    while True:
                        if self.can_send_to_server:
                            break
                        c_can_send_to_server.wait()
                    self.nt.send_to_node(self.connected_server, m_get)
                    c_can_send_to_server.release()
                    if TERM_LOG:
                        print(self.uid, "releases c_can_send_to_server in receive.Get")

                elif buf.mtype == "Delete":
                    w = Write(self.node_id, None, "Delete", None, 0,
                              (buf.content, self.version_vector))
                    m_delete = Message(self.node_id, None, "Write", w)
                    c_can_send_to_server.acquire()
                    while True:
                        if self.can_send_to_server:
                            break
                        c_can_send_to_server.wait()
                    self.nt.send_to_node(self.connected_server, m_delete)
                    c_can_send_to_server.release()

                elif buf.mtype == "GetAck":
                    (song_name, song_url, server_vv) = buf.content
                    get_content = ""
                    # print("read set: " + str(self.version_vector) +
                    #       "server_accept_time: " + str(server_vv))
                    err_dep = False
                    union_keys = set(self.version_vector.keys())\
                    .union(server_vv.keys())
                    for i in union_keys:
                        if not i in server_vv:
                            get_content = song_name+":ERR_DEP"
                            err_dep = True
                            break
                        elif i in self.version_vector and \
                             self.version_vector[i] > server_vv[i]:
                            get_content = song_name+":ERR_DEP"
                            err_dep = True
                            break
                    if not err_dep:
                        get_content = song_name+":"+song_url
                    for i in union_keys:
                        if i in server_vv and i in self.version_vector and\
                        self.version_vector[i] < server_vv[i]:
                            self.version_vector[i] = server_vv[i]
                        if not i in self.version_vector:
                            self.version_vector[i] = server_vv[i]
                    m_get_msg = Message(self.node_id, None, "MGetAck", get_content)
                    self.nt.send_to_master(m_get_msg)

                elif buf.mtype == "Done":
                    # print("read set: " + str(self.version_vector) + "server_accept_time: " + str(buf.content))
                    union_keys = set(self.version_vector.keys())\
                                     .union(buf.content.keys())
#.........这里部分代码省略.........
开发者ID:yuhc,项目名称:bayou-playlist,代码行数:103,代码来源:client.py

示例2: Condition

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

#.........这里部分代码省略.........
        if self.node_id in self.available_list:
            self.available_list.remove(self.node_id)
        if not self.available_list or self.is_paused:
            threading.Timer(
                random.randint(self.ANTI_ENTROPY_LOWER,
                               self.ANTI_ENTROPY_UPPER)/100.0,
                self.timer_anti_entropy
            ).start()
            return

        if LOCK_LOG:
            print(self.uid, "tries to acquire a c_antientropy lock in",
                  "timer_anti_entropy")
        self.c_antientropy.acquire()
        if LOCK_LOG:
            print(self.uid, "acquires a c_antientropy lock in",
                  "timer_anti_entropy")

        rand_dest = list(self.available_list)[0]

        if TERM_LOG:
            print(self.uid, " selects to anti-entropy with Server#",
                  rand_dest, sep="")
        succeed_anti = self.anti_entropy(rand_dest)

        # select a new primary
        if self.is_retired and succeed_anti:
            if self.is_primary:
                m_elect = Message(self.node_id, self.unique_id, "Elect",
                                  None)
                self.nt.send_to_node(rand_dest, m_elect)
            m_retire = Message(self.node_id, self.unique_id,
                               "RetireAck", None)
            self.nt.send_to_master(m_retire)

        # finish the anti-entropy
        if succeed_anti:
            self.available_list.remove(rand_dest)
            self.sent_list.add(rand_dest)
            m_finish = Message(self.node_id, self.unique_id, "AntiEn_Finsh",
                               None)
            self.nt.send_to_node(rand_dest, m_finish)

        if self.is_retired and succeed_anti:
            if TERM_LOG:
                print(self.uid, "kills itself")
            os.kill(os.getpid(), signal.SIGKILL)


        threading.Timer(
            random.randint(self.ANTI_ENTROPY_LOWER,
                           self.ANTI_ENTROPY_UPPER)/100.0,
            self.timer_anti_entropy
        ).start()

        self.c_antientropy.release()
        if LOCK_LOG:
            print(self.uid, "releases a c_antientropy lock in",
                  "timer_anti_entropy")

    '''
    Process a received message of type of AntiEntropy. Stated in the paper
    Flexible Update Propagation '''
    def anti_entropy(self, receiver_id):
        if TERM_LOG and DETAIL_LOG:
            print(self.uid, " requests anti-entropy to Server#",
开发者ID:yuhc,项目名称:bayou-playlist,代码行数:70,代码来源:server.py

示例3: __init__

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import send_to_master [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

示例4: __init__

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import send_to_master [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


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