Package lacewing :: Module protocol
[frames] | no frames]

Source Code for Module lacewing.protocol

 1  # Copyright (c) 2011 Mathias Kaerlev. 
 2  # See LICENSE for details. 
 3   
 4  """ 
 5  The base for server and client. 
 6  """ 
 7   
 8  from twisted.internet import protocol 
 9  from lacewing.multidict import MultikeyDict 
10  from lacewing.exceptions import OutOfData 
11   
12 -class BaseProtocol(protocol.Protocol):
13 """ 14 Baseclass that contains properties and methods common to 15 both server and client protocols. 16 17 @ivar name: The name of the client. 18 @ivar id: The ID of the client. 19 @ivar privateAllowed: False if the client has requested no 20 private messages must be sent to it. 21 @ivar isAccepted: True if the client connection has been 22 accepted. 23 @ivar loggedIn: True if the client is logged in with a name. 24 @ivar channels: Channels the client is signed on to 25 (has keys for both the ID and name). 26 @type channels: MultikeyDict 27 """ 28 revision = 'revision 3' 29 30 name = None 31 id = None 32 isAccepted = False 33 loggedIn = False 34 udpEnabled = False 35 36 channels = None 37 38 # for packet parsing 39 40 _packetBuffer = '' 41
42 - def connectionMade(self):
43 self.channels = MultikeyDict()
44
45 - def dataReceived(self, data):
46 packetBuffer = self._packetBuffer + data 47 newPacket = self._receivePacket() 48 try: 49 while 1: 50 bytesRead = newPacket.read(packetBuffer) 51 self.loaderReceived(newPacket.loader) 52 packetBuffer = packetBuffer[bytesRead:] 53 if len(packetBuffer) == 0: 54 break 55 except OutOfData: 56 pass 57 self._packetBuffer = packetBuffer
58
59 - def sendLoader(self, loader, asDatagram = False, **settings):
60 raise NotImplementedError('sendLoader() not implemented')
61
62 - def loaderReceived(self, loader, isDatagram = False):
63 """ 64 Called when a packet is received. 65 66 @param isDatagram: True if the message recieved was sent 67 using UDP. 68 """ 69 raise NotImplementedError('loaderReceived() method not implemented')
70
71 - def validName(self, name):
72 """ 73 Returns True if name is a valid Lacewing 74 channel or client name. 75 """ 76 if not name.strip() == '': 77 return True 78 return False
79