本文整理汇总了Python中network.Network.send_to_client方法的典型用法代码示例。如果您正苦于以下问题:Python Network.send_to_client方法的具体用法?Python Network.send_to_client怎么用?Python Network.send_to_client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类network.Network
的用法示例。
在下文中一共展示了Network.send_to_client方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import send_to_client [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
#.........这里部分代码省略.........
示例2: str
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import send_to_client [as 别名]
str(num_nodes), str(num_clients),
str(False)])
nodes.append(p.pid)
leader_id = 0
if TERM_LOG:
print("Server#", i, " pid:", p.pid, sep="")
# ensure the establish of sockets
while waitfor_server and waitfor_leader:
pass
if line[0] == 'sendMessage':
client_index = int(line[1])
message = ''.join(str(item) for item in line[2::])
""" Instruct the client specified by client_index to send the message
to the proper paxos node """
nt.send_to_client(client_index, str(("sendMessage", message)))
if line[0] == 'printChatLog':
client_index = int(line[1])
""" 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 """