source: rtems-tools/tester/rt/tftpy/TftpPacketFactory.py @ f24e116

5
Last change on this file since f24e116 was f24e116, checked in by Chris Johns <chrisj@…>, on 11/07/18 at 03:58:17

tester: Update the Python TFTP server to fix Python3 issues.

Updated to af2f2fe89a3bf45748b78703820efb0986a8207a.
Repo is https://github.com/msoulier/tftpy.git

  • Property mode set to 100644
File size: 1.5 KB
Line 
1# vim: ts=4 sw=4 et ai:
2# -*- coding: utf8 -*-
3"""This module implements the TftpPacketFactory class, which can take a binary
4buffer, and return the appropriate TftpPacket object to represent it, via the
5parse() method."""
6
7
8from .TftpShared import *
9from .TftpPacketTypes import *
10import logging
11
12log = logging.getLogger('tftpy.TftpPacketFactory')
13
14class TftpPacketFactory(object):
15    """This class generates TftpPacket objects. It is responsible for parsing
16    raw buffers off of the wire and returning objects representing them, via
17    the parse() method."""
18    def __init__(self):
19        self.classes = {
20            1: TftpPacketRRQ,
21            2: TftpPacketWRQ,
22            3: TftpPacketDAT,
23            4: TftpPacketACK,
24            5: TftpPacketERR,
25            6: TftpPacketOACK
26            }
27
28    def parse(self, buffer):
29        """This method is used to parse an existing datagram into its
30        corresponding TftpPacket object. The buffer is the raw bytes off of
31        the network."""
32        log.debug("parsing a %d byte packet" % len(buffer))
33        (opcode,) = struct.unpack(str("!H"), buffer[:2])
34        log.debug("opcode is %d" % opcode)
35        packet = self.__create(opcode)
36        packet.buffer = buffer
37        return packet.decode()
38
39    def __create(self, opcode):
40        """This method returns the appropriate class object corresponding to
41        the passed opcode."""
42        tftpassert(opcode in self.classes,
43                   "Unsupported opcode: %d" % opcode)
44
45        packet = self.classes[opcode]()
46
47        return packet
Note: See TracBrowser for help on using the repository browser.