Commit a00ab78b authored by Julien Muchembled's avatar Julien Muchembled

protocol: small cleanup in packet registration

I made a mistake in commit 13a64cfe
("Simplify definition of packets by computing automatically their codes").
My intention was that the code an answer packet continues to only differ by the
highest bit, as implemented now by this commit.

Before:
  0x0001, 0x8002   Ask1, Answer1
  0x0003           Notify2
  0x0004, 0x8005   Ask3, Answer3
  0x0006, 0x8007   Ask4, Answer4

After:
  0x0001, 0x8001   Ask1, Answer1
  0x0002           Notify2
  0x0003, 0x8003   Ask3, Answer3
  0x0004, 0x8004   Ask4, Answer4

This makes the protocol easier to document.

And by not wasting the range of possible values, it seems we have enough
space to shrink to a single byte.

This also removes code that became meaningless since that codes are generated
automatically.
parent b3dd6973
...@@ -1535,16 +1535,16 @@ class Truncate(Packet): ...@@ -1535,16 +1535,16 @@ class Truncate(Packet):
_answer = Error _answer = Error
StaticRegistry = {} _next_code = 0
def register(request, ignore_when_closed=None): def register(request, ignore_when_closed=None):
""" Register a packet in the packet registry """ """ Register a packet in the packet registry """
code = len(StaticRegistry) global _next_code
code = _next_code
assert code < RESPONSE_MASK
_next_code = code + 1
if request is Error: if request is Error:
code |= RESPONSE_MASK code |= RESPONSE_MASK
# register the request # register the request
StaticRegistry[code] = request
if request is None:
return # None registered only to skip a code number (for compatibility)
request._code = code request._code = code
answer = request._answer answer = request._answer
if ignore_when_closed is None: if ignore_when_closed is None:
...@@ -1557,32 +1557,28 @@ def register(request, ignore_when_closed=None): ...@@ -1557,32 +1557,28 @@ def register(request, ignore_when_closed=None):
if answer in (Error, None): if answer in (Error, None):
return request return request
# build a class for the answer # build a class for the answer
answer = type('Answer%s' % (request.__name__, ), (Packet, ), {}) answer = type('Answer' + request.__name__, (Packet, ), {})
answer._fmt = request._answer answer._fmt = request._answer
answer.poll_thread = request.poll_thread answer.poll_thread = request.poll_thread
# compute the answer code
code = code | RESPONSE_MASK
answer._request = request answer._request = request
assert answer._code is None, "Answer of %s is already used" % (request, ) assert answer._code is None, "Answer of %s is already used" % (request, )
answer._code = code answer._code = code | RESPONSE_MASK
request._answer = answer request._answer = answer
# and register the answer packet return request, answer
assert code not in StaticRegistry, "Duplicate response packet code"
StaticRegistry[code] = answer
return (request, answer)
class Packets(dict): class Packets(dict):
""" """
Packet registry that checks packet code uniqueness and provides an index Packet registry that checks packet code uniqueness and provides an index
""" """
def __metaclass__(name, base, d): def __metaclass__(name, base, d):
# this builds a "singleton"
cls = type('PacketRegistry', base, d)()
for k, v in d.iteritems(): for k, v in d.iteritems():
if isinstance(v, type) and issubclass(v, Packet): if isinstance(v, type) and issubclass(v, Packet):
v.handler_method_name = k[0].lower() + k[1:] v.handler_method_name = k[0].lower() + k[1:]
# this builds a "singleton" cls[v._code] = v
return type('PacketRegistry', base, d)(StaticRegistry) return cls
# notifications
Error = register( Error = register(
Error) Error)
RequestIdentification, AcceptIdentification = register( RequestIdentification, AcceptIdentification = register(
......
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