Exemplo n.º 1
0
 def handle(self, m_type, prev_envelope):
     message = None
     if m_type == SavePictureInfoRequest:
         self.totalPicParts = prev_envelope.message.numberOfParts
         self.update_protocol(self.totalPicParts, False, 3)
         message = SavePictureInfoReply(True)
     if m_type == SavePicturePartRequest:
         picture = prev_envelope.message.picturePart
         self.pictureParts.append(picture)
         self.picPartsRecieved += 1
         message = SavePicturePartReply(True, self.picPartsRecieved)
         if prev_envelope:
             message.setConversationId(prev_envelope.message.conversationId)
             envelope = Envelope(message=message, endpoint=prev_envelope.endpoint)
             if envelope:
                 self.sendNewMessage(envelope)
         if self.picPartsRecieved == self.totalPicParts:
             picture = PictureManager.combinePicture(self.pictureParts)
             message = GetPictureReply(True, picture)
             prev_envelope.message = message
             super(InitiatedGetPictureConversation, self).receivedNewMessage(prev_envelope)
             message = None
         message = None
     if message and prev_envelope:
         message.setConversationId(prev_envelope.message.conversationId)
         envelope = Envelope(message=message, endpoint=prev_envelope.endpoint)
         if envelope:
             self.sendNewMessage(envelope)
     else:
         super(InitiatedGetPictureConversation, self).handle(m_type, prev_envelope)
Exemplo n.º 2
0
 def test_protocol_updates_on_receive(self):
     cf = ConversationFactory()
     r_message = RegisterRequest("test")
     env = Envelope(message=r_message, endpoint='endpoint')
     is_outgoing = True
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     reply_m = RegisterReply("test", "test", "test")
     env2 = Envelope(message=reply_m, endpoint='endpoint')
     pro = [pro for pro in convo.protocol if pro['type'] == type(reply_m)]
     self.assertFalse(pro[0]['status'])
     convo.receivedNewMessage(env2)
     pro = [pro for pro in convo.protocol if pro['type'] == type(reply_m)]
     self.assertTrue(pro[0]['status'])
Exemplo n.º 3
0
 def test_protocol_convo_reieves_invalid_message(self):
     cf = ConversationFactory()
     r_message = RegisterRequest("test")
     env = Envelope(message=r_message, endpoint='endpoint')
     is_outgoing = True
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     reply_s = ServerListRequest(ProcessType.MainServer)
     env2 = Envelope(message=reply_s, endpoint='endpoint')
     pro = [pro for pro in convo.protocol if pro['status'] == False]
     prev_length = len(pro)
     convo.receivedNewMessage(env2)
     pro2 = [pro for pro in convo.protocol if pro['status'] == False]
     current_length = len(pro2)
     self.assertEqual(prev_length, current_length)
Exemplo n.º 4
0
 def handle(self, m_type, prev_envelope):
     message = None
     if m_type == SavePictureInfoRequest:
         self.cameraName = prev_envelope.message.cameraName
         self.timeStamp = prev_envelope.message.timeStamp
         self.totalPicParts = prev_envelope.message.numberOfParts
         self.update_protocol(self.totalPicParts, False, 2)
         message = SavePictureInfoReply(True)
     if m_type == SavePicturePartRequest:
         picture = prev_envelope.message.picturePart
         self.pictureParts.append(picture.picturePart)
         self.picPartsRecieved += 1
         if self.picPartsRecieved == self.totalPicParts:
             picture = PictureManager.combinePicture(self.pictureParts)
             pictureInfo = PictureInfo(cameraName=self.cameraName,
                 timeStamp=self.timeStamp, picture=picture)
             message = SaveCombinedPictureRequest(pictureInfo)
             prev_envelope.message = message
             super(ReceivedTransferMotionImageConversation, self).receivedNewMessage(prev_envelope)
             message = None
         else:
             message = SavePicturePartReply(True, picture.partNumber)
     if message and prev_envelope:
         message.setConversationId(prev_envelope.message.conversationId)
         envelope = Envelope(message=message, endpoint=prev_envelope.endpoint)
         if envelope:
             self.sendNewMessage(envelope)
     else:
         super(ReceivedTransferMotionImageConversation, self).handle(m_type, prev_envelope)
Exemplo n.º 5
0
 def handle(self, m_type, prev_envelope):
     message = None
     if m_type == SaveMotionRequest:
         picture = prev_envelope.message.pictureInfo
         parts, part_count = PictureManager.splitPicture(picture.picture)
         self.pictureParts = parts
         self.totalPicParts = part_count
         super(InitiatedTransferMotionImageConversation,self).update_protocol(part_count, True, 3)
         message = SavePictureInfoRequest(part_count, picture.timeStamp, picture.cameraName)
     if m_type == SavePictureInfoReply or m_type == SavePicturePartReply:
         if self.picPartsSent < self.totalPicParts:
             picPart = self.pictureParts[self.picPartsSent]
             picPart = PicturePart(picPart, self.picPartsSent, '')
             message = SavePicturePartRequest(picPart)
             self.picPartsSent += 1
     if m_type == MotionDetectedReply:
         if self.destructFunc:
             self.destructFunc(prev_envelope.message.conversationId)
     if message and prev_envelope:
         message.setConversationId(prev_envelope.message.conversationId)
         envelope = Envelope(message=message, endpoint=prev_envelope.endpoint)
         if envelope:
             self.sendNewMessage(envelope)
     else:
         super(InitiatedTransferMotionImageConversation, self).handle(m_type, prev_envelope)
Exemplo n.º 6
0
 def test_create_outgoing_conversation(self):
     cf = ConversationFactory()
     s_message = ServerListRequest(ProcessType.MainServer)
     env = Envelope(message=s_message, endpoint='endpoint')
     is_outgoing = False
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     self.assertEqual(type(convo), ReceivedMainServerListConversation)
Exemplo n.º 7
0
 def test_convo_handles(self):
     cf = ConversationFactory()
     a_message = AliveRequest()
     env = Envelope(message=a_message, endpoint='endpoint')
     is_outgoing = False
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     pro = [pro for pro in convo.protocol if pro['status'] == False]
     self.assertEqual(len(pro), 0)
Exemplo n.º 8
0
 def handle(self, m_type, prev_envelope):
     # can be overridden in the subclass or to added to still call super()
     message = None
     if m_type == AliveRequest:
         message = AliveReply(True)
     if message and prev_envelope:
         message.setConversationId(prev_envelope.message.conversationId)
         envelope = Envelope(message=message, endpoint=prev_envelope.endpoint)
         if envelope:
             self.sendNewMessage(envelope)
Exemplo n.º 9
0
 def test_protocol_get_current_message(self):
     cf = ConversationFactory()
     r_message = RegisterRequest("test")
     env = Envelope(message=r_message, endpoint='endpoint')
     is_outgoing = True
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     expected_type = convo.protocol[1]['type']
     expected_is_last = True
     type, is_last = convo.getCurrentMessage()
     self.assertEqual(type, expected_type)
     self.assertEqual(is_last, expected_is_last)
Exemplo n.º 10
0
 def test_conversation_creation_updates_protocol(self):
     cf = ConversationFactory()
     r_message = RegisterRequest("test")
     env = Envelope(message=r_message, endpoint='endpoint')
     is_outgoing = True
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     pro = [
         pro for pro in convo.protocol
         if pro['type'] == convo.initiation_message
     ]
     self.assertTrue(pro[0]['status'])
Exemplo n.º 11
0
 def test_create_conversation_with_invalid_message(self):
     cf = ConversationFactory()
     a_message = AliveReply(True)
     env = Envelope(message=a_message, endpoint='endpoint')
     is_outgoing = False
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     self.assertEqual(convo, None)
     is_outgoing = True
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     self.assertEqual(convo, None)
Exemplo n.º 12
0
 def test_create_conversation(self):
     cf = ConversationFactory()
     r_message = RegisterRequest("test")
     env = Envelope(message=r_message, endpoint='endpoint')
     is_outgoing = True
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     self.assertEqual(type(convo), RegistrationConversation)
     is_outgoing = False
     convo = cf.create_conversation(env, is_outgoing, Queue.Queue(),
                                    Queue.Queue(), None)
     self.assertEqual(type(convo), RegistrationConversation)
Exemplo n.º 13
0
    def test_CommunicationSubsystem_GetMessage(self):
        commSub = CommunicationSubsystem.CommunicationSubsystem()
        self.assertIsNot(commSub, None)

        getMessageResponse = commSub.getMessage()
        self.assertEqual(getMessageResponse[0], False)
        self.assertEqual(getMessageResponse[1], "")

        envelope = Envelope(('localhost', 50000), AliveRequest())
        commSub.fromConversationQueue.put(envelope)
        getMessageResponse = commSub.getMessage()
        self.assertEqual(getMessageResponse[0], True)
        self.assertEqual(getMessageResponse[1], envelope)

        getMessageResponse = commSub.getMessage()
        self.assertEqual(getMessageResponse[0], False)
        self.assertEqual(getMessageResponse[1], "")
Exemplo n.º 14
0
 def __receiveMessage(self, udpSocket):
     try:
         data, addr = udpSocket.recvfrom(32768)
         if data:
             if (data[0:16] == 'encryptedRequest'):
                 logging.debug('Decrypting Register Request message')
                 data = self.decryptRegisterRequest(data[16:])
             if (data[0:14] == 'encryptedReply'):
                 logging.debug('Decrypting Register Request message')
                 data = self.decryptRegisterReply(data[14:])
             message = Message.decode(data)
             #print "Received message: ", message, " from ", addr
             logging.debug("Received message " + repr(message) + \
                 " from " + repr(addr))
             envelope = Envelope(addr, message)
             self.incomingMessageQueue.put(envelope)
     except:
         return
Exemplo n.º 15
0
    def test_ConversationManager__SendMessage(self):
        endpoint = ('localhost', 0)
        fromQueue = Queue.Queue()
        connMan = ConversationManager.ConversationManager(
            fromQueue, endpoint, False)

        class Conversation:
            def __init__(self, ):
                pass

            def sendNewMessage(self, envelope):
                self.envelope = envelope

        connMan.conversations["Test"] = Conversation()
        envelope = Envelope(('localhost', 50000), AliveRequest())
        envelope.message.setConversationId("Test")

        connMan.sendMessage(envelope)
        self.assertEqual(connMan.conversations["Test"].envelope, envelope)
Exemplo n.º 16
0
 def handle(self, m_type, prev_envelope):
     message = None
     if m_type == GetPictureReply:
         picture = prev_envelope.message.picture
         parts, part_count = PictureManager.splitPicture(picture)
         self.pictureParts = parts
         self.totalPicParts = part_count
         super(ReceivedGetPictureConversation,self).update_protocol(part_count, True, 4)
         message = SavePictureInfoRequest(part_count, None, None)
     if m_type == SavePictureInfoReply or m_type == SavePicturePartReply:
         if self.picPartsSent < self.totalPicParts:
             picPart = self.pictureParts[self.picPartsSent]
             message = SavePicturePartRequest(picPart)
             self.picPartsSent += 1
             if self.picPartsSent == self.totalPicParts:
                 if self.destructFunc:
                     self.destructFunc(prev_envelope.message.conversationId)
     if message and prev_envelope:
         message.setConversationId(prev_envelope.message.conversationId)
         envelope = Envelope(message=message, endpoint=prev_envelope.endpoint)
         if envelope:
             self.sendNewMessage(envelope)
     else:
         super(ReceivedGetPictureConversation, self).handle(m_type, prev_envelope)