99 lines
2.2 KiB
Python
99 lines
2.2 KiB
Python
#!/usr/bin/python
|
|
|
|
__author__ = "Daniel Egger"
|
|
__date__ = "22 March 2002"
|
|
__email__ = "egger@interearth.com"
|
|
__version__ = "$Revision: 1.1 $"[11:-2]
|
|
|
|
from tpdu import TPDU, TPDU_DT
|
|
|
|
# String representation for a DT TPDU
|
|
DT_STRING = \
|
|
"""\
|
|
Length: %d
|
|
Number: 0x%02x
|
|
Contained Data: %s
|
|
"""
|
|
|
|
def fromstring (data):
|
|
"""
|
|
Decode the binary representation of a TPDU from the given
|
|
parameter and note the data in the instance of the object.
|
|
|
|
Parameters:
|
|
data: The binary form of the TPDU
|
|
"""
|
|
tpdu = DT_TPDU ()
|
|
tpdu.data = data[3:]
|
|
return tpdu
|
|
|
|
|
|
class DT_TPDU (TPDU):
|
|
def __init__ (self, data = "", *args, **args2):
|
|
self.code = TPDU_DT
|
|
self.data = data
|
|
|
|
# Bit 8 indicates complete DT Packet, may need to
|
|
# be fixed though
|
|
self.number = 0x80
|
|
#self.number = 0x00 # is fragment
|
|
self.__recalclen ()
|
|
return
|
|
|
|
def identify (self):
|
|
"""
|
|
This method can be used to identify a TPDU by string.
|
|
"""
|
|
return "DT type TPDU (Data transmit)\n"
|
|
|
|
def __recalclen (self):
|
|
"""
|
|
Return the length of the TPDU. This function is for
|
|
internal use only!
|
|
"""
|
|
self.len = 2 + len (self.data)
|
|
|
|
def __repr__ (self):
|
|
"""
|
|
Print a representation of the TPDU. Use this method via
|
|
`-pair to transfer it over the wire. This will just return
|
|
the header data and not the real data!
|
|
"""
|
|
# Note that the data is not included in the length
|
|
return "%c%c%c" % (2, self.code << 4, self.number) + self.data
|
|
|
|
def __str__ (self):
|
|
"""
|
|
Return a readable and quite verbose overview of the TPDU instance.
|
|
Use this for debugging purposes or if you're just curious.
|
|
"""
|
|
string = self.identify () + DT_STRING % (self.len, self.number, self.data)
|
|
return string
|
|
|
|
# Integrated tests start here
|
|
if __name__ == '__main__':
|
|
testnum = 1
|
|
|
|
tpdu = DT_TPDU ("")
|
|
print "Test %0d sucessful" % testnum
|
|
testnum += 1
|
|
|
|
tpdu = DT_TPDU (" Noch")
|
|
print "Test %0d sucessful" % testnum
|
|
print tpdu
|
|
testnum += 1
|
|
|
|
if tpdu.len == 14:
|
|
print "Test %0d sucessful" % testnum
|
|
testnum += 1
|
|
else:
|
|
raise Warning, "Test %0d failed" % testnum
|
|
|
|
print tpdu
|
|
print "Test %0d sucessful" % testnum
|
|
testnum += 1
|
|
|
|
print `tpdu`
|
|
print "Test %0d sucessful" % testnum
|
|
testnum += 1
|