本文整理汇总了Python中network.Network.send_to_node方法的典型用法代码示例。如果您正苦于以下问题:Python Network.send_to_node方法的具体用法?Python Network.send_to_node怎么用?Python Network.send_to_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类network.Network
的用法示例。
在下文中一共展示了Network.send_to_node方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Condition
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import send_to_node [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:
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import send_to_node [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())
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import send_to_node [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)])
#.........这里部分代码省略.........