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

Source Code for Module lacewing.protocol

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