| Trees | Indices | Help |
|
|---|
|
|
1 ##bzProtocol.py handles BZoo protocol. 2 ##Copyright (C) 2006 Jean-Baptiste PERIN 3 ## 4 ##This program is free software; you can redistribute it and/or 5 ##modify it under the terms of the GNU General Public License 6 ##as published by the Free Software Foundation; either version 2 7 ##of the License, or (at your option) any later version. 8 ## 9 ##This program is distributed in the hope that it will be useful, 10 ##but WITHOUT ANY WARRANTY; without even the implied warranty of 11 ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 ##GNU General Public License for more details. 13 ## 14 ##You should have received a copy of the GNU General Public License 15 ##along with this program; if not, write to the Free Software 16 ##Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 """ 18 This module contains classes involved in dialogue handling between clients and server. 19 """ 20 21 # TODO: Faire en sorte de gerer une taille max de packet client/serveur et serveur client 22 # TODO: mettre en place la transmission de "gros pacquets" en tache de fond 23 24 import struct 25 import bzMessage 2628 """ 29 This class provides BZoo applications with message packing and unpacking services. 30 31 Exemple:: 32 import bzMessage 33 34 def myHandlerFct(values): 35 messages = values 36 print "I've just received message(s): ", messages 37 38 39 # initialisation of protocol and message list 40 aProtocol = bzProtocol() 41 message_queue = [] 42 message_queue.append(bzMessage.SayMessage("Hello World")) 43 44 # setup a handler on reception of message 45 self.aProtocol(bzMessage.CLIENTS_MESSAGES_MESSAGE,myHandlerFct) 46 47 # prepare raw data to send from current list of message 48 data2send = aProtocol.getNextDataMessage(message_queue, 1024) 49 50 SendData (data2send) 51 52 # process incoming message 53 data = ReceiveData () 54 if (data is not None): 55 aProtocol.processDataMessage(data) 56 57 @ivar MessageTable: 58 @type MessageTable: 59 @ivar HandlerTable: gives the functions to 60 @type HandlerTable: list of list of handler function 61 """ 62143 144 if __name__ == "__main__": 145 146 147 aProtocol = bzProtocol() 148 149 aPR = bzMessage.PositionReport (position = [0.0, 0.0, 0.0], orientation = [[1.0, 0.0, 0.0],[0.0, 1.0, 0.0],[0.0, 0.0, 1.0]], action = 0) 150 151 del aProtocol 15264 65 self.MessageTable = [ None for i in bzMessage.msgid] 66 #TODO: Put this table in bzMessage Module so that bzProtocol has nothing to do with messages types 67 self.MessageTable[bzMessage.POSITION_REPORT_MESSAGE] = bzMessage.PositionReport () 68 self.MessageTable[bzMessage.SITUATION_REPORT_MESSAGE] = bzMessage.ClientsSituationReport () 69 self.MessageTable[bzMessage.SAY_MESSAGE] = bzMessage.SayMessage () 70 self.MessageTable[bzMessage.CLIENTS_MESSAGES_MESSAGE] = bzMessage.ClientsMessages () 71 self.MessageTable[bzMessage.PRIVATE_SAY_MESSAGE] = bzMessage.PrivateSayMessage () 72 self.MessageTable[bzMessage.WELCOME_MESSAGE] = bzMessage.WelcomeMessage () 73 self.MessageTable[bzMessage.INIT_INVENTORY_MESSAGE] = bzMessage.InitInventory () 74 self.MessageTable[bzMessage.GRAB_OBJECT_MESSAGE] = bzMessage.GrabObject () 75 self.MessageTable[bzMessage.VANISH_OBJECT_MESSAGE] = bzMessage.VanishObject () 76 self.MessageTable[bzMessage.INVENTORY_ADD_MESSAGE] = bzMessage.InventoryAdd () 77 self.MessageTable[bzMessage.INSTANCIATE_OBJECT_MESSAGE] = bzMessage.InstanciateObject () 78 self.MessageTable[bzMessage.ACTIVATE_OBJECT_MESSAGE] = bzMessage.ActivateObject () 79 self.MessageTable[bzMessage.RELEASE_OBJECT_MESSAGE] = bzMessage.ReleaseObject () 80 self.MessageTable[bzMessage.RELEASE_OBJECT_MESSAGE] = bzMessage.ReleaseObject () 81 self.MessageTable[bzMessage.STORE_AVATAR_MESSAGE] = bzMessage.StoreAvatar () 82 self.MessageTable[bzMessage.DECLARE_VAR_MESSAGE] = bzMessage.DeclareVar () 83 84 self.HandlerTable = [ [] for i in bzMessage.msgid]85 86 87 #self.state = 0 88 9193 """ 94 Add a handler on reception of a peculiar message 95 @param msgType: the type of the message to attach the handler to 96 @type msgType: integer. 97 @param fct: the handler function. 98 @type fct: function. 99 """ 100 self.HandlerTable[msgType].append(fct)101 102104 """ 105 Build a packed data block 106 @param msg_list: The server hostname or IP adress 107 @type msg_list: String. 108 @param max_size: the maximum size of the data packet to produce 109 @type max_size: Integer 110 @return: the next packed data block to send. 111 @rtype: byte array. 112 """ 113 current_size = 0 114 data = '' 115 while len(msg_list) != 0: 116 msg = msg_list.pop(0) 117 data = data + msg.Encode() 118 return data119 120 121123 """ 124 Unpack received data into messages and call associated handler. 125 @param data: A packed data block 126 @type data: byte array. 127 """ 128 129 130 idx_byte = 0 131 while (idx_byte < len(data)): 132 try: 133 134 [msgType] = struct.unpack('B', data[idx_byte]) 135 nb_byte, values = self.MessageTable[msgType].Decode(data[idx_byte:len(data)]) 136 for hdlr in self.HandlerTable[msgType]: 137 hdlr(values) 138 except struct.error: 139 print "ERROR: Unable to unpack data" 140 break 141 142 idx_byte = idx_byte + nb_byte
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0beta1 on Sun Sep 23 10:16:20 2007 | http://epydoc.sourceforge.net |