Exemplo n.º 1
0
def main(argv):
   print '##################################################################'
   print '#           Test Azure Service bus queue connection              #'
   print '##################################################################'

   configFileName = os.path.dirname(os.path.abspath(__file__)) + '/config.json'

   print 'Start Parameters:'
   print '  configFileName:', configFileName 

   json_data=open(configFileName)
   config_data = json.load(json_data)
   json_data.close()

   print '      Company ID:', config_data["Server"]["id"]
   print '      Gateway ID:', config_data["Server"]["Deviceid"]
   print '     Service Bus:', config_data["Servicebus"]["namespace"]

   queue_name = 'custom_' + config_data["Server"]["id"] + '_' + config_data["Server"]["Deviceid"]
   bus_service = ServiceBusService( service_namespace=config_data["Servicebus"]["namespace"], 
                                    shared_access_key_name=config_data["Servicebus"]["shared_access_key_name"], 
                                    shared_access_key_value=config_data["Servicebus"]["shared_access_key_value"])
   try:
      bus_service.receive_queue_message(queue_name, peek_lock=False)
      print '  Actuator queue: ' + queue_name
   except:
      queue_options = Queue()
      queue_options.max_size_in_megabytes = '1024'
      queue_options.default_message_time_to_live = 'PT15M'
      bus_service.create_queue(queue_name, queue_options)
      print '  Actuator queue: ' + queue_name + ' (Created)'	   
	   
   # List to hold all commands that was send no ACK received
   localCommandSendAckWaitList = []
   while True:
      cloudCommand = bus_service.receive_queue_message(queue_name, peek_lock=False)

      if cloudCommand.body is not None:
          stringCommand = str(cloudCommand.body)
          print 'C: Received "' + stringCommand + '" => ',

          #Tranlate External/Cloud ID to local network ID 
          temp = stringCommand.split("-")
          #print 'stringCommand.split = ', temp
          localNetworkDeviceID = config_data["Devices"].keys()[config_data["Devices"].values().index(temp[0])]
          print 'for DeviceId=' + localNetworkDeviceID

      time.sleep(10)
Exemplo n.º 2
0
class ServiceBusTaskSource(TaskSource):
    '''
    Azure Service Bus implementation of TaskSource.
    '''
    def __init__(self):
        self.service_bus = ServiceBusService(
            ServiceBusTaskSource.service_bus_namespace(),
            shared_access_key_name=os.environ.get(
                'AZURE_SERVICE_BUS_ACCESS_KEY_NAME'),
            shared_access_key_value=os.environ.get(
                'AZURE_SERVICE_BUS_ACCESS_KEY_VALUE'))
        self.queue_name = os.environ.get('AZURE_SERVICE_BUS_QUEUE_NAME',
                                         'training')

    @classmethod
    def service_bus_namespace(self):
        return os.environ.get('AZURE_SERVICE_BUS_NAMESPACE')

    @classmethod
    def is_supported(self):
        return self.service_bus_namespace()

    def receive(self):
        message = self.service_bus.receive_queue_message(self.queue_name)
        if message:
            return Task(source=self, content=None, user_info=message)
        return None

    def commit(self, task):
        task.user_info.delete()
Exemplo n.º 3
0
def receiver():
    bus_service = ServiceBusService(
        service_namespace='comp6905',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='rK2FMzVKvCjpad7xVSj1AB3hDimhxZq3WtEE4y28yaM=')
    table_service = TableService(
        account_name='comp6905kirk',
        account_key=
        'H1YuP8hBxJ2PKw2hoW4Dr+DMAMvKZ/nGhstHw+87mE+OSBTb23cBxhkUvILgKOHWHA3hi3oaoohwVkp6lOXOlA=='
    )
    while (True):
        msg = bus_service.receive_queue_message('queue1', peek_lock=False)
        msg1 = msg.body.decode("utf-8")
        print(msg1)
        parsed_json = json.loads(msg1)
        #print(parsed_json['UserId'
        task = {
            'PartitionKey': 'Zanko',
            'RowKey': parsed_json['TransactionID'],
            'UserId': parsed_json['UserId'],
            'SellerId': parsed_json['SellerID'],
            'ProductName': parsed_json['Product Name'],
            'SalePrice': parsed_json['Sale Price'],
            'TransactionDate': parsed_json['Transaction Date']
        }
        table_service.insert_entity('Requests', task)
Exemplo n.º 4
0
class AzureConnection():
    def __init__(self):
        self.bus_service = ServiceBusService(
            service_namespace='msgtestsb',
            shared_access_key_name='RootManageSharedAccessKey',
            shared_access_key_value=
            'Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
        queue_options = Queue()
        queue_options.max_size_in_megabytes = '5120'
        queue_options.default_message_time_to_live = 'PT96H'

        self.bus_service.create_queue('process_incoming', queue_options)
        self.bus_service.create_queue('whatsapp_sender', queue_options)
        self.bus_service.create_queue('myapp_sender', queue_options)

    def receive(self):
        msg = self.bus_service.receive_queue_message('process_incoming',
                                                     peek_lock=False)

        if msg != None and msg.body:
            logger.info(msg.body)
            return json.loads(msg.body)
        else:
            return None

    def send(self, jsondict):
        t = json.dumps(jsondict)
        msg = Message(t)
        logger.info(t)
        Q = jsondict['medium'] + '_sender'

        self.bus_service.send_queue_message(Q, msg)
Exemplo n.º 5
0
def poll_service_bus(sb_connection_string, sb_queue_name, queue):
    SERVICE_NAMESPACE, SHARED_ACCESS_KEY_NAME, SHARED_ACCESS_KEY_VALUE = parse_connection_string(
        sb_connection_string)

    bus_service = ServiceBusService(
        service_namespace=SERVICE_NAMESPACE,
        shared_access_key_name=SHARED_ACCESS_KEY_NAME,
        shared_access_key_value=SHARED_ACCESS_KEY_VALUE)

    while True:
        timestamps = []
        device_ids = []
        telemetry_entities = []
        for _ in range(BATCH_SIZE):
            msg = bus_service.receive_queue_message(sb_queue_name,
                                                    peek_lock=False)
            enqueued_time = datetime.datetime.strptime(
                msg.broker_properties['EnqueuedTimeUtc'],
                '%a, %d %b %Y %H:%M:%S %Z')
            device_id = msg.custom_properties['iothub-connection-device-id']
            telemetry_entity = pickle.loads(msg.body)
            preprocess_telemetry_entity(telemetry_entity)
            timestamps.append(enqueued_time)
            device_ids.append(device_id)
            telemetry_entities.append(telemetry_entity)

        queue.put((timestamps, device_ids, telemetry_entities))
class azure_service_bus_listener(object):
	
    def __init__(self, azure_settings):
        self.bus_service = ServiceBusService(
            service_namespace= azure_settings['name_space'],
            shared_access_key_name = azure_settings['key_name'],
            shared_access_key_value = azure_settings['key_value'])
		
        self.queue_name = azure_settings['queue_name']
	
    def wait_for_message(self, on_receive_target, on_timeout_target):
		# just in case it isn't there
        self.create_queue()
		
        message = self.bus_service.receive_queue_message(self.queue_name, peek_lock=False)
        
        if (message.body == None):
            print("[ASB_Listener]: No Message Received")
            on_timeout_target()
        else:
            message_string = message.body.decode('utf-8')
            on_receive_target(message_string)
		
    def create_queue(self):
        q_opt = Queue()
        q_opt.max_size_in_megabytes = '1024'
        q_opt.default_message_time_to_live = 'PT1M'
        self.bus_service.create_queue(self.queue_name, q_opt)
Exemplo n.º 7
0
class AzureConnection():
    def __init__(self):
        self.bus_service = ServiceBusService(
            service_namespace='msgtestsb',
            shared_access_key_name='RootManageSharedAccessKey',
            shared_access_key_value='Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
        queue_options = Queue()
        queue_options.max_size_in_megabytes = '5120'
        queue_options.default_message_time_to_live = 'PT96H'

        self.bus_service.create_queue('process_incoming', queue_options)
        self.bus_service.create_queue('whatsapp_sender', queue_options)
        self.bus_service.create_queue('myapp_sender', queue_options)
 

    def receive(self):
        msg = self.bus_service.receive_queue_message('process_incoming', peek_lock=False)
        
        if msg != None and msg.body:
                logger.info(  msg.body)
                return json.loads(msg.body)
        else:
                return None

    def send(self, jsondict):
        t = json.dumps(jsondict)
        msg = Message(t)
        logger.info(  t)
        Q = jsondict['medium'] + '_sender'
            
        self.bus_service.send_queue_message(Q, msg)
Exemplo n.º 8
0
def watchAzureQueue():
    global Counter
    if AZURE_RECEIVING == False:
        return

    bus_service = ServiceBusService(
        service_namespace='msgtestsb',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
    queue_options = Queue()
    queue_options.max_size_in_megabytes = '5120'
    queue_options.default_message_time_to_live = 'PT96H'

    bus_service.create_queue('process_incoming', queue_options)
    bus_service.create_queue('whatsapp_sender', queue_options)

    while not isfile('/home/bitnami1/whatsapp/.stopwhatsapp') and Counter > 1:
        msg = bus_service.receive_queue_message('whatsapp_sender',
                                                peek_lock=False)
        if msg != None and msg.body:
            logging.info('%s ' % datetime.now() + msg.body)
            SendQueue.put(msg.body)
        else:
            logging.info('%s ' % datetime.now() + "Empty Azure Queue")
            time.sleep(4.6)
Exemplo n.º 9
0
def setup_client():
    bus_service = ServiceBusService(
        service_namespace=SERVICE_BUS_NAME,
        shared_access_key_name=SHARED_ACCESS_POLICY_NAME,
        shared_access_key_value=SHARED_ACCESS_POLICY_KEY_VALUE)

    while True:
        msg = bus_service.receive_queue_message(QUEUE_NAME, peek_lock=False)
        if msg.body != None:
            print(msg.body)
            msg_dict = eval(str(msg.body, 'utf-8'))
            st=0
            if msg_dict["Bike State"]:
                st=1
            # bike=Bike(id=int(msg_dict["Sensor ID"].split(" ")[1]),lon=msg_dict["Longitude"],lat=msg_dict["Latitude"],status= st)
            device_id = int(msg_dict["Sensor ID"].split("_")[1])
            bike = Bike.query.get(device_id)
            # if bike is None:
            bike_new = Bike(device_id=device_id,lon=msg_dict["Longitude"],lat=msg_dict["Latitude"], status=st)
            # else:
            #     bike.lon = msg_dict["Longitude"]
            #     bike.lat = msg_dict["Latitude"]
            print(bike.to_dict())
            bike_new=conn.session.merge(bike_new)
            #conn.session.add(bike)
            conn.session.commit()
            print("___", conn)
        # else:
        #     print("There is no data in the queue.")
        time.sleep(30)
Exemplo n.º 10
0
def workerthread():
    # outer loop to run while waiting
    from azure.servicebus import ServiceBusService, Message, Queue
    bus_service = ServiceBusService(
        service_namespace='jairaj007',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='lGSHGfaf8RBQ9lHbso85PCvGD2BCBVzpMKHHXLwluhg=')
    while (True):
        # main loop to thrash the CPI
        while (keepworking == True):
            msg = bus_service.receive_queue_message('myqueue', peek_lock=False)
        time.sleep(3)
def workerthread():
    # outer loop to run while waiting
    from azure.servicebus import ServiceBusService, Message, Queue
    bus_read_service = ServiceBusService(
        service_namespace='vmsseight',
        shared_access_key_name='ListenOneTime',
        shared_access_key_value='OhFgmG5Cr/K9aOrE29YL7eXERzmUb3Fpf7J+FoBhiMw=')
    while (True):
        # main loop to thrash the CPI
        while (keepworking == True):
            msg = bus_read_service.receive_queue_message('vmsseightqueue', peek_lock=False)
        time.sleep(3)
Exemplo n.º 12
0
class AzureServiceBusQueue(Queue):
    """
    Implements a Queue backed by a Windows Azure Service Bus Queue.
    """

    # Timeout in seconds. receive_message is blocking and returns as soon as one of two
    # conditions occurs: a message is received or the timeout period has elapsed.
    polling_timeout = 60

    def __init__(self, namespace, key, issuer, shared_access_key_name,
                 shared_access_key_value, name):
        self.service = ServiceBusService(
            service_namespace=namespace,
            account_key=key,
            issuer=issuer,
            shared_access_key_name=shared_access_key_name,
            shared_access_key_value=shared_access_key_value)
        self.name = name
        self.max_retries = 3
        self.wait = lambda count: 1.0 * (2**count)

    def _try_request(self, fn, retry_count=0, fail=None):
        '''Helper to retry request for sending and receiving messages.'''
        try:
            return fn()
        except (WindowsAzureError) as e:
            if retry_count < self.max_retries:
                logger.error(
                    "Retrying request after error occurred. Attempt %s of %s.",
                    retry_count + 1, self.max_retries)
                wait_interval = self.wait(retry_count)
                if wait_interval > 0.0:
                    sleep(wait_interval)
                return self._try_request(fn,
                                         retry_count=retry_count + 1,
                                         fail=fail)
            else:
                if fail is not None:
                    fail()
                raise e

    def receive_message(self):
        op = lambda: self.service.receive_queue_message(
            self.name, peek_lock=False, timeout=self.polling_timeout)
        msg = self._try_request(op)
        return None if msg.body is None else AzureServiceBusQueueMessage(
            self, msg)

    def send_message(self, body):
        op = lambda: self.service.send_queue_message(self.name, Message(body))
        fail = lambda: logger.error(
            "Failed to send message. Message body is:\n%s", body)
        self._try_request(op, fail=fail)
def workerthread():
    # outer loop to run while waiting
    from azure.servicebus import ServiceBusService, Message, Queue
    bus_read_service = ServiceBusService(
        service_namespace='vmsseight',
        shared_access_key_name='ListenOneTime',
        shared_access_key_value='OhFgmG5Cr/K9aOrE29YL7eXERzmUb3Fpf7J+FoBhiMw=')
    while (True):
        # main loop to thrash the CPI
        while (keepworking == True):
            msg = bus_read_service.receive_queue_message('vmsseightqueue',
                                                         peek_lock=False)
        time.sleep(3)
Exemplo n.º 14
0
    def test_two_identities(self):
        # In order to run this test, 2 service bus service identities are created using
        # the sbaztool available at:
        # http://code.msdn.microsoft.com/windowsazure/Authorization-SBAzTool-6fd76d93
        #
        # Use the following commands to create 2 identities and grant access rights.
        # Replace <servicebusnamespace> with the namespace specified in the test .json file
        # Replace <servicebuskey> with the key specified in the test .json file
        # This only needs to be executed once, after the service bus namespace is created.
        #
        # sbaztool makeid user1 NoHEoD6snlvlhZm7yek9Etxca3l0CYjfc19ICIJZoUg= -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Send /path1 user1 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Listen /path1 user1 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Manage /path1 user1 -n <servicebusnamespace> -k <servicebuskey>

        # sbaztool makeid user2 Tb6K5qEgstyRBwp86JEjUezKj/a+fnkLFnibfgvxvdg= -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Send /path2 user2 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Listen /path2 user2 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Manage /path2 user2 -n <servicebusnamespace> -k <servicebuskey>

        sbs1 = ServiceBusService(
            credentials.getServiceBusNamespace(),
            'NoHEoD6snlvlhZm7yek9Etxca3l0CYjfc19ICIJZoUg=', 'user1')
        sbs2 = ServiceBusService(
            credentials.getServiceBusNamespace(),
            'Tb6K5qEgstyRBwp86JEjUezKj/a+fnkLFnibfgvxvdg=', 'user2')

        queue1_name = 'path1/queue' + str(random.randint(1, 10000000))
        queue2_name = 'path2/queue' + str(random.randint(1, 10000000))

        try:
            # Create queues, success
            sbs1.create_queue(queue1_name)
            sbs2.create_queue(queue2_name)

            # Receive messages, success
            msg = sbs1.receive_queue_message(queue1_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs1.receive_queue_message(queue1_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs2.receive_queue_message(queue2_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs2.receive_queue_message(queue2_name, True, 1)
            self.assertIsNone(msg.body)

            # Receive messages, failure
            with self.assertRaises(HTTPError):
                msg = sbs1.receive_queue_message(queue2_name, True, 1)
            with self.assertRaises(HTTPError):
                msg = sbs2.receive_queue_message(queue1_name, True, 1)
        finally:
            try:
                sbs1.delete_queue(queue1_name)
            except:
                pass
            try:
                sbs2.delete_queue(queue2_name)
            except:
                pass
    def test_two_identities(self):
        # In order to run this test, 2 service bus service identities are created using
        # the sbaztool available at:
        # http://code.msdn.microsoft.com/windowsazure/Authorization-SBAzTool-6fd76d93
        #
        # Use the following commands to create 2 identities and grant access rights.
        # Replace <servicebusnamespace> with the namespace specified in the test .json file
        # Replace <servicebuskey> with the key specified in the test .json file
        # This only needs to be executed once, after the service bus namespace is created.
        #
        # sbaztool makeid user1 NoHEoD6snlvlhZm7yek9Etxca3l0CYjfc19ICIJZoUg= -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Send /path1 user1 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Listen /path1 user1 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Manage /path1 user1 -n <servicebusnamespace> -k <servicebuskey>

        # sbaztool makeid user2 Tb6K5qEgstyRBwp86JEjUezKj/a+fnkLFnibfgvxvdg= -n <servicebusnamespace> -k <servicebuskey> 
        # sbaztool grant Send /path2 user2 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Listen /path2 user2 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Manage /path2 user2 -n <servicebusnamespace> -k <servicebuskey>

        sbs1 = ServiceBusService(credentials.getServiceBusNamespace(), 
                                 'NoHEoD6snlvlhZm7yek9Etxca3l0CYjfc19ICIJZoUg=', 
                                 'user1')
        sbs2 = ServiceBusService(credentials.getServiceBusNamespace(), 
                                 'Tb6K5qEgstyRBwp86JEjUezKj/a+fnkLFnibfgvxvdg=', 
                                 'user2')

        queue1_name = 'path1/queue' + str(random.randint(1, 10000000))
        queue2_name = 'path2/queue' + str(random.randint(1, 10000000))

        try:
            # Create queues, success
            sbs1.create_queue(queue1_name)
            sbs2.create_queue(queue2_name)

            # Receive messages, success
            msg = sbs1.receive_queue_message(queue1_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs1.receive_queue_message(queue1_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs2.receive_queue_message(queue2_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs2.receive_queue_message(queue2_name, True, 1)
            self.assertIsNone(msg.body)

            # Receive messages, failure
            with self.assertRaises(HTTPError):
                msg = sbs1.receive_queue_message(queue2_name, True, 1)
            with self.assertRaises(HTTPError):
                msg = sbs2.receive_queue_message(queue1_name, True, 1)
        finally:
            try:
                sbs1.delete_queue(queue1_name)
            except: pass
            try:
                sbs2.delete_queue(queue2_name)
            except: pass
class AzureServiceBusQueue(Queue):
    """
    Implements a Queue backed by a Windows Azure Service Bus Queue.
    """

    # Timeout in seconds. receive_message is blocking and returns as soon as one of two
    # conditions occurs: a message is received or the timeout period has elapsed.
    polling_timeout = 60

    def __init__(self, namespace, key, issuer, shared_access_key_name, shared_access_key_value, name):
        self.service = ServiceBusService(service_namespace=namespace, account_key=key,
                                         issuer=issuer, shared_access_key_name=shared_access_key_name,
                                         shared_access_key_value=shared_access_key_value)
        self.name = name
        self.max_retries = 3
        self.wait = lambda count: 1.0*(2**count)

    def _try_request(self, fn, retry_count=0, fail=None):
        '''Helper to retry request for sending and receiving messages.'''
        try:
            return fn()
        except (WindowsAzureError) as e:
            if retry_count < self.max_retries:
                logger.error("Retrying request after error occurred. Attempt %s of %s.",
                             retry_count+1, self.max_retries)
                wait_interval = self.wait(retry_count)
                if wait_interval > 0.0:
                    sleep(wait_interval)
                return self._try_request(fn, retry_count=retry_count+1, fail=fail)
            else:
                if fail is not None:
                    fail()
                raise e

    def receive_message(self):
        op = lambda: self.service.receive_queue_message(self.name,
                                                        peek_lock=False,
                                                        timeout=self.polling_timeout)
        msg = self._try_request(op)
        return None if msg.body is None else AzureServiceBusQueueMessage(self, msg)

    def send_message(self, body):
        op = lambda: self.service.send_queue_message(self.name, Message(body))
        fail = lambda: logger.error("Failed to send message. Message body is:\n%s", body)
        self._try_request(op, fail=fail)
Exemplo n.º 17
0
class AzureServiceBusQueue(Queue):
    """
    Implements a Queue backed by a Windows Azure Service Bus Queue.
    """

    # Timeout in seconds. receive_message is blocking and returns as soon as one of two
    # conditions occurs: a message is received or the timeout period has elapsed.
    polling_timeout = 60

    def __init__(self, namespace, key, issuer, name):
        self.service = ServiceBusService(service_namespace=namespace, account_key=key, issuer=issuer)
        self.name = name

    def receive_message(self):
        msg = self.service.receive_queue_message(self.name, peek_lock=False, timeout=self.polling_timeout)
        return None if msg.body is None else AzureServiceBusQueueMessage(self, msg)

    def send_message(self, body):
        self.service.send_queue_message(self.name, Message(body))
Exemplo n.º 18
0
class azure_service_bus_listener(object):
	
	def __init__(self, azure_settings):
		self.bus_service = ServiceBusService(
			service_namespace= azure_settings.name_space,
			shared_access_key_name = azure_settings.key_name,
			shared_access_key_value = azure_settings.key_value)
		
		self.queue_name = azure_settings(queue_name)
	
	def wait_for_message(self, on_receive_target):
		message = self.bus_service.receive_queue_message(self.queue_name, peek_lock=False)
		message_string = message.body.decode('utf-8')
		on_receive_target(message_string)
		
	def create_queue(self):
		q_opt = Queue()
		q_opt.max_size_in_megabytes = '1024'
		q_opt.default_message_time_to_live = 'PT1M'
		self.bus_service.create_queue(self.queue_name, q_opt)
Exemplo n.º 19
0
def sender():
    bus_service = ServiceBusService(
        service_namespace='comp6905',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='rK2FMzVKvCjpad7xVSj1AB3hDimhxZq3WtEE4y28yaM=')
    x = 0
    while (True):
        msg1 = bus_service.receive_queue_message('numbers', peek_lock=False)
        #    print (msg1.body.decode("utf-8"))
        msg2 = msg1.body.decode("utf-8")
        msg3 = str(int(msg2) + 501)

        msg4 = Message(msg3.encode('utf-8'))
        bus_service.send_queue_message('numbers', msg4)
        premessage = '{"TransactionID":"' + msg2 + '","UserId":"A1","SellerID":"S1","Product Name":"Financial Trap","Sale Price":"1000000","Transaction Date":"2016-11-01T13:19:42.963Z"}'
        #   print(premessage)
        preMbytes = premessage.encode('utf-8')
        msg = Message(preMbytes)
        x = x + 1
        #print(x)
        bus_service.send_queue_message('queue1', msg)
Exemplo n.º 20
0
def main(argv):
    print 'Starting program...'

    internalDeviceId = argv[0]
    json_data=open('config.json')
    config_data = json.load(json_data)
    json_data.close()

    externalDeviceId = config_data["Devices"][internalDeviceId]
    queue_name = 'custom_' + config_data["Server"]["id"] + '_' + config_data["Server"]["Deviceid"] + '_' + externalDeviceId
    print (queue_name)
    
    bus_service = ServiceBusService( service_namespace='rdciot', shared_access_key_name='RootManageSharedAccessKey', shared_access_key_value='EXeZe7r49jCoDz79fESxtMdXwYU6iQwG1Gbo8J4HXyY=')
    print bus_service
    
    while True:
        msg = bus_service.receive_queue_message(queue_name, peek_lock=False)
        print(msg.body)
    
    
    print 'Program finished.'
Exemplo n.º 21
0
class ServiceBusQueue(object):
    def __init__(self, namespace, access_key_name, access_key_value, q_name, q_max_size="5120", msg_ttl="PT1M"):
        self.bus_svc = ServiceBusService(
            service_namespace=namespace,
            shared_access_key_name=access_key_name,
            shared_access_key_value=access_key_value,
        )
        self.queue_options = Queue()
        self.queue_options.max_size_in_megabytes = q_max_size
        self.queue_options.default_message_time_to_live = msg_ttl

        self.queue_name = q_name
        self.bus_svc.create_queue(self.queue_name, self.queue_options)

    def send(self, msg):
        message = bytes(msg)
        message = Message(message)
        self.bus_svc.send_queue_message(self.queue_name, message)

    def receive(self):
        msg = self.bus_svc.receive_queue_message(self.queue_name, peek_lock=False)
        data = ast.literal_eval(msg.body)
        return data
Exemplo n.º 22
0
def watchAzureQueue():
        global Counter
        if AZURE_RECEIVING == False:
            return 

        bus_service = ServiceBusService(
            service_namespace='msgtestsb',
            shared_access_key_name='RootManageSharedAccessKey',
            shared_access_key_value='Ar9fUCZQdTL7cVWgerdNOB7sbQp0cWEeQyTRYUjKwpk=')
        queue_options = Queue()
        queue_options.max_size_in_megabytes = '5120'
        queue_options.default_message_time_to_live = 'PT96H'

        bus_service.create_queue('process_incoming', queue_options)
        bus_service.create_queue('whatsapp_sender', queue_options)
 
        while not isfile('/home/bitnami1/whatsapp/.stopwhatsapp') and Counter > 1:
            msg = bus_service.receive_queue_message('whatsapp_sender', peek_lock=False)
            if msg != None and msg.body:
                logging.info( '%s ' % datetime.now() +  msg.body)
                SendQueue.put(msg.body)
            else:
                logging.info( '%s ' % datetime.now() +  "Empty Azure Queue")
    	        time.sleep(4.6)
Exemplo n.º 23
0
Arquivo: az.py Projeto: dtstk/IoT_Acc
def main(argv):
    print 'Starting program...'

    internalDeviceId = argv[0]
    json_data = open('config.json')
    config_data = json.load(json_data)
    json_data.close()

    externalDeviceId = config_data["Devices"][internalDeviceId]
    queue_name = 'custom_' + config_data["Server"]["id"] + '_' + config_data[
        "Server"]["Deviceid"] + '_' + externalDeviceId
    print(queue_name)

    bus_service = ServiceBusService(
        service_namespace='rdciot',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='EXeZe7r49jCoDz79fESxtMdXwYU6iQwG1Gbo8J4HXyY=')
    print bus_service

    while True:
        msg = bus_service.receive_queue_message(queue_name, peek_lock=False)
        print(msg.body)

    print 'Program finished.'
Exemplo n.º 24
0
import pyodbc
from azure.servicebus import ServiceBusService

bus_service = ServiceBusService(
    service_namespace='queueoperativosServiceBus',
    shared_access_key_name='RootManageSharedAccessKey',
    shared_access_key_value='Nr4RASSWsVNWCM/8XBzAAUbTtpzT5h0lsYJsaS4iJjE=')

server = 'sistemasoperativos.database.windows.net'
database = 'sistemasOperativos'
username = '******'
password = '******'
driver = '{ODBC Driver 13 for SQL Server}'
conn = pyodbc.connect('DRIVER=' + driver + ';SERVER=' + server +
                      ';PORT=1443;DATABASE=' + database + ';UID=' + username +
                      ';PWD=' + password)
cursor = conn.cursor()

while True:
    msg = bus_service.receive_queue_message('queueoperativos', peek_lock=False)
    if msg.type == 'application/atom+xml;type=entry;charset=utf-8':
        msg = msg.body.decode()
        print('Operacion: ' + msg)
        cursor.execute(msg)
        cursor.commit()
    else:
        print('No hay mensajes en la cola')
Exemplo n.º 25
0
from azure.servicebus import ServiceBusService, Message, Queue

bus_service = ServiceBusService(
    service_namespace='maxqueue-ns',
    shared_access_key_name='MAXRULE1',
    shared_access_key_value='UBJelXoKJFsfTRnTA3c9jGWW6JXnp5Tc78cOZzdb0fI=')


msg = Message(b'Test Message')
bus_service.send_queue_message('MAXQUEUE', msg)

msg = bus_service.receive_queue_message('MAXQUEUE', peek_lock=False)
print(msg.body)
Exemplo n.º 26
0
    io.output(in_pin8, False)
    io.output(in_pin9, False)
    io.output(in_pin10, False)
    io.output(in_pin11, False)
    io.output(in_pin12, False)


bus_service = ServiceBusService(
    service_namespace='appcandy-ns',
    shared_access_key_name='MachineListen',
    shared_access_key_value='OLYoVII6DU85jRQ70F06aHhX3wJwoTq+LC7WiW0zO6Q=')

dog = 1
print("dog " + str(dog))
while (dog < 4):
    msg = bus_service.receive_queue_message('appcandy', peek_lock=False)
    print(msg.body)

    # get the number from the return message
    #number = str(msg.body[len(msg.body)-1])
    number = int(msg.body[-1:])
    print('number is: ' + str(number) + ' len: ' + str(len(msg.body)))
    #NOTE: we may need to do a different check for pins 10, 11, 12
    print('lets go')
    # set the correct pin high
    if number == 1:
        print("I would do trigger 1")
        trigger1()
    elif number == 2:
        trigger2()
    elif number == 3:
Exemplo n.º 27
0
class ServiceBusTest(AzureTestCase):
    def setUp(self):
        self.sbs = ServiceBusService(credentials.getServiceBusNamespace(),
                                     credentials.getServiceBusKey(), 'owner')

        proxy_host = credentials.getProxyHost()
        proxy_port = credentials.getProxyPort()
        if proxy_host:
            self.sbs.set_proxy(proxy_host, proxy_port)

        __uid = getUniqueTestRunID()

        queue_base_name = u'mytestqueue%s' % (__uid)
        topic_base_name = u'mytesttopic%s' % (__uid)

        self.queue_name = getUniqueNameBasedOnCurrentTime(queue_base_name)
        self.topic_name = getUniqueNameBasedOnCurrentTime(topic_base_name)

        self.additional_queue_names = []
        self.additional_topic_names = []

    def tearDown(self):
        self.cleanup()
        return super(ServiceBusTest, self).tearDown()

    def cleanup(self):
        try:
            self.sbs.delete_queue(self.queue_name)
        except:
            pass

        for name in self.additional_queue_names:
            try:
                self.sbs.delete_queue(name)
            except:
                pass

        try:
            self.sbs.delete_topic(self.topic_name)
        except:
            pass

        for name in self.additional_topic_names:
            try:
                self.sbs.delete_topic(name)
            except:
                pass

    #--Helpers-----------------------------------------------------------------
    def _create_queue(self, queue_name):
        self.sbs.create_queue(queue_name, None, True)

    def _create_queue_and_send_msg(self, queue_name, msg):
        self._create_queue(queue_name)
        self.sbs.send_queue_message(queue_name, msg)

    def _create_topic(self, topic_name):
        self.sbs.create_topic(topic_name, None, True)

    def _create_topic_and_subscription(self, topic_name, subscription_name):
        self._create_topic(topic_name)
        self._create_subscription(topic_name, subscription_name)

    def _create_subscription(self, topic_name, subscription_name):
        self.sbs.create_subscription(topic_name, subscription_name, None, True)

    #--Test cases for service bus service -------------------------------------
    def test_create_service_bus_missing_arguments(self):
        # Arrange
        if os.environ.has_key(AZURE_SERVICEBUS_NAMESPACE):
            del os.environ[AZURE_SERVICEBUS_NAMESPACE]
        if os.environ.has_key(AZURE_SERVICEBUS_ACCESS_KEY):
            del os.environ[AZURE_SERVICEBUS_ACCESS_KEY]
        if os.environ.has_key(AZURE_SERVICEBUS_ISSUER):
            del os.environ[AZURE_SERVICEBUS_ISSUER]

        # Act
        with self.assertRaises(WindowsAzureError):
            sbs = ServiceBusService()

        # Assert

    def test_create_service_bus_env_variables(self):
        # Arrange
        os.environ[
            AZURE_SERVICEBUS_NAMESPACE] = credentials.getServiceBusNamespace()
        os.environ[AZURE_SERVICEBUS_ACCESS_KEY] = credentials.getServiceBusKey(
        )
        os.environ[AZURE_SERVICEBUS_ISSUER] = 'owner'

        # Act
        sbs = ServiceBusService()

        if os.environ.has_key(AZURE_SERVICEBUS_NAMESPACE):
            del os.environ[AZURE_SERVICEBUS_NAMESPACE]
        if os.environ.has_key(AZURE_SERVICEBUS_ACCESS_KEY):
            del os.environ[AZURE_SERVICEBUS_ACCESS_KEY]
        if os.environ.has_key(AZURE_SERVICEBUS_ISSUER):
            del os.environ[AZURE_SERVICEBUS_ISSUER]

        # Assert
        self.assertIsNotNone(sbs)
        self.assertEquals(sbs.service_namespace,
                          credentials.getServiceBusNamespace())
        self.assertEquals(sbs.account_key, credentials.getServiceBusKey())
        self.assertEquals(sbs.issuer, 'owner')

    #--Test cases for queues --------------------------------------------------
    def test_create_queue_no_options(self):
        # Arrange

        # Act
        created = self.sbs.create_queue(self.queue_name)

        # Assert
        self.assertTrue(created)

    def test_create_queue_no_options_fail_on_exist(self):
        # Arrange

        # Act
        created = self.sbs.create_queue(self.queue_name, None, True)

        # Assert
        self.assertTrue(created)

    def test_create_queue_with_options(self):
        # Arrange

        # Act
        queue_options = Queue()
        queue_options.default_message_time_to_live = 'PT1M'
        queue_options.duplicate_detection_history_time_window = 'PT5M'
        queue_options.enable_batched_operations = False
        queue_options.dead_lettering_on_message_expiration = False
        queue_options.lock_duration = 'PT1M'
        queue_options.max_delivery_count = 15
        queue_options.max_size_in_megabytes = 5120
        queue_options.message_count = 0
        queue_options.requires_duplicate_detection = False
        queue_options.requires_session = False
        queue_options.size_in_bytes = 0
        created = self.sbs.create_queue(self.queue_name, queue_options)

        # Assert
        self.assertTrue(created)
        queue = self.sbs.get_queue(self.queue_name)
        self.assertEquals('PT1M', queue.default_message_time_to_live)
        self.assertEquals('PT5M',
                          queue.duplicate_detection_history_time_window)
        self.assertEquals(False, queue.enable_batched_operations)
        self.assertEquals(False, queue.dead_lettering_on_message_expiration)
        self.assertEquals('PT1M', queue.lock_duration)
        self.assertEquals(15, queue.max_delivery_count)
        self.assertEquals(5120, queue.max_size_in_megabytes)
        self.assertEquals(0, queue.message_count)
        self.assertEquals(False, queue.requires_duplicate_detection)
        self.assertEquals(False, queue.requires_session)
        self.assertEquals(0, queue.size_in_bytes)

    def test_create_queue_with_already_existing_queue(self):
        # Arrange

        # Act
        created1 = self.sbs.create_queue(self.queue_name)
        created2 = self.sbs.create_queue(self.queue_name)

        # Assert
        self.assertTrue(created1)
        self.assertFalse(created2)

    def test_create_queue_with_already_existing_queue_fail_on_exist(self):
        # Arrange

        # Act
        created = self.sbs.create_queue(self.queue_name)
        with self.assertRaises(WindowsAzureError):
            self.sbs.create_queue(self.queue_name, None, True)

        # Assert
        self.assertTrue(created)

    def test_get_queue_with_existing_queue(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        queue = self.sbs.get_queue(self.queue_name)

        # Assert
        self.assertIsNotNone(queue)
        self.assertEquals(queue.name, self.queue_name)

    def test_get_queue_with_non_existing_queue(self):
        # Arrange

        # Act
        with self.assertRaises(WindowsAzureError):
            resp = self.sbs.get_queue(self.queue_name)

        # Assert

    def test_list_queues(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        queues = self.sbs.list_queues()
        for queue in queues:
            name = queue.name

        # Assert
        self.assertIsNotNone(queues)
        self.assertNamedItemInContainer(queues, self.queue_name)

    def test_list_queues_with_special_chars(self):
        # Arrange
        # Name must start and end with an alphanumeric and can only contain
        # letters, numbers, periods, hyphens, forward slashes and underscores.
        other_queue_name = self.queue_name + 'foo/.-_123'
        self.additional_queue_names = [other_queue_name]
        self._create_queue(other_queue_name)

        # Act
        queues = self.sbs.list_queues()

        # Assert
        self.assertIsNotNone(queues)
        self.assertNamedItemInContainer(queues, other_queue_name)

    def test_delete_queue_with_existing_queue(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        deleted = self.sbs.delete_queue(self.queue_name)

        # Assert
        self.assertTrue(deleted)
        queues = self.sbs.list_queues()
        self.assertNamedItemNotInContainer(queues, self.queue_name)

    def test_delete_queue_with_existing_queue_fail_not_exist(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        deleted = self.sbs.delete_queue(self.queue_name, True)

        # Assert
        self.assertTrue(deleted)
        queues = self.sbs.list_queues()
        self.assertNamedItemNotInContainer(queues, self.queue_name)

    def test_delete_queue_with_non_existing_queue(self):
        # Arrange

        # Act
        deleted = self.sbs.delete_queue(self.queue_name)

        # Assert
        self.assertFalse(deleted)

    def test_delete_queue_with_non_existing_queue_fail_not_exist(self):
        # Arrange

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.delete_queue(self.queue_name, True)

        # Assert

    def test_send_queue_message(self):
        # Arrange
        self._create_queue(self.queue_name)
        sent_msg = Message('send message')

        # Act
        self.sbs.send_queue_message(self.queue_name, sent_msg)

        # Assert

    def test_receive_queue_message_read_delete_mode(self):
        # Assert
        sent_msg = Message('receive message')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_queue_message_read_delete_mode_throws_on_delete(self):
        # Assert
        sent_msg = Message('receive message')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)
        with self.assertRaises(WindowsAzureError):
            received_msg.delete()

        # Assert

    def test_receive_queue_message_read_delete_mode_throws_on_unlock(self):
        # Assert
        sent_msg = Message('receive message')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)
        with self.assertRaises(WindowsAzureError):
            received_msg.unlock()

        # Assert

    def test_receive_queue_message_peek_lock_mode(self):
        # Arrange
        sent_msg = Message('peek lock message')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, True)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_queue_message_delete(self):
        # Arrange
        sent_msg = Message('peek lock message delete')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, True)
        received_msg.delete()

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_queue_message_unlock(self):
        # Arrange
        sent_msg = Message('peek lock message unlock')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, True)
        received_msg.unlock()

        # Assert
        received_again_msg = self.sbs.receive_queue_message(
            self.queue_name, True)
        received_again_msg.delete()
        self.assertIsNotNone(received_msg)
        self.assertIsNotNone(received_again_msg)
        self.assertEquals(sent_msg.body, received_msg.body)
        self.assertEquals(received_again_msg.body, received_msg.body)

    def test_send_queue_message_with_custom_message_type(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        sent_msg = Message(
            '<text>peek lock message custom message type</text>',
            type='text/xml')
        self.sbs.send_queue_message(self.queue_name, sent_msg)
        received_msg = self.sbs.receive_queue_message(self.queue_name, True, 5)
        received_msg.delete()

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals('text/xml', received_msg.type)

    def test_send_queue_message_with_custom_message_properties(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        props = {
            'hello': 'world',
            'foo': 42,
            'active': True,
            'deceased': False,
            'large': 8555111000,
            'floating': 3.14,
            'dob': datetime(2011, 12, 14)
        }
        sent_msg = Message('message with properties', custom_properties=props)
        self.sbs.send_queue_message(self.queue_name, sent_msg)
        received_msg = self.sbs.receive_queue_message(self.queue_name, True, 5)
        received_msg.delete()

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(received_msg.custom_properties['hello'], 'world')
        self.assertEquals(received_msg.custom_properties['foo'], 42)
        self.assertEquals(received_msg.custom_properties['active'], True)
        self.assertEquals(received_msg.custom_properties['deceased'], False)
        self.assertEquals(received_msg.custom_properties['large'], 8555111000)
        self.assertEquals(received_msg.custom_properties['floating'], 3.14)
        self.assertEquals(received_msg.custom_properties['dob'],
                          datetime(2011, 12, 14))

    def test_receive_queue_message_timeout_5(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        start = time.clock()
        received_msg = self.sbs.receive_queue_message(self.queue_name, True, 5)
        duration = time.clock() - start

        # Assert
        self.assertTrue(duration > 3 and duration < 7)
        self.assertIsNotNone(received_msg)
        self.assertIsNone(received_msg.body)

    def test_receive_queue_message_timeout_50(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        start = time.clock()
        received_msg = self.sbs.receive_queue_message(self.queue_name, True,
                                                      50)
        duration = time.clock() - start

        # Assert
        self.assertTrue(duration > 48 and duration < 52)
        self.assertIsNotNone(received_msg)
        self.assertIsNone(received_msg.body)

    #--Test cases for topics/subscriptions ------------------------------------
    def test_create_topic_no_options(self):
        # Arrange

        # Act
        created = self.sbs.create_topic(self.topic_name)

        # Assert
        self.assertTrue(created)

    def test_create_topic_no_options_fail_on_exist(self):
        # Arrange

        # Act
        created = self.sbs.create_topic(self.topic_name, None, True)

        # Assert
        self.assertTrue(created)

    def test_create_topic_with_options(self):
        # Arrange

        # Act
        topic_options = Topic()
        topic_options.default_message_time_to_live = 'PT1M'
        topic_options.duplicate_detection_history_time_window = 'PT5M'
        topic_options.enable_batched_operations = False
        topic_options.max_size_in_megabytes = 5120
        topic_options.requires_duplicate_detection = False
        topic_options.size_in_bytes = 0
        #TODO: MaximumNumberOfSubscriptions is not supported?
        created = self.sbs.create_topic(self.topic_name, topic_options)

        # Assert
        self.assertTrue(created)
        topic = self.sbs.get_topic(self.topic_name)
        self.assertEquals('PT1M', topic.default_message_time_to_live)
        self.assertEquals('PT5M',
                          topic.duplicate_detection_history_time_window)
        self.assertEquals(False, topic.enable_batched_operations)
        self.assertEquals(5120, topic.max_size_in_megabytes)
        self.assertEquals(False, topic.requires_duplicate_detection)
        self.assertEquals(0, topic.size_in_bytes)

    def test_create_topic_with_already_existing_topic(self):
        # Arrange

        # Act
        created1 = self.sbs.create_topic(self.topic_name)
        created2 = self.sbs.create_topic(self.topic_name)

        # Assert
        self.assertTrue(created1)
        self.assertFalse(created2)

    def test_create_topic_with_already_existing_topic_fail_on_exist(self):
        # Arrange

        # Act
        created = self.sbs.create_topic(self.topic_name)
        with self.assertRaises(WindowsAzureError):
            self.sbs.create_topic(self.topic_name, None, True)

        # Assert
        self.assertTrue(created)

    def test_topic_backwards_compatibility_warning(self):
        # Arrange
        topic_options = Topic()
        topic_options.max_size_in_megabytes = 5120

        # Act
        val = topic_options.max_size_in_mega_bytes

        # Assert
        self.assertEqual(val, 5120)

        # Act
        topic_options.max_size_in_mega_bytes = 1024

        # Assert
        self.assertEqual(topic_options.max_size_in_megabytes, 1024)

    def test_get_topic_with_existing_topic(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        topic = self.sbs.get_topic(self.topic_name)

        # Assert
        self.assertIsNotNone(topic)
        self.assertEquals(topic.name, self.topic_name)

    def test_get_topic_with_non_existing_topic(self):
        # Arrange

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.get_topic(self.topic_name)

        # Assert

    def test_list_topics(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        topics = self.sbs.list_topics()
        for topic in topics:
            name = topic.name

        # Assert
        self.assertIsNotNone(topics)
        self.assertNamedItemInContainer(topics, self.topic_name)

    def test_list_topics_with_special_chars(self):
        # Arrange
        # Name must start and end with an alphanumeric and can only contain
        # letters, numbers, periods, hyphens, forward slashes and underscores.
        other_topic_name = self.topic_name + 'foo/.-_123'
        self.additional_topic_names = [other_topic_name]
        self._create_topic(other_topic_name)

        # Act
        topics = self.sbs.list_topics()

        # Assert
        self.assertIsNotNone(topics)
        self.assertNamedItemInContainer(topics, other_topic_name)

    def test_delete_topic_with_existing_topic(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        deleted = self.sbs.delete_topic(self.topic_name)

        # Assert
        self.assertTrue(deleted)
        topics = self.sbs.list_topics()
        self.assertNamedItemNotInContainer(topics, self.topic_name)

    def test_delete_topic_with_existing_topic_fail_not_exist(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        deleted = self.sbs.delete_topic(self.topic_name, True)

        # Assert
        self.assertTrue(deleted)
        topics = self.sbs.list_topics()
        self.assertNamedItemNotInContainer(topics, self.topic_name)

    def test_delete_topic_with_non_existing_topic(self):
        # Arrange

        # Act
        deleted = self.sbs.delete_topic(self.topic_name)

        # Assert
        self.assertFalse(deleted)

    def test_delete_topic_with_non_existing_topic_fail_not_exist(self):
        # Arrange

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.delete_topic(self.topic_name, True)

        # Assert

    def test_create_subscription(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        created = self.sbs.create_subscription(self.topic_name,
                                               'MySubscription')

        # Assert
        self.assertTrue(created)

    def test_create_subscription_with_options(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        subscription_options = Subscription()
        subscription_options.dead_lettering_on_filter_evaluation_exceptions = False
        subscription_options.dead_lettering_on_message_expiration = False
        subscription_options.default_message_time_to_live = 'PT15M'
        subscription_options.enable_batched_operations = False
        subscription_options.lock_duration = 'PT1M'
        subscription_options.max_delivery_count = 15
        #message_count is read-only
        subscription_options.message_count = 0
        subscription_options.requires_session = False
        created = self.sbs.create_subscription(self.topic_name,
                                               'MySubscription',
                                               subscription_options)

        # Assert
        self.assertTrue(created)
        subscription = self.sbs.get_subscription(self.topic_name,
                                                 'MySubscription')
        self.assertEquals(
            False, subscription.dead_lettering_on_filter_evaluation_exceptions)
        self.assertEquals(False,
                          subscription.dead_lettering_on_message_expiration)
        self.assertEquals('PT15M', subscription.default_message_time_to_live)
        self.assertEquals(False, subscription.enable_batched_operations)
        self.assertEquals('PT1M', subscription.lock_duration)
        #self.assertEquals(15, subscription.max_delivery_count) #no idea why max_delivery_count is always 10
        self.assertEquals(0, subscription.message_count)
        self.assertEquals(False, subscription.requires_session)

    def test_create_subscription_fail_on_exist(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        created = self.sbs.create_subscription(self.topic_name,
                                               'MySubscription', None, True)

        # Assert
        self.assertTrue(created)

    def test_create_subscription_with_already_existing_subscription(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        created1 = self.sbs.create_subscription(self.topic_name,
                                                'MySubscription')
        created2 = self.sbs.create_subscription(self.topic_name,
                                                'MySubscription')

        # Assert
        self.assertTrue(created1)
        self.assertFalse(created2)

    def test_create_subscription_with_already_existing_subscription_fail_on_exist(
            self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        created = self.sbs.create_subscription(self.topic_name,
                                               'MySubscription')
        with self.assertRaises(WindowsAzureError):
            self.sbs.create_subscription(self.topic_name, 'MySubscription',
                                         None, True)

        # Assert
        self.assertTrue(created)

    def test_list_subscriptions(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription2')

        # Act
        subscriptions = self.sbs.list_subscriptions(self.topic_name)

        # Assert
        self.assertIsNotNone(subscriptions)
        self.assertEquals(len(subscriptions), 1)
        self.assertEquals(subscriptions[0].name, 'MySubscription2')

    def test_get_subscription_with_existing_subscription(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription3')

        # Act
        subscription = self.sbs.get_subscription(self.topic_name,
                                                 'MySubscription3')

        # Assert
        self.assertIsNotNone(subscription)
        self.assertEquals(subscription.name, 'MySubscription3')

    def test_get_subscription_with_non_existing_subscription(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription3')

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.get_subscription(self.topic_name, 'MySubscription4')

        # Assert

    def test_delete_subscription_with_existing_subscription(self):
        # Arrange
        self._create_topic(self.topic_name)
        self._create_subscription(self.topic_name, 'MySubscription4')
        self._create_subscription(self.topic_name, 'MySubscription5')

        # Act
        deleted = self.sbs.delete_subscription(self.topic_name,
                                               'MySubscription4')

        # Assert
        self.assertTrue(deleted)
        subscriptions = self.sbs.list_subscriptions(self.topic_name)
        self.assertIsNotNone(subscriptions)
        self.assertEquals(len(subscriptions), 1)
        self.assertEquals(subscriptions[0].name, 'MySubscription5')

    def test_delete_subscription_with_existing_subscription_fail_not_exist(
            self):
        # Arrange
        self._create_topic(self.topic_name)
        self._create_subscription(self.topic_name, 'MySubscription4')
        self._create_subscription(self.topic_name, 'MySubscription5')

        # Act
        deleted = self.sbs.delete_subscription(self.topic_name,
                                               'MySubscription4', True)

        # Assert
        self.assertTrue(deleted)
        subscriptions = self.sbs.list_subscriptions(self.topic_name)
        self.assertIsNotNone(subscriptions)
        self.assertEquals(len(subscriptions), 1)
        self.assertEquals(subscriptions[0].name, 'MySubscription5')

    def test_delete_subscription_with_non_existing_subscription(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        deleted = self.sbs.delete_subscription(self.topic_name,
                                               'MySubscription')

        # Assert
        self.assertFalse(deleted)

    def test_delete_subscription_with_non_existing_subscription_fail_not_exist(
            self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.delete_subscription(self.topic_name, 'MySubscription',
                                         True)

        # Assert

    def test_create_rule_no_options(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1')

        # Assert
        self.assertTrue(created)

    def test_create_rule_no_options_fail_on_exist(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1', None, True)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_already_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        created1 = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                        'MyRule1')
        created2 = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                        'MyRule1')

        # Assert
        self.assertTrue(created1)
        self.assertFalse(created2)

    def test_create_rule_with_already_existing_rule_fail_on_exist(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1')
        with self.assertRaises(WindowsAzureError):
            self.sbs.create_rule(self.topic_name, 'MySubscription', 'MyRule1',
                                 None, True)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_sql_filter(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.filter_type = 'SqlFilter'
        rule1.filter_expression = 'foo > 40'
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_true_filter(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.filter_type = 'TrueFilter'
        rule1.filter_expression = '1=1'
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_false_filter(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.filter_type = 'FalseFilter'
        rule1.filter_expression = '1=0'
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_correlation_filter(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.filter_type = 'CorrelationFilter'
        rule1.filter_expression = 'myid'
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_empty_rule_action(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.action_type = 'EmptyRuleAction'
        rule1.action_expression = ''
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_sql_rule_action(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.action_type = 'SqlRuleAction'
        rule1.action_expression = "SET foo = 5"
        created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                       'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_list_rules(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        resp = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                    'MyRule2')

        # Act
        rules = self.sbs.list_rules(self.topic_name, 'MySubscription')

        # Assert
        self.assertEquals(len(rules), 2)

    def test_get_rule_with_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule = self.sbs.get_rule(self.topic_name, 'MySubscription', '$Default')

        # Assert
        self.assertIsNotNone(rule)
        self.assertEquals(rule.name, '$Default')

    def test_get_rule_with_non_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.get_rule(self.topic_name, 'MySubscription',
                              'NonExistingRule')

        # Assert

    def test_get_rule_with_existing_rule_with_options(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_rule = Rule()
        sent_rule.filter_type = 'SqlFilter'
        sent_rule.filter_expression = 'foo > 40'
        sent_rule.action_type = 'SqlRuleAction'
        sent_rule.action_expression = 'SET foo = 5'
        self.sbs.create_rule(self.topic_name, 'MySubscription', 'MyRule1',
                             sent_rule)

        # Act
        received_rule = self.sbs.get_rule(self.topic_name, 'MySubscription',
                                          'MyRule1')

        # Assert
        self.assertIsNotNone(received_rule)
        self.assertEquals(received_rule.name, 'MyRule1')
        self.assertEquals(received_rule.filter_type, sent_rule.filter_type)
        self.assertEquals(received_rule.filter_expression,
                          sent_rule.filter_expression)
        self.assertEquals(received_rule.action_type, sent_rule.action_type)
        self.assertEquals(received_rule.action_expression,
                          sent_rule.action_expression)

    def test_delete_rule_with_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        resp = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                    'MyRule3')
        resp = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                    'MyRule4')

        # Act
        deleted1 = self.sbs.delete_rule(self.topic_name, 'MySubscription',
                                        'MyRule4')
        deleted2 = self.sbs.delete_rule(self.topic_name, 'MySubscription',
                                        '$Default')

        # Assert
        self.assertTrue(deleted1)
        self.assertTrue(deleted2)
        rules = self.sbs.list_rules(self.topic_name, 'MySubscription')
        self.assertIsNotNone(rules)
        self.assertEquals(len(rules), 1)
        self.assertEquals(rules[0].name, 'MyRule3')

    def test_delete_rule_with_existing_rule_fail_not_exist(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        resp = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                    'MyRule3')
        resp = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                    'MyRule4')

        # Act
        deleted1 = self.sbs.delete_rule(self.topic_name, 'MySubscription',
                                        'MyRule4', True)
        deleted2 = self.sbs.delete_rule(self.topic_name, 'MySubscription',
                                        '$Default', True)

        # Assert
        self.assertTrue(deleted1)
        self.assertTrue(deleted2)
        rules = self.sbs.list_rules(self.topic_name, 'MySubscription')
        self.assertIsNotNone(rules)
        self.assertEquals(len(rules), 1)
        self.assertEquals(rules[0].name, 'MyRule3')

    def test_delete_rule_with_non_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        deleted = self.sbs.delete_rule(self.topic_name, 'MySubscription',
                                       'NonExistingRule')

        # Assert
        self.assertFalse(deleted)

    def test_delete_rule_with_non_existing_rule_fail_not_exist(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.delete_rule(self.topic_name, 'MySubscription',
                                 'NonExistingRule', True)

        # Assert

    def test_send_topic_message(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')

        # Act
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Assert

    def test_receive_subscription_message_read_delete_mode(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_subscription_message_read_delete_mode_throws_on_delete(
            self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)
        with self.assertRaises(WindowsAzureError):
            received_msg.delete()

        # Assert

    def test_receive_subscription_message_read_delete_mode_throws_on_unlock(
            self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)
        with self.assertRaises(WindowsAzureError):
            received_msg.unlock()

        # Assert

    def test_receive_subscription_message_peek_lock_mode(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', True, 5)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_subscription_message_delete(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', True, 5)
        received_msg.delete()

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_subscription_message_unlock(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', True)
        received_msg.unlock()

        # Assert
        received_again_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', True)
        received_again_msg.delete()
        self.assertIsNotNone(received_msg)
        self.assertIsNotNone(received_again_msg)
        self.assertEquals(sent_msg.body, received_msg.body)
        self.assertEquals(received_again_msg.body, received_msg.body)

    def test_with_filter(self):
        # Single filter
        called = []

        def my_filter(request, next):
            called.append(True)
            return next(request)

        sbs = self.sbs.with_filter(my_filter)
        sbs.create_topic(self.topic_name + '0', None, True)

        self.assertTrue(called)

        del called[:]

        sbs.delete_topic(self.topic_name + '0')

        self.assertTrue(called)
        del called[:]

        # Chained filters
        def filter_a(request, next):
            called.append('a')
            return next(request)

        def filter_b(request, next):
            called.append('b')
            return next(request)

        sbs = self.sbs.with_filter(filter_a).with_filter(filter_b)
        sbs.create_topic(self.topic_name + '0', None, True)

        self.assertEqual(called, ['b', 'a'])

        sbs.delete_topic(self.topic_name + '0')

        self.assertEqual(called, ['b', 'a', 'b', 'a'])

    def test_two_identities(self):
        # In order to run this test, 2 service bus service identities are created using
        # the sbaztool available at:
        # http://code.msdn.microsoft.com/windowsazure/Authorization-SBAzTool-6fd76d93
        #
        # Use the following commands to create 2 identities and grant access rights.
        # Replace <servicebusnamespace> with the namespace specified in the test .json file
        # Replace <servicebuskey> with the key specified in the test .json file
        # This only needs to be executed once, after the service bus namespace is created.
        #
        # sbaztool makeid user1 NoHEoD6snlvlhZm7yek9Etxca3l0CYjfc19ICIJZoUg= -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Send /path1 user1 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Listen /path1 user1 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Manage /path1 user1 -n <servicebusnamespace> -k <servicebuskey>

        # sbaztool makeid user2 Tb6K5qEgstyRBwp86JEjUezKj/a+fnkLFnibfgvxvdg= -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Send /path2 user2 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Listen /path2 user2 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Manage /path2 user2 -n <servicebusnamespace> -k <servicebuskey>

        sbs1 = ServiceBusService(
            credentials.getServiceBusNamespace(),
            'NoHEoD6snlvlhZm7yek9Etxca3l0CYjfc19ICIJZoUg=', 'user1')
        sbs2 = ServiceBusService(
            credentials.getServiceBusNamespace(),
            'Tb6K5qEgstyRBwp86JEjUezKj/a+fnkLFnibfgvxvdg=', 'user2')

        queue1_name = 'path1/queue' + str(random.randint(1, 10000000))
        queue2_name = 'path2/queue' + str(random.randint(1, 10000000))

        try:
            # Create queues, success
            sbs1.create_queue(queue1_name)
            sbs2.create_queue(queue2_name)

            # Receive messages, success
            msg = sbs1.receive_queue_message(queue1_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs1.receive_queue_message(queue1_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs2.receive_queue_message(queue2_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs2.receive_queue_message(queue2_name, True, 1)
            self.assertIsNone(msg.body)

            # Receive messages, failure
            with self.assertRaises(HTTPError):
                msg = sbs1.receive_queue_message(queue2_name, True, 1)
            with self.assertRaises(HTTPError):
                msg = sbs2.receive_queue_message(queue1_name, True, 1)
        finally:
            try:
                sbs1.delete_queue(queue1_name)
            except:
                pass
            try:
                sbs2.delete_queue(queue2_name)
            except:
                pass

    def test_unicode_create_queue_unicode_name(self):
        # Arrange
        self.queue_name = self.queue_name + u'啊齄丂狛狜'

        # Act
        with self.assertRaises(WindowsAzureError):
            created = self.sbs.create_queue(self.queue_name)

        # Assert

    def test_unicode_receive_queue_message_unicode_data(self):
        # Assert
        sent_msg = Message('receive message啊齄丂狛狜')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_unicode_receive_queue_message_binary_data(self):
        # Assert
        base64_data = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/wABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=='
        binary_data = base64.b64decode(base64_data)
        sent_msg = Message(binary_data)
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_unicode_create_subscription_unicode_name(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        with self.assertRaises(WindowsAzureError):
            created = self.sbs.create_subscription(self.topic_name,
                                                   u'MySubscription啊齄丂狛狜')

        # Assert

    def test_unicode_create_rule_unicode_name(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        with self.assertRaises(WindowsAzureError):
            created = self.sbs.create_rule(self.topic_name, 'MySubscription',
                                           'MyRule啊齄丂狛狜')

        # Assert

    def test_unicode_receive_subscription_message_unicode_data(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message啊齄丂狛狜')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_unicode_receive_subscription_message_binary_data(self):
        # Arrange
        base64_data = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/wABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=='
        binary_data = base64.b64decode(base64_data)
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message(binary_data)
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)
        im = Image.open(output_stream)
        im.thumbnail(size)
        im.save(input_stream, 'JPEG')

        filename = splitext(basename(ad.imageURL))[0] + "thumb.jpg"
        block_blob_service.create_blob_from_bytes('images', filename,
                                                  input_stream.getvalue())

        ad.thumbnailURL = 'https://' + STORAGE_ACCOUNT_NAME + '.blob.core.windows.net/images/' + filename
        print(ad.thumbnailURL)

        ad.save()
    except Exception as e:
        print(e)


if __name__ == '__main__':
    while True:
        try:
            msg = bus_service.receive_queue_message('adqueue',
                                                    peek_lock=False,
                                                    timeout=20)
        except Exception as e:
            print(e)

        if msg.body:
            ProcessQueueMessage(msg)
        else:
            sleep(1.0)
from azure.servicebus import ServiceBusService


service_namespace = 'dev-montavo'
account_key = '3me4sCOHDWBK8PkjkznLnFkksQ0H/aWL3FU/JlzF5d0='
sbs = ServiceBusService(service_namespace, account_key, 'owner')
[q.name for q in sbs.list_queues()]
q = sbs.list_queues()[0]
m = sbs.receive_queue_message(q.name)
m.body
m.custom_properties
Exemplo n.º 30
0
import os
import sys
from azure.servicebus import ServiceBusService, Message

# Create the QueueClient
sbs = ServiceBusService(
    "magicButtonMB",
    shared_access_key_name="RootManageSharedAccessKey",
    shared_access_key_value="SNZewWOTdOkaVShECrp+OCKmSFqW5JCEjEM52slp5TM=")

# Receive the message from the queue
msg = (sbs.receive_queue_message(sys.argv[1],
                                 peek_lock=False)).body.decode("utf-8")

#save answer in file
tmpFile = open("./data.txt", "w")
tmpFile.write(msg)
tmpFile.close()
Exemplo n.º 31
0
    io.output(in_pin7,  False)
    io.output(in_pin8,  False)
    io.output(in_pin9,  False)
    io.output(in_pin10, False)
    io.output(in_pin11, False)
    io.output(in_pin12, False)
    
bus_service = ServiceBusService(
    service_namespace='appcandy-ns',
    shared_access_key_name='MachineListen',
    shared_access_key_value='OLYoVII6DU85jRQ70F06aHhX3wJwoTq+LC7WiW0zO6Q=')

dog = 1
print("dog " + str(dog))
while (dog < 4) :
    msg = bus_service.receive_queue_message('appcandy', peek_lock=False)
    print(msg.body)

    # get the number from the return message
    #number = str(msg.body[len(msg.body)-1])
    number = int(msg.body[-1:])
    print ('number is: ' + str(number) + ' len: ' + str(len(msg.body)) )
    #NOTE: we may need to do a different check for pins 10, 11, 12
    print ('lets go')
    # set the correct pin high
    if number == 1  :
        print ("I would do trigger 1")
        trigger1()
    elif number == 2  :
        trigger2()
    elif number == 3  :
Exemplo n.º 32
0
GPIO.setup(18,GPIO.OUT)
GPIO.setup(17,GPIO.OUT)

failedBuilds = []

status = ""

if sys.argv[1] != "off":
    try:
        while True:
            bus_service = ServiceBusService(
                service_namespace='',
                shared_access_key_name='',
                shared_access_key_value='')
            
            msg = bus_service.receive_queue_message('buildFailures', peek_lock=True)
            
            if msg.body != None:
                jsonString = json.loads(msg.body.decode("utf-8"))
                status = jsonString["resource"]["status"]
                defID = jsonString["resource"]["definition"]["id"]
                defName = jsonString["resource"]["definition"]["name"]
                print("==============================")
                print("Definition Name: " + defName)
                print("Definition ID: " + str(defID))
                print("Status: " + status)
                if len(failedBuilds) > 0:
                    for build in failedBuilds:
                        if build == defID:
                            failedBuilds.remove(build)
                if status == "failed":
Exemplo n.º 33
0
        print('wget failed')
        outfile = None
    os.remove(infile)
    return outfile


def handle_output(outputname):
    block_blob_service = BlockBlobService(account_name=config.storage_acc_name,
                                          account_key=config.storage_acc_key)
    block_blob_service.create_blob_from_path(config.storage_container,
                                             outputname, outputname)


if __name__ == "__main__":
    bus_service = ServiceBusService(service_namespace=config.sb_name,
                                    shared_access_key_name=config.sb_key_name,
                                    shared_access_key_value=config.sb_key_val)
    while True:
        msg = bus_service.receive_queue_message(config.q_name,
                                                peek_lock=False,
                                                timeout=60)
        if msg.body:
            print('got new messgae: ' + msg.body)
            process_params = json.loads(msg.body)
            filename = get_image(process_params['url'])
            processed = process(filename)
            handle_output(processed)
            bus_service.send_topic_message(config.topic_name,
                                           Message(json.dumps(process_params)))
        else:
            print('no new msg in queue')
Exemplo n.º 34
0
import string
import sys
import serial
import time
import os
import subprocess
from azure.servicebus import ServiceBusService, Message, Queue
# PUERTO_ARDUINO debe contener el valor de puerto serial que estas usando para tu Arduino
PUERTO_ARDUINO = 'COM3'
# BAUDIOS_ARDUINO debe contener los baudios que definiste en la apertura del serial
BAUDIOS_ARDUINO = 115200

bus_service = ServiceBusService(
    service_namespace='ti01usmp',
    shared_access_key_name='RootManageSharedAccessKey',
    shared_access_key_value='QN+1Qq1rsmmqTZj8ueQSGkaMYrsI9dACZI/fkT04OTw=')

arduino = serial.Serial(PUERTO_ARDUINO, BAUDIOS_ARDUINO, timeout=.1)
time.sleep(2)
subprocess.Popen(
    ['C:\Program Files (x86)\Microsoft\Skype for Desktop\Skype.exe'])

while True:
    msg = bus_service.receive_queue_message('instruccion', peek_lock=False)
    if msg.body is not None:
        arduino.write(msg.body)
        raw = arduino.readline()
        print(raw)
Exemplo n.º 35
0
    creds = get_servicebus_credentials()
    namespace = creds['Endpoint'].split('.')[0][5:]
    sak_name = creds['SharedAccessKeyName']
    sak_value = creds['SharedAccessKey']
    bus_service = ServiceBusService(
        service_namespace=namespace,
        shared_access_key_name=sak_name,
        shared_access_key_value=sak_value
    )

    def window_repr(data):
        r = ', '.join([t.get('value') for t in data.get('collect')])
        return '[' + r + ']'

    while True:
        msg = bus_service.receive_queue_message(Q_NAME, peek_lock=False)

        if not msg.body:
            continue

        start = msg.body.find('{')
        end = msg.body.rfind('}')
        body = msg.body[start : end + 1]
        try:
            body = json.loads(body)
            window_end = body.get('ts')
            body = window_repr(body)
            print body, window_end
        except ValueError:
            print body
Exemplo n.º 36
0
from azure.servicebus import ServiceBusService, Message, Queue

namespace = "nsgtrnamespace"
endpoint = "sb://" + namespace + ".servicebus.windows.net/"
keyName = "all"
key = "KbuVe06trwIaVPeYYg/F+WfmojIi+pdWe4SrD5BBczY="
queueName = "nsgtrqueue"

bus_service = ServiceBusService(service_namespace=namespace,
                                shared_access_key_name=keyName,
                                shared_access_key_value=key)

sentMsg = Message(b'hi')
bus_service.send_queue_message(queueName, sentMsg)
receivedMsg = bus_service.receive_queue_message(queueName, peek_lock=False)
print(receivedMsg.body)

#print(bus_service.receive_queue_message(queueName, peek_lock=False))
Exemplo n.º 37
0
from azure.servicebus import ServiceBusService

key_name = 'RootManageSharedAccessKey'  # SharedAccessKeyName from Azure portal
key_value = 'zUCBEyvkIml8p7ClqSibF4yLkkNwWU2dyfotP0vcMTY='  # SharedAccessKey from Azure portal

sbs = ServiceBusService('ArrowTest',
                        shared_access_key_name=key_name,
                        shared_access_key_value=key_value)

msg = sbs.receive_queue_message('inbound_encounter')

print(msg)
Exemplo n.º 38
0
from azure.servicebus import ServiceBusService, Message, Queue
bus_read_service = ServiceBusService(
    service_namespace='<enter sb namespace>',
    shared_access_key_name='<enter name of sb shared access key>',
    shared_access_key_value='<enter value for sb shared access key>')
msg = bus_read_service.receive_queue_message('<enter name of sb queue>',
                                             peek_lock=False)
print(msg.body)
Exemplo n.º 39
0
class AzureConnector():

    def __init__(self, config):

        tree = ET.parse('SharedConfig.xml')
        self.myMachineName = tree.find('.//Instance').get("id")

        self.sms = ServiceManagementService(
            subscription_id=config.get("azure", "subscription_id"),
            cert_file=config.get("azure", "cert_file")
        );

        self.bus_service = ServiceBusService(
            service_namespace=config.get("azure", "bus_namespace"),
            shared_access_key_name=config.get("azure", "bus_shared_access_key_name"),
            shared_access_key_value=config.get("azure", "bus_shared_access_key_value"))

        self.command_queue = config.get("azure", "commandQueuePath")
        for tries in range(1,10):
            try:
                self.bus_service.create_queue(self.command_queue)
                break
            except:
                print "Esperando"
            
        self.status_topic = config.get("azure", "statusTopicPath")
        self.bus_service.create_queue(self.status_topic)

        self.storage = BlobService(account_name=config.get("azure", "account_name"),
                                   account_key=config.get("azure", "account_key"))

        self.algo_storage_name = config.get("azure", "algorithm_storage_name")
        self.storage.create_container(self.algo_storage_name, fail_on_exist=False)

        self.proj_storage_name = config.get("azure", "project_storage_name")
        self.storage.create_container(self.proj_storage_name, fail_on_exist=False)

    def check_new_tasks(self):

        for tries in range(1,2):
            try:
                message = self.bus_service.receive_queue_message(self.command_queue, peek_lock=False, timeout=60)
                break
            except:
                message = None

        if message is None or message.body is None:
            return None

        job_description = json.loads(message.body.replace('/AzureBlobStorage/', ''))

        command = CommandMetadata(
            command_id = job_description["command_id"],
            algorithm_directory = job_description["algorithm_prfx"],
            project_prfx = job_description["project_prfx"],
            project_input_files = job_description["project_input_files"],
            algorithm_executable_name = job_description["algorithm_executable_name"],
            algorithm_parameters = job_description["algorithm_parameters"],
            sent_timestamp = datetime.datetime.strptime(job_description["sent_timestamp"], "%d/%m/%Y %H:%M:%S"),
            machine_size=job_description["machine_size"])

        # Retornar dados sobre o comando consumido da fila
        return command

        # Não há nada na fila
        return None

    def list_algo_files(self, prfx):

        list = self.storage.list_blobs(container_name=self.algo_storage_name, prefix=prfx)
        result = []
        for blob in list:
            result.append(blob.name)
        return result

    def download_algo_zip(self, algorithm_bin_file, tmp_file):
        print "download_algo_zip(algorithm_bin_file="+algorithm_bin_file+", tmp_file="+tmp_file+")"
        for tries in range(1,5):
            try:
                self.storage.get_blob_to_path(self.algo_storage_name, algorithm_bin_file, tmp_file,
                                 open_mode='wb', snapshot=None, x_ms_lease_id=None,
                                 progress_callback=None)
                break

            except Exception as e:

                if tries == 5:
                    print("Muitos erros de conexão. Operação abortada.")
                else:
                    print("Erro de conexão com serviço. Retentando..." + e.__str__())

    def download_file_to_project(self, project_name, blob_name, dir):
        print "download_file_to_project(project_name="+project_name+", blob_name="+blob_name+", dir="+dir+")"
        for tries in range(1,5):
            try:
                self.storage.get_blob_to_path(self.proj_storage_name,
                                              os.path.join(project_name,blob_name),
                                              os.path.join(dir,os.path.join(project_name,blob_name)),
                                              open_mode='wb', snapshot=None, x_ms_lease_id=None,
                                              progress_callback=None)
                break

            except Exception as e:

                if tries == 5:
                    print("Muitos erros de conexão. Operação abortada.")
                else:
                    print("Erro de conexão com serviço. Retentando..." + e.__str__())

    def download_file_to_project(self, project_name, blob_name, dir):
        print "download_file_to_project(project_name="+project_name+", blob_name="+blob_name+", dir="+dir+")"
        for tries in range(1,5):
            try:
                self.storage.get_blob_to_path(self.proj_storage_name,
                                              os.path.join(project_name,blob_name),
                                              os.path.join(dir,os.path.join(project_name,blob_name)),
                                              open_mode='wb', snapshot=None, x_ms_lease_id=None,
                                              progress_callback=None)
                break

            except Exception as e:

                if tries == 5:
                    print("Muitos erros de conexão. Operação abortada.")
                else:
                    print("Erro de conexão com serviço. Retentando..." + e.__str__())

    def upload_proj_file(self, project_name, blob_name, dir):
        print "upload_proj_file(project_name="+project_name+", blob_name="+blob_name+", dir="+dir+")"
        if blob_name[0] == '/':
            blob_name = blob_name[1:]
        for tries in range(1,5):
            try:
                self.storage.put_block_blob_from_path(self.proj_storage_name,
                                              os.path.join(project_name,blob_name),
                                              os.path.join(dir,os.path.join(project_name,blob_name)))
                break

            except Exception as e:

                if tries == 5:
                    print("Muitos erros de conexão. Operação abortada.")
                else:
                    print("Erro de conexão com serviço. Retentando..." + e.__str__())

    def download_file_to_algo(self, blob_name, dir):
        print "download_file_to_algo(blob_name="+blob_name+", dir="+dir+")"

        for tries in range(1,5):
            try:
                self.storage.get_blob_to_path(container_name=self.algo_storage_name,
                                              blob_name=os.path.join(blob_name),
                                              file_path=os.path.join(dir,blob_name),
                                              open_mode='wb', snapshot=None, x_ms_lease_id=None,
                                              progress_callback=None)
                break

            except Exception as e:

                if tries == 5:
                    print("Muitos erros de conexão. Operação abortada.")
                else:
                    print("Erro de conexão com serviço. Retentando..." + e.__str__())


    def send_status(self, main_status):
        for tries in range(1,5):
            try:
                self.bus_service.send_topic_message(topic_name=self.status_topic,
                                                    message=Message(main_status.encode('utf-8')))
                break

            except Exception as e:

                if tries == 5:
                    print("Muitos erros de conexão. Operação abortada.")
                else:
                    print("Erro de conexão com serviço. Retentando..." + e.__str__())

    def shutdown_myself(self):

        # A máquina virtual irá cometer suicídio.
        print("Removendo máquina virtual da nuvem...")
        for tries in range(1,5):
            try:
                self.sms.delete_deployment(
                    service_name=self.myMachineName,
                    deployment_name=self.myMachineName, delete_vhd=True)
                exit(0)
                break

            except Exception as e:

                if tries == 5:
                    print("Muitos erros de conexão. Operação abortada.")
                else:
                    print("Erro de conexão com serviço. Retentando..." + e.__str__())
Exemplo n.º 40
0
from azure.servicebus import ServiceBusService, Message, Queue
bus_service = ServiceBusService(
    service_namespace='cloudassignment34ed0',
    shared_access_key_name='RootManageSharedAccessKey',
    shared_access_key_value='P6TUMCQVFg8ZIG8Z5KiPAIFaAHzvTcX9g7n8fNYAbZ0=')

# bus_service.create_queue('taskqueue')
# msg = Message(b'Test Message David 2')
# bus_service.send_queue_message('taskqueue', msg)

# bus_service_r = ServiceBusService(
#     service_namespace='cloudassignment34ed0',
#     shared_access_key_name='RootManageSharedAccessKey',
#     shared_access_key_value='P6TUMCQVFg8ZIG8Z5KiPAIFaAHzvTcX9g7n8fNYAbZ0=')

# for i in range(0,bus_service.get_queue('taskqueue').message_count-1):
# val=bus_service.get_queue('taskqueue').message_count #number of messages on the quque
# print (val)
print(bus_service)
# print (i)
msg = bus_service.receive_queue_message('taskqueue', peek_lock=True)
print(msg.body)
# queue_service.delete_queue('taskqueue')
class ServiceBusTest(AzureTestCase):

    def setUp(self):
        self.sbs = ServiceBusService(credentials.getServiceBusNamespace(),
                                     credentials.getServiceBusKey(),
                                     'owner')

        self.sbs.set_proxy(credentials.getProxyHost(),
                           credentials.getProxyPort(),
                           credentials.getProxyUser(),
                           credentials.getProxyPassword())

        __uid = getUniqueTestRunID()

        queue_base_name = u'mytestqueue%s' % (__uid)
        topic_base_name = u'mytesttopic%s' % (__uid)

        self.queue_name = getUniqueNameBasedOnCurrentTime(queue_base_name)
        self.topic_name = getUniqueNameBasedOnCurrentTime(topic_base_name)

        self.additional_queue_names = []
        self.additional_topic_names = []

    def tearDown(self):
        self.cleanup()
        return super(ServiceBusTest, self).tearDown()

    def cleanup(self):
        try:
            self.sbs.delete_queue(self.queue_name)
        except:
            pass

        for name in self.additional_queue_names:
            try:
                self.sbs.delete_queue(name)
            except:
                pass

        try:
            self.sbs.delete_topic(self.topic_name)
        except:
            pass

        for name in self.additional_topic_names:
            try:
                self.sbs.delete_topic(name)
            except:
                pass

    #--Helpers-----------------------------------------------------------------
    def _create_queue(self, queue_name):
        self.sbs.create_queue(queue_name, None, True)

    def _create_queue_and_send_msg(self, queue_name, msg):
        self._create_queue(queue_name)
        self.sbs.send_queue_message(queue_name, msg)

    def _create_topic(self, topic_name):
        self.sbs.create_topic(topic_name, None, True)

    def _create_topic_and_subscription(self, topic_name, subscription_name):
        self._create_topic(topic_name)
        self._create_subscription(topic_name, subscription_name)

    def _create_subscription(self, topic_name, subscription_name):
        self.sbs.create_subscription(topic_name, subscription_name, None, True)

    #--Test cases for service bus service -------------------------------------
    def test_create_service_bus_missing_arguments(self):
        # Arrange
        if os.environ.has_key(AZURE_SERVICEBUS_NAMESPACE):
            del os.environ[AZURE_SERVICEBUS_NAMESPACE]
        if os.environ.has_key(AZURE_SERVICEBUS_ACCESS_KEY):
            del os.environ[AZURE_SERVICEBUS_ACCESS_KEY]
        if os.environ.has_key(AZURE_SERVICEBUS_ISSUER):
            del os.environ[AZURE_SERVICEBUS_ISSUER]

        # Act
        with self.assertRaises(WindowsAzureError):
            sbs = ServiceBusService()

        # Assert

    def test_create_service_bus_env_variables(self):
        # Arrange
        os.environ[
            AZURE_SERVICEBUS_NAMESPACE] = credentials.getServiceBusNamespace()
        os.environ[
            AZURE_SERVICEBUS_ACCESS_KEY] = credentials.getServiceBusKey()
        os.environ[AZURE_SERVICEBUS_ISSUER] = 'owner'

        # Act
        sbs = ServiceBusService()

        if os.environ.has_key(AZURE_SERVICEBUS_NAMESPACE):
            del os.environ[AZURE_SERVICEBUS_NAMESPACE]
        if os.environ.has_key(AZURE_SERVICEBUS_ACCESS_KEY):
            del os.environ[AZURE_SERVICEBUS_ACCESS_KEY]
        if os.environ.has_key(AZURE_SERVICEBUS_ISSUER):
            del os.environ[AZURE_SERVICEBUS_ISSUER]

        # Assert
        self.assertIsNotNone(sbs)
        self.assertEquals(sbs.service_namespace,
                          credentials.getServiceBusNamespace())
        self.assertEquals(sbs.account_key, credentials.getServiceBusKey())
        self.assertEquals(sbs.issuer, 'owner')

    #--Test cases for queues --------------------------------------------------
    def test_create_queue_no_options(self):
        # Arrange

        # Act
        created = self.sbs.create_queue(self.queue_name)

        # Assert
        self.assertTrue(created)

    def test_create_queue_no_options_fail_on_exist(self):
        # Arrange

        # Act
        created = self.sbs.create_queue(self.queue_name, None, True)

        # Assert
        self.assertTrue(created)

    def test_create_queue_with_options(self):
        # Arrange

        # Act
        queue_options = Queue()
        queue_options.default_message_time_to_live = 'PT1M'
        queue_options.duplicate_detection_history_time_window = 'PT5M'
        queue_options.enable_batched_operations = False
        queue_options.dead_lettering_on_message_expiration = False
        queue_options.lock_duration = 'PT1M'
        queue_options.max_delivery_count = 15
        queue_options.max_size_in_megabytes = 5120
        queue_options.message_count = 0
        queue_options.requires_duplicate_detection = False
        queue_options.requires_session = False
        queue_options.size_in_bytes = 0
        created = self.sbs.create_queue(self.queue_name, queue_options)

        # Assert
        self.assertTrue(created)
        queue = self.sbs.get_queue(self.queue_name)
        self.assertEquals('PT1M', queue.default_message_time_to_live)
        self.assertEquals(
            'PT5M', queue.duplicate_detection_history_time_window)
        self.assertEquals(False, queue.enable_batched_operations)
        self.assertEquals(False, queue.dead_lettering_on_message_expiration)
        self.assertEquals('PT1M', queue.lock_duration)
        self.assertEquals(15, queue.max_delivery_count)
        self.assertEquals(5120, queue.max_size_in_megabytes)
        self.assertEquals(0, queue.message_count)
        self.assertEquals(False, queue.requires_duplicate_detection)
        self.assertEquals(False, queue.requires_session)
        self.assertEquals(0, queue.size_in_bytes)

    def test_create_queue_with_already_existing_queue(self):
        # Arrange

        # Act
        created1 = self.sbs.create_queue(self.queue_name)
        created2 = self.sbs.create_queue(self.queue_name)

        # Assert
        self.assertTrue(created1)
        self.assertFalse(created2)

    def test_create_queue_with_already_existing_queue_fail_on_exist(self):
        # Arrange

        # Act
        created = self.sbs.create_queue(self.queue_name)
        with self.assertRaises(WindowsAzureError):
            self.sbs.create_queue(self.queue_name, None, True)

        # Assert
        self.assertTrue(created)

    def test_get_queue_with_existing_queue(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        queue = self.sbs.get_queue(self.queue_name)

        # Assert
        self.assertIsNotNone(queue)
        self.assertEquals(queue.name, self.queue_name)

    def test_get_queue_with_non_existing_queue(self):
        # Arrange

        # Act
        with self.assertRaises(WindowsAzureError):
            resp = self.sbs.get_queue(self.queue_name)

        # Assert

    def test_list_queues(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        queues = self.sbs.list_queues()
        for queue in queues:
            name = queue.name

        # Assert
        self.assertIsNotNone(queues)
        self.assertNamedItemInContainer(queues, self.queue_name)

    def test_list_queues_with_special_chars(self):
        # Arrange
        # Name must start and end with an alphanumeric and can only contain
        # letters, numbers, periods, hyphens, forward slashes and underscores.
        other_queue_name = self.queue_name + 'foo/.-_123'
        self.additional_queue_names = [other_queue_name]
        self._create_queue(other_queue_name)

        # Act
        queues = self.sbs.list_queues()

        # Assert
        self.assertIsNotNone(queues)
        self.assertNamedItemInContainer(queues, other_queue_name)

    def test_delete_queue_with_existing_queue(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        deleted = self.sbs.delete_queue(self.queue_name)

        # Assert
        self.assertTrue(deleted)
        queues = self.sbs.list_queues()
        self.assertNamedItemNotInContainer(queues, self.queue_name)

    def test_delete_queue_with_existing_queue_fail_not_exist(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        deleted = self.sbs.delete_queue(self.queue_name, True)

        # Assert
        self.assertTrue(deleted)
        queues = self.sbs.list_queues()
        self.assertNamedItemNotInContainer(queues, self.queue_name)

    def test_delete_queue_with_non_existing_queue(self):
        # Arrange

        # Act
        deleted = self.sbs.delete_queue(self.queue_name)

        # Assert
        self.assertFalse(deleted)

    def test_delete_queue_with_non_existing_queue_fail_not_exist(self):
        # Arrange

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.delete_queue(self.queue_name, True)

        # Assert

    def test_send_queue_message(self):
        # Arrange
        self._create_queue(self.queue_name)
        sent_msg = Message('send message')

        # Act
        self.sbs.send_queue_message(self.queue_name, sent_msg)

        # Assert

    def test_receive_queue_message_read_delete_mode(self):
        # Assert
        sent_msg = Message('receive message')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_queue_message_read_delete_mode_throws_on_delete(self):
        # Assert
        sent_msg = Message('receive message')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)
        with self.assertRaises(WindowsAzureError):
            received_msg.delete()

        # Assert

    def test_receive_queue_message_read_delete_mode_throws_on_unlock(self):
        # Assert
        sent_msg = Message('receive message')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)
        with self.assertRaises(WindowsAzureError):
            received_msg.unlock()

        # Assert

    def test_receive_queue_message_peek_lock_mode(self):
        # Arrange
        sent_msg = Message('peek lock message')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, True)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_queue_message_delete(self):
        # Arrange
        sent_msg = Message('peek lock message delete')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, True)
        received_msg.delete()

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_queue_message_unlock(self):
        # Arrange
        sent_msg = Message('peek lock message unlock')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, True)
        received_msg.unlock()

        # Assert
        received_again_msg = self.sbs.receive_queue_message(
            self.queue_name, True)
        received_again_msg.delete()
        self.assertIsNotNone(received_msg)
        self.assertIsNotNone(received_again_msg)
        self.assertEquals(sent_msg.body, received_msg.body)
        self.assertEquals(received_again_msg.body, received_msg.body)

    def test_send_queue_message_with_custom_message_type(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        sent_msg = Message(
            '<text>peek lock message custom message type</text>', type='text/xml')
        self.sbs.send_queue_message(self.queue_name, sent_msg)
        received_msg = self.sbs.receive_queue_message(self.queue_name, True, 5)
        received_msg.delete()

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals('text/xml', received_msg.type)

    def test_send_queue_message_with_custom_message_properties(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        props = {'hello': 'world',
                 'foo': 42,
                 'active': True,
                 'deceased': False,
                 'large': 8555111000,
                 'floating': 3.14,
                 'dob': datetime(2011, 12, 14)}
        sent_msg = Message('message with properties', custom_properties=props)
        self.sbs.send_queue_message(self.queue_name, sent_msg)
        received_msg = self.sbs.receive_queue_message(self.queue_name, True, 5)
        received_msg.delete()

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(received_msg.custom_properties['hello'], 'world')
        self.assertEquals(received_msg.custom_properties['foo'], 42)
        self.assertEquals(received_msg.custom_properties['active'], True)
        self.assertEquals(received_msg.custom_properties['deceased'], False)
        self.assertEquals(received_msg.custom_properties['large'], 8555111000)
        self.assertEquals(received_msg.custom_properties['floating'], 3.14)
        self.assertEquals(
            received_msg.custom_properties['dob'], datetime(2011, 12, 14))

    def test_receive_queue_message_timeout_5(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        start = time.clock()
        received_msg = self.sbs.receive_queue_message(self.queue_name, True, 5)
        duration = time.clock() - start

        # Assert
        self.assertTrue(duration > 3 and duration < 7)
        self.assertIsNotNone(received_msg)
        self.assertIsNone(received_msg.body)

    def test_receive_queue_message_timeout_50(self):
        # Arrange
        self._create_queue(self.queue_name)

        # Act
        start = time.clock()
        received_msg = self.sbs.receive_queue_message(
            self.queue_name, True, 50)
        duration = time.clock() - start

        # Assert
        self.assertTrue(duration > 48 and duration < 52)
        self.assertIsNotNone(received_msg)
        self.assertIsNone(received_msg.body)

    #--Test cases for topics/subscriptions ------------------------------------
    def test_create_topic_no_options(self):
        # Arrange

        # Act
        created = self.sbs.create_topic(self.topic_name)

        # Assert
        self.assertTrue(created)

    def test_create_topic_no_options_fail_on_exist(self):
        # Arrange

        # Act
        created = self.sbs.create_topic(self.topic_name, None, True)

        # Assert
        self.assertTrue(created)

    def test_create_topic_with_options(self):
        # Arrange

        # Act
        topic_options = Topic()
        topic_options.default_message_time_to_live = 'PT1M'
        topic_options.duplicate_detection_history_time_window = 'PT5M'
        topic_options.enable_batched_operations = False
        topic_options.max_size_in_megabytes = 5120
        topic_options.requires_duplicate_detection = False
        topic_options.size_in_bytes = 0
        # TODO: MaximumNumberOfSubscriptions is not supported?
        created = self.sbs.create_topic(self.topic_name, topic_options)

        # Assert
        self.assertTrue(created)
        topic = self.sbs.get_topic(self.topic_name)
        self.assertEquals('PT1M', topic.default_message_time_to_live)
        self.assertEquals(
            'PT5M', topic.duplicate_detection_history_time_window)
        self.assertEquals(False, topic.enable_batched_operations)
        self.assertEquals(5120, topic.max_size_in_megabytes)
        self.assertEquals(False, topic.requires_duplicate_detection)
        self.assertEquals(0, topic.size_in_bytes)

    def test_create_topic_with_already_existing_topic(self):
        # Arrange

        # Act
        created1 = self.sbs.create_topic(self.topic_name)
        created2 = self.sbs.create_topic(self.topic_name)

        # Assert
        self.assertTrue(created1)
        self.assertFalse(created2)

    def test_create_topic_with_already_existing_topic_fail_on_exist(self):
        # Arrange

        # Act
        created = self.sbs.create_topic(self.topic_name)
        with self.assertRaises(WindowsAzureError):
            self.sbs.create_topic(self.topic_name, None, True)

        # Assert
        self.assertTrue(created)

    def test_topic_backwards_compatibility_warning(self):
        # Arrange
        topic_options = Topic()
        topic_options.max_size_in_megabytes = 5120

        # Act
        val = topic_options.max_size_in_mega_bytes

        # Assert
        self.assertEqual(val, 5120)

        # Act
        topic_options.max_size_in_mega_bytes = 1024

        # Assert
        self.assertEqual(topic_options.max_size_in_megabytes, 1024)

    def test_get_topic_with_existing_topic(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        topic = self.sbs.get_topic(self.topic_name)

        # Assert
        self.assertIsNotNone(topic)
        self.assertEquals(topic.name, self.topic_name)

    def test_get_topic_with_non_existing_topic(self):
        # Arrange

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.get_topic(self.topic_name)

        # Assert

    def test_list_topics(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        topics = self.sbs.list_topics()
        for topic in topics:
            name = topic.name

        # Assert
        self.assertIsNotNone(topics)
        self.assertNamedItemInContainer(topics, self.topic_name)

    def test_list_topics_with_special_chars(self):
        # Arrange
        # Name must start and end with an alphanumeric and can only contain
        # letters, numbers, periods, hyphens, forward slashes and underscores.
        other_topic_name = self.topic_name + 'foo/.-_123'
        self.additional_topic_names = [other_topic_name]
        self._create_topic(other_topic_name)

        # Act
        topics = self.sbs.list_topics()

        # Assert
        self.assertIsNotNone(topics)
        self.assertNamedItemInContainer(topics, other_topic_name)

    def test_delete_topic_with_existing_topic(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        deleted = self.sbs.delete_topic(self.topic_name)

        # Assert
        self.assertTrue(deleted)
        topics = self.sbs.list_topics()
        self.assertNamedItemNotInContainer(topics, self.topic_name)

    def test_delete_topic_with_existing_topic_fail_not_exist(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        deleted = self.sbs.delete_topic(self.topic_name, True)

        # Assert
        self.assertTrue(deleted)
        topics = self.sbs.list_topics()
        self.assertNamedItemNotInContainer(topics, self.topic_name)

    def test_delete_topic_with_non_existing_topic(self):
        # Arrange

        # Act
        deleted = self.sbs.delete_topic(self.topic_name)

        # Assert
        self.assertFalse(deleted)

    def test_delete_topic_with_non_existing_topic_fail_not_exist(self):
        # Arrange

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.delete_topic(self.topic_name, True)

        # Assert

    def test_create_subscription(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        created = self.sbs.create_subscription(
            self.topic_name, 'MySubscription')

        # Assert
        self.assertTrue(created)

    def test_create_subscription_with_options(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        subscription_options = Subscription()
        subscription_options.dead_lettering_on_filter_evaluation_exceptions = False
        subscription_options.dead_lettering_on_message_expiration = False
        subscription_options.default_message_time_to_live = 'PT15M'
        subscription_options.enable_batched_operations = False
        subscription_options.lock_duration = 'PT1M'
        subscription_options.max_delivery_count = 15
        #message_count is read-only
        subscription_options.message_count = 0
        subscription_options.requires_session = False
        created = self.sbs.create_subscription(
            self.topic_name, 'MySubscription', subscription_options)

        # Assert
        self.assertTrue(created)
        subscription = self.sbs.get_subscription(
            self.topic_name, 'MySubscription')
        self.assertEquals(
            False, subscription.dead_lettering_on_filter_evaluation_exceptions)
        self.assertEquals(
            False, subscription.dead_lettering_on_message_expiration)
        self.assertEquals('PT15M', subscription.default_message_time_to_live)
        self.assertEquals(False, subscription.enable_batched_operations)
        self.assertEquals('PT1M', subscription.lock_duration)
        # self.assertEquals(15, subscription.max_delivery_count) #no idea why
        # max_delivery_count is always 10
        self.assertEquals(0, subscription.message_count)
        self.assertEquals(False, subscription.requires_session)

    def test_create_subscription_fail_on_exist(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        created = self.sbs.create_subscription(
            self.topic_name, 'MySubscription', None, True)

        # Assert
        self.assertTrue(created)

    def test_create_subscription_with_already_existing_subscription(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        created1 = self.sbs.create_subscription(
            self.topic_name, 'MySubscription')
        created2 = self.sbs.create_subscription(
            self.topic_name, 'MySubscription')

        # Assert
        self.assertTrue(created1)
        self.assertFalse(created2)

    def test_create_subscription_with_already_existing_subscription_fail_on_exist(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        created = self.sbs.create_subscription(
            self.topic_name, 'MySubscription')
        with self.assertRaises(WindowsAzureError):
            self.sbs.create_subscription(
                self.topic_name, 'MySubscription', None, True)

        # Assert
        self.assertTrue(created)

    def test_list_subscriptions(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription2')

        # Act
        subscriptions = self.sbs.list_subscriptions(self.topic_name)

        # Assert
        self.assertIsNotNone(subscriptions)
        self.assertEquals(len(subscriptions), 1)
        self.assertEquals(subscriptions[0].name, 'MySubscription2')

    def test_get_subscription_with_existing_subscription(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription3')

        # Act
        subscription = self.sbs.get_subscription(
            self.topic_name, 'MySubscription3')

        # Assert
        self.assertIsNotNone(subscription)
        self.assertEquals(subscription.name, 'MySubscription3')

    def test_get_subscription_with_non_existing_subscription(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription3')

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.get_subscription(self.topic_name, 'MySubscription4')

        # Assert

    def test_delete_subscription_with_existing_subscription(self):
        # Arrange
        self._create_topic(self.topic_name)
        self._create_subscription(self.topic_name, 'MySubscription4')
        self._create_subscription(self.topic_name, 'MySubscription5')

        # Act
        deleted = self.sbs.delete_subscription(
            self.topic_name, 'MySubscription4')

        # Assert
        self.assertTrue(deleted)
        subscriptions = self.sbs.list_subscriptions(self.topic_name)
        self.assertIsNotNone(subscriptions)
        self.assertEquals(len(subscriptions), 1)
        self.assertEquals(subscriptions[0].name, 'MySubscription5')

    def test_delete_subscription_with_existing_subscription_fail_not_exist(self):
        # Arrange
        self._create_topic(self.topic_name)
        self._create_subscription(self.topic_name, 'MySubscription4')
        self._create_subscription(self.topic_name, 'MySubscription5')

        # Act
        deleted = self.sbs.delete_subscription(
            self.topic_name, 'MySubscription4', True)

        # Assert
        self.assertTrue(deleted)
        subscriptions = self.sbs.list_subscriptions(self.topic_name)
        self.assertIsNotNone(subscriptions)
        self.assertEquals(len(subscriptions), 1)
        self.assertEquals(subscriptions[0].name, 'MySubscription5')

    def test_delete_subscription_with_non_existing_subscription(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        deleted = self.sbs.delete_subscription(
            self.topic_name, 'MySubscription')

        # Assert
        self.assertFalse(deleted)

    def test_delete_subscription_with_non_existing_subscription_fail_not_exist(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.delete_subscription(
                self.topic_name, 'MySubscription', True)

        # Assert

    def test_create_rule_no_options(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1')

        # Assert
        self.assertTrue(created)

    def test_create_rule_no_options_fail_on_exist(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1', None, True)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_already_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        created1 = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1')
        created2 = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1')

        # Assert
        self.assertTrue(created1)
        self.assertFalse(created2)

    def test_create_rule_with_already_existing_rule_fail_on_exist(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1')
        with self.assertRaises(WindowsAzureError):
            self.sbs.create_rule(
                self.topic_name, 'MySubscription', 'MyRule1', None, True)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_sql_filter(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.filter_type = 'SqlFilter'
        rule1.filter_expression = 'foo > 40'
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_true_filter(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.filter_type = 'TrueFilter'
        rule1.filter_expression = '1=1'
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_false_filter(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.filter_type = 'FalseFilter'
        rule1.filter_expression = '1=0'
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_correlation_filter(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.filter_type = 'CorrelationFilter'
        rule1.filter_expression = 'myid'
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_empty_rule_action(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.action_type = 'EmptyRuleAction'
        rule1.action_expression = ''
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_create_rule_with_options_sql_rule_action(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule1 = Rule()
        rule1.action_type = 'SqlRuleAction'
        rule1.action_expression = "SET foo = 5"
        created = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1', rule1)

        # Assert
        self.assertTrue(created)

    def test_list_rules(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        resp = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule2')

        # Act
        rules = self.sbs.list_rules(self.topic_name, 'MySubscription')

        # Assert
        self.assertEquals(len(rules), 2)

    def test_get_rule_with_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        rule = self.sbs.get_rule(self.topic_name, 'MySubscription', '$Default')

        # Assert
        self.assertIsNotNone(rule)
        self.assertEquals(rule.name, '$Default')

    def test_get_rule_with_non_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.get_rule(self.topic_name,
                              'MySubscription', 'NonExistingRule')

        # Assert

    def test_get_rule_with_existing_rule_with_options(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_rule = Rule()
        sent_rule.filter_type = 'SqlFilter'
        sent_rule.filter_expression = 'foo > 40'
        sent_rule.action_type = 'SqlRuleAction'
        sent_rule.action_expression = 'SET foo = 5'
        self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule1', sent_rule)

        # Act
        received_rule = self.sbs.get_rule(
            self.topic_name, 'MySubscription', 'MyRule1')

        # Assert
        self.assertIsNotNone(received_rule)
        self.assertEquals(received_rule.name, 'MyRule1')
        self.assertEquals(received_rule.filter_type, sent_rule.filter_type)
        self.assertEquals(received_rule.filter_expression,
                          sent_rule.filter_expression)
        self.assertEquals(received_rule.action_type, sent_rule.action_type)
        self.assertEquals(received_rule.action_expression,
                          sent_rule.action_expression)

    def test_delete_rule_with_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        resp = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule3')
        resp = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule4')

        # Act
        deleted1 = self.sbs.delete_rule(
            self.topic_name, 'MySubscription', 'MyRule4')
        deleted2 = self.sbs.delete_rule(
            self.topic_name, 'MySubscription', '$Default')

        # Assert
        self.assertTrue(deleted1)
        self.assertTrue(deleted2)
        rules = self.sbs.list_rules(self.topic_name, 'MySubscription')
        self.assertIsNotNone(rules)
        self.assertEquals(len(rules), 1)
        self.assertEquals(rules[0].name, 'MyRule3')

    def test_delete_rule_with_existing_rule_fail_not_exist(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        resp = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule3')
        resp = self.sbs.create_rule(
            self.topic_name, 'MySubscription', 'MyRule4')

        # Act
        deleted1 = self.sbs.delete_rule(
            self.topic_name, 'MySubscription', 'MyRule4', True)
        deleted2 = self.sbs.delete_rule(
            self.topic_name, 'MySubscription', '$Default', True)

        # Assert
        self.assertTrue(deleted1)
        self.assertTrue(deleted2)
        rules = self.sbs.list_rules(self.topic_name, 'MySubscription')
        self.assertIsNotNone(rules)
        self.assertEquals(len(rules), 1)
        self.assertEquals(rules[0].name, 'MyRule3')

    def test_delete_rule_with_non_existing_rule(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        deleted = self.sbs.delete_rule(
            self.topic_name, 'MySubscription', 'NonExistingRule')

        # Assert
        self.assertFalse(deleted)

    def test_delete_rule_with_non_existing_rule_fail_not_exist(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        with self.assertRaises(WindowsAzureError):
            self.sbs.delete_rule(
                self.topic_name, 'MySubscription', 'NonExistingRule', True)

        # Assert

    def test_send_topic_message(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')

        # Act
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Assert

    def test_receive_subscription_message_read_delete_mode(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_subscription_message_read_delete_mode_throws_on_delete(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)
        with self.assertRaises(WindowsAzureError):
            received_msg.delete()

        # Assert

    def test_receive_subscription_message_read_delete_mode_throws_on_unlock(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)
        with self.assertRaises(WindowsAzureError):
            received_msg.unlock()

        # Assert

    def test_receive_subscription_message_peek_lock_mode(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', True, 5)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_subscription_message_delete(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', True, 5)
        received_msg.delete()

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_receive_subscription_message_unlock(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', True)
        received_msg.unlock()

        # Assert
        received_again_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', True)
        received_again_msg.delete()
        self.assertIsNotNone(received_msg)
        self.assertIsNotNone(received_again_msg)
        self.assertEquals(sent_msg.body, received_msg.body)
        self.assertEquals(received_again_msg.body, received_msg.body)

    def test_with_filter(self):
         # Single filter
        called = []

        def my_filter(request, next):
            called.append(True)
            return next(request)

        sbs = self.sbs.with_filter(my_filter)
        sbs.create_topic(self.topic_name + '0', None, True)

        self.assertTrue(called)

        del called[:]

        sbs.delete_topic(self.topic_name + '0')

        self.assertTrue(called)
        del called[:]

        # Chained filters
        def filter_a(request, next):
            called.append('a')
            return next(request)

        def filter_b(request, next):
            called.append('b')
            return next(request)

        sbs = self.sbs.with_filter(filter_a).with_filter(filter_b)
        sbs.create_topic(self.topic_name + '0', None, True)

        self.assertEqual(called, ['b', 'a'])

        sbs.delete_topic(self.topic_name + '0')

        self.assertEqual(called, ['b', 'a', 'b', 'a'])

    def test_two_identities(self):
        # In order to run this test, 2 service bus service identities are created using
        # the sbaztool available at:
        # http://code.msdn.microsoft.com/windowsazure/Authorization-SBAzTool-6fd76d93
        #
        # Use the following commands to create 2 identities and grant access rights.
        # Replace <servicebusnamespace> with the namespace specified in the test .json file
        # Replace <servicebuskey> with the key specified in the test .json file
        # This only needs to be executed once, after the service bus namespace is created.
        #
        # sbaztool makeid user1 NoHEoD6snlvlhZm7yek9Etxca3l0CYjfc19ICIJZoUg= -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Send /path1 user1 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Listen /path1 user1 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Manage /path1 user1 -n <servicebusnamespace> -k
        # <servicebuskey>

        # sbaztool makeid user2 Tb6K5qEgstyRBwp86JEjUezKj/a+fnkLFnibfgvxvdg= -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Send /path2 user2 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Listen /path2 user2 -n <servicebusnamespace> -k <servicebuskey>
        # sbaztool grant Manage /path2 user2 -n <servicebusnamespace> -k
        # <servicebuskey>

        sbs1 = ServiceBusService(credentials.getServiceBusNamespace(),
                                 'NoHEoD6snlvlhZm7yek9Etxca3l0CYjfc19ICIJZoUg=',
                                 'user1')
        sbs2 = ServiceBusService(credentials.getServiceBusNamespace(),
                                 'Tb6K5qEgstyRBwp86JEjUezKj/a+fnkLFnibfgvxvdg=',
                                 'user2')

        queue1_name = 'path1/queue' + str(random.randint(1, 10000000))
        queue2_name = 'path2/queue' + str(random.randint(1, 10000000))

        try:
            # Create queues, success
            sbs1.create_queue(queue1_name)
            sbs2.create_queue(queue2_name)

            # Receive messages, success
            msg = sbs1.receive_queue_message(queue1_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs1.receive_queue_message(queue1_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs2.receive_queue_message(queue2_name, True, 1)
            self.assertIsNone(msg.body)
            msg = sbs2.receive_queue_message(queue2_name, True, 1)
            self.assertIsNone(msg.body)

            # Receive messages, failure
            with self.assertRaises(HTTPError):
                msg = sbs1.receive_queue_message(queue2_name, True, 1)
            with self.assertRaises(HTTPError):
                msg = sbs2.receive_queue_message(queue1_name, True, 1)
        finally:
            try:
                sbs1.delete_queue(queue1_name)
            except:
                pass
            try:
                sbs2.delete_queue(queue2_name)
            except:
                pass

    def test_unicode_create_queue_unicode_name(self):
        # Arrange
        self.queue_name = self.queue_name + u'啊齄丂狛狜'

        # Act
        with self.assertRaises(WindowsAzureError):
            created = self.sbs.create_queue(self.queue_name)

        # Assert

    def test_unicode_receive_queue_message_unicode_data(self):
        # Assert
        sent_msg = Message('receive message啊齄丂狛狜')
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_unicode_receive_queue_message_binary_data(self):
        # Assert
        base64_data = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/wABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=='
        binary_data = base64.b64decode(base64_data)
        sent_msg = Message(binary_data)
        self._create_queue_and_send_msg(self.queue_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_queue_message(self.queue_name, False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_unicode_create_subscription_unicode_name(self):
        # Arrange
        self._create_topic(self.topic_name)

        # Act
        with self.assertRaises(WindowsAzureError):
            created = self.sbs.create_subscription(
                self.topic_name, u'MySubscription啊齄丂狛狜')

        # Assert

    def test_unicode_create_rule_unicode_name(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')

        # Act
        with self.assertRaises(WindowsAzureError):
            created = self.sbs.create_rule(
                self.topic_name, 'MySubscription', 'MyRule啊齄丂狛狜')

        # Assert

    def test_unicode_receive_subscription_message_unicode_data(self):
        # Arrange
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message('subscription message啊齄丂狛狜')
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)

    def test_unicode_receive_subscription_message_binary_data(self):
        # Arrange
        base64_data = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/wABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=='
        binary_data = base64.b64decode(base64_data)
        self._create_topic_and_subscription(self.topic_name, 'MySubscription')
        sent_msg = Message(binary_data)
        self.sbs.send_topic_message(self.topic_name, sent_msg)

        # Act
        received_msg = self.sbs.receive_subscription_message(
            self.topic_name, 'MySubscription', False)

        # Assert
        self.assertIsNotNone(received_msg)
        self.assertEquals(sent_msg.body, received_msg.body)
Exemplo n.º 42
0
from azure.servicebus import ServiceBusService, Message, Queue

namespace = "nsgtrnamespace"
endpoint = "sb://" + namespace + ".servicebus.windows.net/"
keyName = "all"
key = "KbuVe06trwIaVPeYYg/F+WfmojIi+pdWe4SrD5BBczY="
queueName = "nsgtrqueue"

bus_service = ServiceBusService(
    service_namespace=namespace,
    shared_access_key_name=keyName,
    shared_access_key_value=key
)

sentMsg = Message(b'hi')
bus_service.send_queue_message(queueName, sentMsg)
receivedMsg = bus_service.receive_queue_message(queueName, peek_lock=False)
print(receivedMsg.body)

#print(bus_service.receive_queue_message(queueName, peek_lock=False))
Exemplo n.º 43
0
f = open('RDP.bat','w')
f.write(str(l1))
f.write("\n")
f.write(str(l2))
f.close()
import subprocess
subprocess.Popen('RDP.bat')

#Reading msg from service Bus
while True:
    from azure.servicebus import ServiceBusService, Message, Queue
    bus_service = ServiceBusService(
        service_namespace='SVDServiceBus',
        shared_access_key_name='RootManageSharedAccessKey',
        shared_access_key_value='**********************UXm0hGnxezXyx0=')
    msg = bus_service.receive_queue_message('resultqueue', peek_lock=True)
    print(msg.body)
    if(msg.body ==bytes(video_name, encoding='utf-8')):
        break
msg.delete()

# In[2]:


with open('meetingLink.txt', 'r') as myfile:
    meetingLink=myfile.read().replace('\n', '')
meetingID=meetingLink.rsplit('/', 1)[1]

from azure.storage.blob import BlockBlobService
from azure.storage.blob import PublicAccess
account_name='intelligentappstorage01'
from azure.servicebus import ServiceBusService, Message, Queue
bus_read_service = ServiceBusService(
    service_namespace='vmsseight',
    shared_access_key_name='ListenOneTime',
    shared_access_key_value='OhFgmG5Cr/K9aOrE29YL7eXERzmUb3Fpf7J+FoBhiMw=')
msg = bus_read_service.receive_queue_message('vmsseightqueue', peek_lock=False)
print(msg.body)
Exemplo n.º 45
0
class AzureHook(CloudHook):
    def __init__(self, serviceKeyValues=middle_earth):
        # Call super class constructor
        CloudHook.__init__(self, serviceKeyValues)
        self.queue = pyqueue.Queue()

    # Implement abstract methods
    def setupHook(self, kv):
        self.serviceBus = ServiceBusService(
            service_namespace=kv['service_namespace'],
            shared_access_key_name=kv['shared_access_key_name'],
            shared_access_key_value=kv['shared_access_key_value'])

    def createQueue(self, queueName, queueOptions=None):
        if queueOptions is None:
            queueOptions = Queue()
            queueOptions.max_size_in_megabytes = '5120'
            queueOptions.default_message_time_to_live = 'PT1M'
        self.serviceBus.create_queue(queueName, queueOptions)

    def sendQueueMessage(self, queueName, messageBody):
        self.serviceBus.send_queue_message(queueName, Message(messageBody))

    def receiveQueueMessage(self, queueName, peek_lock=False):
        return self.serviceBus.receive_queue_message(queueName, peek_lock)

    def createTopic(self, topicName, topicOptions=None):
        if topicOptions is None:
            topicOptions = Topic()
            topicOptions.max_size_in_megabytes = '5120'
            topicOptions.default_message_time_to_live = 'PT1M'
        self.serviceBus.create_topic(topicName, topicOptions)

    def publish(self, topicName, messageBody, extra=None):
        if hasConnectivity():
            self.flushQueue()
            message = Message(messageBody, custom_properties=extra)
            self.serviceBus.send_topic_message(topicName, message)
            return True
        else:
            self.queue.put({
                'topicName': topicName,
                'messageBody': messageBody,
                'extra': extra
            })
            return False

    def subscribe(self, topicName, subscriptionName):
        self.serviceBus.create_subscription(topicName, subscriptionName)

    def getMessage(self,
                   topicName,
                   subscriptionName,
                   peek_lock=False,
                   timeout='60'):
        return self.serviceBus.receive_subscription_message(
            topicName, subscriptionName, peek_lock=peek_lock, timeout=timeout)

    # We could persist the data in a local sqllite database, which would be easy
    # but we feel that that goes beyond the scope of this project
    def flushQueue(self):
        while not self.queue.empty():
            try:
                dic = self.queue.get_nowait()
            except Queue.Empty:
                break
            message = Message(dic['messageBody'],
                              custom_properties=dic['extra'])
            self.serviceBus.send_topic_message(dic['topicName'], message)
Exemplo n.º 46
0
from azure.servicebus import ServiceBusService, Message, Queue

from random import randint

# Initialize

bus_service = ServiceBusService(
    service_namespace='YOUR_NAMESPACE',
    shared_access_key_name='RootManageSharedAccessKey',
    shared_access_key_value='YOUR_KEY')


# Create a queue
bus_service.create_queue('myqueue')

# queue_options = Queue()
# queue_options.max_size_in_megabytes = '5120'
# queue_options.default_message_time_to_live = 'PT1M'
# bus_service.create_queue('myqueue', queue_options)

# Add message to queue
msg = Message(b'Test Message ' + str(randint(1000,9999)))
bus_service.send_queue_message('myqueue', msg)

# Read messages
msg = bus_service.receive_queue_message('myqueue', peek_lock=False)
print(msg.body)
Exemplo n.º 47
0
def main(argv):
   print '##################################################################'
   print '#                         NRF24 gateway                          #'
   print '##################################################################'


   # Wait while internet appear
   import urllib2 
   loop_value = 1
   while (loop_value < 10):
      try:
           urllib2.urlopen("http://google.com")
      except:
           print( "Network: currently down." )
           time.sleep( 10 )
           loop_value = loop_value + 1
      else:
           print( "Network: Up and running." )
           loop_value = 10


   pipes = [[0xf0, 0xf0, 0xf0, 0xf0, 0xd2], [0xf0, 0xf0, 0xf0, 0xf0, 0xe1]]
   configFileName = os.path.dirname(os.path.abspath(__file__)) + '/config.json'
   debugMode = 0

   try:
      opts, args = getopt.getopt(argv,"hd:f:",["debug=","configFile="])
   except getopt.GetoptError:
      print 'smart_gateway.py -d <debugMode:0/1>'
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print 'Example for call it in debug mode: smart_gateway.py -d 1 -f config.json'
         sys.exit()
      elif opt in ("-d", "--debug"):
         debugMode = arg
      elif opt in ("-f", "--configFile"):
         configFileName = arg

   print 'Start Parameters:'
   print '       debugMode:', debugMode
   print '  configFileName:', configFileName 

   json_data=open(configFileName)
   config_data = json.load(json_data)
   json_data.close()

   print '      Server URL:', config_data["Server"]["url"]
   print '      Company ID:', config_data["Server"]["id"]
   print '      Gateway ID:', config_data["Server"]["Deviceid"]
   print '     Service Bus:', config_data["Servicebus"]["namespace"]

   print ''
   nowPI = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")

   href = config_data["Server"]["url"] + 'API/Device/GetServerDateTime'
   token = config_data["Server"]["key"]
   authentication = config_data["Server"]["id"] + ':' + token
   headers = {'Content-Type': 'application/json; charset=utf-8', 'Accept': 'application/json', 'Authentication': authentication}
   #r = requests.get(href, headers=headers, verify=False)
   r = requests.get(href, headers=headers)
   if r.status_code == 200:
       nowPI = r.json()
       print ("Setting up time to: " + nowPI)
       os.popen('sudo -S date -s "' + nowPI + '"', 'w').write("123")
   else:
       print 'Error in setting time. Server response code: %i' % r.status_code

   queue_name = 'custom_' + config_data["Server"]["id"] + '_' + config_data["Server"]["Deviceid"]
   bus_service = ServiceBusService( service_namespace=config_data["Servicebus"]["namespace"], 
                                    shared_access_key_name=config_data["Servicebus"]["shared_access_key_name"], 
                                    shared_access_key_value=config_data["Servicebus"]["shared_access_key_value"])
   try:
      bus_service.receive_queue_message(queue_name, peek_lock=False)
      print '  Actuator queue: ' + queue_name
   except:
      queue_options = Queue()
      queue_options.max_size_in_megabytes = '1024'
      queue_options.default_message_time_to_live = 'PT15M'
      bus_service.create_queue(queue_name, queue_options)
      print '  Actuator queue: ' + queue_name + ' (Created)'	   
	   
   href = config_data["Server"]["url"] + 'api/Device/DeviceConfigurationUpdate'
   token = config_data["Server"]["key"]
   authentication = config_data["Server"]["id"] + ":" + token


   if debugMode==1: print(authentication)
   headers = {'Content-Type': 'application/json; charset=utf-8', 'Accept': 'application/json', 'Timestamp': nowPI, 'Authentication': authentication}
    
   deviceDetail = {}
   deviceDetail["DeviceIdentifier"] = config_data["Server"]["Deviceid"]
   deviceDetail["DeviceType"] = "Custom"
   deviceDetail["DeviceConfigurations"] = [{'Key':'IPPrivate','Value':[(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]},
                                           {'Key':'IPPublic','Value': requests.get('http://icanhazip.com/').text},
                                           {'Key': 'Configuration', 'Value': json.dumps(config_data) },
                                           {'Key':'MAC','Value': ':'.join(("%012X" % get_mac())[i:i+2] for i in range(0, 12, 2))}
                                          ]

   payload = {'Device': deviceDetail}
   if debugMode == 1: print 'Request Content: {0}'.format(json.dumps(payload))
   #r = requests.post(href, headers=headers, data=json.dumps(payload), verify=False)
   r = requests.post(href, headers=headers, data=json.dumps(payload))


   if r.status_code == 200:
      if debugMode == 1: print 'Configuration Response Content: {0}'.format(r.content)
      data = json.loads(r.text)    
      for entry in data['Device']['DeviceConfigurations']:
          if entry['Key'] == 'Configuration':     
             with open(configFileName, 'w') as outfile:
                json.dump(json.loads(entry['Value']), outfile)
      print 'Device configuration Successfully updated'
   else:
      print 'Error in Device configuration update. Server response code: {0} {1}'.format(r.status_code, r.content)


   GPIO.setwarnings(False)

   radio = NRF24()
   radio.begin(0, 0, 25, 18) #set gpio 25 as CE pin
   radio.setRetries(15,15)
   radio.setPayloadSize(32)
   radio.setChannel(0x4c)
   radio.setDataRate(NRF24.BR_250KBPS)
   radio.setPALevel(NRF24.PA_MAX)
   radio.setAutoAck(1)
   radio.openWritingPipe(pipes[1])
   radio.openReadingPipe(1, pipes[0])

   radio.startListening()
   radio.stopListening()

   print ''
   print 'NRF24 Module Configuration Details:'
   radio.printDetails()
   radio.startListening()

   print ''
   cloudCommandLastCheck = datetime.now()

   # List to hold all commands that was send no ACK received
   localCommandSendAckWaitList = []
   while True:
       pipe = [0]
       cloudCommand = ''
       nowPI = datetime.now()
       while not radio.available(pipe, True):
           radioTime = datetime.now()
           tdelta = nowPI-radioTime
           time.sleep(1)
           if (abs(tdelta.total_seconds()) > 10): break

       recv_buffer = []
       radio.read(recv_buffer)
       out = ''.join(chr(i) for i in recv_buffer)

       nowPI = datetime.now()

       if debugMode == 1: print localCommandSendAckWaitList

       if out.find(';')>0:
          out = out.split(';')[0]
          print 'L: Received: ' + out

          temp =out.split("_")
          if debugMode == 1: print (temp)
    
          if temp[0] in config_data["Devices"]:
             if temp[1] == 'ack':
               # Clean list once ACK from SN is received
                localCommandSendAckWaitList= [x for x in localCommandSendAckWaitList if x != temp[2]]
                print '<- Broadcast complete, ACK received for: ' + temp[2]
             else:
                sendMeasure(config_data, nowPI.strftime("%Y-%m-%dT%H:%M:%S"), temp[1], temp[2], config_data["Devices"][temp[0]], debugMode)
                print 'L: Generated selfok'
                sendMeasure(config_data, nowPI.strftime("%Y-%m-%dT%H:%M:%S"), 'live', 1, config_data["Server"]["Deviceid"], debugMode)
          else:
             if temp[0] == '???':
                 print 'New Device Registration, HandshakeID=' + temp[2]
                 localId = sendSensorRegistration(config_data,  nowPI.strftime("%Y-%m-%dT%H:%M:%S"), 'new custom device (' + temp[2] + ')')
                 if localId != 0:
                      localCommandSendAckWaitList.append('???_v02_' + temp[2] + '_' + localId) 
                      # Reload config data as we succesfully registered new device
                      json_data=open(configFileName)
                      config_data = json.load(json_data)
                      json_data.close()
             else:
                 print '-> ignore'

       if queue_name <> '':
          # if check timeout is gone go to Azure and grab command to execute
          tdelta = nowPI-cloudCommandLastCheck
          if (abs(tdelta.total_seconds()) > 10):
             cloudCommandLastCheck = datetime.now()
             thread = Thread(target=checkCloudCommand, args=(bus_service, queue_name, localCommandSendAckWaitList, config_data))
             thread.start()

          if debugMode == 1: print localCommandSendAckWaitList
          # Repeat sending/span commands while list is not empty
          for localCommand in localCommandSendAckWaitList:
             radio.stopListening()
             buf = list(localCommand)
             radio.write(buf)
             print 'L: Broadcast Command: ' + localCommand 
             time.sleep(1)
             radio.startListening()
Exemplo n.º 48
0
from azure.servicebus import ServiceBusService, Message, Queue
bus_write_service = ServiceBusService(
    service_namespace='vmsseight',
    shared_access_key_name='SenderSender',
    shared_access_key_value='Owc7z2HYRo7mBETLjE/U//thLJ5W6yyRefJc1WIYxLo=')
#msg = Message(b'Test Message')
#bus_write_service.send_queue_message('vmsseightqueue', msg)
bus_read_service = ServiceBusService(
    service_namespace='vmsseight',
    shared_access_key_name='ListenOneTime',
    shared_access_key_value='OhFgmG5Cr/K9aOrE29YL7eXERzmUb3Fpf7J+FoBhiMw=')
msg = bus_read_service.receive_queue_message('vmsseightqueue', peek_lock=False)
print(msg.body)