예제 #1
0
파일: utils.py 프로젝트: Skaradams/tilota
def play(game_id, cmd):
    tmp_ipc = 'ipc://%s/%s.ipc' % (settings.IPC_PATH, str(uuid.uuid1()))
    tmp_socket = zmq.Socket(zmq.Context(), zmq.PULL)
    tmp_socket.bind(tmp_ipc)
    daemon_socket = zmq.Socket(zmq.Context(), zmq.PUSH)
    daemon_socket.connect(settings.DAEMON_INBOX)
    game = Console(os.path.join(
        settings.IPC_PATH,
        'dmtcp_restart_script_%s.sh' % game_id
    ))
    return_text = game.cmd(cmd)
    daemon_socket.send_json({'name': 'save_checkpoints', 'reply_ipc': tmp_ipc})
    tmp_socket.recv_json()
    tmp_socket.close()
    lines = return_text.split('\n')
    lines.reverse()
    end_of_text_reached = False
    formated_text = ''
    for line in lines:
        if end_of_text_reached:
            if re.compile('(|[^\w])%s(|[^\w])' % cmd).search(line):
                break
            else:
                formated_text = line + '\n' + formated_text
        else:
            if re.compile('[\w]').search(line) \
              and not re.compile('^(|[^\w])*%s(|[^\w])*$' % cmd).match(line):
                end_of_text_reached = True
                formated_text = line
    return formated_text
예제 #2
0
def play(checkpoint_path, cmd):
    folder, checkpoint_name = os.path.split(checkpoint_path)
    game = Console('dmtcp_restart -q -q %s' % checkpoint_name, cwd=folder)
    answer = game.cmd(cmd)
    formated_answer = ''
    for line in answer.split('\n'):
        if not re.compile('^' + cmd + '$').match(line):
            formated_answer += line
    os.system('dmtcp_command -bc')
    return formated_answer
예제 #3
0
파일: tilotad.py 프로젝트: Skaradams/tilota
class TilotaDaemon(Daemon):

    def initialize(self):
        os.environ['DMTCP_CHECKPOINT_DIR'] = settings.IPC_PATH
        self._coordinator = Console('dmtcp_coordinator')
        self._coordinator.read()
        self._inbox = zmq.Socket(zmq.Context(), zmq.PULL)
        self._inbox.bind(settings.DAEMON_INBOX)
        self.CALLBACKS = {
            'get_game_id': self.get_game_id,
            'save_checkpoints': self.save_checkpoints,
        }

    def _dispatch(self, message):
        message_name = message.get('name', None)
        if message_name in self.CALLBACKS:
            self.CALLBACKS[message_name](message)

    def reply(self, input_message, output_message):
        reply_ipc = input_message.get('reply_ipc', None)
        if reply_ipc:
            outbox = zmq.Socket(zmq.Context(), zmq.PUSH)
            outbox.connect(reply_ipc)
            outbox.send_json(output_message)

    def save_checkpoints(self, message):
        self._coordinator.cmd('c')
        self.reply(message, {})

    def get_game_id(self, message):
        if not message.get('pid', None):
            raise ValueError
        response = self._coordinator.cmd('l')
        search_result = re.compile(
            '[0-9]+\, [\w]+\[%d\]\@[\w]+\,' \
            ' ([\w\-]+)\, RUNNING' % message['pid']
        ).search(response)
        game_id = None
        if search_result:
            game_id = search_result.group(1)
        self.reply(message, {'game_id': game_id})

    def run(self):
        while True:
            message = self._inbox.recv_json()
            self._dispatch(message)
예제 #4
0
파일: tilotad.py 프로젝트: Skaradams/tilota
 def initialize(self):
     os.environ['DMTCP_CHECKPOINT_DIR'] = settings.IPC_PATH
     self._coordinator = Console('dmtcp_coordinator')
     self._coordinator.read()
     self._inbox = zmq.Socket(zmq.Context(), zmq.PULL)
     self._inbox.bind(settings.DAEMON_INBOX)
     self.CALLBACKS = {
         'get_game_id': self.get_game_id,
         'save_checkpoints': self.save_checkpoints,
     }