def on_dir(directory, configuration_files, configuration_values):
        parser = argparse.ArgumentParser(usage='%(prog)s locations DIR [options]')
        parser.add_argument('--redownload', action='store_true', help='Force redownload of databases')
        parser.add_argument('--reset-database', action='store_true', help='Reset the database, forcing the server to download all the data again')
        parser.add_argument('--reset-cache', action='store_true', help='Reset the database, forcing the server to download all the data again')
        args = parser.parse_args(sys.argv[3:])

        config = ConfigurationManager.create(directory, configuration_files, configuration_values)

        for filename in 'GeoLite2-Country.mmdb', 'GeoLite2-City.mmdb':
            if os.path.exists(filename):
                print("Found %s" % filename)
                if args.redownload:
                    print("Redownloading...")
            else:
                print("%s not found. Downloading..." % filename)

            if args.redownload or not os.path.exists(filename):
                r = requests.get("http://geolite.maxmind.com/download/geoip/database/%s.gz" % filename)
                open('%s.gz' % filename,'w').write(r.content)
                uncompressed = gzip.open('%s.gz' % filename).read()
                open(filename, 'w').write(uncompressed)
                print("Downloaded")

        db = DatabaseGateway(config)

        if args.reset_database:
            print("Resetting database")
            db.reset_locations_database()

        if args.reset_cache:
            print("Resetting cache")
            db.reset_locations_cache()
    def setUp(self):

        cfg_manager = ConfigurationManager.ConfigurationManager()
        cfg_manager.append_module(configuration)
        self.dbmanager = DatabaseGateway(cfg_manager)
        self.dbmanager._delete_all_uses()
        session = self.dbmanager.Session()
        try:
            student1 = self.dbmanager._get_user(session, 'student1')
        finally:
            session.close()

        self.initial_store = TemporalInformationStore.InitialTemporalInformationStore(
        )
        self.finished_store = TemporalInformationStore.FinishTemporalInformationStore(
        )
        self.commands_store = TemporalInformationStore.CommandsTemporalInformationStore(
        )
        self.completed_store = TemporalInformationStore.CompletedInformationStore(
        )

        self.retriever = TemporalInformationRetriever.TemporalInformationRetriever(
            cfg_manager, self.initial_store, self.finished_store,
            self.commands_store, self.completed_store, self.dbmanager)
        self.retriever.timeout = 0.001  # Be quicker instead of waiting for half a second

        self.initial_time = self.end_time = datetime.datetime.now()
        self.initial_timestamp = self.end_timestamp = time.time()

        request_info = {
            'username': '******',
            'role': 'student',
            'permission_scope': 'user',
            'permission_id': student1.id
        }
        exp_id = ExperimentId('ud-dummy', 'Dummy Experiments')

        self.entry1 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION1, exp_id, coord_addr('ser:inst@mach'), DATA1,
            self.initial_time, self.end_time, request_info.copy(),
            DATA_REQUEST1)
        self.entry2 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION2, exp_id, coord_addr('ser:inst@mach'), DATA2,
            self.initial_time, self.end_time, request_info.copy(),
            DATA_REQUEST2)
        self.entry3 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION3, exp_id, coord_addr('ser:inst@mach'), DATA3,
            self.initial_time, self.end_time, request_info.copy(),
            DATA_REQUEST3)
        self.entry4 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION4, exp_id, coord_addr('ser:inst@mach'), DATA4,
            self.initial_time, self.end_time, request_info.copy(),
            DATA_REQUEST3)
    def setUp(self):

        cfg_manager = ConfigurationManager.ConfigurationManager()
        cfg_manager.append_module(configuration)
        self.dbmanager = DatabaseGateway(cfg_manager)
        self.dbmanager._delete_all_uses()

        self.initial_store  = TemporalInformationStore.InitialTemporalInformationStore()
        self.finished_store = TemporalInformationStore.FinishTemporalInformationStore()
        self.commands_store = TemporalInformationStore.CommandsTemporalInformationStore()
        self.completed_store = TemporalInformationStore.CompletedInformationStore()

        self.retriever = TemporalInformationRetriever.TemporalInformationRetriever(cfg_manager, self.initial_store, self.finished_store, self.commands_store, self.completed_store, self.dbmanager)
        self.retriever.timeout = 0.001 # Be quicker instead of waiting for half a second

        self.initial_time = self.end_time = datetime.datetime.now()
        self.initial_timestamp = self.end_timestamp = time.time()

        request_info = {'username':'******','role':'student'}
        exp_id = ExperimentId('ud-dummy','Dummy Experiments')

        self.entry1 = TemporalInformationStore.InitialInformationEntry(
                        RESERVATION1, exp_id, coord_addr('ser:inst@mach'),
                        DATA1, self.initial_time, self.end_time, request_info.copy(), DATA_REQUEST1)
        self.entry2 = TemporalInformationStore.InitialInformationEntry(
                        RESERVATION2, exp_id, coord_addr('ser:inst@mach'),
                        DATA2, self.initial_time, self.end_time, request_info.copy(), DATA_REQUEST2)
        self.entry3 = TemporalInformationStore.InitialInformationEntry(
                        RESERVATION3, exp_id, coord_addr('ser:inst@mach'),
                        DATA3, self.initial_time, self.end_time, request_info.copy(), DATA_REQUEST3)
        self.entry4 = TemporalInformationStore.InitialInformationEntry(
                        RESERVATION4, exp_id, coord_addr('ser:inst@mach'),
                        DATA4, self.initial_time, self.end_time, request_info.copy(), DATA_REQUEST3)
Exemple #4
0
    def on_dir(directory, configuration_files, configuration_values):
        parser = argparse.ArgumentParser(usage='%(prog)s locations DIR [options]')
        parser.add_argument('--redownload', action='store_true', help='Force redownload of databases')
        parser.add_argument('--reset-database', action='store_true', help='Reset the database, forcing the server to download all the data again')
        parser.add_argument('--reset-cache', action='store_true', help='Reset the database, forcing the server to download all the data again')
        args = parser.parse_args(sys.argv[3:])

        config = ConfigurationManager.create(directory, configuration_files, configuration_values)

        for filename in 'GeoLite2-Country.mmdb', 'GeoLite2-City.mmdb':
            if os.path.exists(filename):
                print("Found %s" % filename)
                if args.redownload:
                    print("Redownloading...")
            else:
                print("%s not found. Downloading..." % filename)

            if args.redownload or not os.path.exists(filename):
                r = requests.get("http://geolite.maxmind.com/download/geoip/database/%s.gz" % filename)
                open('%s.gz' % filename,'wb').write(r.content)
                uncompressed = gzip.open('%s.gz' % filename).read()
                open(filename, 'wb').write(uncompressed)
                print("Downloaded")

        db = DatabaseGateway(config)

        if args.reset_database:
            print("Resetting database")
            db.reset_locations_database()

        if args.reset_cache:
            print("Resetting cache")
            db.reset_locations_cache()
    def setUp(self):

        cfg_manager = ConfigurationManager.ConfigurationManager()
        cfg_manager.append_module(configuration)
        self.dbmanager = DatabaseGateway(cfg_manager)
        self.dbmanager._delete_all_uses()
        session = self.dbmanager.Session()
        try:
            student1 = self.dbmanager._get_user(session, "student1")
        finally:
            session.close()

        self.initial_store = TemporalInformationStore.InitialTemporalInformationStore()
        self.finished_store = TemporalInformationStore.FinishTemporalInformationStore()
        self.commands_store = TemporalInformationStore.CommandsTemporalInformationStore()
        self.completed_store = TemporalInformationStore.CompletedInformationStore()

        self.retriever = TemporalInformationRetriever.TemporalInformationRetriever(
            cfg_manager,
            self.initial_store,
            self.finished_store,
            self.commands_store,
            self.completed_store,
            self.dbmanager,
        )
        self.retriever.timeout = 0.001  # Be quicker instead of waiting for half a second

        self.initial_time = self.end_time = datetime.datetime.now()
        self.initial_timestamp = self.end_timestamp = time.time()

        request_info = {
            "username": "******",
            "role": "student",
            "permission_scope": "user",
            "permission_id": student1.id,
        }
        exp_id = ExperimentId("ud-dummy", "Dummy Experiments")

        self.entry1 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION1,
            exp_id,
            coord_addr("ser:inst@mach"),
            DATA1,
            self.initial_time,
            self.end_time,
            request_info.copy(),
            DATA_REQUEST1,
        )
        self.entry2 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION2,
            exp_id,
            coord_addr("ser:inst@mach"),
            DATA2,
            self.initial_time,
            self.end_time,
            request_info.copy(),
            DATA_REQUEST2,
        )
        self.entry3 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION3,
            exp_id,
            coord_addr("ser:inst@mach"),
            DATA3,
            self.initial_time,
            self.end_time,
            request_info.copy(),
            DATA_REQUEST3,
        )
        self.entry4 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION4,
            exp_id,
            coord_addr("ser:inst@mach"),
            DATA4,
            self.initial_time,
            self.end_time,
            request_info.copy(),
            DATA_REQUEST3,
        )
class TemporalInformationRetrieverTestCase(unittest.TestCase):
    def setUp(self):

        cfg_manager = ConfigurationManager.ConfigurationManager()
        cfg_manager.append_module(configuration)
        self.dbmanager = DatabaseGateway(cfg_manager)
        self.dbmanager._delete_all_uses()
        session = self.dbmanager.Session()
        try:
            student1 = self.dbmanager._get_user(session, "student1")
        finally:
            session.close()

        self.initial_store = TemporalInformationStore.InitialTemporalInformationStore()
        self.finished_store = TemporalInformationStore.FinishTemporalInformationStore()
        self.commands_store = TemporalInformationStore.CommandsTemporalInformationStore()
        self.completed_store = TemporalInformationStore.CompletedInformationStore()

        self.retriever = TemporalInformationRetriever.TemporalInformationRetriever(
            cfg_manager,
            self.initial_store,
            self.finished_store,
            self.commands_store,
            self.completed_store,
            self.dbmanager,
        )
        self.retriever.timeout = 0.001  # Be quicker instead of waiting for half a second

        self.initial_time = self.end_time = datetime.datetime.now()
        self.initial_timestamp = self.end_timestamp = time.time()

        request_info = {
            "username": "******",
            "role": "student",
            "permission_scope": "user",
            "permission_id": student1.id,
        }
        exp_id = ExperimentId("ud-dummy", "Dummy Experiments")

        self.entry1 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION1,
            exp_id,
            coord_addr("ser:inst@mach"),
            DATA1,
            self.initial_time,
            self.end_time,
            request_info.copy(),
            DATA_REQUEST1,
        )
        self.entry2 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION2,
            exp_id,
            coord_addr("ser:inst@mach"),
            DATA2,
            self.initial_time,
            self.end_time,
            request_info.copy(),
            DATA_REQUEST2,
        )
        self.entry3 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION3,
            exp_id,
            coord_addr("ser:inst@mach"),
            DATA3,
            self.initial_time,
            self.end_time,
            request_info.copy(),
            DATA_REQUEST3,
        )
        self.entry4 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION4,
            exp_id,
            coord_addr("ser:inst@mach"),
            DATA4,
            self.initial_time,
            self.end_time,
            request_info.copy(),
            DATA_REQUEST3,
        )

    def test_initial_finish(self):
        self.retriever.start()
        try:
            usages = self.dbmanager.list_usages_per_user("student1")
            self.assertEquals(0, len(usages))

            self.initial_store.put(self.entry1)
            self.initial_store.put(self.entry2)
            self.initial_store.put(self.entry3)
            self.finished_store.put(RESERVATION4, DATA4, self.initial_time, self.end_time)

            # Wait and then populate the RESERVATION3 (the last one in the queue)
            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user("student1")
            # There are 3, and RESERVATION4 is waiting
            self.assertEquals(3, len(usages))

            # Check that it has been stored
            full_usage1 = self.dbmanager.retrieve_usage(usages[0].experiment_use_id)

            self.assertEquals("@@@initial::request@@@", full_usage1.commands[-2].command.commandstring)
            self.assertEquals(DATA_REQUEST1, full_usage1.commands[-2].response.commandstring)
            self.assertEquals("@@@initial::response@@@", full_usage1.commands[-1].command.commandstring)
            self.assertEquals(DATA1, full_usage1.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage1.end_date)

            full_usage2 = self.dbmanager.retrieve_usage(usages[1].experiment_use_id)
            self.assertEquals(DATA2, full_usage2.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage2.end_date)
            full_usage3 = self.dbmanager.retrieve_usage(usages[2].experiment_use_id)
            self.assertEquals(DATA3, full_usage3.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage3.end_date)

            wait_for(self.retriever)

            self.initial_store.put(self.entry4)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user("student1")
            # RESERVATION4 achieved
            self.assertEquals(4, len(usages))

            # And end_date is filled for 4
            full_usage4 = self.dbmanager.retrieve_usage(usages[3].experiment_use_id)
            self.assertEquals(DATA4, full_usage4.commands[-1].response.commandstring)
            self.assertNotEqual(None, full_usage4.end_date)

            # While in the rest it's not yet filled

            full_usage1 = self.dbmanager.retrieve_usage(usages[0].experiment_use_id)
            self.assertEquals(DATA1, full_usage1.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage1.end_date)

            full_usage2 = self.dbmanager.retrieve_usage(usages[1].experiment_use_id)
            self.assertEquals(DATA2, full_usage2.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage2.end_date)

            full_usage3 = self.dbmanager.retrieve_usage(usages[2].experiment_use_id)
            self.assertEquals(DATA3, full_usage3.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage3.end_date)

            # But if we add to the finish store, and we wait:
            self.finished_store.put(RESERVATION1, DATA1, self.initial_time, self.end_time)

            wait_for(self.retriever)

            # Then it is filled
            full_usage1 = self.dbmanager.retrieve_usage(usages[0].experiment_use_id)
            self.assertEquals(DATA1, full_usage1.commands[-1].response.commandstring)
            self.assertNotEqual(None, full_usage1.end_date)

        finally:
            self.retriever.stop()
            self.retriever.join(1)
            self.assertFalse(self.retriever.isAlive())

    def test_commands(self):
        self.retriever.start()
        try:
            usages = self.dbmanager.list_usages_per_user("student1")
            self.assertEquals(0, len(usages))

            self.initial_store.put(self.entry1)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user("student1")

            self.assertEquals(1, len(usages))

            entry_id1 = 58131
            entry_id2 = 14214
            entry_id3 = 84123

            pre_command1 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION1, True, True, entry_id1, Command(DATA_REQUEST1), self.initial_timestamp
            )
            post_command1 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION1, False, True, entry_id1, Command(DATA1), self.initial_timestamp
            )

            pre_command2 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION2, True, True, entry_id2, Command(DATA_REQUEST2), self.initial_timestamp
            )
            post_command2 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION2, False, True, entry_id2, Command(DATA2), self.initial_timestamp
            )

            pre_command3 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION3, True, True, entry_id3, Command(DATA_REQUEST3), self.initial_timestamp
            )
            post_command3 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION3, False, True, entry_id3, Command(DATA3), self.initial_timestamp
            )

            # The reservation is stored, therefore this command will
            # also be stored
            self.commands_store.put(pre_command1)

            # This reservation has not been stored, therefore this command
            # will not be stored yet
            self.commands_store.put(pre_command2)

            # Neither this reservation or the pre_command3 have been stored,
            # therefore this command will not be stored
            self.commands_store.put(post_command3)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user("student1")
            self.assertEquals(1, len(usages))

            full_usage1 = self.dbmanager.retrieve_usage(usages[0].experiment_use_id)
            self.assertEquals(DATA_REQUEST1, full_usage1.commands[-1].command.commandstring)
            self.assertEquals(None, full_usage1.commands[-1].response.commandstring)

            # So we add the post_command1, to avoid the "None"
            self.commands_store.put(post_command1)
            # And the pre_command3, to see if it is correctly enqueued
            self.commands_store.put(pre_command3)
            # And the entry 2, to let pre_command2 enter
            self.initial_store.put(self.entry2)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user("student1")
            self.assertEquals(2, len(usages))

            full_usage1 = self.dbmanager.retrieve_usage(usages[0].experiment_use_id)
            self.assertEquals(DATA_REQUEST1, full_usage1.commands[-1].command.commandstring)
            self.assertEquals(DATA1, full_usage1.commands[-1].response.commandstring)

            full_usage2 = self.dbmanager.retrieve_usage(usages[1].experiment_use_id)
            self.assertEquals(DATA_REQUEST2, full_usage2.commands[-1].command.commandstring)
            self.assertEquals(None, full_usage2.commands[-1].response.commandstring)

            # So now we add the rest

            self.commands_store.put(post_command2)
            self.initial_store.put(self.entry3)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user("student1")
            self.assertEquals(3, len(usages))

            full_usage1 = self.dbmanager.retrieve_usage(usages[0].experiment_use_id)
            self.assertEquals(DATA_REQUEST1, full_usage1.commands[-1].command.commandstring)
            self.assertEquals(DATA1, full_usage1.commands[-1].response.commandstring)

            full_usage2 = self.dbmanager.retrieve_usage(usages[1].experiment_use_id)
            self.assertEquals(DATA_REQUEST2, full_usage2.commands[-1].command.commandstring)
            self.assertEquals(DATA2, full_usage2.commands[-1].response.commandstring)

            full_usage3 = self.dbmanager.retrieve_usage(usages[2].experiment_use_id)
            self.assertEquals(DATA_REQUEST3, full_usage3.commands[-1].command.commandstring)
            self.assertEquals(DATA3, full_usage3.commands[-1].response.commandstring)

        finally:
            self.retriever.stop()
            self.retriever.join(1)
            self.assertFalse(self.retriever.isAlive())
class TemporalInformationRetrieverTestCase(unittest.TestCase):
    def setUp(self):

        cfg_manager = ConfigurationManager.ConfigurationManager()
        cfg_manager.append_module(configuration)
        self.dbmanager = DatabaseGateway(cfg_manager)
        self.dbmanager._delete_all_uses()
        session = self.dbmanager.Session()
        try:
            student1 = self.dbmanager._get_user(session, 'student1')
        finally:
            session.close()

        self.initial_store = TemporalInformationStore.InitialTemporalInformationStore(
        )
        self.finished_store = TemporalInformationStore.FinishTemporalInformationStore(
        )
        self.commands_store = TemporalInformationStore.CommandsTemporalInformationStore(
        )
        self.completed_store = TemporalInformationStore.CompletedInformationStore(
        )

        self.retriever = TemporalInformationRetriever.TemporalInformationRetriever(
            cfg_manager, self.initial_store, self.finished_store,
            self.commands_store, self.completed_store, self.dbmanager)
        self.retriever.timeout = 0.001  # Be quicker instead of waiting for half a second

        self.initial_time = self.end_time = datetime.datetime.now()
        self.initial_timestamp = self.end_timestamp = time.time()

        request_info = {
            'username': '******',
            'role': 'student',
            'permission_scope': 'user',
            'permission_id': student1.id
        }
        exp_id = ExperimentId('ud-dummy', 'Dummy Experiments')

        self.entry1 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION1, exp_id, coord_addr('ser:inst@mach'), DATA1,
            self.initial_time, self.end_time, request_info.copy(),
            DATA_REQUEST1)
        self.entry2 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION2, exp_id, coord_addr('ser:inst@mach'), DATA2,
            self.initial_time, self.end_time, request_info.copy(),
            DATA_REQUEST2)
        self.entry3 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION3, exp_id, coord_addr('ser:inst@mach'), DATA3,
            self.initial_time, self.end_time, request_info.copy(),
            DATA_REQUEST3)
        self.entry4 = TemporalInformationStore.InitialInformationEntry(
            RESERVATION4, exp_id, coord_addr('ser:inst@mach'), DATA4,
            self.initial_time, self.end_time, request_info.copy(),
            DATA_REQUEST3)

    def test_initial_finish(self):
        self.retriever.start()
        try:
            usages = self.dbmanager.list_usages_per_user('student1')
            self.assertEquals(0, len(usages))

            self.initial_store.put(self.entry1)
            self.initial_store.put(self.entry2)
            self.initial_store.put(self.entry3)
            self.finished_store.put(RESERVATION4, DATA4, self.initial_time,
                                    self.end_time)

            # Wait and then populate the RESERVATION3 (the last one in the queue)
            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user('student1')
            # There are 3, and RESERVATION4 is waiting
            self.assertEquals(3, len(usages))

            # Check that it has been stored
            full_usage1 = self.dbmanager.retrieve_usage(
                usages[0].experiment_use_id)

            self.assertEquals("@@@initial::request@@@",
                              full_usage1.commands[-2].command.commandstring)
            self.assertEquals(DATA_REQUEST1,
                              full_usage1.commands[-2].response.commandstring)
            self.assertEquals("@@@initial::response@@@",
                              full_usage1.commands[-1].command.commandstring)
            self.assertEquals(DATA1,
                              full_usage1.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage1.end_date)

            full_usage2 = self.dbmanager.retrieve_usage(
                usages[1].experiment_use_id)
            self.assertEquals(DATA2,
                              full_usage2.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage2.end_date)
            full_usage3 = self.dbmanager.retrieve_usage(
                usages[2].experiment_use_id)
            self.assertEquals(DATA3,
                              full_usage3.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage3.end_date)

            wait_for(self.retriever)

            self.initial_store.put(self.entry4)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user('student1')
            # RESERVATION4 achieved
            self.assertEquals(4, len(usages))

            # And end_date is filled for 4
            full_usage4 = self.dbmanager.retrieve_usage(
                usages[3].experiment_use_id)
            self.assertEquals(DATA4,
                              full_usage4.commands[-1].response.commandstring)
            self.assertNotEqual(None, full_usage4.end_date)

            # While in the rest it's not yet filled

            full_usage1 = self.dbmanager.retrieve_usage(
                usages[0].experiment_use_id)
            self.assertEquals(DATA1,
                              full_usage1.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage1.end_date)

            full_usage2 = self.dbmanager.retrieve_usage(
                usages[1].experiment_use_id)
            self.assertEquals(DATA2,
                              full_usage2.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage2.end_date)

            full_usage3 = self.dbmanager.retrieve_usage(
                usages[2].experiment_use_id)
            self.assertEquals(DATA3,
                              full_usage3.commands[-1].response.commandstring)
            self.assertEquals(None, full_usage3.end_date)

            # But if we add to the finish store, and we wait:
            self.finished_store.put(RESERVATION1, DATA1, self.initial_time,
                                    self.end_time)

            wait_for(self.retriever)

            # Then it is filled
            full_usage1 = self.dbmanager.retrieve_usage(
                usages[0].experiment_use_id)
            self.assertEquals(DATA1,
                              full_usage1.commands[-1].response.commandstring)
            self.assertNotEqual(None, full_usage1.end_date)

        finally:
            self.retriever.stop()
            self.retriever.join(1)
            self.assertFalse(self.retriever.isAlive())

    def test_commands(self):
        self.retriever.start()
        try:
            usages = self.dbmanager.list_usages_per_user('student1')
            self.assertEquals(0, len(usages))

            self.initial_store.put(self.entry1)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user('student1')

            self.assertEquals(1, len(usages))

            entry_id1 = 58131
            entry_id2 = 14214
            entry_id3 = 84123

            pre_command1 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION1, True, True, entry_id1, Command(DATA_REQUEST1),
                self.initial_timestamp)
            post_command1 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION1, False, True, entry_id1, Command(DATA1),
                self.initial_timestamp)

            pre_command2 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION2, True, True, entry_id2, Command(DATA_REQUEST2),
                self.initial_timestamp)
            post_command2 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION2, False, True, entry_id2, Command(DATA2),
                self.initial_timestamp)

            pre_command3 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION3, True, True, entry_id3, Command(DATA_REQUEST3),
                self.initial_timestamp)
            post_command3 = TemporalInformationStore.CommandOrFileInformationEntry(
                RESERVATION3, False, True, entry_id3, Command(DATA3),
                self.initial_timestamp)

            # The reservation is stored, therefore this command will
            # also be stored
            self.commands_store.put(pre_command1)

            # This reservation has not been stored, therefore this command
            # will not be stored yet
            self.commands_store.put(pre_command2)

            # Neither this reservation or the pre_command3 have been stored,
            # therefore this command will not be stored
            self.commands_store.put(post_command3)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user('student1')
            self.assertEquals(1, len(usages))

            full_usage1 = self.dbmanager.retrieve_usage(
                usages[0].experiment_use_id)
            self.assertEquals(DATA_REQUEST1,
                              full_usage1.commands[-1].command.commandstring)
            self.assertEquals(None,
                              full_usage1.commands[-1].response.commandstring)

            # So we add the post_command1, to avoid the "None"
            self.commands_store.put(post_command1)
            # And the pre_command3, to see if it is correctly enqueued
            self.commands_store.put(pre_command3)
            # And the entry 2, to let pre_command2 enter
            self.initial_store.put(self.entry2)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user('student1')
            self.assertEquals(2, len(usages))

            full_usage1 = self.dbmanager.retrieve_usage(
                usages[0].experiment_use_id)
            self.assertEquals(DATA_REQUEST1,
                              full_usage1.commands[-1].command.commandstring)
            self.assertEquals(DATA1,
                              full_usage1.commands[-1].response.commandstring)

            full_usage2 = self.dbmanager.retrieve_usage(
                usages[1].experiment_use_id)
            self.assertEquals(DATA_REQUEST2,
                              full_usage2.commands[-1].command.commandstring)
            self.assertEquals(None,
                              full_usage2.commands[-1].response.commandstring)

            # So now we add the rest

            self.commands_store.put(post_command2)
            self.initial_store.put(self.entry3)

            wait_for(self.retriever)

            usages = self.dbmanager.list_usages_per_user('student1')
            self.assertEquals(3, len(usages))

            full_usage1 = self.dbmanager.retrieve_usage(
                usages[0].experiment_use_id)
            self.assertEquals(DATA_REQUEST1,
                              full_usage1.commands[-1].command.commandstring)
            self.assertEquals(DATA1,
                              full_usage1.commands[-1].response.commandstring)

            full_usage2 = self.dbmanager.retrieve_usage(
                usages[1].experiment_use_id)
            self.assertEquals(DATA_REQUEST2,
                              full_usage2.commands[-1].command.commandstring)
            self.assertEquals(DATA2,
                              full_usage2.commands[-1].response.commandstring)

            full_usage3 = self.dbmanager.retrieve_usage(
                usages[2].experiment_use_id)
            self.assertEquals(DATA_REQUEST3,
                              full_usage3.commands[-1].command.commandstring)
            self.assertEquals(DATA3,
                              full_usage3.commands[-1].response.commandstring)

        finally:
            self.retriever.stop()
            self.retriever.join(1)
            self.assertFalse(self.retriever.isAlive())
Exemple #8
0
    def __init__(self, coord_address, locator, cfg_manager, dont_start = False, *args, **kwargs):
        super(UserProcessingServer,self).__init__(*args, **kwargs)

        log.log( UserProcessingServer, log.level.Info, "Starting Core Server...")

        self._stopping = False
        self._cfg_manager    = cfg_manager
        self.config          = cfg_manager
        self._locator        = locator

        self.core_server_url = cfg_manager.get_doc_value(configuration_doc.CORE_SERVER_URL)

        if cfg_manager.get_value(configuration_doc.CORE_UNIVERSAL_IDENTIFIER, 'default') == 'default' or cfg_manager.get_value(configuration_doc.CORE_UNIVERSAL_IDENTIFIER_HUMAN, 'default') == 'default':
            generated = uuid.uuid1()
            msg = "Property %(property)s or %(property_human)s not configured. Please establish: %(property)s = '%(uuid)s' and %(property_human)s = 'server at university X'. Otherwise, when federating the experiment it could enter in an endless loop." % {
                'property'       : configuration_doc.CORE_UNIVERSAL_IDENTIFIER,
                'property_human' : configuration_doc.CORE_UNIVERSAL_IDENTIFIER_HUMAN,
                'uuid'           : generated
            }
            print(msg)
            print(msg, file = sys.stderr)
            log.log( UserProcessingServer, log.level.Error, msg)

        self.core_server_universal_id       = cfg_manager.get_doc_value(configuration_doc.CORE_UNIVERSAL_IDENTIFIER)
        self.core_server_universal_id_human = cfg_manager.get_doc_value(configuration_doc.CORE_UNIVERSAL_IDENTIFIER_HUMAN)

        #
        # Create session managers
        #

        session_type_str    = cfg_manager.get_doc_value(configuration_doc.WEBLAB_CORE_SERVER_SESSION_TYPE)
        if not hasattr(SessionType, session_type_str):
            raise coreExc.NotASessionTypeError( 'Not a session type: %s' % session_type_str )
        session_type = getattr(SessionType, session_type_str)

        session_pool_id       = cfg_manager.get_doc_value(configuration_doc.WEBLAB_CORE_SERVER_SESSION_POOL_ID)
        self._session_manager = SessionManager.SessionManager( cfg_manager, session_type, session_pool_id )

        reservations_session_pool_id = cfg_manager.get_value(WEBLAB_CORE_SERVER_RESERVATIONS_SESSION_POOL_ID, "CoreServerReservations")
        self._reservations_session_manager = SessionManager.SessionManager( cfg_manager, session_type, reservations_session_pool_id )

        #
        # Coordination
        #

        coordinator_implementation = cfg_manager.get_doc_value(configuration_doc.COORDINATOR_IMPL)
        self._coordinator    = coordinator_create(coordinator_implementation, self._locator, cfg_manager)

        #
        # Database and information storage managers
        #

        self._db_manager     = DatabaseGateway(cfg_manager)
        self.db = self._db_manager

        cfg_manager.client = DbConfig(self.db.client_configuration)
        cfg_manager.server = DbConfig(self.db.server_configuration)

        self._commands_store = TemporalInformationStore.CommandsTemporalInformationStore()

        self._temporal_information_retriever = TemporalInformationRetriever.TemporalInformationRetriever(cfg_manager, self._coordinator.initial_store, self._coordinator.finished_store, self._commands_store, self._coordinator.completed_store, self._db_manager)
        self._temporal_information_retriever.start()

        clean = cfg_manager.get('core_number') == 0
        if clean:
            self._location_retriever = LocationRetriever(cfg_manager, self._db_manager)
            self._location_retriever.start()
        else:
            self._location_retriever = None
        #
        # Alive users
        #

        self._alive_users_collection = AliveUsersCollection.AliveUsersCollection(
                self._locator, self._cfg_manager, session_type, self._reservations_session_manager, self._coordinator, self._commands_store, self._coordinator.finished_reservations_store)

        # Login Manager
        self._login_manager  = LoginManager(self._db_manager, self)

        #
        # Initialize facade (comm) servers
        #

        self._server_route   = cfg_manager.get_doc_value(configuration_doc.CORE_FACADE_SERVER_ROUTE)

        self.flask_server = WebLabFlaskServer(self, cfg_manager)
        self.babel = self.flask_server.babel

        self.dont_start = cfg_manager.get_value('dont_start', dont_start)
        if not self.dont_start:
            self.flask_server.start()

        self.app = self.flask_server.app

        #
        # Start checking times
        #
        checking_time = self._cfg_manager.get_value(CHECKING_TIME_NAME, DEFAULT_CHECKING_TIME)
        timer = threading.Timer(checking_time, self._renew_checker_timer)
        timer.setName(counter.next_name("ups-checker-timer"))
        timer.setDaemon(True)
        timer.start()
        _resource_manager.add_resource(timer)