Changeset 1584
- Timestamp:
- 01/20/10 20:33:32 (2 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
lotatc_server/lotatc_radarServer.py (modified) (36 diffs)
-
textNewCnx.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lotatc_server/lotatc_radarServer.py
r1276 r1584 25 25 from configuration_object import ConfigurationObject 26 26 from lotatc_radarList import * 27 from cnxObj import * 27 28 28 29 # List of commands … … 86 87 self.logger.debug('__init__') 87 88 89 # Create cnxObj used to communicate with RadarClient 90 self.cnxObj = CnxObj( request) 91 88 92 # This is the PSEUDO of the troller 89 93 self.controllerId = None … … 95 99 # This the dictionary used for the objects sent to this troller 96 100 self.objects={} 97 98 101 99 102 # There is no protection, but there is not a lot of risk ... … … 159 162 # @param self instance id 160 163 def handle(self): 161 162 # 'incompleteMessage' is the start of a message which has been truncated during transmission ...163 # It has to be prepend to the first message of the next buffer.164 incompleteMessage=""165 166 164 while(True): 167 # Get one segment168 165 try: 169 buffer = self.request.recv(1024) 170 #- If client close the connection, buffer is void... 171 if not buffer: 172 #- Buffer void, return 173 return 166 frames = self.cnxObj.receiveFrame() 167 # Test if client has close the connection 168 if len( frames[0]): 169 return 174 170 except: 175 return 176 177 # Detecte if the last message of the buffer is complete or not 178 if buffer.endswith( os.linesep): 179 lastMessageIsComplete = True 180 else: 181 lastMessageIsComplete = False 171 return 182 172 183 173 # Test for EndOfMission notified by LuaServer … … 197 187 self.radarServer.coalitionLocks[cst_coalitionLut[0]].release() 198 188 199 # split buffer in messages 200 msgList = buffer.split(os.linesep) 201 202 # If there is an incompleteMessage, pre-pend this incompleteMessage 203 # with the first message of the buffer. 204 if incompleteMessage !="": 205 msgList[0] = incompleteMessage + msgList[0] 206 incompleteMessage ="" 207 208 # Process each message 209 nbrMessages = len(msgList) 210 for index in range(nbrMessages): 211 if lastMessageIsComplete or (index != nbrMessages-1): 212 msg = msgList[index] 213 if len(msg) and msg != os.linesep: 214 data = msg.strip() 215 request = data.split("|", 1)[0] 216 217 if request == DATA_REQ: 218 self.dataRequest() 219 220 elif request == BROADCAST_REQ: 221 # items[1] => sendTo 222 # items[2] => incomming message 223 items = data.split("|", 2) 224 message = MSG + "|" + items[1] + "|" + self.controllerId + "|" + items[2] + os.linesep 225 self.server.broadcastMessage(message, self.controllerCoalition, items[1], self.controllerId) 226 227 elif request == TAG_SET: 228 self.tagObject(data) 229 230 elif request == GRAPH_ADD: 231 self.graphAdd(data) 232 233 elif request == GRAPH_DEL: 234 self.graphDelete(data) 235 236 elif request == GRAPH_MOD: 237 self.graphModify(data) 238 239 elif request == GRAPH_MSG_ID_REQ: 240 self.graphGetMsgId(data) 241 242 elif request == GRAPH_LOCK_REQ: 243 self.graphLock(data) 189 190 # Process each received frame 191 for frame in frames: 192 opCode = frame.split("|", 1)[0] 193 194 if opCode == DATA_REQ: 195 self.dataRequest() 196 197 elif opCode == BROADCAST_REQ: 198 # items[1] => sendTo 199 # items[2] => incomming message 200 items = frame.split("|", 2) 201 message = MSG + "|" + items[1] + "|" + self.controllerId + "|" + items[2] 202 self.server.broadcastMessage(message, self.controllerCoalition, items[1], self.controllerId) 203 204 elif opCode == TAG_SET: 205 self.tagObject(frame) 206 207 elif opCode == GRAPH_ADD: 208 self.graphAdd(frame) 209 210 elif opCode == GRAPH_DEL: 211 self.graphDelete(frame) 212 213 elif opCode == GRAPH_MOD: 214 self.graphModify(frame) 215 216 elif opCode == GRAPH_MSG_ID_REQ: 217 self.graphGetMsgId(frame) 218 219 elif opCode == GRAPH_LOCK_REQ: 220 self.graphLock(frame) 221 222 elif opCode == GRAPH_UNLOCK_REQ: 223 self.graphUnlock(frame) 224 225 elif opCode == LOGIN: 226 self.login(frame) 227 228 elif opCode == CONFIG_REQ: 229 self.configRequest(frame) 230 231 elif opCode == MISSION_REQ: 232 self.sendMissionData() 233 234 elif opCode == SEND_TO_ONE_PLAYER: 235 items = frame.split("|", 2) 236 playerId = items[1] 237 message = items[2] 238 self.server.sendToPlayer( playerId, message) 239 240 elif opCode == SEND_TO_ALL_PLAYERS: 241 self.server.sendToPlayer( None, frame.split("|", 1)[1]) 242 243 else: 244 self.logger.debug("Unkwon command: %s" % opCode) 244 245 245 elif request == GRAPH_UNLOCK_REQ:246 self.graphUnlock(data)247 248 elif request == LOGIN:249 self.login(data)250 251 elif request == CONFIG_REQ:252 self.configRequest(data)253 254 elif request == MISSION_REQ:255 self.sendMissionData()256 257 elif request == SEND_TO_ONE_PLAYER:258 items = data.split("|", 2)259 playerId = items[1]260 message = items[2]261 self.server.sendToPlayer( playerId, message)262 263 elif request == SEND_TO_ALL_PLAYERS:264 self.server.sendToPlayer( None, data.split("|", 1)[1])265 266 else:267 self.logger.debug("Unkwon command: %s" % request)268 269 # End of: if request == DATA_REQ270 # End of: if len(msg) and msg != LINE_SEP271 # End of: if lastMessageIsComplete or (index != nbrMessages-1):272 else:273 incompleteMessage = msgList[index]274 275 246 # End of: for index in range(nbrMessages): 276 247 # End of: while(True): … … 293 264 # Yes -> remove lock and notify other client about Unlock 294 265 self.graph[graphId]["LockBy"] = None 295 self.server.broadcastMessage( GRAPH_UNLOCK_REP + "|%s|%s|%s" % (0, graphId, "1" + os.linesep), self.controllerCoalition, "ALL")266 self.server.broadcastMessage( GRAPH_UNLOCK_REP + "|%s|%s|%s" % (0, graphId, "1"), self.controllerCoalition, "ALL") 296 267 self.lock.release() 297 268 … … 318 289 debugMessage = "|--> Incorrect client version" 319 290 self.logger.debug(debugMessage) 320 self. request.send(LOGIN_NACK + debugMessage + os.linesep)291 self.cnxObj.sendFrame( LOGIN_NACK + debugMessage) 321 292 return 322 293 if password == controllerPwd and self.radarServer.controllerOk(self): … … 327 298 self.graph = self.mainInfo["GRAPH"][coalition] 328 299 self.logger.info("The troller '%s' is working for '%s' coalition" % (self.controllerId, self.controllerCoalition)) 329 self. request.send(LOGIN_ACK + "|%s|%s" % (coalition, str( CST_LOTATC_NETWORK_VERSION ) + os.linesep) )300 self.cnxObj.sendFrame( LOGIN_ACK + "|%s|%s" % (coalition, str( CST_LOTATC_NETWORK_VERSION)) ) 330 301 331 302 # MissionStartTime is to be sent … … 348 319 message += "|%s|%s" % (key,graph["Data"]) 349 320 350 message += os.linesep 351 self.request.send(message) 321 self.cnxObj.sendFrame( message) 352 322 # End of: if len(self.graph): 353 323 … … 358 328 debugMessage = "|--> Incorrect logging password" 359 329 self.logger.debug(debugMessage) 360 self. request.send(LOGIN_NACK + debugMessage + os.linesep)330 self.cnxObj.sendFrame( LOGIN_NACK + debugMessage) 361 331 else: 362 332 debugMessage = "|--> Incorrect logging message length" 363 333 self.logger.debug(debugMessage) 364 self. request.send(LOGIN_NACK + debugMessage + os.linesep)334 self.cnxObj.sendFrame( LOGIN_NACK + debugMessage) 365 335 # End of: if len(items) == 3: 366 336 … … 388 358 389 359 # Send response 390 self. request.send(configRep + os.linesep)360 self.cnxObj.sendFrame( configRep) 391 361 392 362 … … 400 370 if self.globalServerConfig.Mission: 401 371 for m in self.globalServerConfig.Mission: 402 self. request.send(MISSION_REP + "|" + m + os.linesep)372 self.cnxObj.sendFrame( MISSION_REP + "|" + m) 403 373 # End of: for m in self.globalServerConfig.Mission: 404 374 405 375 # For the moment it is a stub 406 self. request.send(MISSION_END + os.linesep)376 self.cnxObj.sendFrame(MISSION_END) 407 377 408 378 else: 409 379 message = "Mission has not been imported ..." 410 380 self.logger.debug(message) 411 self. request.send("%s|%s" % (MISSION_NACK, message+os.linesep))381 self.cnxObj.sendFrame("%s|%s" % ( MISSION_NACK, message)) 412 382 # End of: if self.globalServerConfig.Mission: 413 383 … … 431 401 if (self.mainObjects): 432 402 # Send MissionStartTime 433 message = MISSION_START_TIME + "|%s \n" % self.mainInfo["MissionStartTime"]434 self. request.send(message)403 message = MISSION_START_TIME + "|%s" % self.mainInfo["MissionStartTime"] 404 self.cnxObj.sendFrame( message) 435 405 self.sendMissionStartTime = False 436 406 print "Send MissionStartTime" … … 439 409 self.lock.acquire() 440 410 # Send DATA_REP with timestamp for the time slot 441 message = "%s|%s\n" % (DATA_START, self.mainInfo["TimeStamp"][self.controllerCoalition])411 frame = self.cnxObj.buildFrame( "%s|%s" % (DATA_START, self.mainInfo["TimeStamp"][self.controllerCoalition])) 442 412 # Loop over detected objects 443 413 for (key, thisObject) in self.mainObjects.iteritems(): … … 448 418 self.objects[key] = thisObject.clone() 449 419 # self.logger.info("Static info for id:%s %s %s" % (key, thisObject.unitName, thisObject.pilotName)) 450 message = message + (DATA_REP0 + "|%s|%s|%s|%s|%s" %451 (key, thisObject.type.encode('utf-8'), thisObject.unitClsId, thisObject.pilotName.encode('utf-8'), thisObject.coalition + os.linesep))420 frame += self.cnxObj.buildFrame( DATA_REP0 + "|%s|%s|%s|%s|%s" % 421 (key, thisObject.type.encode('utf-8'), thisObject.unitClsId, thisObject.pilotName.encode('utf-8'), thisObject.coalition)) 452 422 453 423 # ------------------------------------------------------------------------------------ 454 424 # Send variable part 455 message = message + (DATA_REP1 + "|%s|%s|%s|%s|%s" %456 (key, thisObject.x, thisObject.y, thisObject.z, str(thisObject.bearing) + os.linesep))425 frame += self.cnxObj.buildFrame( DATA_REP1 + "|%s|%s|%s|%s|%s" % 426 (key, thisObject.x, thisObject.y, thisObject.z, str(thisObject.bearing))) 457 427 458 428 # As it is a new object, test for Tag to be sent. 459 429 if self.tag.has_key(key): 460 430 tag = self.tag[key] 461 message = message + TAG_SET + "|%s|%s" % (key, tag + os.linesep)431 frame += self.cnxObj.buildFrame( TAG_SET + "|%s|%s" % (key, tag)) 462 432 # self.logger.info("Send tag id:%s Tag:%s" % (key, tag)) 463 433 # End of: if self.tag.has_key(key): … … 467 437 # self.logger.debug("Update info for id:%s Name:%s" % (key, localObject.unitName)) 468 438 # We have just to send the differences 469 message = message +(DATA_REP1 + "|%s" % key)439 message = (DATA_REP1 + "|%s" % key) 470 440 if localObject.x != thisObject.x: 471 441 message = message + ("|%s" % thisObject.x) … … 492 462 message = message + "|" 493 463 494 message = message + os.linesep464 frame += self.cnxObj.buildFrame( message) 495 465 # End of: if not self.objects.has_key(key): 496 466 # End of: for (key, thisObject) in self.mainObjects.iteritems(): 497 467 498 468 # Send "end of transmition" 499 message = message + (DATA_END + os.linesep)500 self. request.send(message)469 frame += self.cnxObj.buildFrame( DATA_END) 470 self.cnxObj.sendFrame( message, multiFrame=True) 501 471 502 472 … … 504 474 # Check if there are new mobile radars 505 475 newRadarFound = False 506 message = CONFIG_REP1476 frame = CONFIG_REP1 507 477 for (key, thisObject) in self.mainObjects.iteritems(): 508 478 if cst_coalitionLut[int(thisObject.coalition)] == self.controllerCoalition: … … 512 482 newRadarFound = True 513 483 self.mobileRadarKeys.append(key) 514 message = message + "|%s|%s|%s" % (key, thisObject.unitName.encode('utf-8'), thisObject.pilotName.encode('utf-8'))484 frame = frame + "|%s|%s|%s" % (key, thisObject.unitName.encode('utf-8'), thisObject.pilotName.encode('utf-8')) 515 485 # End of: if thisObject.unitName in self.mobileRadarTypes: 516 486 # End of: if not (key in self.mobileRadarKeys): … … 519 489 520 490 if newRadarFound: 521 message = message + os.linesep 522 self.request.send(message) 491 self.cnxObj.sendFrame( frame) 523 492 # End of: if newRadarFound: 524 493 … … 531 500 # Radar has been killed, send a message and tag it as "to remove" 532 501 self.logger.info("Radar killed: id:%s" % key) 533 message = "%s|%s%s" %(RADAR_KILLED, key, os.linesep)534 self. request.send(message)502 frame = "%s|%s" %( RADAR_KILLED, key) 503 self.cnxObj.sendFrame( frame) 535 504 keysToRemove.append(key) 536 505 # End of: if not self.mainObjects.has_key(key): … … 557 526 message = "Login or ConfigSet is missing ..." 558 527 self.logger.debug(message) 559 self. request.send(DATA_REQ_NACK + "|--> %s" % message + os.linesep)528 self.cnxObj.sendFrame( DATA_REQ_NACK + "|--> %s" % message) 560 529 # End of: if self.controllerId : 561 530 … … 566 535 # @param message message to be sent 567 536 def sendMessage(self, message): 568 self. request.send(message)537 self.cnxObj.sendFrame( message) 569 538 570 539 … … 585 554 # Save tag, then it is accessible by every troller. 586 555 self.tag[objectId] = tag 587 self.server.broadcastMessage( TAG_SET + "|%s|%s" % (objectId, tag + os.linesep), self.controllerCoalition, "ALL")556 self.server.broadcastMessage( TAG_SET + "|%s|%s" % (objectId, tag ), self.controllerCoalition, "ALL") 588 557 589 558 # ============================================================ … … 611 580 def graphDelete(self, data): 612 581 self.logger.info("GraphDel request") 613 items = data.split("|", 1)614 graphId = items[1]582 items = data.split("|", 1) 583 graphId = items[1] 615 584 616 585 self.server.graphDelete( self.controllerCoalition, self.graph, graphId, self.controllerId, True) … … 634 603 if self.graph[graphId]["LockBy"] == self.controllerId: 635 604 self.graph[graphId]["Data"] = graphData 636 self.server.broadcastMessage( GRAPH_MOD + "|%s|%s" % (graphId, graphData + os.linesep), self.controllerCoalition, "ALL")605 self.server.broadcastMessage( GRAPH_MOD + "|%s|%s" % (graphId, graphData), self.controllerCoalition, "ALL") 637 606 else: 638 607 self.logger.error("Troller %s tries to modify graph %s, but lock is missing" % (self.controllerId, graphId) ) … … 654 623 self.lock.acquire() 655 624 print "ASSIGN", msgId, self.controllerId 656 self.server.broadcastMessage( GRAPH_MSG_ID_REP + "|%s" % (str(msgId) + os.linesep), self.controllerCoalition, self.controllerId)625 self.server.broadcastMessage( GRAPH_MSG_ID_REP + "|%s" % (str(msgId)), self.controllerCoalition, self.controllerId) 657 626 msgId += 1 658 627 self.lock.release() … … 675 644 if self.graph[graphId]["LockBy"] == None: 676 645 self.graph[graphId]["LockBy"] = self.controllerId 677 self.server.broadcastMessage( GRAPH_LOCK_REP + "|%s|%s|%s" % (msgId, graphId, "1" + os.linesep), self.controllerCoalition, "ALL")646 self.server.broadcastMessage( GRAPH_LOCK_REP + "|%s|%s|%s" % (msgId, graphId, "1"), self.controllerCoalition, "ALL") 678 647 else: 679 self.server.broadcastMessage( GRAPH_LOCK_REP + "|%s|%s|%s" % (msgId, graphId, "0" + os.linesep), self.controllerCoalition, self.controllerId)648 self.server.broadcastMessage( GRAPH_LOCK_REP + "|%s|%s|%s" % (msgId, graphId, "0"), self.controllerCoalition, self.controllerId) 680 649 681 650 else: … … 701 670 if self.graph[graphId]["LockBy"] == self.controllerId: 702 671 self.graph[graphId]["LockBy"] = None 703 self.server.broadcastMessage( GRAPH_UNLOCK_REP + "|%s|%s|%s" % (msgId, graphId, "1" + os.linesep), self.controllerCoalition, "ALL")672 self.server.broadcastMessage( GRAPH_UNLOCK_REP + "|%s|%s|%s" % (msgId, graphId, "1"), self.controllerCoalition, "ALL") 704 673 else: 705 self.server.broadcastMessage( GRAPH_UNLOCK_REP + "|%s|%s|%s" % (msgId, graphId, "0" + os.linesep), self.controllerCoalition, self.controllerId)674 self.server.broadcastMessage( GRAPH_UNLOCK_REP + "|%s|%s|%s" % (msgId, graphId, "0"), self.controllerCoalition, self.controllerId) 706 675 else: 707 676 self.logger.warning("There is no graph with this key %s" % graphId) … … 837 806 nbrOfTroller += 1 838 807 839 message = message + os.linesep 840 self.broadcastMessage(message, coalition, "ALL") 808 self.broadcastMessage( message, coalition, "ALL") 841 809 842 810 self.luaServer.setTrollerNumber(coalition, nbrOfTroller) … … 872 840 nbrOfTroller += 1 873 841 874 message = message + os.linesep 875 self.broadcastMessage(message, coalition, "ALL") 876 877 self.luaServer.setTrollerNumber(coalition, nbrOfTroller) 842 self.broadcastMessage( message, coalition, "ALL") 843 844 self.luaServer.setTrollerNumber( coalition, nbrOfTroller) 878 845 879 846 … … 887 854 # @param controllerId destination 888 855 def broadcastMessage(self, message, coalition, controllerId, senderId=None): 889 self.logger.debug("broadcastMessage: '%s' to [%s, %s]" % ( message, coalition, controllerId))856 self.logger.debug("broadcastMessage: '%s' to [%s, %s]" % ( message, coalition, controllerId)) 890 857 891 858 # Is the message to be sent to all the trollers ? … … 893 860 # Yes => loop over all the trollers of the coalition 894 861 for requestHandle in self.requestHandleInfo[coalition]: 895 requestHandle.sendMessage( message)862 requestHandle.sendMessage( message) 896 863 897 864 else: … … 899 866 for requestHandle in self.requestHandleInfo[coalition]: 900 867 if requestHandle.controllerId == controllerId: 901 requestHandle.sendMessage( message)868 requestHandle.sendMessage( message) 902 869 elif requestHandle.controllerId == senderId: 903 requestHandle.sendMessage( message)870 requestHandle.sendMessage( message) 904 871 905 872 … … 930 897 if ( check == False ) or ( graphs[graphId]["LockBy"] == controlerId ): 931 898 del graphs[graphId] 932 self.broadcastMessage( GRAPH_DEL + "|%s" % (graphId + os.linesep), coalition, "ALL")899 self.broadcastMessage( GRAPH_DEL + "|%s" % graphId, coalition, "ALL") 933 900 else: 934 901 self.logger.error("Troller %s tries to delete graph %s, but lock is missing" % (controlerId, graphId) ) … … 956 923 graphId = str(graphId) 957 924 graphs[graphId] = {} 958 graphs[graphId]["Data"] = graphData925 graphs[graphId]["Data"] = graphData 959 926 graphs[graphId]["LockBy"] = None 960 927 self.coalitionLocks[coalition].release() 961 928 962 929 # Broadcast message 963 self.broadcastMessage( GRAPH_ADD + "|%s|%s" % (graphId, graphData + os.linesep), coalition, "ALL")930 self.broadcastMessage( GRAPH_ADD + "|%s|%s" % (graphId, graphData), coalition, "ALL") 964 931 965 932 # ============================================================ -
trunk/textNewCnx.py
r1582 r1584 1 # -*- coding: utf-8 -*- 2 # vim: ts=4:sw=4 3 # This file is part of LOME. 4 # 5 # LOME is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # LOME is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with LOME. If not, see <http://www.gnu.org/licenses/>. 17 # 18 1 19 import os 2 20 import sys … … 14 32 15 33 34 sys.path.append( os.path.join( sys.path[0], 'common')) 35 from cnxObj import * 16 36 17 #==============================================================================18 ## Object used to send and receive data upon a socket or a request.19 class BaseCnxObj():20 21 # Declaration of the two bits reserved for the transmission information22 ZIPPED_DATA = 0x8000000023 MULTI_FRAME = 0x4000000024 25 # ----------------------------------------------------------------------------26 # Constructor27 # @param self object instance28 def __init__( self):29 pass30 31 # ----------------------------------------------------------------------------32 # Methode used to build a multiFrame.33 # @param self object instance34 # @param datas datas to be sent35 def buildFrame( self, datas):36 datasLength = len(datas)37 frame = struct.pack('L', datasLength)38 frame += datas39 return frame40 41 # ----------------------------------------------------------------------------42 # Methode used to display frames from a multiframe.43 # @param self object instance44 # @param multiFrame multi frame to display45 def displayMultiFrame( self, multiFrame):46 index = 047 while ( index < len( multiFrame)):48 length = struct.unpack('L', multiFrame[index:index+4])[0]49 index += 450 print multiFrame[index:index+length]51 index += length52 53 54 # ----------------------------------------------------------------------------55 # Methode used to send a frame56 # @param self object instance57 # @param datas datas to be sent58 # @param forceZip flag used to force the the transmission of zipped data59 # @param multiFrame flag used to indicate that the frame is a multiframe60 def sendFrame( self, datas, forceZip=False, multiFrame=False):61 # Process zip mode62 datasLength = len(datas)63 if forceZip or ( datasLength > 100):64 datas = datas.encode("zip")65 datasLength = len(datas) + self.ZIPPED_DATA66 67 # Process multiFrame mode68 if multiFrame == True:69 datasLength += self.MULTI_FRAME70 71 # Build message72 message = struct.pack('L', datasLength)73 message += datas74 75 # Send message76 self.send( message)77 78 79 # ----------------------------------------------------------------------------80 # Methode used to receive a frame. It returns the clear data.81 # @param self object instance82 def receiveFrame( self):83 zippedData = False84 multiFrame = False85 # Read bufferLength from socket86 buffer = ""87 while( len( buffer) != 4):88 buffer += self.recv( 4 - len( buffer))89 if buffer == "":90 break91 92 # Check if socket has been closed93 if buffer != "":94 dataLength = struct.unpack('L', buffer[0:4])[0]95 96 # Process dataLength for zipped data97 if dataLength >= self.ZIPPED_DATA:98 zippedData = True99 dataLength -= self.ZIPPED_DATA100 if dataLength >= self.MULTI_FRAME:101 multiFrame = True102 dataLength -= self.MULTI_FRAME103 104 # Receive datas105 buffer = ""106 while ( len( buffer) != dataLength):107 buffer += self.recv( dataLength - len( buffer))108 109 # Unzip zipped data110 if zippedData:111 buffer = buffer.decode("zip")112 113 # Extract each frame of the multiFrame114 if multiFrame == True:115 bufferArray = []116 index = 0117 while ( index < len( buffer)):118 length = struct.unpack('L', buffer[index:index+4])[0]119 index += 4120 bufferArray.append( buffer[index:index+length])121 index += length122 return bufferArray123 124 # Return data125 return [buffer]126 127 128 #==============================================================================129 ## Object used to send and receive data upon a socket or a request.130 class QTCnxObj( BaseCnxObj, QTcpSocket):131 132 # ----------------------------------------------------------------------------133 # Constructor134 # @param self object instance135 def __init__( self):136 BaseCnxObj.__init__( self)137 QTcpSocket.__init__( self)138 139 # ----------------------------------------------------------------------------140 # Methode used to send a message141 # @param self object instance142 # @param datas datas to be sent143 def send( self, datas):144 self.write( datas)145 146 # ----------------------------------------------------------------------------147 # Methode used to receive a message. It returns the clear data and a flag148 # indicating if the data were zipped or not149 # @param self object instance150 def recv( self, size):151 return self.read( size)152 153 154 #==============================================================================155 ## Object used to send and receive data upon a socket or a request.156 class CnxObj( BaseCnxObj):157 158 # ----------------------------------------------------------------------------159 # Constructor160 # @param self object instance161 def __init__( self, cnxObject):162 BaseCnxObj.__init__( self)163 self.cnxObj = cnxObject164 165 # ----------------------------------------------------------------------------166 # Methode used to send a message167 # @param self object instance168 # @param datas datas to be sent169 def send( self, datas):170 self.cnxObj.send( datas)171 172 # ----------------------------------------------------------------------------173 # Methode used to receive a message. It returns the clear data and a flag174 # indicating if the data were zipped or not175 # @param self object instance176 def recv( self, size):177 return self.cnxObj.recv( size)178 37 179 38 #============================================================================== … … 217 76 return 218 77 219 if len( frames ) and len(frames[0]):78 if len( frames[0]): 220 79 for frame in frames: 221 80 print "Frame received: %s" % frame … … 259 118 for index in range(0,5): 260 119 frame += cnxObj.buildFrame( "This is the frame: %02d in a multiframe" % index) 261 # cnxObj.displayMultiFrame( frame)262 120 cnxObj.sendFrame( frame, multiFrame = True) 263 121 … … 293 151 status = app.exec_() 294 152 295 296 153 # Create and connect socket 297 #sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) 298 #print "socket created" 299 #try: 300 # sock.connect( ( 'localhost', 10312)) 301 # print "socket connected" 302 #except socket.error, msg: 303 # print msg 304 # return 305 #cnxObj = CnxObj( sock) 306 #cnxObj.sendFrame( "This is a buffer tansmitted in the clear mode") 307 #cnxObj.sendFrame( "This is a buffer tansmitted in the zip mode" , forceZip=True) 308 #cnxObj.sendFrame( "This is a buffer tansmitted in the zip mode\n"*10) 309 310 #sleep(2) 311 #sock.shutdown(2) 312 #sleep(1) 154 sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) 155 print "socket created" 156 try: 157 sock.connect( ( 'localhost', 10312)) 158 print "socket connected" 159 except socket.error, msg: 160 print msg 161 return 162 cnxObj = CnxObj( sock) 163 cnxObj.sendFrame( "This is a buffer tansmitted in the clear mode") 164 cnxObj.sendFrame( "This is a buffer tansmitted in the zip mode" , forceZip=True) 165 cnxObj.sendFrame( "This is a buffer tansmitted in the zip mode\n"*10) 166 167 sleep(1) 168 sock.shutdown(2) 313 169 314 170 # =================================================
Note: See TracChangeset
for help on using the changeset viewer.
