From 4963dd6098eeeb78d6f16b564a22d09087c77cf3 Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Sat, 11 Apr 2009 21:52:56 +0000 Subject: [PATCH] IMC2Packet class is looking pretty stout now. We should be ready to start sub-classing it to the various IMC2 packet types. --- src/imc2/connection.py | 16 +++++------- src/imc2/packets.py | 56 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/imc2/connection.py b/src/imc2/connection.py index 9b5a133ebb..f0609807b3 100644 --- a/src/imc2/connection.py +++ b/src/imc2/connection.py @@ -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()) diff --git a/src/imc2/packets.py b/src/imc2/packets.py index 104f07e9a2..068c6a11b1 100644 --- a/src/imc2/packets.py +++ b/src/imc2/packets.py @@ -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: # @ @ @@ -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.