"""Common utilities for both the chatclient and chatserver programs. Written by Tom Magrino """ MSG_SIZE_LIMIT = 4096 def decode_message(msg_text): """Create a message given the str representation of the message.""" msg_parts = msg_text.split("|") src = msg_parts[0] dst = msg_parts[1] action = msg_parts[2] body = "|".join(msg_parts[3:]) return Message(src, dst, action, body) class Message(object): def __init__(self, src, dst, action, body): self.src = src self.dst = dst self.action = action self.body = body def __str__(self): """String representation of Message: >>> m = Message("1.1.1.1", "1.1.1.2", "hello", "") >>> str(m) '1.1.1.1|1.1.1.2|hello|' """ return "{0}|{1}|{2}|{3}".format(self.src, self.dst, self.action, self.body) def __repr__(self): """Representation of Message for REPL: >>> m = Message("1.1.1.1", "1.1.1.2", "hello", "") >>> repr(m) 'Message("1.1.1.1", "1.1.1.2", "hello", "")' """ return 'Message("{0}", "{1}", "{2}", "{3}")'.format(self.src, self.dst, self.action, self.body)