IMC2Packet class is looking pretty stout now. We should be ready to start sub-classing it to the various IMC2 packet types.

This commit is contained in:
Greg Taylor 2009-04-11 21:52:56 +00:00
parent 4428fbd336
commit 4963dd6098
2 changed files with 60 additions and 12 deletions

View file

@ -9,6 +9,7 @@ from twisted.internet import reactor, task
from twisted.conch.telnet import StatefulTelnetProtocol
from django.conf import settings
from src.imc2.packets import *
from src import logger
# The active instance of IMC2Protocol. Set at server startup.
IMC2_PROTOCOL_INSTANCE = None
@ -76,14 +77,9 @@ class IMC2Protocol(StatefulTelnetProtocol):
if not self.is_authenticated:
self._parse_auth_response(line)
else:
split_line = line.split(' ')
packet_type = split_line[3]
if packet_type == "is-alive":
pass
elif packet_type == "user-cache":
pass
else:
print "receive:", line
logger.log_infomsg("PACKET: %s" % line)
logger.log_infomsg(IMC2Packet(packet_str = line))
#print "receive:", line
class IMC2ClientFactory(ClientFactory):
"""
@ -93,7 +89,7 @@ class IMC2ClientFactory(ClientFactory):
protocol = IMC2Protocol
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
logger.log_errmsg('connection failed: %s' % reason.getErrorMessage())
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
logger.log_errmsg('connection lost: %s' % reason.getErrorMessage())

View file

@ -2,12 +2,13 @@
IMC2 packets. These are pretty well documented at:
http://www.mudbytes.net/index.php?a=articles&s=imc2_protocol
"""
import shlex
from django.conf import settings
class IMC2Packet(object):
"""
Base IMC2 packet class. This should never be used directly. Sub-class
and profit.
Base IMC2 packet class. This is generally sub-classed, aside from using it
to parse incoming packets from the IMC2 network server.
"""
# The following fields are all according to the basic packet format of:
# <sender>@<origin> <sequence> <route> <packet-type> <target>@<destination> <data...>
@ -23,6 +24,57 @@ class IMC2Packet(object):
# Reference to the IMC2Protocol object doing the sending.
imc2_protocol = None
def __init__(self, packet_str=None):
"""
Optionally, parse a packet and load it up.
"""
if packet_str:
split_packet = shlex.split(packet_str)
# Get values for the sender and origin attributes.
sender_origin = split_packet[0]
split_sender_origin = sender_origin.split('@')
self.sender = split_sender_origin[0].strip()
self.origin = split_sender_origin[1]
self.sequence = split_packet[1]
self.route = split_packet[2]
self.packet_type = split_packet[3]
# Get values for the target and destination attributes.
target_destination = split_packet[4]
split_target_destination = target_destination.split('@')
self.target = split_target_destination[0]
self.destination = split_sender_origin[1]
# Populate optional data.
self.optional_data = []
data_list = split_packet[5:]
for pair in data_list:
key, value = pair.split('=')
self.optional_data.append((key, value))
def __str__(self):
retval = """
-- Begin Packet Display --
Sender: %s
Origin: %s
Sequence: %s
Route: %s
Type: %s
Target: %s
Destination: %s
Data: %s
- End Packet Display --""" % (self.sender,
self.origin,
self.sequence,
self.route,
self.packet_type,
self.target,
self.destination,
self.optional_data)
return retval
def _get_optional_data_string(self):
"""
Generates the optional data string to tack on to the end of the packet.