Commit 9b259cdb authored by Vincent Pelletier's avatar Vincent Pelletier

Use time.sleep instead of a busy loop to wait for one second to pass.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@313 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent b8ee52f6
...@@ -22,7 +22,7 @@ from neo.protocol import PING, Packet, CLIENT_NODE_TYPE, FINISH_TRANSACTION ...@@ -22,7 +22,7 @@ from neo.protocol import PING, Packet, CLIENT_NODE_TYPE, FINISH_TRANSACTION
from neo.connection import MTClientConnection from neo.connection import MTClientConnection
from neo.node import MasterNode from neo.node import MasterNode
from time import time import time
import logging import logging
class Dispatcher(Thread): class Dispatcher(Thread):
...@@ -76,71 +76,69 @@ class Dispatcher(Thread): ...@@ -76,71 +76,69 @@ class Dispatcher(Thread):
if app.pt is not None: if app.pt is not None:
app.pt.clear() app.pt.clear()
master_index = 0 master_index = 0
t = 0
conn = None conn = None
# Make application execute remaining message if any # Make application execute remaining message if any
app._waitMessage() app._waitMessage()
handler = ClientEventHandler(app, app.dispatcher) handler = ClientEventHandler(app, app.dispatcher)
while 1: while 1:
if t + 1 < time(): if app.pt is not None and app.pt.operational():
if app.pt is not None and app.pt.operational(): # Connected to primary master node and got all informations
# Connected to primary master node and got all informations break
break app.local_var.node_not_ready = 0
app.local_var.node_not_ready = 0 if app.primary_master_node is None:
if app.primary_master_node is None: # Try with master node defined in config
# Try with master node defined in config
try:
addr, port = app.master_node_list[master_index].split(':')
except IndexError:
master_index = 0
addr, port = app.master_node_list[master_index].split(':')
port = int(port)
else:
addr, port = app.primary_master_node.getServer()
# Request Node Identification
conn = MTClientConnection(app.em, handler, (addr, port), connector_handler=connector)
if app.nm.getNodeByServer((addr, port)) is None:
n = MasterNode(server = (addr, port))
app.nm.add(n)
conn.lock()
try: try:
msg_id = conn.getNextId() addr, port = app.master_node_list[master_index].split(':')
p = Packet() except IndexError:
p.requestNodeIdentification(msg_id, CLIENT_NODE_TYPE, app.uuid, master_index = 0
'0.0.0.0', 0, app.name) addr, port = app.master_node_list[master_index].split(':')
port = int(port)
else:
addr, port = app.primary_master_node.getServer()
# Request Node Identification
conn = MTClientConnection(app.em, handler, (addr, port), connector_handler=connector)
if app.nm.getNodeByServer((addr, port)) is None:
n = MasterNode(server = (addr, port))
app.nm.add(n)
# Send message conn.lock()
conn.addPacket(p) try:
conn.expectMessage(msg_id) msg_id = conn.getNextId()
self.register(conn, msg_id, app.getQueue()) p = Packet()
finally: p.requestNodeIdentification(msg_id, CLIENT_NODE_TYPE, app.uuid,
conn.unlock() '0.0.0.0', 0, app.name)
# Wait for answer # Send message
while 1: conn.addPacket(p)
try: conn.expectMessage(msg_id)
self.em.poll(1) self.register(conn, msg_id, app.getQueue())
except TypeError: finally:
conn.unlock()
# Wait for answer
while 1:
try:
self.em.poll(1)
except TypeError:
break
app._waitMessage()
# Now check result
if app.primary_master_node is not None:
if app.primary_master_node == -1:
# Connection failed, try with another master node
app.primary_master_node = None
master_index += 1
break
elif app.primary_master_node.getServer() != (addr, port):
# Master node changed, connect to new one
break
elif app.local_var.node_not_ready:
# Wait a bit and reask again
break
elif app.pt is not None and app.pt.operational():
# Connected to primary master node
break break
app._waitMessage() time.sleep(1)
# Now check result
if app.primary_master_node is not None:
if app.primary_master_node == -1:
# Connection failed, try with another master node
app.primary_master_node = None
master_index += 1
break
elif app.primary_master_node.getServer() != (addr, port):
# Master node changed, connect to new one
break
elif app.local_var.node_not_ready:
# Wait a bit and reask again
break
elif app.pt is not None and app.pt.operational():
# Connected to primary master node
break
t = time()
logging.info("connected to primary master node %s:%d" %app.primary_master_node.getServer()) logging.info("connected to primary master node %s:%d" %app.primary_master_node.getServer())
app.master_conn = conn app.master_conn = conn
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment