Commit 75cd5ecb authored by Julien Muchembled's avatar Julien Muchembled

Small code cleanups

- remove useless calls to 'bool'
- small optimizations in lib.protocol.Packet.__init__
- code simplification in IdentificationHandler
- fix typo in docstring
- neo/tests/__init__.py: 2 lines were indented with 2-spaces instead of 4-spaces

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2671 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 9eb67c81
......@@ -614,7 +614,7 @@ class Connection(BaseConnection):
if self.connector is None:
return
was_empty = not bool(self.write_buf)
was_empty = not self.write_buf
self.write_buf.extend(packet.encode())
......
......@@ -80,7 +80,7 @@ class EventHandler(object):
self.connectionClosed(conn)
def checkClusterName(self, name):
# raise an exception if the fiven name mismatch the current cluster name
# raise an exception if the given name mismatch the current cluster name
if self.app.name != name:
neo.lib.logging.error('reject an alien cluster')
raise ProtocolError('invalid cluster name')
......
......@@ -158,19 +158,17 @@ class Packet(object):
_id = None
def __init__(self, *args, **kw):
args = list(args)
assert self._code is not None, "Packet class not registered"
if args or kw:
assert self._fmt is not None
args = list(args)
buf = StringIO()
# load named arguments
for item in self._fmt._items[len(args):]:
args.append(kw.get(item._name))
self._fmt.encode(buf.write, args)
body = buf.getvalue()
self._body = buf.getvalue()
else:
body = ''
self._body = body
self._body = ''
def decode(self):
assert self._body is not None
......
......@@ -120,7 +120,7 @@ class Application(object):
# Start a normal operation.
while True:
# (Re)elect a new primary master.
self.primary = not bool(self.nm.getMasterList())
self.primary = not self.nm.getMasterList()
if not self.primary:
self.electPrimary(bootstrap=bootstrap)
bootstrap = False
......
......@@ -22,7 +22,6 @@ from neo.lib.protocol import BrokenNodeDisallowedError, ProtocolError
from neo.master.handlers import MasterHandler
class IdentificationHandler(MasterHandler):
"""This class deals with messages from the admin node only"""
def nodeLost(self, conn, node):
neo.lib.logging.warning(
......@@ -31,27 +30,19 @@ class IdentificationHandler(MasterHandler):
def requestIdentification(self, conn, node_type, uuid, address, name):
self.checkClusterName(name)
app, nm = self.app, self.app.nm
node_by_uuid = nm.getByUUID(uuid)
node_by_addr = nm.getByAddress(address)
app = self.app
node_by_uuid = app.nm.getByUUID(uuid)
# handle conflicts and broken nodes
node = node_by_uuid or node_by_addr
if node_by_uuid is not None:
if node.getAddress() == address:
node = node_by_uuid or app.nm.getByAddress(address)
if node:
if node_by_uuid and node.getAddress() == address:
# the node is still alive
if node.isBroken():
raise BrokenNodeDisallowedError
if node.getAddress() != address:
# this node has changed its address
if node.isRunning():
elif node.isRunning():
# still running, reject this new node
raise ProtocolError('invalid server address')
if node_by_uuid is None and node_by_addr is not None:
if node.isRunning():
# still running, reject this new node
raise ProtocolError('invalid server address')
if node is not None:
if node.isConnected():
# more than one connection from this node
raise ProtocolError('already connected')
......@@ -60,7 +51,7 @@ class IdentificationHandler(MasterHandler):
# ask the app the node identification, if refused, an exception is
# raised
result = self.app.identifyNode(node_type, uuid, node)
result = app.identifyNode(node_type, uuid, node)
(uuid, node, state, handler, node_ctor) = result
if uuid is None:
# no valid uuid, give it one
......
......@@ -1075,7 +1075,7 @@ class HandlerSwitcherTests(NeoUnitTestBase):
self.assertEqual(markers[0], (3, self._connection, msg_id_3))
# answer to msg_3 must not be expected anymore (and it was the last
# expected message)
self.assertFalse(bool(self._handlers.isPending()))
self.assertFalse(self._handlers.isPending())
del markers[:]
self._handlers.emit(r4, msg_4_time, OnTimeout(msg_4_on_timeout))
# msg_4 timeout will fire msg_4_on_timeout callback, which lets the
......
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