本文整理汇总了Python中network.Network.receive方法的典型用法代码示例。如果您正苦于以下问题:Python Network.receive方法的具体用法?Python Network.receive怎么用?Python Network.receive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类network.Network
的用法示例。
在下文中一共展示了Network.receive方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import receive [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())
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import receive [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:
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import receive [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
#.........这里部分代码省略.........
示例4: Condition
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import receive [as 别名]
class Server:
ANTI_ENTROPY_LOWER = Config.anti_entropy_lower
ANTI_ENTROPY_UPPER = Config.anti_entropy_upper
c_create = Condition()
c_request_antientropy = Condition()
c_antientropy = Lock()
def __init__(self, node_id, is_primary):
self.node_id = node_id
self.uid = "Server#" + str(node_id)
self.unique_id = (1, node_id) # replica_id in lecture note, <clock,id>
self.is_primary = is_primary # first created server is the primary
self.is_retired = False
self.is_paused = False
self.first_creation = True # whether it is the first Creation
self.version_vector = {} # unique_id: accept_time
if is_primary:
self.version_vector[self.unique_id] = 1
self.playlist = {} # song_name: url
self.history = [] # list of history playlists
self.CSN = -1 # commit sequence number
self.accept_time = 1
self.committed_log = []
self.tentative_log = [] # list of writes
# create the network controller
self.server_list = set() # created by Creation Write
self.client_list = set() # modified by master
self.block_list = set() # list of blocked connections
self.available_list = set() # last available
self.sent_list = set() # has sent to theses servers
self.nt = Network(self.uid)
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")
random.seed(self.node_id)
if is_primary:
self.accept_time = self.accept_time + 1
w_create = Write(self.node_id, self.unique_id, "Creation", None,
self.accept_time, self.unique_id)
w_create.state = "COMMITTED"
self.bayou_write(w_create)
# start Anti-Entropy
threading.Timer(random.randint(4, 10)/10,
self.timer_anti_entropy).start()
def receive(self):
while 1:
buf = self.nt.receive()
if buf:
if TERM_LOG:
print(self.uid, "handles:", str(buf))
# TODO: parse buf
# update logical clock
if isinstance(buf.content, Write):
self.accept_time = max(self.accept_time,
buf.content.accept_time) + 1
if buf.mtype == "Creation":
if buf.sender_id < 0:
try:
Thread(target=self.notify,args=(buf.content,)
).start()
except:
print(self.uid, "error: unable to start new thread")
else:
w = buf.content
if w.content: # first creation
# rewrite Creation's wid
w.sender_uid = (self.accept_time, self.unique_id)
w.wid = (w.accept_time, w.sender_uid)
self.receive_server_writes(w)
m_create_ack = Message(self.node_id, self.unique_id,
"Creation_Ack", self.accept_time)
if not w.content:
m_create_ack.content = None
self.nt.send_to_node(buf.sender_id, m_create_ack)
elif buf.mtype == "Creation_Ack":
# buf.content contains sender's accept_time
if LOCK_LOG:
print(self.uid, "tries to acquire a c_antientropy lock",
"in receive.Creation_Ack")
self.c_create.acquire()
if LOCK_LOG:
#.........这里部分代码省略.........
示例5: __init__
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import receive [as 别名]
class API:
START_SERVER_TIME = Config.start_server_time
STABILIZE_TIME = Config.stabilize_time
def __init__(self):
self.nodes = [] # list of nodes
self.servers = {} # list of servers
self.clients = {} # list of clients
self.client_s = {} # list of clients' partner
self.has_received_log = False
self.has_received_res = False
self.has_retired_res = False
self.c_has_received_res = Condition()
self.uid = "Master#0"
self.nt = Network(self.uid)
try:
t_recv = Thread(target=self.receive)
t_recv.daemon = True
t_recv.start()
except:
print(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))
if buf.mtype == "Playlist":
if buf.content.strip():
print(buf.content.strip())
self.has_received_log = True
elif buf.mtype == "MGetAck":
print(buf.content)
self.c_has_received_res.acquire()
self.has_received_res = True
self.c_has_received_res.notify()
self.c_has_received_res.release()
elif buf.mtype == "Done": # done processing put/delete
self.c_has_received_res.acquire()
if TERM_LOG:
print(self.uid, "acquires c_has_received_res in receive.Done")
self.has_received_res = True
self.c_has_received_res.notify()
if TERM_LOG:
print(self.uid, "notifies c_has_received_res in receive.Done")
self.c_has_received_res.release()
if TERM_LOG:
print(self.uid, "releases c_has_received_res in receive.Done")
elif buf.mtype == "RetireAck":
self.c_has_received_res.acquire()
self.has_retired_res = True
self.c_has_received_res.notify()
self.c_has_received_res.release()
def joinServer(self, server_id):
if not server_id in self.nodes:
# the first server is also the primary
p = subprocess.Popen(["./src/server.py",
str(server_id),
str(False) if self.servers else str(True)])
self.servers[server_id] = p.pid
self.nodes.append(server_id)
time.sleep(self.START_SERVER_TIME)
if TERM_LOG:
print("Server#", server_id, " pid:", p.pid, sep="")
# connect to a server in the system
for index in self.servers:
if index != server_id:
m_create = Message(-1, None, "Creation", index)
self.nt.send_to_node(server_id, m_create)
time.sleep(0.2)
def retireServer(self, server_id):
if self.servers[server_id]:
m_retire = Message(-1, None, "Retire", None)
self.has_retired_res = False
self.nt.send_to_node(server_id, m_retire)
while not self.has_retired_res:
pass
self.nodes.remove(server_id)
self.servers.pop(server_id)
def joinClient(self, client_id, server_id):
if not client_id in self.nodes:
if self.servers[server_id]:
p = subprocess.Popen(["./src/client.py",
str(client_id)])
#.........这里部分代码省略.........