コード例 #1
0
ファイル: MessageReceiver.py プロジェクト: eirikjohannes/ktn
 def __init__(self, client, connection):
     super(MessageReceiver, self).__init__()
     #self.thread = Thread(target=self.run)
     self.daemon = True
     self.client = client
     self.connection = connection
     self.msgParser = MessageParser()
コード例 #2
0
ファイル: MessageReceiver.py プロジェクト: daniena/TTM4100
 def run(self):
     MsgParser = MessageParser()
     while True:
         data = (self.connection.recv(8192)).decode()
         if data:
             MsgParser.parse(data)
     pass
コード例 #3
0
    def run(self):
        # Initiate the connection to the server
        self.connection.connect((self.host, self.server_port))
        self.receiver = MessageReceiver(self, self.connection)
        self.MessagePars = MessageParser()

        while True:
            input_ = raw_input().split(' ')
            if len(input_) != 0:
                if (input_[0] == "login"):
                    self.send_payload({
                        'request': 'login',
                        'content': input_[1]
                    })
                elif (input_[0] == "logout"):
                    self.send_payload({
                        'request': 'logout',
                    })
                elif (input_[0] == "msg"):
                    self.send_payload({'request': 'msg', 'content': input_[1]})
                elif (input_[0] == "names"):
                    self.send_payload({'request': 'names'})
                elif (input_[0] == "help"):
                    self.send_payload({'request': 'help'})
                else:
                    print "Error, type 'help'"
コード例 #4
0
ファイル: ProcessGame.py プロジェクト: lvtangjie690/A3C
    def run(self):
        # connect master to get worker's id  
        self.sock.connect((Config.MASTER_IP, Config.MASTER_PORT))
        worker_id = MessageParser().decode_recv(self.sock)
        print('Game recv worker id', worker_id)

        self.sock.shutdown(2)
        self.sock.close()

        # try to connect worker
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        while True:
            try:
                self.sock.connect(('localhost', Config.WORKER_BASE_PORT+worker_id))
                break
            except Exception as e:
                print(e)
                time.sleep(1)
        

        self.msg_parser.send_game_model_info(self.sock, self.game.get_game_model_info())

        state, reward, done, next_state = self.game.reset()
        while True:
            self.msg_parser.send_sample(self.sock, [state, reward, done, next_state])
            if not done:
                action = self.msg_parser.recv_action(self.sock)
                state, reward, done, next_state = self.game.step(action)
            else:
                state, reward, done, next_state = self.game.reset()
コード例 #5
0
    def run(self):
        # Initiate the connection to the server
        self.connection.connect((self.host, self.port))

        self.connected = True

        self.msg_parser = MessageParser()
        self.msg_receiver = MessageReceiver(self, self.connection)
        self.msg_receiver.start()

        while self.connected:
            inp = raw_input("command <content>:\n").split()
            command = inp[0]
            if command not in legal_requests:
                print("please type a legal command")
                continue

            content = None
            if command == 'login' or command == 'msg':
                if len(inp) <= 1:
                    print("please type <content>")
                    continue
                content = " ".join(inp[1:])

            data = {
                    'request': command,
                    'content': content}

            self.send_payload(json.dumps(data))

        self.msg_receiver.join()
コード例 #6
0
 def __init__(self, name, praw_reddit, triggers, username):
     self.name = name
     self.praw_reddit = praw_reddit
     self.triggers = triggers
     self.username = username
     self.already_done = []
     self.msg_parser = MessageParser(triggers)
コード例 #7
0
    def __init__(self, host, server_port):
        self.host = host
        self.server_port = server_port

        # Set up the socket connection to the server
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.message_parser = MessageParser()
        self.run()
コード例 #8
0
 def run(self):
     print("GameListenerThread starts running")
     while True:
         sock, addr = self.server.accept()
         print('Master accept connection', sock, addr)
         worker = self.master.add_worker()
         MessageParser().send(sock, worker.id)
         time.sleep(0.1)
コード例 #9
0
 def __init__(self, client, connection):
     """
     This method is executed when creating a new MessageReceiver object
     """
     threading.Thread.__init__(self, name=client)
     # Flag to run thread as a deamon
     self.daemon = True
     self.connection = connection
     self.parser = MessageParser()
コード例 #10
0
 def run(self):
     # TODO: Make MessageReceiver receive and handle payloads
     while True:
         message_raw = self.connection.recv(4096) # bit to hardcoded?
         if not message_raw:
             self.client.disconnect()
         print("MessageReceiver received command") # temp
         message_parser = MessageParser()
         response = message_parser.parse(message_raw)
         self.client.print_line(response)
コード例 #11
0
ファイル: Brain.py プロジェクト: japanvik/Juna
    def __init__(self, dispatcher):
        """ Juna's brain. Handles messages and tries to make a reply.
        """
        self.dispatcher = dispatcher
        #Callbacks
        self.dispatcher += Event('rcv', self.learn)
        self.dispatcher += Event('speak_request', self.getMessageString)

        self.parser = MessageParser()
        self.postprocessor = Postprocessor()
        self.DEBUG = DEBUG
コード例 #12
0
ファイル: Client.py プロジェクト: emilte/chat_server
 def __init__(self, host, server_port):
     "This method is run when creating a new Client object"
     # Set up the socket connection to the server
     self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.connection.settimeout(0.1)
     # TODO: Finish init process with necessary code
     self.host = host
     self.server_port = server_port
     self.MessageParser = MessageParser()
     self.disconnected = False
     self.run()
コード例 #13
0
    def __init__(self, host, server_port):
        """
        This method is run when creating a new Client object
        """

        # Set up the socket connection to the server
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = host
        self.server_port = server_port
        self.run()
        self.messageReciever = MessageReceiver(self, self.connection)
        self.messageParser = MessageParser()
        self.chatClient()
コード例 #14
0
    def __init__(self, host, server_port):
        """
		This method is run when creating a new Client object
		"""
        self.host = host
        self.server_port = server_port
        self.logger = Logger(None)
        self.parser = MessageParser()

        # Set up the socket connection to the server
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.run()
コード例 #15
0
class MessageParserTest(unittest.TestCase):
    __message_parser = MessageParser()

    def test_city1(self):
        self.__message_parser._find_city(
            "Как же я люблю сидеть в Санкт-Петербурге и думать про доллары".
            lower())
        self.assertEqual(self.__message_parser._city_url_name,
                         "sankt-peterburg", 'Should be "sankt-peterburg"')
        self.assertEqual(self.__message_parser._city_name, "Санкт-Петербург",
                         'Should be "Санкт-Петербург"')
        self.__message_parser._reset_values()

    def test_city2(self):
        self.__message_parser._find_city(
            "А вот приеду в Краснодаре и буду покупать евро".lower())
        self.assertEqual(self.__message_parser._city_url_name, "krasnodar",
                         'Should be "krasnodar"')
        self.assertEqual(self.__message_parser._city_name, "Краснодар",
                         'Should be "Краснодар"')
        self.__message_parser._reset_values()

    def test_city_no_city(self):
        self.__message_parser._find_city(
            "ывлпофж фл ывд фыда лфыждв л".lower())
        self.assertEqual(self.__message_parser._city_url_name, None,
                         'Should be None')
        self.assertEqual(self.__message_parser._city_name, None,
                         'Should be None')
        self.__message_parser._reset_values()

    def test_currency1(self):
        self.__message_parser._find_currency(
            "А вот во Владивостоке куплю себе CNY".lower())
        self.assertEqual(self.__message_parser._currency_code, "cny",
                         'Should be "cny"')
        self.__message_parser._reset_values()

    def test_currency2(self):
        self.__message_parser._find_currency(
            "Приеду к брату в Томск - куплю себе йен".lower())
        self.assertEqual(self.__message_parser._currency_code, "jpy",
                         'Should be "jpy"')
        self.__message_parser._reset_values()

    def test_currency_no_currency(self):
        self.__message_parser._find_currency(
            "ывлпофж фл ывд фыда лфыждв л".lower())
        self.assertEqual(self.__message_parser._currency_code, None,
                         'Should be None')
        self.__message_parser._reset_values()
コード例 #16
0
 def __init__(self, kafka_topic, kafka_servers, kafka_group_id, logger, db):
     threading.Thread.__init__(self)
     self.consumer = Consumer({
         'bootstrap.servers': kafka_servers,
         'group.id': kafka_group_id,
         'default.topic.config': {
             'auto.offset.reset': 'smallest'
         }
     })
     self.topic = kafka_topic
     self.experiment_id = kafka_topic.split('-')[
         0]  # Get experiment ID from topic name.
     self.log = logger
     self.parser = MessageParser(self.log)
     self.kafkaTimeout = 5
     self.db = db
コード例 #17
0
ファイル: Client.py プロジェクト: ingridstoen/KTN-prosjekt
    def __init__(self, host, server_port):
        """
        This method is run when creating a new Client object
        """
        self.host = host
        self.server_port = server_port
        # Set up the socket connection to the server
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # Set up messageparser
        self.message_parser = MessageParser()

        try:
            self.run()
        except KeyboardInterrupt:  # Disconnect on keyboard interupt
            self.disconnect()
コード例 #18
0
ファイル: Client.py プロジェクト: matiasvc/KTN_project
    def __init__(self, host, server_port):
        """
        This method is run when creating a new Client object
        """

        # Set up the socket connection to the server
        self.connection = socket(AF_INET, SOCK_STREAM)
        self.host = host
        self.server_port = server_port

        self.message_reciever = MessageReceiver(self, self.connection)
        self.message_parser = MessageParser(self)

        self.received_answer = True
        self.is_logged_in = False

        self.run()
コード例 #19
0
    def run(self):
        parser = MessageParser()
        # TODO: Make MessageReceiver receive and handle payloads
        print("Start messageReceiver")
        while True:
            rawRecvMessage = self.connection.recv(4096)
            if rawRecvMessage:
                recvMessage = rawRecvMessage.decode()
                payload = parser.parse(recvMessage)

                if payload['response'] == 'error':
                    self.errorHandler(payload)
                elif payload['response'] == 'info':
                    self.infoHandler(payload)
                elif payload['response'] == 'history':
                    self.historyHandler(payload)
                elif payload['response'] == 'message':
                    self.messageHandler(payload)
コード例 #20
0
ファイル: Client.py プロジェクト: rubenbu/gallis
    def __init__(self, host, server_port):
        """
        This method is run when creating a new Client object
        """
        self.loggedon = False
        # Set up the socket connection to the server
        try:
            self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except socket.error as e:
            print "Failed to create socket. Error message:", e

        self.host = host
        self.server_port = server_port
        self.lastMessageRecieved = None
        self.parser = MessageParser()
        self.run()
        self.rcv = MessageReceiver(self, self.connection)
        self.rcv.start()
コード例 #21
0
ファイル: Client.py プロジェクト: macytter/ktn_prosjekt
    def __init__(self, host, server_port):
        """
		This method is run when creating a new Client object
		"""

        # Set up the socket connection to the server
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = host
        self.server_port = server_port

        self.userLoggedIn = False

        self.messageReceiver = MessageReceiver(
            self, self.connection)  # is a thread by itself.
        self.userInput = UserInput(self)  # is a thread by itself.

        self.parser = MessageParser(self)

        self.run()
コード例 #22
0
ファイル: Client.py プロジェクト: vnvestad/TTM4100
    def __init__(self, host, server_port):
        """
        This method is run when creating a new Client object
        """

        # Set up the socket connection to the server
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # Init
        self.host = host
        self.server_port = server_port
        self.state = State.LOGIN;

        self.messageEncoder = MessageEncoder()
        self.messageParser = MessageParser()

        self.username = ""

        # Run client
        self.run()
コード例 #23
0
    def _writeMessageToSocket(self, data, rewrite=False, nextPart=0, destAddresses=None):

        def getWord(type):
            """ Used to create logging text only """
            if type:
                return '(type: %s)' % type
            else:
                return '(type: UNKNOWN)'

            """
            if type == 'SVC':
                return '(type: SVC)'
            elif type == 'AFTN':
                return '(type: AFTN)'
            elif type == 'RF':
                return '(type: RF)'
            elif type == 'RQ':
                return '(type: RQ)'
            elif type == 'RQM_OK':
                return '(type: RQM_OK)'
            elif type == 'RQM_UNK':
                return '(type: RQM_UNK)'
            elif type == 'RQF_OK':
                return '(type: RQF_OK)'
            elif type == 'RQF_UNK':
                return '(type: RQF_UNK)'
            else:
                return '(type: UNKNOWN)'
            """

        mm = self.mm
        if len(data) >= 1:
            if not rewrite:
                self.logger.info("%d new bulletin will be sent" % len(data))
            else:
                self.logger.info("%d new bulletin will be resent (ack not received / reconnexion)" % len(data))

            for index in range(len(data)):
                if nextPart == 0:
                    # We will have access to the first part of the message here (big or not)
                    mp = MessageParser(data[index], mm, self.logger, True)
                    mm.header, mm.type = mp.getHeader(), mp.getType()
                    self.logger.debug("Header: %s, Type: %s" % (mm.header, mm.type))
                if mm.header== None and mm.type==None:
                    self.logger.info(data[index])
                    self.logger.error("Header %s is not in %s" % (mm.header, mm.routingTable))
                    if self.slow:
                        time.sleep(10)
                    os.unlink(self.dataFromFiles[0][1])
                    self.logger.info("%s has been erased", os.path.basename(self.dataFromFiles[0][1]))
                    del self.dataFromFiles[0]
                    mm.clearPartsToSend()
                    continue

                elif mm.header == None and mm.type=='SVC':
                    #FIXME: Is it possible to rewrite Service Message?
                    # If so, the CSN must not change!
                    mm.setFilingTime()
                    mm.nextCSN()
                    messageAFTN = MessageAFTN(self.logger, data[index], mm.stationID, mm.address, MessageAFTN.PRIORITIES[2],
                                              destAddresses or [mm.otherAddress], mm.CSN, mm.filingTime, mm.dateTime)
                    #self.logger.debug('\n' + messageAFTN.message)
                    #messageAFTN.setLogger(None)
                    #mm.archiveObject(self.archivePath + mm.CSN, messageAFTN)

                elif mm.header == None and mm.type in ['RQ', 'RF']:
                    # We will never have to sent such a message, it is only
                    # there for tests purposes
                    mm.setFilingTime()
                    mm.nextCSN()
                    messageAFTN = MessageAFTN(self.logger, data[index], mm.stationID, mm.address, MessageAFTN.PRIORITIES[2],
                                              destAddresses or [mm.otherAddress], mm.CSN, mm.filingTime, mm.dateTime)
                    #self.logger.debug('\n' + messageAFTN.message)
                    #messageAFTN.setLogger(None)
                    #mm.archiveObject(self.archivePath + mm.CSN, messageAFTN)

                elif mm.header == None and mm.type in MessageParser.REPLY_TYPES:
                    mm.setFilingTime()
                    mm.nextCSN()
                    messageAFTN = MessageAFTN(self.logger, data[index], mm.stationID, mm.address, MessageAFTN.PRIORITIES[3],
                                              destAddresses, mm.CSN, mm.filingTime, mm.dateTime)
                    #self.logger.debug('\n' + messageAFTN.message)
                    #messageAFTN.setLogger(None)
                    #mm.archiveObject(self.archivePath + mm.CSN, messageAFTN)

                elif mm.type == 'PRI_DESTADD_TEXT':
                    mm.destAddress = mp.destAddress
                    if mm.destAddress:
                        mm.priority = mp.priority
                        mm.setFilingTime()
                        mm.nextCSN()
                        messageAFTN = MessageAFTN(self.logger, mp.text, mm.stationID, mm.address, mm.priority, mm.destAddress, mm.CSN, mm.filingTime, mm.dateTime)
                    else:
                        if mm.isFromDisk():
                            try:
                                self.logger.warning("%s has been erased (no valid destination address)", os.path.basename(self.dataFromFiles[0][1]))
                                os.unlink(self.dataFromFiles[0][1])
                            except OSError, e:
                                (type, value, tb) = sys.exc_info()
                                self.logger.error("Unable to unlink %s ! Type: %s, Value: %s" % (self.dataFromFiles[0][1], type, value))
                            del self.dataFromFiles[0]
                            mm.clearPartsToSend()
                        continue

                else:
                    # True if mm.header is in the routing table with destination addresses
                    #if mm.header == None: mm.header = 'SACN31 CWAO'
                    if mm.setInfos(mm.header, rewrite):
                        messageAFTN = MessageAFTN(self.logger, data[index], mm.stationID, mm.address, mm.priority,
                                              mm.destAddress, mm.CSN, mm.filingTime, mm.dateTime)
                    else:
                        if mm.isFromDisk():
                            try:
                                self.logger.warning("%s has been erased", os.path.basename(self.dataFromFiles[0][1]))
                                os.unlink(self.dataFromFiles[0][1])
                            except OSError, e:
                                (type, value, tb) = sys.exc_info()
                                self.logger.error("Unable to unlink %s ! Type: %s, Value: %s" % (self.dataFromFiles[0][1], type, value))
                            del self.dataFromFiles[0]
                            mm.clearPartsToSend()

                        continue
                        
                # If it is the first time we sent this message, we archive it.
                #if not rewrite:
                self.logger.debug('Message as it will be sent:')
                self.logger.debug('\n' + messageAFTN.message)
                messageAFTN.setLogger(None)
                mm.archiveObject(self.archivePath + mm.CSN, messageAFTN)

                nbBytesToSend = len(messageAFTN.message)

                while nbBytesToSend > 0:
                    nbBytesSent = self.socket.send(messageAFTN.message)
                    # This sleep is machiavelic! It permits to see many potential problems
                    #if self.subscriber:
                    #    time.sleep(5)
                    messageAFTN.message = messageAFTN.message[nbBytesSent:]
                    nbBytesToSend = len(messageAFTN.message)
                    self.totBytes += nbBytesSent

                mm.setWaitingForAck(messageAFTN.getTransmitID())
                mm.incrementSendingInfos()

                # Archive State
                mm.state.fill(mm)
                mm.archiveObject(AFTNPaths.STATE, mm.state)
                self.logger.debug("State has been archived")

                # Log the fact that the message has been sent 
                if not rewrite:
                    if mm.isFromDisk():
                        mm.filenameToSend = os.path.basename(self.dataFromFiles[0][1])
                        mm.filenameToErase = self.dataFromFiles[0][1]
                    else:
                        if mp.type == 'SVC':
                            mm.filenameToSend = mp.getServiceName(mp.getServiceType())
                        elif mp.type in MessageParser.REPLY_TYPES:
                            mm.filenameToSend = ''
                        else:
                            # We should never go here
                            self.logger.error("Unknown message type has just been sent. See code!")

                    self.logger.info("(%5d Bytes) Message %s %s (%s/%s) has been sent => delivered" % (self.totBytes, getWord(mm.type), 
                                       mm.filenameToSend, mm.nextPart+1, mm.numberOfParts))
                else:
                    self.logger.info("(%5d Bytes) Message %s %s (%s/%s) has been resent => delivered" % (self.totBytes, getWord(mm.type), 
                                       mm.filenameToSend, mm.nextPart+1, mm.numberOfParts))

                # Reset byte count
                self.totBytes = 0
                
                # If the last part of a message (big or not) has been sent, erase the file.
                # We do this even if we have not yet received the ack. At this point, we have already
                # archived all the parts with their CSN as filename.
                if mm.isLastPart(): 
                    if mm.isFromDisk() and not rewrite:
                        try:
                            os.unlink(self.dataFromFiles[0][1])
                            self.logger.debug("%s has been erased", os.path.basename(self.dataFromFiles[0][1]))
                        except OSError, e:
                            (type, value, tb) = sys.exc_info()
                            self.logger.error("Unable to unlink %s ! Type: %s, Value: %s" % (self.dataFromFiles[0][1], type, value))
                        del self.dataFromFiles[0]
コード例 #24
0
    def readFromSocket(self):
        replyAFTN = ''
        mm = self.mm
        
        buf = self.socket.recv(32768)

        if len(buf): 
            self.logger.debug('Raw Buffer: %s' % repr(buf))
            message, type = mm.parseReadBuffer(buf) # Only to find if it is an AFTN (SVC included) or Ack message
            while message:
                if type == 'AFTN':
                    ##############################################################################################
                    # An AFTN Message has been read on the socket. It can be a SVC Message or a 
                    # Standard Message.
                    ##############################################################################################
                    self.logger.debug("AFTN Message: %s" % repr(message))
                    mm.messageIn = MessageAFTN(self.logger)
                    mm.messageIn.setMessage(message)
                    if not mm.messageIn.messageToValues():
                        # Here we have a problem because our parser is unable to parse the message. We print the message,
                        # get the next message if present and quit the method (no ack sent, no tid verification)
                        self.logger.error("Method MessageAFTN.messageToValues() has not worked correctly (returned 0)")
                        self.logger.error(mm.messageIn.textLines)
                        message, type = mm.parseReadBuffer("") # Only to find if it is an AFTN (SVC included) or Ack message
                        continue

                    self.logger.debug(mm.messageIn.textLines)

                    self.logger.debug('Message as it has been received:')
                    self.logger.debug('\n' + mm.messageIn.message)

                    status = mm.isItPart(mm.messageIn.textLines)

                    # Not part of a big message, possibly a SVC message
                    if status == 0:
                        suffix = 'NOT_SVC_NOR_AFTN'
                        mp = MessageParser(mm.messageIn.textLines)
                        textType = mp.getType()
                        if textType == "SVC": 
                            ##############################################################################################
                            # A Service  Message has been read on the socket. 
                            ##############################################################################################
                            suffix = 'SVC'
                            self.logger.info("SVC Message Received(%s): %s (%s)" % (mm.messageIn.getTransmitID(), str(mm.messageIn.getTextLines()), MessageParser.names.get(mp.serviceType,
                                              "The service type of this message is unknown. Contact NavCanada")))

                            #if mp.serviceType in [8, 9]:
                            #    self.logger.info("*********************** SERVICE MESSAGE *****************************")
                            #    self.logger.info(str(mm.messageIn.getTextLines()))
                            #    self.logger.info("********************* END SERVICE MESSAGE ***************************")

                        elif textType == "AFTN":
                            suffix = ''
                            if mp.getHeader() in ['SI', 'SM']:
                                # Only one message will be in messages
                                messages = mm.completeHeader(mm.messageIn)

                            elif mp.getHeader(): 
                                # Only one message will be in messages
                                messages = ['\n'.join(mm.messageIn.textLines) + '\n'] 
                            else:
                                # Create headers before ingesting
                                messages = mm.addHeaderToMessage(mm.messageIn)

                            # Ingest in met px
                            for m in messages:
                                mm.ingest(m)

                        elif textType in ['RQ', 'RF']:
                            suffix = textType
                            # request for amis or metser
                            from RequestReplyAFTN import RequestReplyAFTN
                            import dateLib
                            date = dateLib.getYYGGgg()
                            if textType == 'RQ': # amis
                                addOn = 'AACN02 ANIK %s\nATTN %s\n\n' % (date, mm.messageIn.originatorAddress)
                                replyAFTN = 'RQM '
                            elif textType == 'RF': # metser
                                addOn = 'AACN44 CWAO %s\nATTN %s\n\n' % (date, mm.messageIn.originatorAddress)
                                replyAFTN = 'RQF '

                            self.logger.info('AFTN Request Received: Type = %s, Value = %s' % (textType, mp.request))

                            # We want to put the answer on amis or metser.
                            try:
                                rr = RequestReplyAFTN(mp.request, addOn, mp.sendOn, self.logger)
                            except:
                                (type, value, tb) = sys.exc_info()
                                self.logger.error("In RequestReplyAFTN: Type = %s, Value = %s" % (type, value))

                            if rr.bulletin:
                                self.logger.info('Reply is not empty, we will put bulletin in the queue of receiver %s and send an OK message' % rr.receiverName)
                                # bulletin is not empty, put it in queue and create an "OK" message
                                rr.putBulletinInQueue()
                                replyAFTN += 'OK'
                            else:
                                self.logger.info('Request is empty, we will send an UNK message')
                                # bulletin is empty, create an "UNK" message
                                replyAFTN += 'UNK'

                        elif textType in ['RQM_UNK', 'RQM_OK', 'RQF_UNK', 'RQM_OK']:
                        # reply about a request. I think a request never originates from us,
                        # so we should never receive such a reply.
                            suffix = textType
                            self.logger.info("Reply received is: %s" % textType)

                        # We keep one copy of all received messages in a special AFTN directory
                        file = open(self.writePath + mm.messageIn.getName() + suffix, 'w')
                        for line in mm.messageIn.textLines:
                            file.write(line + '\n')
                        file.close()

                    # General part of a big message
                    elif status == 1:
                        self.logger.debug("We are in section 'General part of a big message'")
                        pass
                    # Last part of a big message
                    elif status == -1:
                        file = open(self.writePath + mm.messageIn.getName(), 'w')
                        for line in mm.receivedParts:
                            file.write(line + '\n')
                        file.close()

                        # We must ingest the bulletin contained in the message in the px system
                        lines = [line.strip() for line in mm.receivedParts]
                        mp = MessageParser(lines)

                        if mp.getHeader(): 
                            # Only one message will be in messages
                            messages = ['\n'.join(lines) + '\n'] 
                        else:
                            # Create headers before ingesting
                            messages = mm.addHeaderToMessage(mm.messageIn, lines)

                        # Ingest in met px
                        for m in messages:
                            mm.ingest(m)

                        mm.receivedParts = []

                    # FIXME: The number of bytes include the ones associated to the protocol overhead,
                    # maybe a simple substraction should do the job.
                    self.logger.info("(%i Bytes) Message %s has been received" % (len(mm.messageIn.getTextString()), mm.messageIn.getName()))
                    
                    if mm.ackUsed:
                        self._writeAckToSocket(mm.messageIn.getTransmitID())
                        mm.totAck += 1
                        #if mm.totAck == 5:
                        #    mm.ackUsed = False 

                    ##############################################################################################
                    # Is the CSN Order correct? Maybe Service Message would have to be sent?
                    ##############################################################################################
                    tid = mm.messageIn.getTransmitID()
                    if tid == mm.getWaitedTID():
                        self.logger.debug("The TID received (%s) is in correct order" % tid)
                    elif mm.getWaitedTID() == None:
                        self.logger.debug("Waited TID is None => the received TID (%s) is the first since the program start" % tid)
                    else:
                        self.logger.error("The TID received (%s) is not the one we were waiting for (%s)" % (tid, mm.getWaitedTID()))
                        if int(mm.getWaitedTID()[3:]) - int(tid[3:]) == 1:
                            self.logger.error("Difference is 1 => Probably my ack has been lost (or is late) and the other side has resend")
                        # FIXME: A SVC Message should be sent here. Given the fact that we receive the same message
                        # again, can we conclude that it is a retransmission (because our ack has not been received)
                        # or an error in numbering message?
                        diffCSN = int(tid[3:])- int(mm.getWaitedTID()[3:])
                        if diffCSN < 0:
                            messageText = "SVC LR %s EXP %s" % (tid, mm.getWaitedTID())

                        # FIXME: This implementation is done with only a partial comprehension of how this 
                        # message should work. Should be completed later...
                        elif diffCSN > 0: 
                            if diffCSN == 1:
                                messageText = "SVC QTA MIS %s" % mm.getWaitedTID()
                            else:
                                lastCSN = "%04d" % (int(tid[3:]) - 1)
                                messageText = "SVC QTA MIS %s-%s" % (mm.getWaitedTID(), lastCSN)

                        if not mm.getWaitingForAck():
                            mm.partsToSend = [messageText]
                            mm.completePartsToSend(mm.partsToSend)  
                            mm.setFromDisk(False)
                            self._writeMessageToSocket([mm.partsToSend[0]], False, mm.nextPart)
                            mm.setFromDisk(True)
                        else:
                            # We queue the message to send it after we receive the ack we wait for.
                            self.logger.info("A service message (%s) will be queued" % messageText)
                            mm.serviceQueue.append((messageText, []))
                            
                    mm.calcWaitedTID(tid)

                    ##############################################################################################
                    # Do we have to send a reply to a request?
                    ##############################################################################################
                    if replyAFTN:
                        if not mm.getWaitingForAck():
                            self.logger.debug("A reply (%s) to a request will be sent immediately" % replyAFTN)
                            mm.partsToSend = [replyAFTN]
                            mm.completePartsToSend(mm.partsToSend)  
                            mm.setFromDisk(False)
                            self._writeMessageToSocket([mm.partsToSend[0]], False, mm.nextPart, [mm.messageIn.originatorAddress])
                            mm.setFromDisk(True)
                        else:
                            # We queue the message to send it after we receive the ack we wait for.
                            self.logger.info("A reply (%s) to a request will be queued (because we are waiting for an ack)" % replyAFTN)
                            mm.serviceQueue.append((replyAFTN, [mm.messageIn.originatorAddress]))

                elif type == 'ACK':
                    ##############################################################################################
                    # An Ack Message has been read on the socket. 
                    ##############################################################################################
                    self.logger.debug("Ack Message: %s" % repr(message))
                    strippedMessage = message[2:9]
                    mm.setLastAckReceived(strippedMessage)
                    if mm.getLastAckReceived() == mm.getWaitingForAck():
                        mm.setWaitingForAck(None)
                        mm.resetSendingInfos()
                        mm.updatePartsToSend()
                        self.logger.info("Ack received is the ack we wait for: %s" % strippedMessage)
                    else:
                        # FIXME: When deconnexion occurs, it is possible that we received an ack for a previously sent message???
                        self.logger.error("Ack received (%s) is not the ack we wait for: %s" % (strippedMessage, mm.getWaitingForAck()))
                        if mm.getWaitingForAck() == None:
                            pass
                        elif int(mm.getWaitingForAck()[3:]) - int(strippedMessage[3:]) == 1:
                            self.logger.error("Difference is 1 => Probably my original message + the one I resend have been hacked (Timing problem)")

                # Archive State
                mm.state.fill(mm)
                mm.archiveObject(AFTNPaths.STATE, mm.state)
                self.logger.debug("State has been archived")

                message, type = mm.parseReadBuffer("") # Only to find if it is an AFTN (SVC included) or Ack message
                if not message and type:
                    self.logger.debug("Message (type=%s) uncomplete. It's ok. We will try to complete it in the next pass." % type)

        else:
            # If we are here, it normally means the other side has hung up(not sure in this case, because I use
            # select. Maybe it simply not block and return 0 bytes? Maybe it's correct to do nothing and act 
            # only when the POLLHUP state is captured?
            # FIXME: POLLHUP is never present, I don't know why?
            self.logger.error("Zero byte have been read on the socket (Means the other side has HUNG UP?)")
            raise Exception("Zero byte have been read on the socket (Means the other side has HUNG UP?)")
コード例 #25
0
 def receive_message(self, message, connection):
     parser = MessageParser()
     print parser.parse(message)
コード例 #26
0
class TextAnalyzer:
    """ A text Analyzer """
    parser = MessageParser()

    def extractTopics(self, text_list, threshold=0.5):
        """ Return a list of Words which seems to be the current topic """
        if not text_list: return []
        #Make pseudo sentences from the text_list
        pCandidates = []
        for message in text_list:
            pSentence = self.parser.parseSentence(message)
            if not pSentence: return []
            #pick out only the Candidates from the pseudo words
            candidates = [x for x in pSentence if x['main_type'] in [u'名詞']]
            #print 'candidates:%s' % candidates
            #pCandidates is a list of pseudo words which are candidates of the topic
            if candidates: pCandidates.extend(candidates)
            #print 'pCandidates:%s' % pCandidates
        #Next we make a dictionary of occurences of this particular candidates
        #Convert the pseudo words to real words
        true_candidates = Sentence()
        true_candidates.pseudo2real(pCandidates)
        #print 'true_candidates:%s' % true_candidates.words
        #make a dictionary {word_id:occurence}
        sample_words = {}
        for w in true_candidates:
            if sample_words.has_key(int(w.id)):
                sample_words[int(w.id)] += 1
            else:
                sample_words[int(w.id)] = 1

        if len(sample_words) < 1: return []
        #print 'sample_words:%s' % sample_words

        #We make the actual occurences from the database
        base_words = {}
        for word_id in sample_words.keys():
            base_words[word_id] = Word.get(word_id).occurence

        sample_count = 0
        for k, v in sample_words.iteritems():
            sample_count += v

        base_count = 0
        for k, v in base_words.iteritems():
            base_count += v

        scores = {}
        for w in sample_words.keys():
            scores[w] = self.score(float(sample_words[w]), float(sample_count),
                                   float(base_words[w]), float(base_count))

        items = [(v, k) for k, v in scores.items()]
        items.sort()
        items.reverse()  # so largest is first
        candidate_keywords = [x[1] for x in items if x[0] > threshold]
        #Fallback - if no topics are found, we choose a random noun
        # Yucky, but makes Juna more talkative.
        if (not candidate_keywords) and (candidates):
            choice = [candidates[int(random.random() * len(candidates))]]
            s = Sentence()
            s.pseudo2real(choice)
            candidate_keywords = [s[0].id]
        return candidate_keywords

    def score(self, sample_occurence, sample_count, base_occurence,
              base_count):
        """Scoring for relavance """

        if base_count == sample_count: return None
        #print 'score_vars::s_occ:%d s_count:%d b_occ:%d b_count:%d' % (sample_occurence, sample_count, base_occurence, base_count)
        return math.tanh(
            sample_occurence / sample_count * 200) - 5 * math.tanh(
                (base_occurence - sample_occurence) /
                (base_count - sample_count) * 200)
コード例 #27
0
    def __init__(self, options, run_on_localhost=False):

        # DO NOT EDIT ANYTHING BELOW THIS LINE IN __init__
        # -----------------------------------------------------------------------------

        # Use this selector object for the assignment.
        self.sel = selectors.DefaultSelector()

        # Use this MessageParser object for assignment 2
        self.parser = MessageParser()

        # CRCServer accepts an "options" variable on construction, which is used to pass in
        # several important values from the test configuration file. This includes the
        # server's name, the port the server socket should listen on, and a human-readable
        # description of the server (info)

        self.servername = options.servername
        self.port = options.port
        self.info = options.info

        # The next three variables store information about what machine this server should
        # attempt to connect to on initialization. Except for the very first CRCServer in a
        # network, all CRCServers connect to a pre-existing server on initialization. This
        # pre-existing server will be the new server's point of access into the wider network

        # The hostname of the server to connect to on startup (e.g. theshire.nz)
        self.connect_to_host = options.connect_to_host

        # The IP address of the server to connect to on startup.
        # IMPORTANT: You MUST use self.connect_to_host_addr when creating a TCP socket to
        #            connect to this machine. This value is set dynamically based on whether
        #            we are testing on localhost, or running in the wild on real servers
        self.connect_to_host_addr = options.connect_to_host

        # The port to connect to on the host server
        self.connect_to_port = options.connect_to_port

        # If this server is configured to run on localhost, then self.connect_to_host_addr
        # will be overwritten with the loopback address
        self.run_on_localhost = run_on_localhost
        if self.run_on_localhost:
            self.connect_to_host_addr = '127.0.0.1'

        # Store the servers who are directly connected to this server
        # The list should contain the names of the servers
        self.adjacent_servers = []

        # Store all information about servers in this variable
        # The key should be the servername, and the variable a ServerData object
        self.servers_lookuptable = {}

        # Store the users who are directly connected to this server
        # The list should contain the nick of the users
        self.adjacent_users = []

        # Store all information about users in this variable
        # The key should be the user's nick, and the variable a UserData object
        self.users_lookuptable = {}

        # Options to help with debugging and logging
        self.log_file = options.log_file
        self.logger = None
        self.init_logging()

        # THIS WILL BE SET TO TRUE BY CRCTestManager.py WHEN IT IS TIME TO TERMINATE THIS PROCESS
        # DO NOT CHANGE THIS VALUE IN YOUR CODE
        self.request_terminate = False

        # This dictionary contains mappings from commands to command handlers.
        # Upon receiving a command X, the appropriate command handler can be called with: self.message_handlers[X](...args)
        self.message_handlers = {
            # Connection Registration message handlers
            "USER": self.handle_user_message,
            "SERVER": self.handle_server_message,
            "QUIT": self.handle_quit_message,
        }

        # This dictionary maps human-readable reply/error messages to their numerical representations.
        # The numerical representation must be sent to clients, not the human-readable version.
        # The full format for each reply/error message is included next to each command as a comment
        self.reply_codes = {
            "RPL_WELCOME":
            1,  # :server_name ### :Welcome to the Internet Relay Network <nick>!<user>@<host>
            "ERR_NOSUCHNICK": 401,  # :server_name ### <nick> :No such nick
            "ERR_NICKCOLLISION":
            436,  # :server_name ### <nick> :Nickname collision KILL from <user>@<host>
            "ERR_NEEDMOREPARAMS":
            461,  # :server_name ### <command> :Not enough parameters
        }
コード例 #28
0
 def receive_message(self, message):
     parser = MessageParser()
     parsed_message = parser.parse(message)
     print(parsed_message, '\n> ', end='')
コード例 #29
0
    def run(self):

        self.MessageParser = MessageParser()
        while True:
            payload = self.connection.recv(45096)
            self.MessageParser.parse(payload)
コード例 #30
0
 def receive_message(self, message):
     parser = MessageParser()
     messageType = parser.parse(message)
     print messageType