Skip to content

Commit

Permalink
Move PacketDispatcher from util to net
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Aug 14, 2015
1 parent 19aa055 commit bdf65ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
36 changes: 35 additions & 1 deletion quarry/net/protocol.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import string
import logging
import zlib

Expand All @@ -7,7 +8,6 @@
from quarry.util.crypto import Cipher
from quarry.util.buffer import Buffer, BufferUnderrun
from quarry.util.tasks import Tasks
from quarry.util.dispatch import PacketDispatcher


logging.basicConfig(format="%(name)s | %(levelname)s | %(message)s")
Expand All @@ -21,6 +21,40 @@
protocol_modes_inv = dict(((v, k) for k, v in protocol_modes.items()))


class PacketDispatcher(object):
def dispatch(self, lookup_args, buff):
handler = getattr(self, "packet_%s" % "_".join(lookup_args), None)
if handler is not None:
handler(buff)
return True
return False

def dump_packet(self, data):
lines = ['Packet dump:']
bytes_read = 0
while len(data) > 0:
data_line, data = data[:16], data[16:]

l_hex = []
l_str = []
for c in data_line:
l_hex.append("%02x" % ord(c))
l_str.append(c if c in string.printable else ".")

l_hex.extend([' ']*(16-len(l_hex)))
l_hex.insert(8,'')


lines.append("%08x %s |%s|" % (
bytes_read,
" ".join(l_hex),
"".join(l_str)))

bytes_read += len(data_line)

return "\n ".join(lines + ["%08x" % bytes_read])


class ProtocolError(Exception):
pass

Expand Down
34 changes: 0 additions & 34 deletions quarry/util/dispatch.py

This file was deleted.

0 comments on commit bdf65ce

Please sign in to comment.