<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005 onwards University of Deusto
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# This software consists of contributions made by many individuals,
# listed below:
#
# Author: Pablo Orduña <pablo@ordunya.com>
#

import sys
import time
import unittest

import voodoo.gen.loader.ServerLoader as ServerLoader
from voodoo.gen.registry.server_registry import _registry

from weblab.data.command import Command
from weblab.data.experiments import ExperimentId, WaitingReservationResult, RunningReservationResult
from weblab.core.coordinator.clients.weblabdeusto import WebLabDeustoClient
from weblab.core.reservations import Reservation

DEBUG = False

def debug(msg):
    if DEBUG:
        print
        print "DEBUG:",msg
        print
        sys.stdout.flush()

class AbstractFederatedWebLabDeustoTestCase(object):
    def setUp(self):

        # Clean the global registry of servers
        _registry.clear()

        CONSUMER_CONFIG_PATH  = self.FEDERATED_DEPLOYMENTS + '/consumer/'
        PROVIDER1_CONFIG_PATH = self.FEDERATED_DEPLOYMENTS + '/provider1/'
        PROVIDER2_CONFIG_PATH = self.FEDERATED_DEPLOYMENTS + '/provider2/'

        self.server_loader     = ServerLoader.ServerLoader()

        self.consumer_handler  = self.server_loader.load_instance( CONSUMER_CONFIG_PATH,   'consumer_machine', 'main_instance' )
        self.provider1_handler = self.server_loader.load_instance( PROVIDER1_CONFIG_PATH,  'provider1_machine', 'main_instance' )
        self.provider2_handler = self.server_loader.load_instance( PROVIDER2_CONFIG_PATH,  'provider2_machine', 'main_instance' )

        self.consumer_login_client = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 18645 )
        self.consumer_core_client  = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 18345 )

        self.provider1_login_client = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 28645 )
        self.provider1_core_client  = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 28345 )

        self.provider2_login_client = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 38645 )
        self.provider2_core_client  = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 38345 )

        # dummy1: deployed in consumer, provider1, provider2
        self.dummy1 = ExperimentId("dummy1", "Dummy experiments")
        # dummy2: deployed in consumer
        self.dummy2 = ExperimentId("dummy2", "Dummy experiments")
        # dummy3: deployed in provider1 as "dummy3_with_other_name"
        self.dummy3 = ExperimentId("dummy3", "Dummy experiments")
        # dummy4: deployed in provider2
        self.dummy4 = ExperimentId("dummy4", "Dummy experiments")

    def tearDown(self):
        self.consumer_handler.stop()
        self.provider1_handler.stop()
        self.provider2_handler.stop()

    #
    # This test may take even 20-30 seconds; therefore it is not splitted
    # into subtests (the setup and teardown are long)
    #
    def test_federated_experiment(self):
        debug("Test test_federated_experiment starts")

        #######################################################
        #
        #   Local testing  (to check that everything is right)
        #
        #   We enter as a student of Consumer, and we ask for an
        #   experiment that only the Consumer university has
        #   (dummy2).
        #
        session_id = self.consumer_login_client.login('fedstudent1', 'password')

        reservation_id = self._test_reservation(session_id, self.dummy2, 'Consumer', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_core_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Consumer')

        #######################################################
        #
        #   Simple federation
        #
        #   Now we ask for an experiment that only Provider 1
        #   has. There is no load balance, neither
        #   subcontracting
        #
        reservation_id = self._test_reservation(session_id, self.dummy3, 'Provider 1', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_core_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Provider 1')

        #######################################################
        #
        #   Subcontracted federation
        #
        #   Now we ask for an experiment that only Provider 2
        #   has. There is no load balance, but Consumer will
        #   contact Provider 1, which will contact Provider 2
        #
        reservation_id = self._test_reservation(session_id, self.dummy4, 'Provider 2', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_core_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Provider 2')

        #######################################################
        #
        #   Cross-domain load balancing
        #
        #   Now we ask for an experiment that Consumer has,
        #   but also Provider 1 and Provider 2.
        #

        reservation_id1 = self._test_reservation(session_id, self.dummy1, 'Consumer',  True, False,  user_agent = 'Chrome')
        reservation_id2 = self._test_reservation(session_id, self.dummy1, 'Provider 1', True, False, user_agent = 'Firefox')
        reservation_id3 = self._test_reservation(session_id, self.dummy1, 'Provider 2', True, False, user_agent = 'Safari')

        reservation_ids = (reservation_id1, reservation_id2, reservation_id3)
        reservation_results = self.consumer_core_client.get_experiment_uses_by_id(session_id, reservation_ids)

        self.assertEquals(RunningReservationResult(), reservation_results[0])
        self.assertEquals(RunningReservationResult(), reservation_results[1])
        self.assertEquals(RunningReservationResult(), reservation_results[2])

        #
        # What if one of them goes out and another comes? Is the load of experiments balanced correctly?
        #
        self.consumer_core_client.finished_experiment(reservation_id2)

        # Wait a couple of seconds to check that it has been propagated
        self._wait_multiple_reservations(20, session_id, reservation_ids, [1])

        reservation_results = self.consumer_core_client.get_experiment_uses_by_id(session_id, reservation_ids)

        # The other two are still running
        self.assertEquals(RunningReservationResult(), reservation_results[0])
        self.assertEquals(RunningReservationResult(), reservation_results[2])

        # But the one finished is actually finished
        self.assertTrue( reservation_results[1].is_finished() )
        self.assertEquals('Firefox', reservation_results[1].experiment_use.request_info['user_agent'])
        self.assertEquals(4, len(reservation_results[1].experiment_use.commands))
        self.assertEquals('Provider 1', reservation_results[1].experiment_use.commands[2].response.commandstring)

        reservation_id2b = self._test_reservation(session_id, self.dummy1, 'Provider 1', True, False)

        self.consumer_core_client.finished_experiment(reservation_id1)
        self._test_reservation(session_id, self.dummy1, 'Consumer', True, False)
        reservation_status = self.consumer_core_client.get_reservation_status(reservation_id3)
        provider2_reservation_id = reservation_status.remote_reservation_id

        self.consumer_core_client.finished_experiment(reservation_id3)
        self._test_reservation(session_id, self.dummy1, 'Provider 2', True, False)

        # Check for the other uses
        self._wait_multiple_reservations(70, session_id, reservation_ids, [0,2])

        reservation_results = self.consumer_core_client.get_experiment_uses_by_id(session_id, reservation_ids)
        self.assertTrue( reservation_results[0].is_finished() )
        self.assertEquals('Chrome', reservation_results[0].experiment_use.request_info['user_agent'])
        self.assertEquals('Consumer', reservation_results[0].experiment_use.commands[2].response.commandstring)

        self.assertTrue( reservation_results[2].is_finished() )
        self.assertEquals('Safari', reservation_results[2].experiment_use.request_info['user_agent'])
        self.assertEquals('Provider 2', reservation_results[2].experiment_use.commands[2].response.commandstring)

        provider2_session_id = self.provider2_login_client.login('provider1', 'password')
        provider2_result = self.provider2_core_client.get_experiment_use_by_id(provider2_session_id, provider2_reservation_id)
        self.assertTrue(provider2_result.is_finished())
        self.assertEquals('Safari', provider2_result.experiment_use.request_info['user_agent'])

        #
        # What if another 2 come in? What is the position of their queues?
        #

        reservation_4 = self._test_reservation(session_id, self.dummy1, '', False, False)
        reservation_status = self.consumer_core_client.get_reservation_status(reservation_4)
        self.assertEquals(Reservation.WAITING, reservation_status.status)
        self.assertEquals(0, reservation_status.position)

        reservation_5 = self._test_reservation(session_id, self.dummy1, '', False, False)
        reservation_status = self.consumer_core_client.get_reservation_status(reservation_5)
        self.assertEquals(Reservation.WAITING, reservation_status.status)
        self.assertEquals(1, reservation_status.position)

        #
        # Once again, freeing a session affects them?
        #
        self.consumer_core_client.finished_experiment(reservation_id2b)
        self._wait_reservation(reservation_4, 'Provider 1', True)

        self.consumer_core_client.finished_experiment(reservation_4)
        self._wait_reservation(reservation_5, 'Provider 1', True)

        # Check for the other uses
        for _ in range(50):
            time.sleep(0.5)
            # Checking every half second
            results = self.consumer_core_client.get_experiment_uses_by_id(session_id, (reservation_id2b, reservation_4))
            if results[0].is_finished() and results[1].is_finished():
                break

        final_reservation_results = self.consumer_core_client.get_experiment_uses_by_id(session_id, (reservation_id2b, reservation_4))
        self.assertTrue(final_reservation_results[0].is_finished())
        self.assertTrue(final_reservation_results[1].is_finished())
        self.assertEquals('Provider 1', final_reservation_results[0].experiment_use.commands[2].response.commandstring)
        self.assertEquals('Provider 1', final_reservation_results[1].experiment_use.commands[2].response.commandstring)

        debug("Test test_federated_experiment finishes successfully")

    def _wait_multiple_reservations(self, times, session_id, reservation_ids, reservations_to_wait):
        for _ in range(times):
            time.sleep(0.5)
            # Checking every half second
            results = self.consumer_core_client.get_experiment_uses_by_id(session_id, reservation_ids)
            all_finished = True

            for reservation_to_wait in reservations_to_wait:
                all_finished = all_finished and results[reservation_to_wait].is_finished()

            if all_finished:
                break

    def _find_command(self, reservation_result, expected_response):
        found = False
        commands = reservation_result.experiment_use.commands
        for command in commands:
            if command.command.commandstring == 'server_info':
                found = True
                response = command.response.commandstring
                self.assertEquals(expected_response, response, "Message %s not found in commands %s; instead found %s" % (expected_response, commands, response))
        self.assertTrue(found, "server_info not found in commands")

    def _test_reservation(self, session_id, experiment_id, expected_server_info, wait, finish, user_agent = None):
        debug("Reserving with session_id %r a experiment %r; will I wait? %s; will I finish? %s" % (session_id, experiment_id, wait, finish))
        reservation_status = self.consumer_core_client.reserve_experiment(session_id, experiment_id, "{}", "{}", user_agent = user_agent)

        reservation_id = reservation_status.reservation_id

        if not wait:
            if finish:
                debug("Finishing... %r" % reservation_id)
                self.consumer_core_client.finished_experiment(reservation_id)
            debug("Not waiting... %r" % reservation_id)
            return reservation_id

        reservation_id = self._wait_reservation(reservation_id, expected_server_info, finish)
        debug("Finished waiting... %r" % reservation_id)
        return reservation_id

    def _wait_reservation(self, reservation_id, expected_server_info, finish):
        max_timeout = 10
        initial_time = time.time()

        reservation_status = self.consumer_core_client.get_reservation_status(reservation_id)
        while reservation_status.status in (Reservation.WAITING, Reservation.WAITING_CONFIRMATION):
            if time.time() - initial_time > max_timeout:
                self.fail("Waiting too long in the queue for %s" % expected_server_info)
            time.sleep(0.1)
            reservation_status = self.consumer_core_client.get_reservation_status(reservation_id)

        self.assertEquals(Reservation.CONFIRMED, reservation_status.status)

        experiment_reservation_id = reservation_status.remote_reservation_id
        if experiment_reservation_id.id == '':
            experiment_reservation_id = reservation_id

        client = WebLabDeustoClient( reservation_status.url )

        response = client.send_command(experiment_reservation_id, Command("server_info"))
        self.assertEquals(expected_server_info, response.get_command_string())

        if finish:
            self.consumer_core_client.finished_experiment(reservation_id)

        return reservation_id

class SqlFederatedWebLabDeustoTestCase(AbstractFederatedWebLabDeustoTestCase, unittest.TestCase):
    FEDERATED_DEPLOYMENTS = 'test/deployments/federated_basic_sql'

try:
    import redis
    assert redis is not None
except ImportError:
    REDIS_AVAILABLE = False
else:
    REDIS_AVAILABLE = True

if REDIS_AVAILABLE:
    class RedisFederatedWebLabDeustoTestCase(AbstractFederatedWebLabDeustoTestCase, unittest.TestCase):
        FEDERATED_DEPLOYMENTS = 'test/deployments/federated_basic_redis'

def suite():
    suites = [unittest.makeSuite(SqlFederatedWebLabDeustoTestCase)]
    if REDIS_AVAILABLE:
        suites.append(unittest.makeSuite(RedisFederatedWebLabDeustoTestCase))
    else:
        print >> sys.stderr, "redis not available. Skipping redis-based federation integration tests"
    return unittest.TestSuite( suites )

if __name__ == '__main__':
    unittest.main()

<commit_msg>Fix pyflakes warning<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005 onwards University of Deusto
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# This software consists of contributions made by many individuals,
# listed below:
#
# Author: Pablo Orduña <pablo@ordunya.com>
#

import sys
import time
import unittest

import voodoo.gen.loader.ServerLoader as ServerLoader
from voodoo.gen.registry.server_registry import _registry

from weblab.data.command import Command
from weblab.data.experiments import ExperimentId, RunningReservationResult
from weblab.core.coordinator.clients.weblabdeusto import WebLabDeustoClient
from weblab.core.reservations import Reservation

DEBUG = False

def debug(msg):
    if DEBUG:
        print
        print "DEBUG:",msg
        print
        sys.stdout.flush()

class AbstractFederatedWebLabDeustoTestCase(object):
    def setUp(self):

        # Clean the global registry of servers
        _registry.clear()

        CONSUMER_CONFIG_PATH  = self.FEDERATED_DEPLOYMENTS + '/consumer/'
        PROVIDER1_CONFIG_PATH = self.FEDERATED_DEPLOYMENTS + '/provider1/'
        PROVIDER2_CONFIG_PATH = self.FEDERATED_DEPLOYMENTS + '/provider2/'

        self.server_loader     = ServerLoader.ServerLoader()

        self.consumer_handler  = self.server_loader.load_instance( CONSUMER_CONFIG_PATH,   'consumer_machine', 'main_instance' )
        self.provider1_handler = self.server_loader.load_instance( PROVIDER1_CONFIG_PATH,  'provider1_machine', 'main_instance' )
        self.provider2_handler = self.server_loader.load_instance( PROVIDER2_CONFIG_PATH,  'provider2_machine', 'main_instance' )

        self.consumer_login_client = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 18645 )
        self.consumer_core_client  = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 18345 )

        self.provider1_login_client = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 28645 )
        self.provider1_core_client  = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 28345 )

        self.provider2_login_client = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 38645 )
        self.provider2_core_client  = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % 38345 )

        # dummy1: deployed in consumer, provider1, provider2
        self.dummy1 = ExperimentId("dummy1", "Dummy experiments")
        # dummy2: deployed in consumer
        self.dummy2 = ExperimentId("dummy2", "Dummy experiments")
        # dummy3: deployed in provider1 as "dummy3_with_other_name"
        self.dummy3 = ExperimentId("dummy3", "Dummy experiments")
        # dummy4: deployed in provider2
        self.dummy4 = ExperimentId("dummy4", "Dummy experiments")

    def tearDown(self):
        self.consumer_handler.stop()
        self.provider1_handler.stop()
        self.provider2_handler.stop()

    #
    # This test may take even 20-30 seconds; therefore it is not splitted
    # into subtests (the setup and teardown are long)
    #
    def test_federated_experiment(self):
        debug("Test test_federated_experiment starts")

        #######################################################
        #
        #   Local testing  (to check that everything is right)
        #
        #   We enter as a student of Consumer, and we ask for an
        #   experiment that only the Consumer university has
        #   (dummy2).
        #
        session_id = self.consumer_login_client.login('fedstudent1', 'password')

        reservation_id = self._test_reservation(session_id, self.dummy2, 'Consumer', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_core_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Consumer')

        #######################################################
        #
        #   Simple federation
        #
        #   Now we ask for an experiment that only Provider 1
        #   has. There is no load balance, neither
        #   subcontracting
        #
        reservation_id = self._test_reservation(session_id, self.dummy3, 'Provider 1', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_core_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Provider 1')

        #######################################################
        #
        #   Subcontracted federation
        #
        #   Now we ask for an experiment that only Provider 2
        #   has. There is no load balance, but Consumer will
        #   contact Provider 1, which will contact Provider 2
        #
        reservation_id = self._test_reservation(session_id, self.dummy4, 'Provider 2', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_core_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Provider 2')

        #######################################################
        #
        #   Cross-domain load balancing
        #
        #   Now we ask for an experiment that Consumer has,
        #   but also Provider 1 and Provider 2.
        #

        reservation_id1 = self._test_reservation(session_id, self.dummy1, 'Consumer',  True, False,  user_agent = 'Chrome')
        reservation_id2 = self._test_reservation(session_id, self.dummy1, 'Provider 1', True, False, user_agent = 'Firefox')
        reservation_id3 = self._test_reservation(session_id, self.dummy1, 'Provider 2', True, False, user_agent = 'Safari')

        reservation_ids = (reservation_id1, reservation_id2, reservation_id3)
        reservation_results = self.consumer_core_client.get_experiment_uses_by_id(session_id, reservation_ids)

        self.assertEquals(RunningReservationResult(), reservation_results[0])
        self.assertEquals(RunningReservationResult(), reservation_results[1])
        self.assertEquals(RunningReservationResult(), reservation_results[2])

        #
        # What if one of them goes out and another comes? Is the load of experiments balanced correctly?
        #
        self.consumer_core_client.finished_experiment(reservation_id2)

        # Wait a couple of seconds to check that it has been propagated
        self._wait_multiple_reservations(20, session_id, reservation_ids, [1])

        reservation_results = self.consumer_core_client.get_experiment_uses_by_id(session_id, reservation_ids)

        # The other two are still running
        self.assertEquals(RunningReservationResult(), reservation_results[0])
        self.assertEquals(RunningReservationResult(), reservation_results[2])

        # But the one finished is actually finished
        self.assertTrue( reservation_results[1].is_finished() )
        self.assertEquals('Firefox', reservation_results[1].experiment_use.request_info['user_agent'])
        self.assertEquals(4, len(reservation_results[1].experiment_use.commands))
        self.assertEquals('Provider 1', reservation_results[1].experiment_use.commands[2].response.commandstring)

        reservation_id2b = self._test_reservation(session_id, self.dummy1, 'Provider 1', True, False)

        self.consumer_core_client.finished_experiment(reservation_id1)
        self._test_reservation(session_id, self.dummy1, 'Consumer', True, False)
        reservation_status = self.consumer_core_client.get_reservation_status(reservation_id3)
        provider2_reservation_id = reservation_status.remote_reservation_id

        self.consumer_core_client.finished_experiment(reservation_id3)
        self._test_reservation(session_id, self.dummy1, 'Provider 2', True, False)

        # Check for the other uses
        self._wait_multiple_reservations(70, session_id, reservation_ids, [0,2])

        reservation_results = self.consumer_core_client.get_experiment_uses_by_id(session_id, reservation_ids)
        self.assertTrue( reservation_results[0].is_finished() )
        self.assertEquals('Chrome', reservation_results[0].experiment_use.request_info['user_agent'])
        self.assertEquals('Consumer', reservation_results[0].experiment_use.commands[2].response.commandstring)

        self.assertTrue( reservation_results[2].is_finished() )
        self.assertEquals('Safari', reservation_results[2].experiment_use.request_info['user_agent'])
        self.assertEquals('Provider 2', reservation_results[2].experiment_use.commands[2].response.commandstring)

        provider2_session_id = self.provider2_login_client.login('provider1', 'password')
        provider2_result = self.provider2_core_client.get_experiment_use_by_id(provider2_session_id, provider2_reservation_id)
        self.assertTrue(provider2_result.is_finished())
        self.assertEquals('Safari', provider2_result.experiment_use.request_info['user_agent'])

        #
        # What if another 2 come in? What is the position of their queues?
        #

        reservation_4 = self._test_reservation(session_id, self.dummy1, '', False, False)
        reservation_status = self.consumer_core_client.get_reservation_status(reservation_4)
        self.assertEquals(Reservation.WAITING, reservation_status.status)
        self.assertEquals(0, reservation_status.position)

        reservation_5 = self._test_reservation(session_id, self.dummy1, '', False, False)
        reservation_status = self.consumer_core_client.get_reservation_status(reservation_5)
        self.assertEquals(Reservation.WAITING, reservation_status.status)
        self.assertEquals(1, reservation_status.position)

        #
        # Once again, freeing a session affects them?
        #
        self.consumer_core_client.finished_experiment(reservation_id2b)
        self._wait_reservation(reservation_4, 'Provider 1', True)

        self.consumer_core_client.finished_experiment(reservation_4)
        self._wait_reservation(reservation_5, 'Provider 1', True)

        # Check for the other uses
        for _ in range(50):
            time.sleep(0.5)
            # Checking every half second
            results = self.consumer_core_client.get_experiment_uses_by_id(session_id, (reservation_id2b, reservation_4))
            if results[0].is_finished() and results[1].is_finished():
                break

        final_reservation_results = self.consumer_core_client.get_experiment_uses_by_id(session_id, (reservation_id2b, reservation_4))
        self.assertTrue(final_reservation_results[0].is_finished())
        self.assertTrue(final_reservation_results[1].is_finished())
        self.assertEquals('Provider 1', final_reservation_results[0].experiment_use.commands[2].response.commandstring)
        self.assertEquals('Provider 1', final_reservation_results[1].experiment_use.commands[2].response.commandstring)

        debug("Test test_federated_experiment finishes successfully")

    def _wait_multiple_reservations(self, times, session_id, reservation_ids, reservations_to_wait):
        for _ in range(times):
            time.sleep(0.5)
            # Checking every half second
            results = self.consumer_core_client.get_experiment_uses_by_id(session_id, reservation_ids)
            all_finished = True

            for reservation_to_wait in reservations_to_wait:
                all_finished = all_finished and results[reservation_to_wait].is_finished()

            if all_finished:
                break

    def _find_command(self, reservation_result, expected_response):
        found = False
        commands = reservation_result.experiment_use.commands
        for command in commands:
            if command.command.commandstring == 'server_info':
                found = True
                response = command.response.commandstring
                self.assertEquals(expected_response, response, "Message %s not found in commands %s; instead found %s" % (expected_response, commands, response))
        self.assertTrue(found, "server_info not found in commands")

    def _test_reservation(self, session_id, experiment_id, expected_server_info, wait, finish, user_agent = None):
        debug("Reserving with session_id %r a experiment %r; will I wait? %s; will I finish? %s" % (session_id, experiment_id, wait, finish))
        reservation_status = self.consumer_core_client.reserve_experiment(session_id, experiment_id, "{}", "{}", user_agent = user_agent)

        reservation_id = reservation_status.reservation_id

        if not wait:
            if finish:
                debug("Finishing... %r" % reservation_id)
                self.consumer_core_client.finished_experiment(reservation_id)
            debug("Not waiting... %r" % reservation_id)
            return reservation_id

        reservation_id = self._wait_reservation(reservation_id, expected_server_info, finish)
        debug("Finished waiting... %r" % reservation_id)
        return reservation_id

    def _wait_reservation(self, reservation_id, expected_server_info, finish):
        max_timeout = 10
        initial_time = time.time()

        reservation_status = self.consumer_core_client.get_reservation_status(reservation_id)
        while reservation_status.status in (Reservation.WAITING, Reservation.WAITING_CONFIRMATION):
            if time.time() - initial_time > max_timeout:
                self.fail("Waiting too long in the queue for %s" % expected_server_info)
            time.sleep(0.1)
            reservation_status = self.consumer_core_client.get_reservation_status(reservation_id)

        self.assertEquals(Reservation.CONFIRMED, reservation_status.status)

        experiment_reservation_id = reservation_status.remote_reservation_id
        if experiment_reservation_id.id == '':
            experiment_reservation_id = reservation_id

        client = WebLabDeustoClient( reservation_status.url )

        response = client.send_command(experiment_reservation_id, Command("server_info"))
        self.assertEquals(expected_server_info, response.get_command_string())

        if finish:
            self.consumer_core_client.finished_experiment(reservation_id)

        return reservation_id

class SqlFederatedWebLabDeustoTestCase(AbstractFederatedWebLabDeustoTestCase, unittest.TestCase):
    FEDERATED_DEPLOYMENTS = 'test/deployments/federated_basic_sql'

try:
    import redis
    assert redis is not None
except ImportError:
    REDIS_AVAILABLE = False
else:
    REDIS_AVAILABLE = True

if REDIS_AVAILABLE:
    class RedisFederatedWebLabDeustoTestCase(AbstractFederatedWebLabDeustoTestCase, unittest.TestCase):
        FEDERATED_DEPLOYMENTS = 'test/deployments/federated_basic_redis'

def suite():
    suites = [unittest.makeSuite(SqlFederatedWebLabDeustoTestCase)]
    if REDIS_AVAILABLE:
        suites.append(unittest.makeSuite(RedisFederatedWebLabDeustoTestCase))
    else:
        print >> sys.stderr, "redis not available. Skipping redis-based federation integration tests"
    return unittest.TestSuite( suites )

if __name__ == '__main__':
    unittest.main()

<|endoftext|>


<commit_before># Copyright 2010-2018 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START program]
"""Vehicles Routing Problem (VRP)."""

# [START import]
from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
# [END import]

# [START solution_printer]
def print_solution(manager, routing, solution):
    """Prints solution on console."""
    max_route_distance = 0
    for vehicle_id in range(manager.GetNumberOfVehicles()):
        index = routing.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        while not routing.IsEnd(index):
            plan_output += ' {} -> '.format(manager.IndexToNode(index))
            previous_index = index
            index = solution.Value(routing.NextVar(index))
            route_distance += routing.GetArcCostForVehicle(
                previous_index, index, vehicle_id)
        plan_output += '{}\n'.format(manager.IndexToNode(index))
        plan_output += 'Distance of the route: {}m\n'.format(route_distance)
        print(plan_output)
        max_route_distance = max(route_distance, max_route_distance)
    print('Maximum of the route distances: {}m'.format(max_route_distance))
# [END solution_printer]

def main():
    """Solve the CVRP problem."""
    # Instantiate the data problem.
    # [START data]
    num_locations = 20;
    num_vehicles = 5;
    depot = 0;
    # [END data]

    # Create the routing index manager.
    # [START index_manager]
    manager = pywrapcp.RoutingIndexManager(
        num_locations, num_vehicles, depot)
    # [END index_manager]

    # Create Routing Model.
    # [START routing_model]
    routing = pywrapcp.RoutingModel(manager)

    # [END routing_model]

    # Create and register a transit callback.
    # [START transit_callback]
    def distance_callback(from_index, to_index):
        """Returns the distance between the two nodes."""
        # Convert from routing variable Index to distance matrix NodeIndex.
        from_node = manager.IndexToNode(from_index)
        to_node = manager.IndexToNode(to_index)
        return 1

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)
    # [END transit_callback]

    # Define cost of each arc.
    # [START arc_cost]
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
    # [END arc_cost]

    # Add Distance constraint.
    # [START distance_constraint]
    dimension_name = 'Distance'
    routing.AddDimension(
        transit_callback_index,
        0,  # no slack
        3000,  # vehicle maximum travel distance
        True,  # start cumul to zero
        dimension_name)
    distance_dimension = routing.GetDimensionOrDie(dimension_name)
    distance_dimension.SetGlobalSpanCostCoefficient(100)
    # [END distance_constraint]

    # Setting first solution heuristic.
    # [START parameters]
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
    search_parameters.local_search_metaheuristic = (
        routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
    search_parameters.log_search = True
    search_parameters.time_limit.FromSeconds(10)
    # [END parameters]

    # Solve the problem.
    # [START solve]
    solution = routing.SolveWithParameters(search_parameters)
    # [END solve]

    # Print solution on console.
    # [START print_solution]
    if solution:
        print_solution(data, manager, routing, solution)
    # [END print_solution]

if __name__ == '__main__':
    main()
# [END program]
<commit_msg>Fix vrp_with_time_limit<commit_after># Copyright 2010-2018 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START program]
"""Vehicles Routing Problem (VRP)."""

# [START import]
from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
# [END import]

# [START solution_printer]
def print_solution(manager, routing, solution):
    """Prints solution on console."""
    max_route_distance = 0
    for vehicle_id in range(manager.GetNumberOfVehicles()):
        index = routing.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        while not routing.IsEnd(index):
            plan_output += ' {} -> '.format(manager.IndexToNode(index))
            previous_index = index
            index = solution.Value(routing.NextVar(index))
            route_distance += routing.GetArcCostForVehicle(
                previous_index, index, vehicle_id)
        plan_output += '{}\n'.format(manager.IndexToNode(index))
        plan_output += 'Distance of the route: {}m\n'.format(route_distance)
        print(plan_output)
        max_route_distance = max(route_distance, max_route_distance)
    print('Maximum of the route distances: {}m'.format(max_route_distance))
# [END solution_printer]

def main():
    """Solve the CVRP problem."""
    # Instantiate the data problem.
    # [START data]
    num_locations = 20;
    num_vehicles = 5;
    depot = 0;
    # [END data]

    # Create the routing index manager.
    # [START index_manager]
    manager = pywrapcp.RoutingIndexManager(
        num_locations, num_vehicles, depot)
    # [END index_manager]

    # Create Routing Model.
    # [START routing_model]
    routing = pywrapcp.RoutingModel(manager)

    # [END routing_model]

    # Create and register a transit callback.
    # [START transit_callback]
    def distance_callback(from_index, to_index):
        """Returns the distance between the two nodes."""
        # Convert from routing variable Index to distance matrix NodeIndex.
        from_node = manager.IndexToNode(from_index)
        to_node = manager.IndexToNode(to_index)
        return 1

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)
    # [END transit_callback]

    # Define cost of each arc.
    # [START arc_cost]
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
    # [END arc_cost]

    # Add Distance constraint.
    # [START distance_constraint]
    dimension_name = 'Distance'
    routing.AddDimension(
        transit_callback_index,
        0,  # no slack
        3000,  # vehicle maximum travel distance
        True,  # start cumul to zero
        dimension_name)
    distance_dimension = routing.GetDimensionOrDie(dimension_name)
    distance_dimension.SetGlobalSpanCostCoefficient(100)
    # [END distance_constraint]

    # Setting first solution heuristic.
    # [START parameters]
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
    search_parameters.local_search_metaheuristic = (
        routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
    search_parameters.log_search = True
    search_parameters.time_limit.FromSeconds(10)
    # [END parameters]

    # Solve the problem.
    # [START solve]
    solution = routing.SolveWithParameters(search_parameters)
    # [END solve]

    # Print solution on console.
    # [START print_solution]
    if solution:
        print_solution(manager, routing, solution)
    # [END print_solution]

if __name__ == '__main__':
    main()
# [END program]
<|endoftext|>


<commit_before>"""
@package mi.instrument.teledyne.workhorse.driver
@file marine-integrations/mi/instrument/teledyne/workhorse/driver.py
@author Sung Ahn
@brief generic Driver for the Workhorse
Release notes:

Generic Driver for ADCPS-K, ADCPS-I, ADCPT-B and ADCPT-DE
"""

__author__ = 'Sung Ahn'
__license__ = 'Apache 2.0'

import socket
from mi.instrument.teledyne.driver import TeledyneInstrumentDriver
from mi.instrument.teledyne.driver import TeledyneProtocol
from mi.instrument.teledyne.driver import TeledynePrompt
from mi.instrument.teledyne.driver import TeledyneParameter
from mi.instrument.teledyne.driver import TeledyneCapability
from mi.instrument.teledyne.particles import *
from mi.core.instrument.chunker import StringChunker

###############################################################################
# Driver
###############################################################################
class WorkhorseParameter(TeledyneParameter):
    """
    Device parameters
    """

class WorkhorseInstrumentDriver(TeledyneInstrumentDriver):
    """
    InstrumentDriver subclass for Workhorse driver.
    Subclasses SingleConnectionInstrumentDriver with connection state
    machine.
    """
    def __init__(self, evt_callback):
        """
        InstrumentDriver constructor.
        @param evt_callback Driver process event callback.
        """
        #Construct superclass.
        TeledyneInstrumentDriver.__init__(self, evt_callback)

    ########################################################################
    # Protocol builder.
    ########################################################################

    def _build_protocol(self):
        """
        Construct the driver protocol state machine.
        """
        self._protocol = WorkhorseProtocol(TeledynePrompt, NEWLINE, self._driver_event)

###########################################################################
# Protocol
###########################################################################

class WorkhorseProtocol(TeledyneProtocol):
    """
    Instrument protocol class
    Subclasses CommandResponseInstrumentProtocol
    """

    @staticmethod
    def sieve_function(raw_data):
        """
        Chunker sieve method to help the chunker identify chunks.
        @returns a list of chunks identified, if any.
        The chunks are all the same type.
        """

        sieve_matchers = [ADCP_SYSTEM_CONFIGURATION_REGEX_MATCHER,
                          ADCP_COMPASS_CALIBRATION_REGEX_MATCHER,
                          ADCP_PD0_PARSED_REGEX_MATCHER]

        return_list = []

        for matcher in sieve_matchers:
            if matcher == ADCP_PD0_PARSED_REGEX_MATCHER:
                #
                # Have to cope with variable length binary records...
                # lets grab the length, then write a proper query to
                # snag it.
                #
                matcher2 = re.compile(r'\x7f\x7f(..)', re.DOTALL)
                for match in matcher2.finditer(raw_data):
                    l = unpack("H", match.group(1))
                    outer_pos = match.start()
                    ADCP_PD0_PARSED_TRUE_MATCHER = re.compile(r'\x7f\x7f(.{' + str(l[0]) + '})', re.DOTALL)
                    for match in ADCP_PD0_PARSED_TRUE_MATCHER.finditer(raw_data, outer_pos):
                        inner_pos = match.start()

                        if (outer_pos == inner_pos):
                            return_list.append((match.start(), match.end()))
            else:
                for match in matcher.finditer(raw_data):
                    return_list.append((match.start(), match.end()))

        return return_list

    def __init__(self, prompts, newline, driver_event):
        """
        Protocol constructor.
        @param prompts A BaseEnum class containing instrument prompts.
        @param newline The newline.
        @param driver_event Driver process event callback.
        """

        log.debug("IN WorkhorseProtocol.__init__")
        # Construct protocol superclass.
        TeledyneProtocol.__init__(self, prompts, newline, driver_event)

        self._chunker = StringChunker(WorkhorseProtocol.sieve_function)

    def _build_command_dict(self):
        self._cmd_dict.add(TeledyneCapability.START_AUTOSAMPLE,
                           timeout=300,
                           display_name="start autosample",
                           description="Place the instrument into autosample mode")
        self._cmd_dict.add(TeledyneCapability.STOP_AUTOSAMPLE,
                           display_name="stop autosample",
                           description="Exit autosample mode and return to command mode")
        self._cmd_dict.add(TeledyneCapability.CLOCK_SYNC,
                           display_name="sync clock")
        self._cmd_dict.add(TeledyneCapability.GET_CALIBRATION,
                           display_name="get calibration")
        self._cmd_dict.add(TeledyneCapability.GET_CONFIGURATION,
                           timeout=300,
                           display_name="get configuration")
        self._cmd_dict.add(TeledyneCapability.GET_INSTRUMENT_TRANSFORM_MATRIX,
                           display_name="get instrument transform matrix")
        self._cmd_dict.add(TeledyneCapability.SAVE_SETUP_TO_RAM,
                           display_name="save setup to ram")
        self._cmd_dict.add(TeledyneCapability.SEND_LAST_SAMPLE,
                           display_name="send last sample")
        self._cmd_dict.add(TeledyneCapability.GET_ERROR_STATUS_WORD,
                           display_name="get error status word")
        self._cmd_dict.add(TeledyneCapability.CLEAR_ERROR_STATUS_WORD,
                           display_name="clear error status word")
        self._cmd_dict.add(TeledyneCapability.GET_FAULT_LOG,
                           display_name="get fault log")
        self._cmd_dict.add(TeledyneCapability.CLEAR_FAULT_LOG,
                           display_name="clear fault log")
        self._cmd_dict.add(TeledyneCapability.RUN_TEST_200,
                           display_name="run test 200")

        self._cmd_dict.add(TeledyneCapability.USER_SETS,
                           display_name="set user sets")
        self._cmd_dict.add(TeledyneCapability.FACTORY_SETS,
                           display_name="set factory sets")

    ########################################################################
    # Private helpers.
    ########################################################################

    def _got_chunk(self, chunk, timestamp):
        """
        The base class got_data has gotten a chunk from the chunker.
        Pass it to extract_sample with the appropriate particle
        objects and REGEXes.
        """

        if (self._extract_sample(ADCP_COMPASS_CALIBRATION_DataParticle,
                                 ADCP_COMPASS_CALIBRATION_REGEX_MATCHER,
                                 chunk,
                                 timestamp)):
            log.debug("_got_chunk - successful match for ADCP_COMPASS_CALIBRATION_DataParticle")

        if (self._extract_sample(ADCP_PD0_PARSED_DataParticle,
                                 ADCP_PD0_PARSED_REGEX_MATCHER,
                                 chunk,
                                 timestamp)):
            log.debug("_got_chunk - successful match for ADCP_PD0_PARSED_DataParticle")

        if (self._extract_sample(ADCP_SYSTEM_CONFIGURATION_DataParticle,
                                 ADCP_SYSTEM_CONFIGURATION_REGEX_MATCHER,
                                 chunk,
                                 timestamp)):
            log.debug("_got_chunk - successful match for ADCP_SYSTEM_CONFIGURATION_DataParticle")

    def _get_params(self):
        return dir(WorkhorseParameter)

    def _getattr_key(self, attr):
        return getattr(WorkhorseParameter, attr)

    def _has_parameter(self, param):
        return WorkhorseParameter.has(param)

    # This will over-write _send_break_cmd in teledyne/driver.py
    def _send_break_cmd(self, delay):
        """
        Send a BREAK to attempt to wake the device.
        """
        # NOTE!!!
        # Once the port agent can handle BREAK, please enable the following line
        #self._connection.send_break(delay)
        # Then remove below lines

        log.trace("IN _send_break_cmd")
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except socket.error, msg:
            log.trace("WHOOPS! 1")

        try:
            sock.connect(('10.180.80.178', 2102))
        except socket.error, msg:
            log.trace("WHOOPS! 2")
        sock.send("break " + str(delay) + "\r\n")
        sock.close()

<commit_msg>Fix response commands<commit_after>"""
@package mi.instrument.teledyne.workhorse.driver
@file marine-integrations/mi/instrument/teledyne/workhorse/driver.py
@author Sung Ahn
@brief generic Driver for the Workhorse
Release notes:

Generic Driver for ADCPS-K, ADCPS-I, ADCPT-B and ADCPT-DE
"""

__author__ = 'Sung Ahn'
__license__ = 'Apache 2.0'

import socket
from mi.instrument.teledyne.driver import TeledyneInstrumentDriver
from mi.instrument.teledyne.driver import TeledyneProtocol
from mi.instrument.teledyne.driver import TeledynePrompt
from mi.instrument.teledyne.driver import TeledyneParameter
from mi.instrument.teledyne.driver import TeledyneCapability
from mi.instrument.teledyne.particles import *
from mi.core.instrument.chunker import StringChunker

###############################################################################
# Driver
###############################################################################
class WorkhorseParameter(TeledyneParameter):
    """
    Device parameters
    """

class WorkhorseInstrumentDriver(TeledyneInstrumentDriver):
    """
    InstrumentDriver subclass for Workhorse driver.
    Subclasses SingleConnectionInstrumentDriver with connection state
    machine.
    """
    def __init__(self, evt_callback):
        """
        InstrumentDriver constructor.
        @param evt_callback Driver process event callback.
        """
        #Construct superclass.
        TeledyneInstrumentDriver.__init__(self, evt_callback)

    ########################################################################
    # Protocol builder.
    ########################################################################

    def _build_protocol(self):
        """
        Construct the driver protocol state machine.
        """
        self._protocol = WorkhorseProtocol(TeledynePrompt, NEWLINE, self._driver_event)

###########################################################################
# Protocol
###########################################################################

class WorkhorseProtocol(TeledyneProtocol):
    """
    Instrument protocol class
    Subclasses CommandResponseInstrumentProtocol
    """

    @staticmethod
    def sieve_function(raw_data):
        """
        Chunker sieve method to help the chunker identify chunks.
        @returns a list of chunks identified, if any.
        The chunks are all the same type.
        """

        sieve_matchers = [ADCP_SYSTEM_CONFIGURATION_REGEX_MATCHER,
                          ADCP_COMPASS_CALIBRATION_REGEX_MATCHER,
                          ADCP_PD0_PARSED_REGEX_MATCHER]

        return_list = []

        for matcher in sieve_matchers:
            if matcher == ADCP_PD0_PARSED_REGEX_MATCHER:
                #
                # Have to cope with variable length binary records...
                # lets grab the length, then write a proper query to
                # snag it.
                #
                matcher2 = re.compile(r'\x7f\x7f(..)', re.DOTALL)
                for match in matcher2.finditer(raw_data):
                    l = unpack("H", match.group(1))
                    outer_pos = match.start()
                    ADCP_PD0_PARSED_TRUE_MATCHER = re.compile(r'\x7f\x7f(.{' + str(l[0]) + '})', re.DOTALL)
                    for match in ADCP_PD0_PARSED_TRUE_MATCHER.finditer(raw_data, outer_pos):
                        inner_pos = match.start()

                        if (outer_pos == inner_pos):
                            return_list.append((match.start(), match.end()))
            else:
                for match in matcher.finditer(raw_data):
                    return_list.append((match.start(), match.end()))

        return return_list

    def __init__(self, prompts, newline, driver_event):
        """
        Protocol constructor.
        @param prompts A BaseEnum class containing instrument prompts.
        @param newline The newline.
        @param driver_event Driver process event callback.
        """

        log.debug("IN WorkhorseProtocol.__init__")
        # Construct protocol superclass.
        TeledyneProtocol.__init__(self, prompts, newline, driver_event)

        self._chunker = StringChunker(WorkhorseProtocol.sieve_function)

    def _build_command_dict(self):
        self._cmd_dict.add(TeledyneCapability.START_AUTOSAMPLE,
                           timeout=300,
                           display_name="start autosample",
                           description="Place the instrument into autosample mode")
        self._cmd_dict.add(TeledyneCapability.STOP_AUTOSAMPLE,
                           display_name="stop autosample",
                           description="Exit autosample mode and return to command mode")
        self._cmd_dict.add(TeledyneCapability.CLOCK_SYNC,
                           display_name="sync clock")
        self._cmd_dict.add(TeledyneCapability.GET_CALIBRATION,
                           display_name="get calibration")
        self._cmd_dict.add(TeledyneCapability.GET_CONFIGURATION,
                           timeout=300,
                           display_name="get configuration")
        #self._cmd_dict.add(TeledyneCapability.GET_INSTRUMENT_TRANSFORM_MATRIX,
        #                   display_name="get instrument transform matrix")
        self._cmd_dict.add(TeledyneCapability.SAVE_SETUP_TO_RAM,
                           display_name="save setup to ram")
        self._cmd_dict.add(TeledyneCapability.SEND_LAST_SAMPLE,
                           display_name="send last sample")
        self._cmd_dict.add(TeledyneCapability.GET_ERROR_STATUS_WORD,
                           display_name="get error status word")
        self._cmd_dict.add(TeledyneCapability.CLEAR_ERROR_STATUS_WORD,
                           display_name="clear error status word")
        self._cmd_dict.add(TeledyneCapability.GET_FAULT_LOG,
                           display_name="get fault log")
        self._cmd_dict.add(TeledyneCapability.CLEAR_FAULT_LOG,
                           display_name="clear fault log")
        self._cmd_dict.add(TeledyneCapability.RUN_TEST_200,
                           display_name="run test 200")

        self._cmd_dict.add(TeledyneCapability.USER_SETS,
                           display_name="set user sets")
        self._cmd_dict.add(TeledyneCapability.FACTORY_SETS,
                           display_name="set factory sets")

    ########################################################################
    # Private helpers.
    ########################################################################

    def _got_chunk(self, chunk, timestamp):
        """
        The base class got_data has gotten a chunk from the chunker.
        Pass it to extract_sample with the appropriate particle
        objects and REGEXes.
        """

        if (self._extract_sample(ADCP_COMPASS_CALIBRATION_DataParticle,
                                 ADCP_COMPASS_CALIBRATION_REGEX_MATCHER,
                                 chunk,
                                 timestamp)):
            log.debug("_got_chunk - successful match for ADCP_COMPASS_CALIBRATION_DataParticle")

        if (self._extract_sample(ADCP_PD0_PARSED_DataParticle,
                                 ADCP_PD0_PARSED_REGEX_MATCHER,
                                 chunk,
                                 timestamp)):
            log.debug("_got_chunk - successful match for ADCP_PD0_PARSED_DataParticle")

        if (self._extract_sample(ADCP_SYSTEM_CONFIGURATION_DataParticle,
                                 ADCP_SYSTEM_CONFIGURATION_REGEX_MATCHER,
                                 chunk,
                                 timestamp)):
            log.debug("_got_chunk - successful match for ADCP_SYSTEM_CONFIGURATION_DataParticle")

    def _get_params(self):
        return dir(WorkhorseParameter)

    def _getattr_key(self, attr):
        return getattr(WorkhorseParameter, attr)

    def _has_parameter(self, param):
        return WorkhorseParameter.has(param)

    # This will over-write _send_break_cmd in teledyne/driver.py
    def _send_break_cmd(self, delay):
        """
        Send a BREAK to attempt to wake the device.
        """
        # NOTE!!!
        # Once the port agent can handle BREAK, please enable the following line
        #self._connection.send_break(delay)
        # Then remove below lines

        log.trace("IN _send_break_cmd")
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except socket.error, msg:
            log.trace("WHOOPS! 1")

        try:
            sock.connect(('10.180.80.178', 2102))
        except socket.error, msg:
            log.trace("WHOOPS! 2")
        sock.send("break " + str(delay) + "\r\n")
        sock.close()

<|endoftext|>


<commit_before>import os
import sys  # noqa: F401
from datetime import date, datetime, timedelta  # noqa: F401
import asyncio
import discord
from discord.ext import commands
from cogs.utils.dataIO import dataIO
from .utils import checks

class AutoRooms:
    """
    auto spawn rooms
    """
    __author__ = "mikeshardmind"
    __version__ = "1.5"

    def __init__(self, bot):
        self.bot = bot
        self.settings = dataIO.load_json('data/autorooms/settings.json')

    @commands.group(name="autoroomset", pass_context=True, no_pm=True)
    async def autoroomset(self, ctx):
        """Configuration settings for AutoRooms"""
        if ctx.invoked_subcommand is None:
            await self.bot.send_cmd_help(ctx)

    def initial_config(self, server_id):
        """makes an entry for the server, defaults to turned off"""

        if server_id not in self.settings:
            self.settings[server_id] = {'toggleactive': False,
                                        'channels': [],
                                        'clones': [],
                                        'cache': []
                                        }
            self.save_json()

    @checks.admin_or_permissions(Manage_channels=True)
    @autoroomset.command(name="toggleactive", pass_context=True, no_pm=True)
    async def autoroomtoggle(self, ctx):
        """
        turns autorooms on and off
        """
        server = ctx.message.server
        if server.id not in self.settings:
            self.initial_config(server.id)

        if self.settings[server.id]['toggleactive'] is True:
            self.settings[server.id]['toggleactive'] = False
            self.save_json()
            await self.bot.say('Auto Rooms disabled.')
        else:
            self.settings[server.id]['toggleactive'] = True
            self.save_json()
            await self.bot.say('Auto Rooms enabled.')

    @checks.admin_or_permissions(Manage_channels=True)
    @autoroomset.command(name="makeclone", pass_context=True, no_pm=True)
    async def settrigger(self, ctx, chan):
        """Takes a channel ID, turns that voice channel into a clone source"""
        server = ctx.message.server
        if server.id not in self.settings:
            self.initial_config(server.id)
        if chan is not None:
            self.settings[server.id]['channels'].append(chan)
            self.save_json()
            await self.bot.say('channel set')

    def save_json(self):
        dataIO.save_json("data/autorooms/settings.json", self.settings)

    async def autorooms(self, memb_before, memb_after):
        """This cog is Self Cleaning"""
        server = memb_before.server
        channels = self.settings[server.id]['channels']
        cache = self.settings[server.id]['cache']
        clones = self.settings[server.id]['clones']

        if self.settings[server.id]['toggleactive']:
            if memb_after.voice.voice_channel is not None:
                chan = memb_after.voice.voice_channel
                if chan.id in channels:
                    overwrites = chan.overwrites
                    cname = "Auto: {}".format(chan.name)
                    channel = await self.bot.create_channel(
                            server, cname, type=discord.ChannelType.voice)
                    for overwrite in overwrites:
                        await self.bot.edit_channel_permissions(channel,
                                                                overwrite)
                    await self.bot.move_member(memb_after, channel)
                    self.settings[server.id]['clones'].append(channel.id)
                self.save_json()

        if memb_after.voice.voice_channel is not None:
            channel = memb_after.voice.voice_channel
            if channel.id in clones:
                if channel.id not in cache:
                    cache.append(channel.id)
                    self.save_json()

        channel = memb_before.voice.voice_channel
        if channel.id in cache:
            if len(channel.voice_members) == 0:
                await self.bot.delete_channel(channel)
                self.settingscleanup(server)

    def settingscleanup(self, server):
        """cleanup of settings"""
        if server.id in self.settings:
            clones = self.settings[server.id]['clones']
            cache = self.settings[server.id]['cache']
            for channel_id in clones:
                channel = server.get_channel(channel_id)
                if channel is None:
                    clones.remove(channel_id)
                    self.save_json()
            for channel_id in cache:
                if channel_id not in clones:
                    cache.remove(channel_id)
                    self.save_json()

def check_folder():
    f = 'data/autorooms'
    if not os.path.exists(f):
        os.makedirs(f)

def check_file():
    f = 'data/autorooms/settings.json'
    if dataIO.is_valid_json(f) is False:
        dataIO.save_json(f, {})

def setup(bot):
    check_folder()
    check_file()
    n = AutoRooms(bot)
    bot.add_listener(n.autorooms, 'on_voice_state_update')
    bot.add_cog(n)
<commit_msg>Added a sanity check to prevent some key errors<commit_after>import os
import sys  # noqa: F401
from datetime import date, datetime, timedelta  # noqa: F401
import asyncio
import discord
from discord.ext import commands
from cogs.utils.dataIO import dataIO
from .utils import checks

class AutoRooms:
    """
    auto spawn rooms
    """
    __author__ = "mikeshardmind"
    __version__ = "1.6"

    def __init__(self, bot):
        self.bot = bot
        self.settings = dataIO.load_json('data/autorooms/settings.json')

    @commands.group(name="autoroomset", pass_context=True, no_pm=True)
    async def autoroomset(self, ctx):
        """Configuration settings for AutoRooms"""
        if ctx.invoked_subcommand is None:
            await self.bot.send_cmd_help(ctx)

    def initial_config(self, server_id):
        """makes an entry for the server, defaults to turned off"""

        if server_id not in self.settings:
            self.settings[server_id] = {'toggleactive': False,
                                        'channels': [],
                                        'clones': [],
                                        'cache': []
                                        }
            self.save_json()

    @checks.admin_or_permissions(Manage_channels=True)
    @autoroomset.command(name="toggleactive", pass_context=True, no_pm=True)
    async def autoroomtoggle(self, ctx):
        """
        turns autorooms on and off
        """
        server = ctx.message.server
        if server.id not in self.settings:
            self.initial_config(server.id)

        if self.settings[server.id]['toggleactive'] is True:
            self.settings[server.id]['toggleactive'] = False
            self.save_json()
            await self.bot.say('Auto Rooms disabled.')
        else:
            self.settings[server.id]['toggleactive'] = True
            self.save_json()
            await self.bot.say('Auto Rooms enabled.')

    @checks.admin_or_permissions(Manage_channels=True)
    @autoroomset.command(name="makeclone", pass_context=True, no_pm=True)
    async def settrigger(self, ctx, chan):
        """Takes a channel ID, turns that voice channel into a clone source"""
        server = ctx.message.server
        if server.id not in self.settings:
            self.initial_config(server.id)
        if chan is not None:
            self.settings[server.id]['channels'].append(chan)
            self.save_json()
            await self.bot.say('channel set')

    def save_json(self):
        dataIO.save_json("data/autorooms/settings.json", self.settings)

    async def autorooms(self, memb_before, memb_after):
        """This cog is Self Cleaning"""
        server = memb_before.server
        channels = self.settings[server.id]['channels']
        cache = self.settings[server.id]['cache']
        clones = self.settings[server.id]['clones']

        if server.id in self.settings:
            if self.settings[server.id]['toggleactive']:
                if memb_after.voice.voice_channel is not None:
                    chan = memb_after.voice.voice_channel
                    if chan.id in channels:
                        overwrites = chan.overwrites
                        cname = "Auto: {}".format(chan.name)
                        channel = await self.bot.create_channel(
                                server, cname, type=discord.ChannelType.voice)
                        for overwrite in overwrites:
                            await self.bot.edit_channel_permissions(channel,
                                                                    overwrite)
                            await self.bot.move_member(memb_after, channel)
                        self.settings[server.id]['clones'].append(channel.id)
                        self.save_json()

        if memb_after.voice.voice_channel is not None:
            channel = memb_after.voice.voice_channel
            if channel.id in clones:
                if channel.id not in cache:
                    cache.append(channel.id)
                    self.save_json()

        channel = memb_before.voice.voice_channel
        if channel.id in cache:
            if len(channel.voice_members) == 0:
                await self.bot.delete_channel(channel)
                self.settingscleanup(server)

    def settingscleanup(self, server):
        """cleanup of settings"""
        if server.id in self.settings:
            clones = self.settings[server.id]['clones']
            cache = self.settings[server.id]['cache']
            for channel_id in clones:
                channel = server.get_channel(channel_id)
                if channel is None:
                    clones.remove(channel_id)
                    self.save_json()
            for channel_id in cache:
                if channel_id not in clones:
                    cache.remove(channel_id)
                    self.save_json()

def check_folder():
    f = 'data/autorooms'
    if not os.path.exists(f):
        os.makedirs(f)

def check_file():
    f = 'data/autorooms/settings.json'
    if dataIO.is_valid_json(f) is False:
        dataIO.save_json(f, {})

def setup(bot):
    check_folder()
    check_file()
    n = AutoRooms(bot)
    bot.add_listener(n.autorooms, 'on_voice_state_update')
    bot.add_cog(n)
<|endoftext|>


<commit_before>#!/usr/bin/env python

"""

ownCloud - News

@author Bernhard Posselt
@copyright 2012 Bernhard Posselt nukeawhale@gmail.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU AFFERO GENERAL PUBLIC LICENSE for more details.

You should have received a copy of the GNU Affero General Public
License along with this library.  If not, see <http://www.gnu.org/licenses/>.

"""

import sys
import time
import json
import argparse
import threading
import urllib.request
import urllib.error

class UpdateThread(threading.Thread):

    lock = threading.Lock()

    def __init__(self, feeds, update_url):
        super().__init__()
        self.feeds = feeds
        self.update_url = update_url

    def run(self):
        with UpdateThread.lock:
            if len(self.feeds) > 0:
                feed = self.feeds.pop()
            else:
                return

        feed['feedId'] = feed['id']
        del feed['id']

        # call the update method of one feed
        data = urllib.parse.urlencode(feed)
        headers = {
            'Content-type': 'application/json',
            'Accept': 'text/plain'
        }
        url = '%s?%s' % (self.update_url, data)

        try:
            response = urllib.request.urlopen(url, timeout=60)
            print('succ!')
            print(url)
            print(response.read())
        except urllib.error.HTTPError as e:
            print('%s: %s' % (url, e))

        self.run()

class Updater:

    def __init__(self, base_url, thread_num, interval):
        self.thread_num = thread_num
        self.interval = interval
        self.base_url = base_url

        if self.base_url[-1] != '/':
            self.base_url += '/'
        self.base_url += 'index.php/apps/news/api/v1-2'

        self.cleanup_url = '%s/cleanup' % self.base_url
        self.all_feeds_url = '%s/feeds/all' % self.base_url
        self.update_url = '%s/feeds/update' % self.base_url

    def run(self):
        try:
            # run the cleanup request and get all the feeds to update
            urllib.request.urlopen(self.cleanup_url)
            feeds_response = urllib.request.urlopen(self.all_feeds_url)
            feeds_json = feeds_response.read().decode('utf-8')
            feeds = json.loads(feeds_json)['feeds']

            # start thread_num for feeds
            threads = []
            for num in range(0, self.thread_num):
                thread = UpdateThread(feeds, self.update_url)
                thread.start()
                threads.append(thread)

            for thread in threads:
                thread.join()

            # wait until the interval finished to run again
            time.sleep(self.interval)
            self.run()

        except ValueError:
            print('%s is not a valid URL' % self.base_url)
        except urllib.error.HTTPError:
            print('%s does not exist' % self.base_url)
            exit(1)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--threads',
        help='How many feeds should be fetched in paralell, defaults to 10',
        default=10,
        type=int)
    parser.add_argument('--interval',
        help='Update interval between fetching the next round of \
            updates in minutes, defaults to 30 minutes',
        default=30,
        type=int)
    parser.add_argument('--user',
        help='A username to log into ownCloud', required=True)
    parser.add_argument('--password',
        help='A password to log into ownCloud', required=True)
    parser.add_argument('url',
        help='The URL where owncloud is installed')
    args = parser.parse_args()

    # register user and password for a certain url
    auth = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    auth.add_password(None, args.url, args.user, args.password)
    auth_handler = urllib.request.HTTPBasicAuthHandler(auth)
    opener = urllib.request.build_opener(auth_handler)
    urllib.request.install_opener(opener)

    # create the updater and run the threads
    updater = Updater(args.url, args.threads, args.interval)
    updater.run()

if __name__ == '__main__':
    if sys.version_info < (3, 0):
        print('Python 3.0 or higher is required to run this script')
    else:
        main()

<commit_msg>remove verbose debug<commit_after>#!/usr/bin/env python

"""

ownCloud - News

@author Bernhard Posselt
@copyright 2012 Bernhard Posselt nukeawhale@gmail.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU AFFERO GENERAL PUBLIC LICENSE for more details.

You should have received a copy of the GNU Affero General Public
License along with this library.  If not, see <http://www.gnu.org/licenses/>.

"""

import sys
import time
import json
import argparse
import threading
import urllib.request
import urllib.error

class UpdateThread(threading.Thread):

    lock = threading.Lock()

    def __init__(self, feeds, update_url):
        super().__init__()
        self.feeds = feeds
        self.update_url = update_url

    def run(self):
        with UpdateThread.lock:
            if len(self.feeds) > 0:
                feed = self.feeds.pop()
            else:
                return

        feed['feedId'] = feed['id']
        del feed['id']

        # call the update method of one feed
        data = urllib.parse.urlencode(feed)
        headers = {
            'Content-type': 'application/json',
            'Accept': 'text/plain'
        }
        url = '%s?%s' % (self.update_url, data)

        try:
            urllib.request.urlopen(url, timeout=60)
        except urllib.error.HTTPError as e:
            print('%s: %s' % (url, e))

        self.run()

class Updater:

    def __init__(self, base_url, thread_num, interval):
        self.thread_num = thread_num
        self.interval = interval
        self.base_url = base_url

        if self.base_url[-1] != '/':
            self.base_url += '/'
        self.base_url += 'index.php/apps/news/api/v1-2'

        self.cleanup_url = '%s/cleanup' % self.base_url
        self.all_feeds_url = '%s/feeds/all' % self.base_url
        self.update_url = '%s/feeds/update' % self.base_url

    def run(self):
        try:
            # run the cleanup request and get all the feeds to update
            urllib.request.urlopen(self.cleanup_url)
            feeds_response = urllib.request.urlopen(self.all_feeds_url)
            feeds_json = feeds_response.read().decode('utf-8')
            feeds = json.loads(feeds_json)['feeds']

            # start thread_num for feeds
            threads = []
            for num in range(0, self.thread_num):
                thread = UpdateThread(feeds, self.update_url)
                thread.start()
                threads.append(thread)

            for thread in threads:
                thread.join()

            # wait until the interval finished to run again
            time.sleep(self.interval)
            self.run()

        except ValueError:
            print('%s is not a valid URL' % self.base_url)
        except urllib.error.HTTPError:
            print('%s does not exist' % self.base_url)
            exit(1)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--threads',
        help='How many feeds should be fetched in paralell, defaults to 10',
        default=10,
        type=int)
    parser.add_argument('--interval',
        help='Update interval between fetching the next round of \
            updates in minutes, defaults to 30 minutes',
        default=30,
        type=int)
    parser.add_argument('--user',
        help='A username to log into ownCloud', required=True)
    parser.add_argument('--password',
        help='A password to log into ownCloud', required=True)
    parser.add_argument('url',
        help='The URL where owncloud is installed')
    args = parser.parse_args()

    # register user and password for a certain url
    auth = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    auth.add_password(None, args.url, args.user, args.password)
    auth_handler = urllib.request.HTTPBasicAuthHandler(auth)
    opener = urllib.request.build_opener(auth_handler)
    urllib.request.install_opener(opener)

    # create the updater and run the threads
    updater = Updater(args.url, args.threads, args.interval)
    updater.run()

if __name__ == '__main__':
    if sys.version_info < (3, 0):
        print('Python 3.0 or higher is required to run this script')
    else:
        main()

<|endoftext|>