示例#1
0
    def __init__(self, service, player, UUID=None):
        # The Service must be a OneToManyService subclass.
        if not isinstance(service, Service.OneToManyService):
            raise OneToManyServiceError
        self.service = service    
        

        # The Player must be a Player instance.
        if not isinstance(player, Player):
            raise PlayerError
        self.player = player
        self.otherPlayers = {}

        # Generate the UUID for this Game when necessary.
        if UUID == None:
            self.UUID = str(uuid.uuid1())
        else:
            self.UUID = UUID
        
        # Thread state variables.
        self.alive = True
        self.die   = False
        self.lock = threading.Condition()
        
        self.messageProcessor = MessageProcessor (service, self.UUID, self.player.UUID)
        self.messageProcessor.start()
        self.SERVER_MOVE_TYPE = self.messageProcessor.SERVER_MOVE_TYPE

        super(Game, self).__init__(name="Game-Thread")
示例#2
0
    def setUp(self):
        """
        Create test messages for each test case.
        """
        sender = 'Test Sender'
        timestamp = datetime.now()

        body1 = '?'
        body2 = 'Pretty TYPICAL content ;)'
        body3 = 'First. Second! Third!! Fourth?! Fifth...!'

        self.message_question_mark = TextMessage(sender, timestamp, body1)
        self.message_sentences = TextMessage(sender, timestamp, body3)
        self.message_simple = TextMessage(sender, timestamp, body2)

        self.processor = MessageProcessor() 
示例#3
0
文件: Main.py 项目: pev115/Courses
import time


from MessageProcessor import MessageProcessor
from DataManipulator import DataManipulator

while True:
  print "sendin a message sneekily hehe" 
  countFetch = DataManipulator()
  satisfaction_count=countFetch.getSatisfactionCount()
  messenger = MessageProcessor()
  messenger.processMessage(satisfaction_count)

  time.sleep(60)





示例#4
0
class Game(threading.Thread):
    """Wrapper around GlobalState that provides mutual exclusion, which is
    necessary for most (if not all) games."""

    MOVE, CHAT, JOIN, WELCOME, LEAVE, FREEZE, UNFREEZE, NONE= range(8)
    RELEASED, WANTED, HELD = range(3)
    MUTEX_MESSAGE_TYPE = 'MUTEX_MESSAGE'
    HISTORY_MESSAGE_TYPE = 'HISTORY_MESSAGE'
    SERVER_MOVE_TYPE = 'SERVER-MESSAGE'

    def __init__(self, service, player, UUID=None):
        # The Service must be a OneToManyService subclass.
        if not isinstance(service, Service.OneToManyService):
            raise OneToManyServiceError
        self.service = service    
        

        # The Player must be a Player instance.
        if not isinstance(player, Player):
            raise PlayerError
        self.player = player
        self.otherPlayers = {}

        # Generate the UUID for this Game when necessary.
        if UUID == None:
            self.UUID = str(uuid.uuid1())
        else:
            self.UUID = UUID
        
        # Thread state variables.
        self.alive = True
        self.die   = False
        self.lock = threading.Condition()
        
        self.messageProcessor = MessageProcessor (service, self.UUID, self.player.UUID)
        self.messageProcessor.start()
        self.SERVER_MOVE_TYPE = self.messageProcessor.SERVER_MOVE_TYPE

        super(Game, self).__init__(name="Game-Thread")
        
    def sendHistory(self, playerUUID, field):
        # only send the history if we are the host
        if self.messageProcessor.host == self.player.UUID:
            players = copy.deepcopy(self.otherPlayers)
            players['host'] = self.player
            moves = []
            
            # append all the moves ever done in the history
            for row in range(field.rows):
                for col in range(field.cols):
                    if field.values[row][col] != -1:
                        moves.append ({'type' : self.MOVE, 'row' : row, 'col' : col, 'player' : field.values[row][col]})
                      
            self.sendMessage({'type' : self.HISTORY_MESSAGE_TYPE, 'players' : players, 'history' : moves, 'target' : playerUUID})
        
    def processHistoryMessage(self, sourceUUID, message):
        if message['target'] == self.player.UUID:
            # add all the players in the gui
            for player in message['players'].keys():
                if player == 'host':
                    self.messageProcessor.host = message['players'][player].UUID
                self.messageProcessor.inbox.put((message['players'][player].UUID, {'type' : self.WELCOME, 'I am' : message['players'][player]}))
            
            # do all the moves done in the past
            for move in message['history']:
                self.messageProcessor.inbox.put((message['players'][player].UUID, {'type' : self.MOVE, 'row' : move['row'], 'col' : move['col'], 'player' : move['player']}))
        
            self.messageProcessor.messageApproval(message)


    #
    # Game-to-Game messages.
    #
    def sendMessage(self, message, sendToSelf = True):
        print 'Game.sendMesssage:', message
        # check if we get a reply for moves and joins done by this player,
        # if we don't get a reply the host has left, and we need to resend our message
        if message['type'] == self.SERVER_MOVE_TYPE or message['type'] == self.JOIN:
            message['timestamp'] = time.time() + self.messageProcessor.NTPoffset
            self.messageProcessor.messagesAwaitingApproval.append(message)
        return self.messageProcessor.sendMessage(message, sendToSelf)


    def countReceivedMessages(self):
        return self.messageProcessor.countReceivedMessages()


    def receiveMessage(self):
        return self.messageProcessor.receiveMessage()
        

    # Thread-related methods.
    #
    def run(self):
        raise NotImplemented


    def kill(self):
        # Let the thread know it should commit suicide.
        with self.lock:
            self.die = True


    def _commitSuicide(self):
        """Commit suicide when asked to. The lock must be acquired before
        calling this method.
        """

        # Kill globalState.
        self.messageProcessor.kill()

        # Stop us from running any further.
        self.alive = False


    #
    # Stats at the time of the function call.
    #
    def stats(self):
        with self.messageProcessor.lock:
            stats = {
                'game UUID' : self.UUID,
                'player UUID' : self.player.UUID,
            }
        return stats
示例#5
0
class TestMessageProcessorMethods(unittest.TestCase):
    """
    Test message processing methods.
    """

    def setUp(self):
        """
        Create test messages for each test case.
        """
        sender = 'Test Sender'
        timestamp = datetime.now()

        body1 = '?'
        body2 = 'Pretty TYPICAL content ;)'
        body3 = 'First. Second! Third!! Fourth?! Fifth...!'

        self.message_question_mark = TextMessage(sender, timestamp, body1)
        self.message_sentences = TextMessage(sender, timestamp, body3)
        self.message_simple = TextMessage(sender, timestamp, body2)

        self.processor = MessageProcessor() 


    # --------------------------------------------------------------------------
    # Tests for average_message_length()
    # --------------------------------------------------------------------------

    def test_average_message_length_empty_list(self):
        result = self.processor.average_message_length([])
        self.assertEqual(result, 0)


    def test_average_message_length_single_message(self):
        result = self.processor.average_message_length([self.message_simple])
        self.assertEqual(result, len(self.message_simple.body))


    def test_average_message_length_multiple_messages(self):
        messages = [self.message_simple, self.message_sentences]
        result = self.processor.average_message_length(messages)

        expected = (len(self.message_simple.body) + \
            len(self.message_sentences.body)) / 2

        self.assertEqual(result, expected)


    # --------------------------------------------------------------------------
    # Tests for average_sentence_length()
    # --------------------------------------------------------------------------

    def test_average_sentence_length_empty_list(self):
        result = self.processor.average_sentence_length([])
        self.assertEqual(result, 0)


    def test_average_sentence_length_single_message(self):
        result = self.processor.average_sentence_length([self.message_simple])
        self.assertEqual(result, len(self.message_simple.body))


    def test_average_sentence_length_multiple_messages(self):
        messages = [self.message_simple, self.message_sentences]
        result = self.processor.average_sentence_length(messages)

        expected = 52.0 / 6
        self.assertEqual(result, expected)